views:

112

answers:

1

I have the following code:

MSG mssg;

// run till completed
while (true) {

 // is there a message to process?
 while(PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) {
  // dispatch the message
  TranslateMessage(&mssg);
  DispatchMessage(&mssg);
 }
 if(mssg.message == WM_QUIT){
  break;
 }
 // our stuff will go here!!
 Render();
 listeners->OnUpdate();
}

Once it enters the inner loop with peekmessage it does not exit until the application is closed. Thus if I place a breakpoint at Render() and OnUpdate(), they will never be called during the lifetime of the application.

This runs contrary to what I'm being told here and here. How do I do this properly?

+1  A: 

A typical game loop has this form:

MSG mssg;
bool notdone = true;
// run till completed
while (  notdone  ) {

        // is there a message to process?
        if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) {
            if (mssg.message == WM_QUIT) notdone = false;

            // dispatch the message
            TranslateMessage(&mssg);
            DispatchMessage(&mssg);
        } else {
            // our stuff will go here!!
            Render();
            listeners->OnUpdate();
        }
}
Nick D
this makes no difference to my predicament, placing a breakpoint in the else brackets shows that if(PeekMessage(...)) is always true in this case
Tom J Nowell
@Tom J Nowell, I fixed a bug. Check it out.
Nick D
Nick: Replace `if(PeekMessage(..` with `while(PeekMessage..`; It's more correct for Tom's usage, which is a game. If you parse one message for every time you render or change game state, events starts clogging up. That's no good.
Mads Elvheim
@Mads Elvheim, no user code will be called until the message queue is empty. Read the code more carefully ;)
Nick D
... actually the 2 loop forms are equivalent.
Nick D