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) { } }
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 } }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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); } }
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
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.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.