Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
97 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 The Message Pump
 Updating the World
 The Solution
 ALT-TABBING

 Printable version

 


ALT-TABBING

Making a game truly multitasking under Windows is perhaps one of the most hazardous issues in game programming. A good application must be able to correctly switch to another application. Some games simply won’t allow the user to ALT-TAB at all. We will try to do it the right way.

As a first attempt, we can try to use the SuspendThread() and ResumeThread() methods in the WindowProc() function (the one that effectively handles the messages), but, although I’ve tried and tried, I've never been able to get them to work. Sometimes, the thread was successfully suspended, but it would never get back to life. If someone has been able able to get this approach to work, I'd love to know what you have done.

What I’ve done to solve this, with very positive results, is to implement waiting code in my FlipSurfaces() function (since it is constantly being called while the game is running). I declared a global bSuspended variable (globals have multithreaded scope) to inform the game thread that the game is in suspended state. Then I’ve inserted this code in FlipSurfaces():

while( bSuspended ) Sleep( 1000 );

It’s quite simple and easy to implement, and, most important, it always works. You just have to set the bSuspended global in the WindowProc() function every time that the application is activated or deactivated. Just remember to restore lost surfaces, and you are ready.

With this simple implementation, when the user ALT-TABs, the game will stop its execution, freeing all the processor’s time to enable the user to do other tasks. If your game needs to not be suspended, just suspend the rendering pipeline, and keep on updating the world.

I hope that you liked this article, and that you find the linear programming model as enjoyable and rewarding as I do. You can contact me at javier@sabarasa.com with any comments, criticism, queries or suggestions about this article. I am really curious if anyone here likes the finite state machine programming model using the message pump.


Javier F. Otaegui is the CTO and Lead Programmer of Sabarasa Entertainment, a game development company of Buenos Aires, Argentina. Sabarasa Entertainment has successfully finished and launched a RTS game called Malvinas 2032 in his country, and will start US and another countries commercialization soon. Current projects include a wide variety of things, among them 3d landscapes, online multiplaying and highly-addictive gameplay. Please check the company website for more information www.sabarasa.com

© 2000 by Javier F. Otaegui - email: javier@sabarasa.com