Video_DX9::Video_DX9() : Video(Video_Base::ZENI_VIDEO_DX9), m_d3d(0), m_d3d_device(0), m_matrix_stack(0), m_textured(false), m_fvf_3d(false), m_render_target(0), m_back_buffer(0) { m_d3d = Direct3DCreate9(D3D_SDK_VERSION); if(!m_d3d) throw Video_Init_Failure(); init(); }
void Video_DX9::init() { set_opengl_flag(false); Video::init(); SDL_SysWMinfo wmInfo; SDL_VERSION(&wmInfo.version); SDL_GetWMInfo(&wmInfo); cout << "Initializing DirectX 9" << endl; m_d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &m_d3d_capabilities); m_dpi = GetDeviceCaps(GetDC(wmInfo.window), LOGPIXELSY); ZeroMemory(&m_d3d_parameters, sizeof(m_d3d_parameters)); m_d3d_parameters.hDeviceWindow = wmInfo.window; m_d3d_parameters.Windowed = true; m_d3d_parameters.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; m_d3d_parameters.BackBufferCount = 1; m_d3d_parameters.BackBufferWidth = UINT(get_screen_width()); m_d3d_parameters.BackBufferHeight = UINT(get_screen_height()); m_d3d_parameters.BackBufferFormat = D3DFMT_A8R8G8B8; m_d3d_parameters.SwapEffect = D3DSWAPEFFECT_DISCARD; m_d3d_parameters.PresentationInterval = get_vertical_sync() ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE; m_d3d_parameters.Flags = 0; //D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; m_d3d_parameters.EnableAutoDepthStencil = true; m_d3d_parameters.AutoDepthStencilFormat = D3DFMT_D16; if(get_multisampling() > 1) switch(get_multisampling()) { case 2: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES; break; case 3: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_3_SAMPLES; break; case 4: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES; break; case 5: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_5_SAMPLES; break; case 6: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_6_SAMPLES; break; case 7: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_7_SAMPLES; break; case 8: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_8_SAMPLES; break; case 9: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_9_SAMPLES; break; case 10: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_10_SAMPLES; break; case 11: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_11_SAMPLES; break; case 12: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_12_SAMPLES; break; case 13: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_13_SAMPLES; break; case 14: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_14_SAMPLES; break; case 15: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_15_SAMPLES; break; case 16: default: m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_16_SAMPLES; break; } else if(get_multisampling() < 0) m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_16_SAMPLES; else m_d3d_parameters.MultiSampleType = D3DMULTISAMPLE_NONE; m_d3d_parameters.MultiSampleQuality = 0; // Initialize the D3D device if(!init_device()) throw Video_Init_Failure(); // Initialize the rendering context init_context(); }
Video * Video::create() { Video * video = 0; File_Ops &fo = get_File_Ops(); const String appdata_path = fo.get_appdata_path(); const String user_normal = appdata_path + "config/zenilib.xml"; const String user_backup = user_normal + ".bak"; const String local_normal = "config/zenilib.xml"; const String local_backup = local_normal + ".bak"; static bool last_resort_taken = false; #ifndef ANDROID try #endif { switch(Video::g_video_mode) { case Video::ZENI_VIDEO_ANY: #ifndef DISABLE_DX9 case Video::ZENI_VIDEO_DX9: video = new Video_DX9(); break; #endif #ifndef DISABLE_GL case Video::ZENI_VIDEO_GL: video = new Video_GL(); break; #endif default: throw Video_Init_Failure(); } } #ifndef ANDROID catch(Video_Init_Failure &) { if(fo.copy_file(user_backup, user_normal) && fo.delete_file(user_backup)) { std::cerr << '\'' << user_normal << "' backup restored due to initialization failure.\n"; Video::preinit_from_file(user_normal); get_Video(); } else if(fo.copy_file(local_backup, local_normal) && fo.delete_file(local_backup)) { std::cerr << '\'' << local_normal << "' backup restored due to initialization failure.\n"; Video::preinit_from_file(local_normal); get_Video(); } else if(!last_resort_taken) { Video::set_failsafe_defaults(); last_resort_taken = true; get_Video(); } else throw; } #endif last_resort_taken = false; return video; }
void Video_GL::init() { std::cout << "Initializing OpenGL" << std::endl; //double buffer, no stencil, no accumulation buffer SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 0); SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0); if(get_multisampling() > 1) { SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, get_multisampling()); SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1); } set_opengl_flag(true); Video::init(); #if SDL_VERSION_ATLEAST(1,3,0) m_context = SDL_GL_CreateContext(get_window()); #endif { const GLenum err = glewInit(); if(GLEW_OK != err) { std::cerr << "GLEW Error: " << glewGetErrorString(err) << std::endl; throw Video_Init_Failure(); } } // Set Fill/Shade Mode glShadeModel(GL_SMOOTH); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_NORMALIZE); //GL_RESCALE_NORMALIZE); // Enable Alpha Blitting glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glBlendEquation(GL_FUNC_ADD); // default // would require ARB ext // Set lighting variables glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); if(glGetError() == GL_INVALID_ENUM) std::cerr << "Quality Warning: Your graphics card does not support separate specular lighting in OpenGL.\n"; // Initialize Assorted Variables glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //glPointSize(static_cast<GLfloat>(sqrt(pow(double(get_screen_width()), 2.) * pow(double(get_screen_height()), 2.)) / 1000000)); //glLineWidth(static_cast<GLfloat>(sqrt(pow(double(get_screen_width()), 2.) * pow(double(get_screen_height()), 2.)) / 1000000)); // Finish with a few function calls set_2d(); set_color(get_color()); set_clear_color(get_clear_color()); set_backface_culling(get_backface_culling()); set_lighting(get_lighting()); set_ambient_lighting(get_ambient_lighting()); set_alpha_test(is_alpha_test_enabled(), get_alpha_test_function(), get_alpha_test_value()); set_zwrite(is_zwrite_enabled()); set_ztest(is_ztest_enabled()); union { void * v; #ifdef _LINUX PFNGLXSWAPINTERVALEXTPROC pglSwapIntervalEXT; PFNGLXSWAPINTERVALSGIPROC pglSwapIntervalSGI; #endif PFNGLBINDBUFFERARBPROC pglBindBufferARB; PFNGLDELETEBUFFERSARBPROC pglDeleteBuffersARB; PFNGLGENBUFFERSARBPROC pglGenBuffersARB; PFNGLBUFFERDATAARBPROC pglBufferDataARB; } ptr; #ifdef _LINUX ptr.v = SDL_GL_GetProcAddress("glXSwapIntervalEXT"); if(!ptr.v) ptr.v = SDL_GL_GetProcAddress("wglSwapIntervalEXT"); m_pglSwapIntervalEXT = ptr.pglSwapIntervalEXT; ptr.v = SDL_GL_GetProcAddress("glXSwapIntervalSGI"); if(!ptr.v) ptr.v = SDL_GL_GetProcAddress("wglSwapIntervalSGI"); m_pglSwapIntervalSGI = ptr.pglSwapIntervalSGI; #endif // Has to be done after finding the function pointer set_vertical_sync(get_vertical_sync()); m_vertex_buffers = strstr(reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)), "ARB_vertex_buffer_object") != 0; if(m_vertex_buffers) { ptr.v = SDL_GL_GetProcAddress("glBindBufferARB"); m_pglBindBufferARB = ptr.pglBindBufferARB; ptr.v = SDL_GL_GetProcAddress("glDeleteBuffersARB"); m_pglDeleteBuffersARB = ptr.pglDeleteBuffersARB; ptr.v = SDL_GL_GetProcAddress("glGenBuffersARB"); m_pglGenBuffersARB = ptr.pglGenBuffersARB; ptr.v = SDL_GL_GetProcAddress("glBufferDataARB"); m_pglBufferDataARB = ptr.pglBufferDataARB; } else std::cerr << "Performance Warning: Your graphics card does not offer Vertex Buffer Objects (VBO) in OpenGL.\n"; if(strstr((char*)glGetString(GL_EXTENSIONS), "GL_EXT_texture_filter_anisotropic")) glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, reinterpret_cast<GLint *>(&m_maximum_anisotropy)); else m_maximum_anisotropy = 0; }
void Video_DX9::init() { SDL_SysWMinfo wmInfo; SDL_VERSION(&wmInfo.version); #if SDL_VERSION_ATLEAST(1,3,0) SDL_GetWindowWMInfo(get_Window().get_window(), &wmInfo); HWND hWnd = wmInfo.win.window; #else SDL_GetWMInfo(&wmInfo); HWND hWnd = wmInfo.window; #endif std::cout << "Initializing DirectX 9" << std::endl; try { m_d3d_capabilities = new D3DCAPS9; m_d3d_parameters = new D3DPRESENT_PARAMETERS; } catch (...) { throw Video_Init_Failure(); } m_d3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_d3d_capabilities); m_dpi = GetDeviceCaps(GetDC(hWnd), LOGPIXELSY); ZeroMemory(m_d3d_parameters, sizeof(m_d3d_parameters)); m_d3d_parameters->hDeviceWindow = hWnd; m_d3d_parameters->Windowed = true; m_d3d_parameters->FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; m_d3d_parameters->BackBufferCount = 1; m_d3d_parameters->BackBufferWidth = UINT(get_Window().get_width()); m_d3d_parameters->BackBufferHeight = UINT(get_Window().get_height()); m_d3d_parameters->BackBufferFormat = D3DFMT_A8R8G8B8; m_d3d_parameters->SwapEffect = D3DSWAPEFFECT_DISCARD; m_d3d_parameters->PresentationInterval = get_vertical_sync() ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE; m_d3d_parameters->Flags = 0; //D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; m_d3d_parameters->EnableAutoDepthStencil = true; m_d3d_parameters->AutoDepthStencilFormat = D3DFMT_D16; if(get_multisampling() > 1) switch(get_multisampling()) { case 2: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_2_SAMPLES; break; case 3: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_3_SAMPLES; break; case 4: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_4_SAMPLES; break; case 5: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_5_SAMPLES; break; case 6: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_6_SAMPLES; break; case 7: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_7_SAMPLES; break; case 8: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_8_SAMPLES; break; case 9: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_9_SAMPLES; break; case 10: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_10_SAMPLES; break; case 11: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_11_SAMPLES; break; case 12: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_12_SAMPLES; break; case 13: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_13_SAMPLES; break; case 14: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_14_SAMPLES; break; case 15: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_15_SAMPLES; break; case 16: default: m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_16_SAMPLES; break; } else if(get_multisampling() < 0) m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_16_SAMPLES; else m_d3d_parameters->MultiSampleType = D3DMULTISAMPLE_NONE; m_d3d_parameters->MultiSampleQuality = 0; // Initialize the D3D device if(!init_device()) throw Video_Init_Failure(); // Initialize the rendering context init_context(); }
Video_DX9::Video_DX9() : m_d3d_capabilities(0), m_d3d_parameters(0), m_d3d(0), m_d3d_device(0), m_matrix_stack(0), m_textured(false), m_fvf_3d(false), m_render_target(0), m_render_to_surface(0), m_back_buffer(0) { m_d3d9 = LoadLibrary("d3d9.dll"); if(!m_d3d9) { std::cerr << "Loading d3d9.dll failed." << std::endl; throw Video_Init_Failure(); } g_Direct3DCreate9 = (Direct3DCreate9_fcn)GetProcAddress(m_d3d9, "Direct3DCreate9"); if(!g_Direct3DCreate9) { std::cerr << "Loading d3d9.dll failed." << std::endl; FreeLibrary(m_d3d9); zero_handles(); throw Video_Init_Failure(); } m_d3dx9 = LoadLibrary("D3DX9_43.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("D3DX9_42.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("D3DX9_41.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("D3DX9_40.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("D3DX9_39.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("D3DX9_38.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("D3DX9_37.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_36.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_35.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_34.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_33.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_32.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_31.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_30.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_29.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_28.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_27.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_26.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_25.dll"); if(!m_d3dx9) m_d3dx9 = LoadLibrary("d3dx9_24.dll"); if(!m_d3dx9) { std::cerr << "Loading d3dx9.dll failed." << std::endl; FreeLibrary(m_d3d9); zero_handles(); throw Video_Init_Failure(); } g_D3DXCreateRenderToSurface = (D3DXCreateRenderToSurface_fcn)GetProcAddress(m_d3dx9, "D3DXCreateRenderToSurface"); g_D3DXCreateTexture = (D3DXCreateTexture_fcn)GetProcAddress(m_d3dx9, "D3DXCreateTexture"); g_D3DXFilterTexture = (D3DXFilterTexture_fcn)GetProcAddress(m_d3dx9, "D3DXFilterTexture"); g_D3DXCreateMatrixStack = (D3DXCreateMatrixStack_fcn)GetProcAddress(m_d3dx9, "D3DXCreateMatrixStack"); if(!g_D3DXCreateRenderToSurface || !g_D3DXCreateTexture || !g_D3DXFilterTexture || !g_D3DXCreateMatrixStack) { std::cerr << "Loading d3dx9.dll failed." << std::endl; FreeLibrary(m_d3dx9); FreeLibrary(m_d3d9); zero_handles(); throw Video_Init_Failure(); } m_d3d = Direct3DCreate9()(D3D_SDK_VERSION); if(!m_d3d) { FreeLibrary(m_d3dx9); FreeLibrary(m_d3d9); zero_handles(); throw Video_Init_Failure(); } try { init(); } catch(...) { delete m_d3d_parameters; delete m_d3d_capabilities; FreeLibrary(m_d3dx9); FreeLibrary(m_d3d9); zero_handles(); throw Video_Init_Failure(); } }