Win32 main loop and framerate issue

Started by
3 comments, last by Colin Jeanne 15 years, 4 months ago
I used to have the following as the main loop of my game: while(1) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); } // Game code here } I recently found out strange peeks in the framerate when pressing and holding any keyboard key, thus making the game behave incorrectly because of incorrect framerate and deltatime values. The solution ended up being quite simple: simply put the game code in a "else" condition, like the following: while(1) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); } else { // Game code here } } So my question is, why is there framerate peeks when the game code isn't in a "else" condition ? When "googling" around, I found that both solutions (with or without else condition) are widely used and nobody seems to have had the same issue =( Thanks! Additional info: - I'm a 99% sure the problem doesn't come from the clock system. - I use DirectInput for keyboard handling. - Here's the WinProc function used: LRESULT CALLBACK BasicWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_ACTIVATE: { // some stuff regarding alt-tab handling return 0; } break; case WM_CLOSE: { PostQuitMessage(0); return 0; } break; case WM_SYSCOMMAND: { switch (wParam) { case SC_SCREENSAVE: case SC_MONITORPOWER: return 0; } } break; default: break; } return DefWindowProc(hWnd,uMsg,wParam,lParam); }
Advertisement
Are you doing any calculations before calling peekmessage? Post all of the code.
The difference is that before after every single message you would've run the game code.

With the "else" change you now process every message before running through the game code. You could make that more visible by using a while loop for PeekMessage as well:

bool Running = true;while ( Running ){  while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )  {    if ( msg.message == WM_QUIT )    {      Running = false;      break;    }    TranslateMessage( &msg );    DispatchMessage( &msg );  }  // Game code here}

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Can't peek message return an error. I believe the correct way looks like:
<snip wrong code, Colin Jeanne corrected me>



[Edited by - Sirisian on November 29, 2008 2:22:10 AM]
Quote:Original post by Sirisian
Can't peek message return an error.

No. GetMessage() does that.

This topic is closed to new replies.

Advertisement