Sign in to follow this  

Message Processing Loop

This topic is 4834 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

Which of the following would be better?
while (true)
{
  MSG msg;
  bool MessageReady = PeekMessage(&msg, ...);
  pGameEngine->RenderFrame();

  if (MessageReady)
  {
    if (msg.message == WM_QUIT)
      break;
  }
}

or

while (true)
{
  MSG msg;
  bool MessageReady = true;
  pGameEngine->RenderFrame();

  while (MessageReady)
  {
    MessageReady = PeekMessage(&msg, ...);
    if (MessageReady)
    {
      if (msg.message == WM_QUIT)
        break;
    }
  }
}


The obvious difference is that the second one will check every message in the queue during this frame and empty it. At 60 FPS, it's highly likely that there won't be more than one message in the queue when you get to this point, but if something happened that caused the frame rate to drop to 1 FPS, you would still process the messages (which would allow the user to exit more quickly). Is this advantageous at all? Could it cause any problems? You'll probably note that I'm rendering in the message processing loop. That's just temporary, as it will become a separate thread soon. I just was curious what others thought about this and if anyone else uses it. Thanks, Chris

Share this post


Link to post
Share on other sites
The second method is better. Its possible that your update function will generate a window message. If that happens and you only process one message per frame, then you'll never empty the message queue.
Think about WM_MOUSEMOVE messages, you could get several in a frame. While it might not cause problems at 60FPS, if your FPS drops, the user will start to experience laggy input.

Share this post


Link to post
Share on other sites
Thanks Steve, that's pretty much what I thought, but when I use the second one, it does something strange in the program. I'll post when I figure out exactly what, but it crashes on close.

That raises another question that I had. I think you answered it. You can get multiple WM_MOUSEMOVE messages in the queue? I figured when the queue already had a WM_MOUSEMOVE, it just got updated to the new mouse coordinates, to keep from storing 800x600 possible MOUSEMOVE messages if you didn't process them for a while (assuming 800x600 screen resolution). Is that not the case?

Chris

Share this post


Link to post
Share on other sites

This topic is 4834 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this