// Resets the D3D device and recreates triangle geometry. HRESULT FxPlayerTiny::ResetD3D() { // Set new mode if( FAILED(pDevice->Reset(&PresentParams) )) return E_FAIL; return S_OK; }
//----------------------------------------------------------------------------- // Name : ResetDisplay () // Desc : Reset the display device, and optionally the window etc. //----------------------------------------------------------------------------- HRESULT CD3DInitialize::ResetDisplay( LPDIRECT3DDEVICE9 pD3DDevice, CD3DSettings& D3DSettings, HWND hWnd ) { D3DPRESENT_PARAMETERS d3dpp = BuildPresentParameters( D3DSettings ); CD3DSettings::Settings *pSettings = D3DSettings.GetSettings(); if ( hWnd ) { // Setup styles based on windowed / fullscreen mode if ( !D3DSettings.Windowed ) { SetMenu( hWnd, NULL ); SetWindowLong( hWnd, GWL_STYLE, WS_VISIBLE | WS_POPUP ); SetWindowPos( hWnd, NULL, 0, 0, pSettings->DisplayMode.Width, pSettings->DisplayMode.Height, SWP_NOZORDER ); } // End if Fullscreen else { SetWindowLong( hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW ); SetWindowPos( hWnd, HWND_NOTOPMOST, 50, 50, 800, 600, SWP_NOACTIVATE | SWP_SHOWWINDOW ); } // End if Windowed } // End if // Reset the device HRESULT hRet = pD3DDevice->Reset( &d3dpp ); if ( FAILED( hRet ) ) return hRet; // Success return S_OK; }
void ResizeWindowDX9(int width, int height) { // 取得Direct3D 9裝置 LPDIRECT3DDEVICE9 device = GutGetGraphicsDeviceDX9(); D3DPRESENT_PARAMETERS d3dpresent; ZeroMemory( &d3dpresent, sizeof(d3dpresent) ); d3dpresent.Windowed = TRUE; // 使用視窗模式 d3dpresent.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpresent.BackBufferFormat = D3DFMT_UNKNOWN; // 使用視窗模式可以不去設定 d3dpresent.BackBufferCount = 1; // 提供一塊backbuffer d3dpresent.EnableAutoDepthStencil = TRUE; // 自動開啟DepthStencil Buffer d3dpresent.AutoDepthStencilFormat = D3DFMT_D24S8; // DepthStencil Buffer模式 device->Reset(&d3dpresent); // 投影矩陣, 重設水平跟垂直方向的視角. float aspect = (float) height / (float) width; Matrix4x4 projection_matrix = GutMatrixPerspectiveRH_DirectX(90.0f, aspect, 0.1f, 100.0f); device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX *) &projection_matrix); // 關閉打光 device->SetRenderState(D3DRS_LIGHTING, FALSE); // 改變三角形正面的面向 device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW); }
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { if (ImGui_ImplDX9_WndProcHandler(hWnd, msg, wParam, lParam)) return true; switch (msg) { case WM_SIZE: if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED) { ImGui_ImplDX9_InvalidateDeviceObjects(); g_d3dpp.BackBufferWidth = LOWORD(lParam); g_d3dpp.BackBufferHeight = HIWORD(lParam); HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp); if (hr == D3DERR_INVALIDCALL) IM_ASSERT(0); ImGui_ImplDX9_CreateDeviceObjects(); } return 0; case WM_SYSCOMMAND: if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu return 0; break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, msg, wParam, lParam); }
HRESULT KG3DGraphicsEngine::Reset() { HRESULT hr = E_FAIL; if (!m_bDeviceObjectsLost) { if (m_pDeviceCallback) { hr = m_pDeviceCallback->OnLostDevice(); KGLOG_COM_PROCESS_ERROR(hr); } m_bDeviceObjectsLost = true; } if (m_bDeviceObjectsLost) { hr = g_pd3dDevice->Reset(&m_PresentParam); KGLOG_COM_PROCESS_ERROR(hr); hr = g_pd3dDevice->EvictManagedResources(); KGLOG_COM_PROCESS_ERROR(hr); hr = GetBackBufferDesc(g_pd3dDevice, &m_BackBufferSurfaceDesc); KGLOG_COM_PROCESS_ERROR(hr); if (m_pDeviceCallback) { D3DLIGHT9 Light; hr = m_pDeviceCallback->OnResetDevice(g_pd3dDevice, &m_BackBufferSurfaceDesc, &m_PresentParam); KGLOG_COM_PROCESS_ERROR(hr); m_bDeviceLost = false; memset(&Light, 0, sizeof(Light)); Light.Type = D3DLIGHT_DIRECTIONAL; Light.Direction.x = 1.0f; Light.Direction.y = 0.0f; Light.Direction.z = 0.0f; hr = g_pd3dDevice->SetLight(0, &Light); KGLOG_COM_PROCESS_ERROR(hr); } m_bDeviceObjectsLost = false; } Exit0: if (hr == D3DERR_DEVICELOST) m_bDeviceLost = true; else KGLOG_COM_CHECK_ERROR(hr); return hr; }
// 描画終了 bool D3D::End() { s_lpD3DDevice->EndScene(); if( FAILED( s_lpD3DDevice->Present( NULL, NULL, NULL, NULL ) ) ) { if( FAILED( s_lpD3DDevice->Reset( &s_D3DPP ) ) ) { return false; } } return true; }
bool restore_objects() { if ( retry_count ) { if ( --retry_count ) return false; release_objects(); if ( lpdev->Reset( &dpp ) != D3D_OK ) return false; } retry_count = 0; LPDIRECT3DSURFACE9 lpbb; HRESULT hr; if( SUCCEEDED( hr = lpdev->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, & lpbb ) ) ) { lpbb->GetDesc( & d3dsd ); lpbb->Release(); } lpdev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); lpdev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); lpdev->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); lpdev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); lpdev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); lpdev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); lpdev->SetRenderState(D3DRS_LIGHTING, false); lpdev->SetRenderState(D3DRS_ZENABLE, false); lpdev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); lpdev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); lpdev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); lpdev->SetRenderState(D3DRS_ALPHABLENDENABLE, false); lpdev->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1); if ( lpdev->CreateVertexBuffer( sizeof(d3dvertex) * 4, flags.v_usage, D3DFVF_XYZRHW | D3DFVF_TEX1, (D3DPOOL)flags.v_pool, &lpvbuf, NULL ) != D3D_OK ) return false; update_filtering( 1 ); lpdev->SetRenderState( D3DRS_DITHERENABLE, TRUE ); if ( lpdev->CreateTexture( surface_width, surface_height, 1, flags.t_usage, D3DFMT_X8R8G8B8, (D3DPOOL)flags.t_pool, &lptex, NULL ) != D3D_OK ) return false; return true; }
void ResetDevice(void) { D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = windowed; d3dpp.hDeviceWindow = g_hWnd_0; d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer d3dpp.BackBufferCount = 1; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; g_pd3dDevice_0->Reset(&d3dpp); }
//因为要调用InitMyScene,所以用UINT作返回值,返回0表示正常 UINT ResetDevice() { // 虽然只需要让弹出之前Release一次即可,但该操作并不费时,且类都有保护措施,重复Release也不会出问题(自动返回) ReleaseMyScene(); d3dpp.Windowed = WindowMode; d3dpp.BackBufferWidth = d3ddm.Width; d3dpp.BackBufferHeight = d3ddm.Height; //若为窗口模式,则取桌面的颜色数,取当前窗口的大小和位置,并重置 if(WindowMode) { d3ddm.Format = DesktopDisplayMode.Format; d3dpp.BackBufferWidth = WindowWidth; d3dpp.BackBufferHeight = WindowHeight; MoveWindow(hWnd, WindowX, WindowY, WindowWidth, WindowHeight, TRUE); } d3dpp.BackBufferFormat = d3ddm.Format; //根据显示模式来确定创建纹理和深度模板缓冲的像素格式 if(d3ddm.Format == D3DFMT_X8R8G8B8) { d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; TextureFormat = D3DFMT_A8R8G8B8; } else { d3dpp.AutoDepthStencilFormat = D3DFMT_D16; TextureFormat = D3DFMT_A4R4G4B4; } UINT hr = FAILED(d3ddevice->Reset(&d3dpp))?1:0; if(hr) return hr; // 弹出对话框之前也要调用该函数,可我们只需要让弹出之后Init一次即可,避免弹出之前也Init,没必要,费时 if(!DialogSign) { hr = InitMyScene(); } CameraChange.ViewTransform(); return hr; }
void D3D9_Resize(HWND window) { // This should only be called from the emu thread. int xres, yres; GetRes(xres, yres); bool w_changed = pp.BackBufferWidth != xres; bool h_changed = pp.BackBufferHeight != yres; if (device && (w_changed || h_changed)) { DX9::fbo_shutdown(); pp.BackBufferWidth = xres; pp.BackBufferHeight = yres; HRESULT hr = device->Reset(&pp); if (FAILED(hr)) { ERROR_LOG_REPORT(G3D, "Unable to reset device: %s", DXGetErrorStringA(hr)); PanicAlert("Unable to reset D3D9 device: %s", DXGetErrorStringA(hr)); } DX9::fbo_init(d3d); } }
// this is the main message handler for the program LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_DESTROY: { ::exit(0); PostQuitMessage(0); return 0; } case WM_SIZE: { // If the device is not NULL and the WM_SIZE message is not a // SIZE_MINIMIZED event, resize the device's swap buffers to match // the new window size. if( d3ddev != NULL && wParam != SIZE_MINIMIZED ) { OnRelease(); ScreenX = LOWORD(lParam); ScreenY = HIWORD(lParam); d3dpp.BackBufferWidth = LOWORD(lParam); d3dpp.BackBufferHeight = HIWORD(lParam); //if( d3ddev->Reset( &d3dpp ) // == D3DERR_INVALIDCALL ) //{ //error //} ; OnInitialize(false); } } break; } return DefWindowProc (hWnd, message, wParam, lParam); }
int resetdevice(const char *) { if(windowed==0) { if(screenwidth==0 || screenheight==0) { D3DDISPLAYMODE displaymode; g_pd3dDevice->GetDisplayMode(0,&displaymode); screenwidth = displaymode.Width; screenheight = displaymode.Height; } Width = screenwidth; Height= screenheight; } else // windowed { Width = (int)(windowwidth ); Height= (int)(windowheight); } oneoverwidth=1.0f/Width; oneoverheight=1.0f/Height; d3dpp.BackBufferWidth = Width; d3dpp.BackBufferHeight = Height; d3dpp.Windowed = (windowed)?1:0; d3dpp.BackBufferFormat = (d3dpp.Windowed)?D3DFMT_UNKNOWN:D3DFMT_X8R8G8B8 ; d3dpp.BackBufferCount = backbuffercount; d3dpp.PresentationInterval = vsync ? D3DPRESENT_INTERVAL_DEFAULT : D3DPRESENT_INTERVAL_IMMEDIATE; vsync_current = vsync; effectlostdevice(); cuberelease(); HRESULT hr = g_pd3dDevice->Reset(&d3dpp); assert(hr==0); effectresetdevice(); if(windowed) SetWindowPos( hWnd, HWND_NOTOPMOST,windowx+window_inset.left,windowy+window_inset.top,windowwidth+window_inset.right-window_inset.left, windowheight+window_inset.bottom-window_inset.top,SWP_SHOWWINDOW ); return hr; }
HRESULT ResetDevice(D3DPRESENT_PARAMETERS d3dpp) { // Check device state HRESULT hr = g_pd3dDevice->TestCooperativeLevel() ; // Device can be reset now if (SUCCEEDED(hr) || hr == D3DERR_DEVICENOTRESET) { // Release resource allocated as D3DPOOL_DEFAULT g_pVB->Release(); // Reset device HRESULT hr = g_pd3dDevice->Reset(&d3dpp) ; if (FAILED(hr)) { const CHAR* errorString = DXGetErrorString(hr) ; DXTRACE_ERR_MSGBOX(errorString, hr) ; } // Recreate the vertex buffer, since it is create by using // D3DPOOL_DEFAULT, the function MUST placed here!!! why? // you cann't place it before the return clause of this function // what's the inner cause? InitVB(); } // Device is still in lost state, wait else if (hr == D3DERR_DEVICELOST) { Sleep(25) ; } else // Other error, Show error box { const CHAR* errorString = DXGetErrorString(hr) ; DXTRACE_ERR_MSGBOX(errorString, hr) ; } return hr ; }
RString SetD3DParams( bool &bNewDeviceOut ) { if( g_pd3dDevice == NULL ) // device is not yet created. We need to create it { bNewDeviceOut = true; HRESULT hr = g_pd3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, GraphicsWindow::GetHwnd(), D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &g_d3dpp, &g_pd3dDevice ); if( FAILED(hr) ) { // Likely D3D_ERR_INVALIDCALL. The driver probably doesn't support this video mode. return ssprintf( "CreateDevice failed: '%s'", GetErrorString(hr).c_str() ); } } else { bNewDeviceOut = false; //LOG->Warn( "Resetting D3D device" ); HRESULT hr = g_pd3dDevice->Reset( &g_d3dpp ); if( FAILED(hr) ) { // Likely D3D_ERR_INVALIDCALL. The driver probably doesn't support this video mode. return ssprintf("g_pd3dDevice->Reset failed: '%s'", GetErrorString(hr).c_str() ); } } g_pd3dDevice->SetRenderState( D3DRS_NORMALIZENORMALS, TRUE ); // Palettes were lost by Reset(), so mark them unloaded. g_TexResourceToPaletteIndex.clear(); return RString(); }
CString SetD3DParams( bool &bNewDeviceOut ) { if( g_pd3dDevice == NULL ) // device is not yet created. We need to create it { bNewDeviceOut = true; HRESULT hr = g_pd3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, #if !defined(XBOX) GraphicsWindow::GetHwnd(), D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, #else NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, #endif &g_d3dpp, &g_pd3dDevice ); if( FAILED(hr) ) { // Likely D3D_ERR_INVALIDCALL. The driver probably doesn't support this video mode. return ssprintf( "CreateDevice failed: '%s'", GetErrorString(hr).c_str() ); } } else { bNewDeviceOut = false; HRESULT hr = g_pd3dDevice->Reset( &g_d3dpp ); if( FAILED(hr) ) { // Likely D3D_ERR_INVALIDCALL. The driver probably doesn't support this video mode. return ssprintf("g_pd3dDevice->Reset failed: '%s'", GetErrorString(hr).c_str() ); } } return ""; }
HRESULT KGraphicsEngine::Toggle2FullScreen() { if (m_lpCurScene) { g_cMeshTable.InvalidateDeviceObjects(); g_SceneTable.InvalidateDeviceObjects(); } g_EffectMgr.InvalidateDeviceObjects(); // Create the application's window m_BaseWinStyle = GetWindowLong(g_hBaseWnd,GWL_STYLE); SetWindowLong( g_hBaseWnd , GWL_STYLE, WS_POPUP|WS_SYSMENU|WS_VISIBLE ); //m_LastRenderWindow = g_hRenderWnd; g_hRenderWnd = g_hBaseWnd; //GetWindowRect(g_hRenderWnd,&m_RenderWindowRect); //m_RenderWindowRect.left = 0; //m_RenderWindowRect.top = 0; //m_RenderWindowRect.right = m_DisplayMode.Width; //m_RenderWindowRect.bottom = m_DisplayMode.Height; //float Width = (m_RenderWindowRect.right-m_RenderWindowRect.left )*1.0f; //float Height = (m_RenderWindowRect.bottom - m_RenderWindowRect.top)*1.0f; m_PresentParam.BackBufferWidth = m_DisplayMode.Width; m_PresentParam.BackBufferHeight= m_DisplayMode.Height; m_cCamera.Aspect = m_DisplayMode.Width*1.0f/m_DisplayMode.Height; m_PresentParam.hDeviceWindow = g_hRenderWnd; m_PresentParam.Windowed = FALSE ; m_PresentParam.BackBufferCount = 2; m_PresentParam.SwapEffect = D3DSWAPEFFECT_DISCARD; m_PresentParam.BackBufferFormat = m_DisplayMode.Format; m_PresentParam.EnableAutoDepthStencil = TRUE; m_PresentParam.MultiSampleType = D3DMULTISAMPLE_NONE; m_PresentParam.Flags = 0; m_PresentParam.FullScreen_RefreshRateInHz = 0; m_PresentParam.PresentationInterval = D3DPRESENT_INTERVAL_ONE;//D3DPRESENT_INTERVAL_IMMEDIATE; if (m_bUseMotionBlur) { BackBufferRelease(); } /*D3DMULTISAMPLE_TYPE Mul = D3DMULTISAMPLE_4_SAMPLES; if( FAILED(g_pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_PresentParam.BackBufferFormat, FALSE, Mul, NULL ) ) && SUCCEEDED(g_pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_PresentParam.AutoDepthStencilFormat, FALSE, Mul, NULL ) ) ) { Mul = D3DMULTISAMPLE_3_SAMPLES; if( FAILED(g_pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_PresentParam.BackBufferFormat, FALSE, Mul, NULL ) ) && SUCCEEDED(g_pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_PresentParam.AutoDepthStencilFormat, FALSE, Mul, NULL ) ) ) { Mul = D3DMULTISAMPLE_2_SAMPLES; if( FAILED(g_pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_PresentParam.BackBufferFormat, FALSE, Mul, NULL ) ) && SUCCEEDED(g_pD3D->CheckDeviceMultiSampleType( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_PresentParam.AutoDepthStencilFormat, FALSE, Mul, NULL ) ) ) { Mul = D3DMULTISAMPLE_NONE; } } } if (Mul != D3DMULTISAMPLE_NONE) { m_PresentParam.MultiSampleType = Mul; }*/ HRESULT hr; // Create the D3DDevice if ( FAILED(hr = g_pd3dDevice->Reset( &m_PresentParam ))) { //fprintf(Note,"Create D3D Device Failed.\n"); if (hr == D3DERR_INVALIDCALL) MessageBox(g_hBaseWnd,"The method call is invalid. For example, a method's parameter may have an invalid value.","Failed Create Device!",0); else if (hr == E_OUTOFMEMORY ) MessageBox(g_hBaseWnd,"Direct3D could not allocate sufficient memory to complete the call.","Failed Create Device!",0); else if (hr == D3DERR_OUTOFVIDEOMEMORY) MessageBox(g_hBaseWnd,"Direct3D does not have enough display memory to perform the operation. ","Failed Create Device!",0); return E_FAIL; } //g_pd3dDevice->SetDialogBoxMode( TRUE ); g_hRenderWnd = g_hBaseWnd; m_bWindowed = FALSE; if (m_lpCurScene) { g_cMeshTable.RestoreDeviceObjects(); g_SceneTable.RestoreDeviceObjects(); } g_EffectMgr.RestoreDeviceObjects(); g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE); if (m_bUseMotionBlur) { BackBufferCreate(); } return E_FAIL; }
HRESULT KGraphicsEngine::Toggle2Windowed() { if (m_lpCurScene) { g_cMeshTable.InvalidateDeviceObjects(); g_SceneTable.InvalidateDeviceObjects(); } g_EffectMgr.InvalidateDeviceObjects(); if (m_bUseMotionBlur) { BackBufferRelease(); } HRESULT hr = S_OK; GetWindowRect(m_LastRenderWindow,&m_RenderWindowRect); //m_PresentParam.BackBufferWidth = m_RenderWindowRect.right-m_RenderWindowRect.left ; //m_PresentParam.BackBufferHeight= m_RenderWindowRect.bottom - m_RenderWindowRect.top; m_PresentParam.BackBufferWidth = m_DisplayMode.Width; m_PresentParam.BackBufferHeight= m_DisplayMode.Height; D3DFORMAT DepFormat; if (SUCCEEDED(g_pD3D->CheckDepthStencilMatch(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_DisplayMode.Format, m_DisplayMode.Format, D3DFMT_D24S8))) { DepFormat = D3DFMT_D24S8; } else { DepFormat = D3DFMT_D16; } m_PresentParam.Windowed = TRUE ; m_PresentParam.SwapEffect = D3DSWAPEFFECT_DISCARD; m_PresentParam.BackBufferFormat = m_DisplayMode.Format; m_PresentParam.EnableAutoDepthStencil = TRUE; m_PresentParam.AutoDepthStencilFormat = DepFormat; m_PresentParam.MultiSampleType = D3DMULTISAMPLE_NONE; m_PresentParam.hDeviceWindow = g_hBaseWnd; m_PresentParam.Flags = 0; m_PresentParam.FullScreen_RefreshRateInHz = 0; m_PresentParam.BackBufferCount = 0; m_PresentParam.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; //just set camera's aspect m_cCamera.Aspect = m_PresentParam.BackBufferWidth*1.0f/m_PresentParam.BackBufferHeight; g_pd3dDevice->SetDialogBoxMode( FALSE ); if ( FAILED(hr = g_pd3dDevice->Reset( &m_PresentParam ))) { //fprintf(Note,"Create D3D Device Failed.\n"); if (hr == D3DERR_INVALIDCALL) MessageBox(g_hBaseWnd,"Toggle to Windowed Failed!","Failed in Toggling",0); else if (hr == E_OUTOFMEMORY ) MessageBox(g_hBaseWnd,"Direct3D could not allocate sufficient memory to complete the call.","Failed Create Device!",0); else if (hr == D3DERR_OUTOFVIDEOMEMORY) MessageBox(g_hBaseWnd,"Direct3D does not have enough display memory to perform the operation. ","Failed Create Device!",0); return E_FAIL; } SetWindowLong( g_hBaseWnd , GWL_STYLE, m_BaseWinStyle ); SetRenderWindow(m_LastRenderWindow); m_bWindowed = TRUE; if (m_lpCurScene) { g_cMeshTable.RestoreDeviceObjects(); g_SceneTable.RestoreDeviceObjects(); } g_EffectMgr.RestoreDeviceObjects(); if (m_bUseMotionBlur) { BackBufferCreate(); } return S_OK; }
//---------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------- void MainLoop() { for(;;) { MSG msg; if (PeekMessage (&msg,NULL,0,0,PM_NOREMOVE)) { if( msg.message == WM_QUIT ) { return ; } GetMessage (&msg,NULL,0,0); TranslateMessage(&msg); DispatchMessage(&msg); } else { // サーバーの更新を行う。 g_server->Update(); if( g_timer % 120 == 0 ) { // エフェクトの停止 g_manager->StopEffect( g_handle ); // エフェクトの再生 g_handle = g_manager->Play( g_effect, 0, 0, 0 ); } // エフェクトの移動処理を行う。 g_manager->AddLocation( g_handle, ::Effekseer::Vector3D( 0.2f, 0.0f, 0.0f ) ); // エフェクトの更新処理を行う。 g_manager->Update(); g_d3d_device->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ); g_d3d_device->BeginScene(); // エフェクトの描画開始処理を行う。 g_renderer->BeginRendering(); // エフェクトの描画を行う。 g_manager->Draw(); // エフェクトの描画終了処理を行う。 g_renderer->EndRendering(); g_d3d_device->EndScene(); g_timer++; { HRESULT hr; hr = g_d3d_device->Present( NULL, NULL, NULL, NULL ); // デバイスロスト処理 switch ( hr ) { // デバイスロスト case D3DERR_DEVICELOST: while ( FAILED( hr = g_d3d_device->TestCooperativeLevel() ) ) { switch ( hr ) { // デバイスロスト case D3DERR_DEVICELOST: ::SleepEx( 1000, true ); break; // デバイスロスト:リセット可 case D3DERR_DEVICENOTRESET: // デバイスロストの処理を行う前に実行する g_renderer->OnLostDevice(); D3DPRESENT_PARAMETERS d3dp; ZeroMemory(&d3dp, sizeof(d3dp)); d3dp.BackBufferWidth = g_window_width; d3dp.BackBufferHeight = g_window_height; d3dp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dp.BackBufferCount = 1; d3dp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dp.Windowed = TRUE; d3dp.hDeviceWindow = g_window_handle; d3dp.EnableAutoDepthStencil = TRUE; d3dp.AutoDepthStencilFormat = D3DFMT_D16; g_d3d_device->Reset( &d3dp ); // デバイスロストの処理の後に実行する g_renderer->OnResetDevice(); break; } } break; } } } } }
int main(int, char**) { // Create application window WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL }; RegisterClassEx(&wc); HWND hwnd = CreateWindow(_T("ImGui Example"), _T("ImGui DirectX9 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL); // Initialize Direct3D LPDIRECT3D9 pD3D; if ((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL) { UnregisterClass(_T("ImGui Example"), wc.hInstance); return 0; } ZeroMemory(&g_d3dpp, sizeof(g_d3dpp)); g_d3dpp.Windowed = TRUE; g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; g_d3dpp.EnableAutoDepthStencil = TRUE; g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; // Present with vsync //g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync, maximum unthrottled framerate // Create the D3DDevice if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0) { pD3D->Release(); UnregisterClass(_T("ImGui Example"), wc.hInstance); return 0; } // Setup Dear ImGui binding IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls ImGui_ImplDX9_Init(hwnd, g_pd3dDevice); // Setup style ImGui::StyleColorsDark(); //ImGui::StyleColorsClassic(); // Load Fonts // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. // - Read 'misc/fonts/README.txt' for more instructions and details. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! //io.Fonts->AddFontDefault(); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); //IM_ASSERT(font != NULL); bool show_demo_window = true; bool show_another_window = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); // Main loop MSG msg; ZeroMemory(&msg, sizeof(msg)); ShowWindow(hwnd, SW_SHOWDEFAULT); UpdateWindow(hwnd); while (msg.message != WM_QUIT) { // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); continue; } ImGui_ImplDX9_NewFrame(); // 1. Show a simple window. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug". { static float f = 0.0f; static int counter = 0; ImGui::Text("Hello, world!"); // Display some text (you can use a format string too) ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state ImGui::Checkbox("Another Window", &show_another_window); if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated) counter++; ImGui::SameLine(); ImGui::Text("counter = %d", counter); ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); } // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows. if (show_another_window) { ImGui::Begin("Another Window", &show_another_window); ImGui::Text("Hello from another window!"); if (ImGui::Button("Close Me")) show_another_window = false; ImGui::End(); } // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui! if (show_demo_window) { ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly! ImGui::ShowDemoWindow(&show_demo_window); } // Rendering ImGui::EndFrame(); g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false); g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false); D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x*255.0f), (int)(clear_color.y*255.0f), (int)(clear_color.z*255.0f), (int)(clear_color.w*255.0f)); g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0); if (g_pd3dDevice->BeginScene() >= 0) { ImGui::Render(); ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); g_pd3dDevice->EndScene(); } HRESULT result = g_pd3dDevice->Present(NULL, NULL, NULL, NULL); // Handle loss of D3D9 device if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) { ImGui_ImplDX9_InvalidateDeviceObjects(); g_pd3dDevice->Reset(&g_d3dpp); ImGui_ImplDX9_CreateDeviceObjects(); } } ImGui_ImplDX9_Shutdown(); ImGui::DestroyContext(); if (g_pd3dDevice) g_pd3dDevice->Release(); if (pD3D) pD3D->Release(); DestroyWindow(hwnd); UnregisterClass(_T("ImGui Example"), wc.hInstance); return 0; }
HRESULT HookIDirect3DDevice9::Reset(LPVOID _this, D3DPRESENT_PARAMETERS* pPresentationParameters) { LOG_API(); return pD3Dev->Reset(pPresentationParameters); }
//---------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------- void MainLoop() { int time = 0; bool reverse = false; for(;;) { MSG msg; if (PeekMessage (&msg,NULL,0,0,PM_NOREMOVE)) { if( msg.message == WM_QUIT ) { return ; } GetMessage (&msg,NULL,0,0); TranslateMessage(&msg); DispatchMessage(&msg); } else { if( time % 120 == 0 ) { // エフェクトの停止 for( size_t i = 0; i < g_handles.size(); i++ ) { g_manager->StopEffect(g_handles[i]); } g_handles.clear(); // エフェクトの再生 for( int i = 0; i < 3; i++ ) { Effekseer::Handle handle = g_manager->Play( g_effects[i], (i - 1) * 10.0f, 0, 0 ); g_handles.push_back(handle); } reverse = !reverse; } // エフェクトの更新処理を行う g_manager->Update(); g_d3d_device->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ); g_d3d_device->BeginScene(); // エフェクトの描画開始処理を行う。 g_renderer->BeginRendering(); // エフェクトを個別に描画を行う。 if(reverse) { for(int i = 0; i < g_handles.size(); i++ ) { g_manager->DrawHandle(g_handles[i]); } } else { for(int i = g_handles.size() - 1; i > -1; i-- ) { g_manager->DrawHandle(g_handles[i]); } } // エフェクトの描画終了処理を行う。 g_renderer->EndRendering(); g_d3d_device->EndScene(); time++; { HRESULT hr; hr = g_d3d_device->Present( NULL, NULL, NULL, NULL ); // デバイスロスト処理 switch ( hr ) { // デバイスロスト case D3DERR_DEVICELOST: while ( FAILED( hr = g_d3d_device->TestCooperativeLevel() ) ) { switch ( hr ) { // デバイスロスト case D3DERR_DEVICELOST: ::SleepEx( 1000, true ); break; // デバイスロスト:リセット可 case D3DERR_DEVICENOTRESET: // デバイスロストの処理を行う前に実行する g_renderer->OnLostDevice(); D3DPRESENT_PARAMETERS d3dp; ZeroMemory(&d3dp, sizeof(d3dp)); d3dp.BackBufferWidth = g_window_width; d3dp.BackBufferHeight = g_window_height; d3dp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dp.BackBufferCount = 1; d3dp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dp.Windowed = TRUE; d3dp.hDeviceWindow = g_window_handle; d3dp.EnableAutoDepthStencil = TRUE; d3dp.AutoDepthStencilFormat = D3DFMT_D16; g_d3d_device->Reset( &d3dp ); // デバイスロストの処理の後に実行する g_renderer->OnResetDevice(); break; } } break; } } } } }
//---------------------------------------------------------------------------------- // //---------------------------------------------------------------------------------- void MainLoop() { for(;;) { MSG msg; if (PeekMessage (&msg,NULL,0,0,PM_NOREMOVE)) { if( msg.message == WM_QUIT ) { return ; } GetMessage (&msg,NULL,0,0); TranslateMessage(&msg); DispatchMessage(&msg); } else { // エフェクトの更新処理を行う。 g_manager->Update(); g_d3d_device->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ); g_d3d_device->BeginScene(); // エフェクトの描画開始処理を行う。 g_renderer->BeginRendering(); // Render rear effects // 背面のエフェクトの描画を行う。 g_manager->DrawBack(); // Distort background and rear effects. // 背景と背面のエフェクトを歪ませる。 DistortingCallback distoring; distoring.OnDistorting(); // Render front effects // 前面のエフェクトの描画を行う。 g_manager->DrawFront(); // エフェクトの描画終了処理を行う。 g_renderer->EndRendering(); g_d3d_device->EndScene(); g_timer++; { HRESULT hr; hr = g_d3d_device->Present( NULL, NULL, NULL, NULL ); // デバイスロスト処理 switch ( hr ) { // デバイスロスト case D3DERR_DEVICELOST: while ( FAILED( hr = g_d3d_device->TestCooperativeLevel() ) ) { switch ( hr ) { // デバイスロスト case D3DERR_DEVICELOST: ::SleepEx( 1000, true ); break; // デバイスロスト:リセット可 case D3DERR_DEVICENOTRESET: // デバイスロストの処理を行う前に実行する g_renderer->OnLostDevice(); D3DPRESENT_PARAMETERS d3dp; ZeroMemory(&d3dp, sizeof(d3dp)); d3dp.BackBufferWidth = g_window_width; d3dp.BackBufferHeight = g_window_height; d3dp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dp.BackBufferCount = 1; d3dp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dp.Windowed = TRUE; d3dp.hDeviceWindow = g_window_handle; d3dp.EnableAutoDepthStencil = TRUE; d3dp.AutoDepthStencilFormat = D3DFMT_D16; g_d3d_device->Reset( &d3dp ); // デバイスロストの処理の後に実行する g_renderer->OnResetDevice(); break; } } break; } } } } }
// Reset the Direct3D device to resize window or toggle fullscreen/windowed bool ResetDevice( HWND hWnd, bool ToggleFullscreen = false ) { // If currently windowed... if (!Fullscreen) { // Get current window and client window dimensions RECT NewClientRect; GetWindowRect( hWnd, &WindowRect ); GetClientRect( hWnd, &NewClientRect ); // If not switching to fullscreen, then we must ensure the window is changing size, if // it isn't then return without doing anything if (!ToggleFullscreen && NewClientRect.right == ClientRect.right && NewClientRect.bottom == ClientRect.bottom) { return true; } ClientRect = NewClientRect; } // Toggle fullscreen if requested if (ToggleFullscreen) { Fullscreen = !Fullscreen; } // Reset the structure used to create the D3DDevice D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Don't wait for vertical sync d3dpp.BackBufferCount = 1; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; if (!Fullscreen) { // Set windowed parameters - need to set the back buffer dimensions when reseting, // match them to the window client area d3dpp.Windowed = TRUE; d3dpp.BackBufferWidth = ClientRect.right; d3dpp.BackBufferHeight = ClientRect.bottom; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; } else { // Get current dimensions of primary monitor MONITORINFO monitorInfo; monitorInfo.cbSize = sizeof(MONITORINFO); if (GetMonitorInfo( g_pD3D->GetAdapterMonitor( D3DADAPTER_DEFAULT ), &monitorInfo )) { d3dpp.BackBufferWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left; d3dpp.BackBufferHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; } else { d3dpp.BackBufferWidth = 1280; d3dpp.BackBufferHeight = 1024; } // Set other fullscreen parameters d3dpp.Windowed = FALSE; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; } ViewportWidth = d3dpp.BackBufferWidth; ViewportHeight = d3dpp.BackBufferHeight; // Need to recreate resources when reseting. Any resources (vertex & index buffers, textures) created // using D3DPOOL_MANAGED rather than D3DPOOL_DEFAULT will be recreated automatically. Dynamic resources // must be in D3DPOOL_DEFAULT, so they must be recreated manually. D3DX fonts are such an example. // Other dynamic resources are those that are updated during the game loop, e.g. procedural textures, // or dynamic terrain if (g_pFont != NULL) g_pFont->Release(); // Reset the Direct3D device with the new settings if (FAILED(g_pd3dDevice->Reset( &d3dpp ))) { return false; } // If reseting to windowed mode, we need to reset the window size if (!Fullscreen) { SetWindowPos( hWnd, HWND_NOTOPMOST, WindowRect.left, WindowRect.top, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, 0 ); } // Need to set up states again after reset g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 2, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 2, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); g_pd3dDevice->SetSamplerState( 2, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR ); // Recreate the font if (FAILED(D3DXCreateFont( g_pd3dDevice, 12, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &g_pFont ))) { return false; } return true; }