Jump to content
  • Advertisement
Sign in to follow this  
dany arcand

Win32 main loop and framerate issue

This topic is 3612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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); }

Share this post


Link to post
Share on other sites
Advertisement
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
}

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!