Game Development Reference
In-Depth Information
sound effects, speech, or music make great use of this design because of the depen-
dency and timing features.
Playing Nicely with the OS
Now that we
ve seen what goes on inside the main loop and some techniques for
managing your various processes, let
'
s take a step out of that and look at how the
game loop fits into the operating system. This is especially important if you
'
re mak-
ing a game for a multitasking platform like Windows. You need to learn how to play
nicely with the operating system and the other applications running on it. For exam-
ple, this code would cause Windows to think your program has stalled:
'
while (true)
{
RunLogic();
RenderScene();
}
The problem here is that the code is completely ignoring all messages being sent to it.
You can
t click the X button at the top right, because none of the mouse messages get
through, and Windows considers the program to be unresponsive. It will eventually
say not responding next to your app in the Task Manager. It ' s important to
respond to messages being sent from the operating system, even if you just pass
them through to the default handler:
'
return DefWindowProc(hwnd, msg, wparam, lparam);
Another problem with working on a multitasking platform like Windows is that you
sometimes have to yield resources to those applications. For example, games typically
acquire exclusive access to system resources like the video card, which allows them to
render in full screen at custom resolutions. If the user Alt-tabs, you will lose that
exclusive control and need to be able to handle that situation. You
ll learn more
about this later in this chapter when we talk about the DirectX 11 Framework.
On Windows, you typically have a message pump like this:
'
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
 
 
Search WWH ::




Custom Search