A simple game loop might look like this:
Writing a game loop in using the Windows API doesn't seem so straight forward. What is one method?Code:while(!exit)
{
showIntro()
while(game)
{
}
}
Printable View
A simple game loop might look like this:
Writing a game loop in using the Windows API doesn't seem so straight forward. What is one method?Code:while(!exit)
{
showIntro()
while(game)
{
}
}
I usually do this. It puts messages as first priority so they won't stack up.
Code:msg Message;
bool GameIsOn = true;
while(GameIsOn)
{
if(PeekMessage(&Message, NULL, 0, 0))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
else
{
//Update/Render/Whatever
//To quit, set GameIsOn to false
}
}
Note the use of PeekMessage() rather than GetMessage().
PeekMessage() will return if there is not a message, allowing the game to continue. This however maens that there will be no point where the game is doing nothing ie100% CPU use
GetMessage() will not return until there is a message for the app. This means the app can sit dormant until it is needed. It will not be able to do background processing unless the app generates a timer msg (very inaccurate)
To be 100% accurate should use the new message loop (which handles the -1 error return)
Code:
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
But in a game loop you DO want to make calculations periodically wether a message has been sent or not. Using GetMessage, as you said, will halt execution until another message is sent.