LRESULT D3DApp::MsgProc(UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CLOSE: DestroyWindow(mhMainWindow); return 0; case WM_KEYDOWN: if (wParam == VK_ESCAPE) { DestroyWindow(mhMainWindow); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; case WM_SIZE: resWidth = LOWORD(lParam); resHeight = HIWORD(lParam); OnResetDevice(); return 0; default: return ::DefWindowProc(mhMainWindow, msg, wParam, lParam); } }
bool D3dApp::IsDeviceLost() { HRESULT hr = m_pMyD3dDevice->TestCooperativeLevel(); if(hr==D3DERR_DEVICELOST) { Sleep(20); return true; } else if(hr==D3DERR_DRIVERINTERNALERROR) { MessageBox(0,_T("Internal Driver Error... Exiting"),0,0); PostQuitMessage(0); return true; } else if(hr==D3DERR_DEVICENOTRESET) { OnLostDevice(); m_pMyD3dDevice->Reset(&m_D3dParams); OnResetDevice(); return false; } else { return false; } }
void Graphics::ResetDevice() { if( !m_init ) { return; } if( !m_windowActive || !m_deviceLost ) { return; } { Locker lock( &m_cs ); HRESULT hr = m_pDirect3DDevice->TestCooperativeLevel(); if( hr==D3DERR_DEVICELOST ) { PostMessage( m_window,WM_USER_RESETDEVICE,0,0 ); return; } m_pDirect3DDevice->Reset( &m_d3dpp ); OnResetDevice(); m_deviceLost = false; } }
void Graphics::Resize( int width,int height ) { if( m_windowed ) { m_d3dpp.BackBufferWidth = width; m_d3dpp.BackBufferHeight = height; Locker lock( &m_cs ); OnLostDevice(); m_pDirect3DDevice->Reset( &m_d3dpp ); OnResetDevice(); D3DVIEWPORT9 vp; vp.X = 0; vp.Y = 0; vp.Width = m_d3dpp.BackBufferWidth; vp.Height = m_d3dpp.BackBufferHeight; vp.MinZ = 0.0f; vp.MaxZ = 1.0f; m_pDirect3DDevice->SetViewport(&vp); } }
HRESULT KG3DShadowMapLevel::Init(KG3DSceneShadowMap* pShadow,int nLevel,KG3DSceneEntityMgr* pSrcMgr) { HRESULT hr = S_OK; nIndex = nLevel; SAFE_RELEASE(m_lpSMShadowMapSurface); SAFE_RELEASE(m_lpSMShadowMap); hr = OnResetDevice(); KGLOG_COM_PROCESS_ERROR(hr); KGLOG_PROCESS_ERROR(m_lpSMShadowMapSurface); if(!m_lpShadowMapEntityMgr) { m_lpShadowMapEntityMgr = new KG3DSceneEntityMgrShadow; m_lpShadowMapEntityMgr->Init( pSrcMgr ); m_lpShadowMapEntityMgr->m_lpTerrainRender->EnableCheckVisibleCount(FALSE); m_lpShadowMapEntityMgr->m_bGetEntityFromContainerDirect = TRUE; if(nLevel == 0) m_lpShadowMapEntityMgr->SetVisibleGate(D3DX_PI*0.000F ,D3DX_PI*0.01F); else if(nLevel == 1) m_lpShadowMapEntityMgr->SetVisibleGate(D3DX_PI*0.08F ,D3DX_PI*0.01F); else m_lpShadowMapEntityMgr->SetVisibleGate(D3DX_PI*0.16F ,D3DX_PI*0.05F); } return S_OK; Exit0: return E_FAIL; }
//-------------------------------------------------------------------------------------- // Handles the GUI events //-------------------------------------------------------------------------------------- void CALLBACK OnGUIEvent( UINT nEvent, int nControlID, CDXUTControl* pControl, void* pUserContext ) { switch( nControlID ) { case IDC_TOGGLEFULLSCREEN: DXUTToggleFullScreen(); break; case IDC_TOGGLEREF: DXUTToggleREF(); break; case IDC_CHANGEDEVICE: g_SettingsDlg.SetActive( !g_SettingsDlg.IsActive() ); break; case IDC_ENABLE_PRESHADER: { g_bEnablePreshader = g_SampleUI.GetCheckBox( IDC_ENABLE_PRESHADER )->GetChecked(); if( DXUTGetD3D9Device() != NULL ) { OnLostDevice( NULL ); OnDestroyDevice( NULL ); OnCreateDevice( DXUTGetD3D9Device(), DXUTGetD3D9BackBufferSurfaceDesc(), NULL ); OnResetDevice( DXUTGetD3D9Device(), DXUTGetD3D9BackBufferSurfaceDesc(), NULL ); } break; } case IDC_ACTIVE_LIGHT: if( !g_LightControl[g_nActiveLight].IsBeingDragged() ) { g_nActiveLight++; g_nActiveLight %= g_nNumActiveLights; } break; case IDC_NUM_LIGHTS: if( !g_LightControl[g_nActiveLight].IsBeingDragged() ) { WCHAR sz[100]; swprintf_s( sz, 100, L"# Lights: %d", g_SampleUI.GetSlider( IDC_NUM_LIGHTS )->GetValue() ); g_SampleUI.GetStatic( IDC_NUM_LIGHTS_STATIC )->SetText( sz ); g_nNumActiveLights = g_SampleUI.GetSlider( IDC_NUM_LIGHTS )->GetValue(); g_nActiveLight %= g_nNumActiveLights; } break; case IDC_LIGHT_SCALE: g_fLightScale = ( float )( g_SampleUI.GetSlider( IDC_LIGHT_SCALE )->GetValue() * 0.10f ); WCHAR sz[100]; swprintf_s( sz, 100, L"Light scale: %0.2f", g_fLightScale ); g_SampleUI.GetStatic( IDC_LIGHT_SCALE_STATIC )->SetText( sz ); break; } }
STDMETHODIMP VMRSurfaceAllocator::PresentImage( DWORD_PTR dwUserID, VMR9PresentationInfo *lpPresInfo ) { HRESULT hr = 0; //IDirect3DTexture9 *lpTexture; CheckPointer( lpPresInfo, E_POINTER ); CheckPointer( lpPresInfo->lpSurf, E_POINTER ); //while( lock ) //{ // Sleep( 1 ); //} mdraw->WaitDrawEnd( 1, 16 ); hr = PresentToTexture( lpPresInfo ); //if(hr == D3D_OK) //{ // if ( texnum < 0 ) // { // hr = lpPresInfo->lpSurf->GetContainer(IID_IDirect3DTexture9,(void **)&lpTexture); // } else // { // lpTexture = m_alpDirect3DTexture[ texnum ]; // lpTexture->AddRef(); // hr = D3D_OK; // } // if ( hr == D3D_OK ) // { // //...ここで必要であればテクスチャを使っての描画処理を行う // lpTexture->Release(); // } //} if ( hr == D3DERR_DEVICELOST ) { OnLostDevice(); hr = D3DDev->TestCooperativeLevel(); if ( hr == D3DERR_DEVICENOTRESET ) { OnResetDevice(); } } //mdraw->UnlockDarawEnd(); return hr; }
DX11::TextureSurface::TextureSurface(DX11::Api * gpu, ID3D11Device * device, ID3D11DeviceContext * context, Format format, PlatformWindow * relativeWindow, float width, float height, bool mips) : DrawSurface(device, context), gpu(gpu), texture(0), relativeWindow(relativeWindow), renderTargetView(0), depthStencilTexture(0), depthStencilView(0), format(format), widthFactor(width), heightFactor(height), generateMips(mips) { viewport = CD3D11_VIEWPORT(0.0f, 0.0f, width, height); OnResetDevice(gpu); }
void TreeGeneratorCanvas::RenderWindow() { Driver* driver = gEngine->GetDriver(); IDirect3DDevice9* d3dDevice = driver->GetD3DDevice(); Camera* extraCamera = gEngine->GetSceneManager()->GetExtraCamera(); switch(d3dDevice->TestCooperativeLevel()) { case D3D_OK: { extraCamera->FrameUpdate(); if(wxWindow::FindFocus() == this) extraCamera->ApplyCameraController(); driver->RenderToSwapChain(swapChainIndex); driver->Clear(D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x7f36404a, 1.0f); driver->BeginScene(); std::vector<Vector3> points; points.push_back(Vector3::Zero); points.push_back(Vector3(2, 0, 0)); DebugDrawer::DrawLine(points, 0xffff0000, extraCamera); if(mTree) mTree->Draw(extraCamera); driver->EndScene(); driver->Present((HWND)GetHWND()); break; } case D3DERR_DEVICELOST: break; case D3DERR_DEVICENOTRESET: { OnLostDevice(); OnResetDevice(); break; } } }
void D3dApp::EnableFullScreen(bool enable) { if(enable) { if(!m_D3dParams.Windowed)return; int Width = GetSystemMetrics(SM_CXSCREEN); int Height = GetSystemMetrics(SM_CYSCREEN); m_D3dParams.BackBufferFormat = D3DFMT_X8R8G8B8; m_D3dParams.BackBufferWidth = Width; m_D3dParams.BackBufferHeight = Height; m_D3dParams.Windowed = false; SetWindowLongPtr(m_hMainWnd, GWL_STYLE, WS_POPUP); SetWindowPos(m_hMainWnd,HWND_TOP,0,0,Width,Height,SWP_NOZORDER | SWP_SHOWWINDOW); } else { if(m_D3dParams.Windowed)return; RECT windowrect = {0,0, 800,600}; AdjustWindowRect(&windowrect,WS_CAPTION,false); m_D3dParams.BackBufferFormat = D3DFMT_UNKNOWN; m_D3dParams.BackBufferWidth = windowrect.right; m_D3dParams.BackBufferHeight = windowrect.bottom; m_D3dParams.Windowed = true; SetWindowLongPtr(m_hMainWnd, GWL_STYLE, WS_CAPTION); SetWindowPos(m_hMainWnd,HWND_TOP,(GetSystemMetrics(SM_CXSCREEN)/2)-(windowrect.right/2), (GetSystemMetrics(SM_CYSCREEN)/2)-(windowrect.bottom/2), windowrect.right,windowrect.bottom,SWP_NOZORDER | SWP_SHOWWINDOW); } OnLostDevice(); m_pMyD3dDevice->Reset(&m_D3dParams); OnResetDevice(); }
HRESULT KG3DShadowMapLevel::Init(DWORD dwSize) { HRESULT hr = S_OK; m_dwShadowmapSize = dwSize; nIndex = 0; SAFE_RELEASE(m_lpSMColorSurface); SAFE_RELEASE(m_lpSMColorMap); SAFE_RELEASE(m_lpSMShadowMapSurface); SAFE_RELEASE(m_lpSMShadowMap); hr = OnResetDevice(); KGLOG_COM_PROCESS_ERROR(hr); KGLOG_PROCESS_ERROR(m_lpSMShadowMapSurface); return S_OK; Exit0: return E_FAIL; }
bool Display(float timeDelta) { if( Device ) { // // spin the cube:旋转立方体 // D3DXMATRIX Rx, Ry; // rotate 45 degrees on x-axis,顺时针旋转 D3DXMatrixRotationX(&Rx, 3.14f / 4.0f); // incremement y-rotation angle each frame static float y = 0.0f; D3DXMatrixRotationY(&Ry, y); y += timeDelta; // reset angle to zero when angle reaches 2*PI if( y >= 6.28f ) y = 0.0f; // combine x- and y-axis rotation transformations. D3DXMATRIX p = Rx * Ry; // 本地坐标变换到世界坐标 Device->SetTransform(D3DTS_WORLD, &p); // // draw the scene: // Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); Device->BeginScene(); // 设置数据流来源 Device->SetStreamSource(0, // 使用0号数据流 VB, // 要加载到流中的顶点内存 0, // 渲染偏移量 sizeof(Vertex));// 每个元素的字节数 // 设置索引缓冲区 Device->SetIndices(IB); // 设置顶点格式 Device->SetFVF(Vertex::FVF); // Draw cube.将几何信息送到渲染管线 Device->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, // 图元类型,这里是三角形 0, // 索引基数,用于指定某个顶点缓冲区(回合并到一个全局的缓冲区,所以要指定) 0, // 引用的最小索引值 8, // 要渲染的顶点数量,这里是8个顶点 0, // 从缓冲区读取的第几个元素 12); // 要渲染的图元数量,这里是12个三角形 Device->EndScene(); HRESULT hr = Device->Present(0, 0, 0, 0); if(hr == D3DERR_DEVICELOST) { if(Device->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) { OnLostDevice(); OnResetDevice(); } } } return true; }
void Graphics::ChangeScreenMode( bool windowed ) { if( windowed==m_windowed ) { return; } m_windowed = windowed; if( m_windowed ) { ZeroMemory( &m_d3dpp, sizeof( m_d3dpp ) ); m_d3dpp.Windowed = TRUE; m_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; m_d3dpp.BackBufferWidth = m_rectClient.right-m_rectClient.left; m_d3dpp.BackBufferHeight = m_rectClient.bottom-m_rectClient.top; m_d3dpp.BackBufferCount = 1; m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; m_d3dpp.EnableAutoDepthStencil = TRUE; m_d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8; m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; } else { D3DDISPLAYMODE d3ddisp; m_pDirect3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT,&d3ddisp ); ZeroMemory( &m_d3dpp, sizeof( m_d3dpp ) ); m_d3dpp.Windowed = FALSE; m_d3dpp.BackBufferFormat = d3ddisp.Format; m_d3dpp.BackBufferWidth = m_screenWidth; m_d3dpp.BackBufferHeight = m_screenHeight; m_d3dpp.BackBufferCount = 1; m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; m_d3dpp.EnableAutoDepthStencil = TRUE; m_d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8; m_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; ::GetWindowRect(m_window, &m_rectWindow); ::GetClientRect(m_window, &m_rectClient); } Locker lock( &m_cs ); OnLostDevice(); m_pDirect3DDevice->Reset( &m_d3dpp ); OnResetDevice(); D3DVIEWPORT9 vp; vp.X = 0; vp.Y = 0; vp.Width = m_d3dpp.BackBufferWidth; vp.Height = m_d3dpp.BackBufferHeight; vp.MinZ = 0.0f; vp.MaxZ = 1.0f; m_pDirect3DDevice->SetViewport(&vp); if( m_windowed ) { // フルスクリーンだった場合、ウィンドウスタイルや位置が自動的に変わるため、 // 元に戻す必要がある。 // フルスクリーン用のウィンドウを作っている場合は、この問題が発生しない(ウィンドウの表示、非表示は切り替える必要がある) ::SetWindowLong(m_window, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE ); ::SetWindowPos(m_window, HWND_NOTOPMOST, m_rectWindow.left, m_rectWindow.top, m_rectWindow.right - m_rectWindow.left, m_rectWindow.bottom - m_rectWindow.top, SWP_SHOWWINDOW ); } else { // フルスクリーンにした段階で、自動的に変更される。 // だが、わかりにくいため念のため明記しておく。 ::SetWindowLong(m_window, GWL_STYLE, WS_POPUP | WS_VISIBLE); } }