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

 


The Solution

To solve this, I developed a fairly simple multithreading model, that frees the game's programmer from the message pump and its undesirable finite state programming model.

Fortunately, Windows supports multithreading. This means that our application runs several simultaneous execution threads. The idea is very simple: put the message pump in one thread, put the game in another thread.

The message pump will remain in the initial thread (being the initial thread does not imply that it has more priviledges or importance). We can thus remove the UpdateWorld() function from the Message Pump and return it to its simplest form.

Now, we just need to add the code necessary to initiate the game thread to the DoInit() function.

HANDLE hMainThread; // Main Thread handle static BOOL doInit( ... ) { .... // Initialize DirectX and everything you want DWORD tid; hMainThread=CreateThread( 0, 0, &MainThread, 0, 0, &tid); return TRUE; }

And MainThread() is defined by:

DWORD WINAPI MainThread( LPVOID arg1 ) { RunGame(); PostMessage(hwnd, WM_CLOSE, 0, 0); return 0; }

Now, here we are. MainThread() will invoke our RunGame() function, and when it is finished, we just post a WM_CLOSE message to tell the other thread to finish execution.




Next : ALT-TABBING