在建立時使用D3DPOOL_MANAGED標誌的資源能夠不須要從新載入,但D3DPOOL_DEFAULT加載的資源就須要先釋放,後重建。
一般須要這樣處理的有ID3DXFont和ID3DXSprite,而.X模型什麼的就不須要。
在發現設備丟失時,咱們要調用 OnLostDevice(void)函數讓D3DPOOL_DEFAULT加載的資源釋放掉。
好在ID3DXFont和ID3DXSprite有設備丟失處理函數,直接調用就行了函數
void OnLostDevice(void)
{
m_sprite->OnLostDevice();
m_font->OnLostDevice();
}oop
但是,怎麼知道設備丟失了呢?
若是設備丟失,Present()函數就會「出問題」,返回值是D3DERR_DEVICELOST。spa
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();
}
}3d
TestCooperativeLevel()== D3DERR_DEVICENOTRESET時,就能夠恢復設備了。
因而,咱們調用OnLostDevice()讓D3DPOOL_DEFAULT加載的資源釋放掉,以後,調用OnResetDevice()來恢復設備就能夠了。
怎麼恢復設備呢?orm
void OnResetDevice(void)
{
if(FAILED(m_pIDirect3DDevice->Reset(&d3dpp)))
{
return;
}
m_sprite->OnResetDevice();
m_font->OnResetDevice();
InitDevice();
}資源
先讓D3D設備reset,而後調用ID3DXFont和ID3DXSprite的恢復函數,最後,須要把D3D設備中的狀態啊,矩陣變換啊這些從新設置下就完成了,也就是調用InitDevice()。
it
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 );
}form
啊,這樣設備丟失就處理好了d3