Sign in to follow this  
sipickles

Toggling fullscreen and windowed....

Recommended Posts

sipickles    240
hi, I am trying to get my app to toggle happily between windowed and fullscreen modes. It runs fine in windowed mode, then seems to go to fullscreen mode, but wont toggle back. In fact, I have to restart after I've gone into full screen mode, since I cant get out of the app, and no other window is accessible, even thru alt-tab, or ctrl-alt-delete! At present, when the toggle request is made, I simple change the IsWindowed boolean variable, and release the device. Every loop, the app checks the device, and if NULL makes a new one. I know this is really sloppy, but I am on a learning curve :) btw, I don't have any device dependant resources to manage yet! Can anyone offer advice on the/a CORRECT way to do this? Much obliged! Si

Share this post


Link to post
Share on other sites
sirob    1181
You'd be much better off resetting the device, and not releasing it and creating a new one. You'll need to release any DEFAULT pool resources you have, but then again, if you release and recreate the device, you'll also need to release the MANAGED pool resources. Also, a reset would be quicker.

Hope this helps :).

Share this post


Link to post
Share on other sites
sipickles    240
Thanks Sirob,

That makes a lot of sense.

I now have this code at the start of my loop:
bool cFramework::Loop()
{
if ( g_device == NULL )
RestoreDevice( true ); // run for first time

if ( g_gameState == GAMESTATE_LOSTDEVICE )
RestoreDevice( false ); // reset device if requested


HRESULT hr = g_device->TestCooperativeLevel();
if ( hr==D3DERR_DEVICELOST )
{
return false; // app doesnt have focus
}
else if ( hr==D3DERR_DEVICENOTRESET )
{
// Reset device code here
RestoreDevice( false ); // reset device if lost
}




When the user presses ALT-ENTER the GAMESTATE_LOSTDEVICE flag is set. My RestoreDevice( bool firstTime ) function sets the presentation parameters, then calls IDirect3D9Device::Create if firstTime==true, or IDirect3D9Device::Reset if not.

This switches into fullscreen correctly, my log showing all relevant resources are released and reallocated after the reset.

However, just as with my previous method, I am unable to switch back to windowed mode. The app seems to hang. At least I can now ctrl-alt-delete to carry on, without a restart!

I am missing something vital.

Do I need to recreate the window or anything like that?

Thanks

Simon

Share this post


Link to post
Share on other sites
sipickles    240
ok, I am embarrassed now!

I think the problem was a break point that was hit while the app was in fullscxreen mode. I couldn't switch to MSVC to see that!

I'll get my coat.....

Si

Share this post


Link to post
Share on other sites
sipickles    240
Now I have the app toggling correctly, I have introduced meshes.

If I remember correctly, if the mesh was loaded using D3DPOOL_MANAGED, then a DX will automatically restore it when the device is reset...

What about the texture used by the mesh? this is loaded manually so does it have to be managed manually?

(btw, I get 3 items unallocated for every time I toggle the app atm! )

Thanks

Simon

Share this post


Link to post
Share on other sites
sirob    1181
Textures are just like any other resource (including Meshes). It all depends on the POOL. If you use the DEFAULT POOL, you need to take care of recreating it. If you use the MANAGED POOL, there is no need to recreate the resources, since they are not lost in the reset.

With a MANAGED POOL mesh, there is no need to reset not because DX takes care of it, but rather because theres simply nothing lost in the process.

This is all the same for Meshes, Surfaces, Textures, VBs, IBs, StateBlocks, and anything else I'm missing at the moment. Also, all the DX settings you modified (Render States, Texture Stage States, etc. etc.) are returned to defaults, so you need to reset them to what you want.

Hope this helps :).

Share this post


Link to post
Share on other sites
sipickles    240
Hi Sirob,

So I should be using D3DXCreateTextureFromFileEx rather than D3DXCreateTextureFromFile since the former supports D3DPOOL_MANAGED?

Or does D3DXCreateTextureFromFile do that by default?

Thanks

Simon

Share this post


Link to post
Share on other sites
sirob    1181
Quote:
From the DX SDK, under D3DXCreateTextureFromFile:
The function is equivalent to D3DXCreateTextureFromFileEx(pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, ppTexture).


This isn't to say you should ALWAYS use managed resources. They do have some drawbacks (they require more system memory and can't be used as render targets). In general, unless theres reason against using them, you should, in my opinion :).

Also, a Reset() or LostDevice doesn't happen just like that. It's usually either the user Alt-Tabbing or changing a setting, or a very unusual hardware issue that would cause it. Even if you were to use DEFAULT POOL resources and recreate from the HD on a reset, it still wouldn't make a huge difference, since it wouldn't happen often. Just stuff to consider.

Hope this helps. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this