Best practices for Alt-Tab support in a DirectX app?

chaos picture chaos · Jun 9, 2009 · Viewed 10.4k times · Source

When writing DirectX applications, obviously it's desirable to support the user suspending the application via Alt-Tab in a way that's fast and error-free. What is the best set of practices for ensuring this? Things that need to be addressed include:

  1. The best methods of detecting when your application has been alt-tabbed out of and when it has been returned to.
  2. What DirectX resources are lost when the user alt-tabs, and the best ways to cope with this.
  3. Major things to do and things to avoid in application architecture for purposes of alt-tab support.
  4. Any significant differences between major DirectX versions as they apply to the above.

Interesting tricks and gotchas are also good to hear about.

Answer

jjxtra picture jjxtra · Jun 9, 2009

I will assume you are using C++ for the purposes of my answers, but if you can afford to use C#, XNA (http://creators.xna.com/) is an excellent game platform that handles all of these issues for you.

1]

This article is helpful for windows events in the window procedure to detect when a window loses or gains focus, you could handle this on your main window: http://www.functionx.com/win32/Lesson05.htm. Also, check out the WM_ACTIVATEAPP message here: http://msdn.microsoft.com/en-us/library/ms632614(VS.85).aspx

2]

3]

I would make sure to never disable Alt-Tab. You probably want minimal CPU load while the application is not active because the user probably Alt-Tabbed because they want to do something else, so you could completely pause the application, or reduce the frames rendered per second. If the application is minimzed, you of course don't need to render anything either. After thinking about a network game, my best solution is that you should still reduce the frames rendered per second as well as the amount of network packets handled, possibly even throwing away many of the packets that come in until the game is re-activated.

4]

Honestly I would just stick to DirectX 9.0c (or DirectX 10 if you want to limit your target operating system to Vista and newer) if at all possible :)

Finally, the DirectX sdk has numerous tutorials and samples: http://www.microsoft.com/downloads/details.aspx?FamilyID=24a541d6-0486-4453-8641-1eee9e21b282&displaylang=en