Archived

This topic is now archived and is closed to further replies.

EdwardD

Losing focus in fullscreen mode....

Recommended Posts

When losing focus in fullscreen mode i.e. tabbing to another application and then coming back, how do you check and restore the surfaces so that it can resume where you left off. Im using C# and Direct3D 9. I jsut want to know how to check for it. Thanks.

Share this post


Link to post
Share on other sites
This function is in C++ but it shouldn't be hard to convert:


//Make sure device is ready for drawing

int CheckDevice ()
{
HRESULT hResult;

//Get device state

hResult = d3dDevice->TestCooperativeLevel();

//Check device state

if(hResult == D3DERR_DEVICELOST)
{
//Device is lost. Nothing can be rendered

return FALSE;
}
else if(hResult == D3DERR_DEVICENOTRESET)
{
//Reset the device

d3dDevice->Reset(&d3dPresent);

//Reset render states

d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

//Reset vertex shader

d3dDevice->SetVertexShader(NULL);
d3dDevice->SetFVF(D3DFVF_TLVERTEX);

//Reset vertex buffer

d3dDevice->SetStreamSource (0, vertexBuffer, 0, sizeof(TLVERTEX));

//Anything created in the DEFAULT pool must be reloaded

return TRUE;
}

//Device is fine

return TRUE;
}


Now obviously you'll have to set your own render states, but you get the idea.
Just use it like this:


if (CheckDevice())
{
d3dDevice->BeginScene();

//Draw

d3dDevice->EndScene();

d3dDevice->Present(whatever);
}


As long as you create all your vertex buffers, textures, etc. in the managed pool, the program should restore itself when it gets focus back.

If you have something in the default pool you have to reload it.

[edited by - glassJAw on August 12, 2003 1:53:12 PM]

Share this post


Link to post
Share on other sites