在创建时使用D3DPOOL_MANAGED标志的资源可以不需要重新载入,但D3DPOOL_DEFAULT加载的资源就需要先释放,后重建。
通常需要这样处理的有ID3DXFont和ID3DXSprite,而.X模型什么的就不需要。 在发现设备丢失时,我们要调用 OnLostDevice(void)函数让D3DPOOL_DEFAULT加载的资源释放掉。 好在ID3DXFont和ID3DXSprite有设备丢失处理函数,直接调用就好了void OnLostDevice(void) {
m_sprite->OnLostDevice(); m_font->OnLostDevice(); } 可是,怎么知道设备丢失了呢? 如果设备丢失,Present()函数就会“出问题”,返回值是D3DERR_DEVICELOST。m_pIDirect3DDevice->EndScene(); HRESULT hr; hr = m_pIDirect3DDevice->Present(NULL, NULL, NULL, NULL);
if(hr == D3DERR_DEVICELOST) { if(m_pIDirect3DDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) { OnLostDevice(); OnResetDevice(); } }TestCooperativeLevel()== D3DERR_DEVICENOTRESET时,就可以恢复设备了。 于是,我们调用OnLostDevice()让D3DPOOL_DEFAULT加载的资源释放掉,之后,调用OnResetDevice()来恢复设备就可以了。 怎么恢复设备呢?
void OnResetDevice(void) { if(FAILED(m_pIDirect3DDevice->Reset(&d3dpp))) { return; } m_sprite->OnResetDevice(); m_font->OnResetDevice(); InitDevice(); }
先让D3D设备reset,然后调用ID3DXFont和ID3DXSprite的恢复函数,最后,需要把D3D设备中的状态啊,矩阵变换啊这些重新设置下就完成了,也就是调用InitDevice()。
void InitDevice() { m_pIDirect3DDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); m_pIDirect3DDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff ); m_pIDirect3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); m_pIDirect3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); m_pIDirect3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT); D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 1000.0f ); m_pIDirect3DDevice->SetTransform( D3DTS_PROJECTION, &matProj ); vEyeVec=D3DXVECTOR3(0.0f,0.0f,-1.0f); vLookatVec=D3DXVECTOR3(0.0f,0.0f,0.0f); vUpVec=D3DXVECTOR3(0.0f,1.0f,0.0f); D3DXMatrixLookAtLH( &matView, &vEyeVec, &vLookatVec, &vUpVec ); m_pIDirect3DDevice->SetTransform( D3DTS_VIEW, &matView ); }
啊,这样设备丢失就处理好了