Sign in to follow this  

[DirectX9][C++] Implementing Alt+Tab

This topic is 2037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently working on a game project with my college class and have been tasked with implementing the ability to alt+tab in/out of our game and to switch between windowed/fullscreen mode with alt+enter. I'm not the strongest person in rendering code, and I wasn't the one to code our renderer, but after a number of google searches I have discovered that it has something to do with losing devices (maybe?), so do you have any suggestions on how to handle and implement these functionalities, any advice you can give I would be truly grateful.

Share this post


Link to post
Share on other sites
If you use fullscreen mode and press alt+tab your device will be lost. When you lose your device, you have to:
- release all memory (textures, vertexbuffers, ...) which is used by the graphics card (I think [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img])
- reset device
- reload everything

You should try this:
[CODE]
bool Graphics_win::End()
{
// device alive
if (!DeviceLost)
{
device->EndScene();
device->Present(0, 0, 0, 0);
}
HRESULT hr = device->TestCooperativeLevel();
if (hr != D3D_OK)
{
if (hr == D3DERR_DEVICELOST)
{
DeviceLost = true;
}
else if (hr == D3DERR_DRIVERINTERNALERROR)
{
Helpers::Logger::GetInstance()->WriteError("Driver internal error.");
PostQuitMessage(0);
}
return false;
}
return true;
}
[/CODE]

and the reset code:

[CODE]
bool Graphics_win::Reset()
{
HRESULT hr = device->TestCooperativeLevel();
if (hr == D3DERR_DEVICENOTRESET)
{
if (SUCCEEDED(device->Reset(&presentParameters)))
{
// reset success
DeviceLost = false;
return true;
}
}
// failed to reset
return false;
}
[/CODE]
and in the main loop when End() is returned by false, you have to release memory, and try to reset the device. When device reset is succeeded, you have to reload all data, and that's it.

EDIT:
You should use a "reset counter" which counts how many times you tried to reset the device, and if this counter is greater than a constant (I used 3), you have to close your program. And of course you should use a little sleep() between every reset attempt. If you can't reset the device, you haven't released every necessery data.

Oh, and sorry for my bad english, but I hope you understand what I wrote [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by csisy

Share this post


Link to post
Share on other sites
[quote name='csisy' timestamp='1337506150' post='4941615']
If you use fullscreen mode and press alt+tab your device will be lost. When you lose your device, you have to:
- release all memory (textures, vertexbuffers, ...) which is used by the graphics card (I think [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img])
- reset device
- reload everything
[/quote]
Unless you use the managed pool.
if you use the managed pool D3D will will handle that for you( it keeps a copy in system memory ), except resetting the device.

Share this post


Link to post
Share on other sites
[quote name='ryan20fun' timestamp='1337521293' post='4941635']
[quote name='csisy' timestamp='1337506150' post='4941615']
If you use fullscreen mode and press alt+tab your device will be lost. When you lose your device, you have to:
- release all memory (textures, vertexbuffers, ...) which is used by the graphics card (I think [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img])
- reset device
- reload everything
[/quote]
Unless you use the managed pool.
if you use the managed pool D3D will will handle that for you( it keeps a copy in system memory ), except resetting the device.
[/quote]
Oh yeah, I always forget the managed pool (because I don't use it :D)

Share this post


Link to post
Share on other sites
Plus you have to switch screen resolution back to desktop resolution then switch back to in game resolution (tabbing out and in respectively). It's not a must programming/D3D-wise, but user-experience-wise it's very important. You should (if I recall correctly) minimize the application to the taskbar too (if it was in full-screen mode) when alt-tabbing out.

The flow of the game should also be taken care of, ask you team-mates or project-leader about the desired behaviour when alt-tabbing out: pausing the game, or not pausing the game. If you don't pause the game, you have to take care of the timing of the loop since v-sync won't affect the loop (if you have that as the capping of the FPS). By this I mean you probably shouldn't let the fps running wild and eating all the CPU while being alt-tabbed out.

EDIT: maybe other things have to be handled too (it was long ago since I looked into the topic), such are releasing the mouse if it was captured.
Or maybe capturing the mouse if the game was switched to windowed mode. If the game has sound, probably that has it's recommendations too.

It's not a simple topic, I don't know how sophisticated you have to make it though. I'm sure others have more ideas what you have to take care of. Edited by szecs

Share this post


Link to post
Share on other sites
Exiting exclusive mode via alt-tab, and going back to it, should automatically handle any resolution/colour-depth switching for you - not necessary to do that in your program.

Also, you should always always always be using the managed pool unless you have a very specific case that is documented as not using it. There's no theoretical performance advantage from not using it for the majority of object types, your code becomes simpler, cleaner and more robust, you get device-loss-handling for free, there's nothing to lose and everything to gain.

Share this post


Link to post
Share on other sites

This topic is 2037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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