Game Loops & Message Pumps

Started by
2 comments, last by Goblin 23 years, 1 month ago
This is driving me nutty. I''ve seen everything from: (Microsoft

BOOL bGotMsg;
    MSG  msg;
    PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );

    while( WM_QUIT != msg.message  )
    {
        // Use PeekMessage() if the app is active, so we can use idle time to
        // render the scene. Else, use GetMessage() to avoid eating CPU time.
        if( m_bActive )
            bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
        else
            bGotMsg = GetMessage( &msg, NULL, 0U, 0U );

        if( bGotMsg )
        {
            // Translate and dispatch the message
            if( 0 == TranslateAccelerator( m_hWnd, hAccel, &msg ) )
            {
                TranslateMessage( &msg );
                DispatchMessage( &msg );
            }
        }
        else
        {
            // Render a frame during idle time (no messages are waiting)
            if( m_bActive && m_bReady )
            {
                if( FAILED( Render3DEnvironment() ) )
                    SendMessage( m_hWnd, WM_CLOSE, 0, 0 );
            }
        }
    }
 
to... (LaMothe... both are fairly similar)

BOOL bGotMsg = FALSE;
PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );
while(WM_QUIT != msg.message ){
	
	// Use PeekMessage() if the app is active, so we can use idle time to
        // render the scene. Else, use GetMessage() to avoid eating CPU time.
        if( g_bIsActive )
            bGotMsg = PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE );
        else
            bGotMsg = GetMessage( &msg, NULL, 0U, 0U );
	
	if( bGotMsg )
        {
            // Translate and dispatch the message
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        } else {
    
		// main game processing goes here
		if(g_bDisplayReady){
			Game_Frame();
		}
	
	}

} // end while
 
to... (Parberry)

while(TRUE)
    if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE)){
      if(!GetMessage(&msg,NULL,0,0))return msg.wParam;
      TranslateMessage(&msg); DispatchMessage(&msg);
    }
    else if(ActiveApp)ProcessFrame(); else WaitMessage();
 
Basically, you don''t need to read any of that code, but I''m just wondering what bActive/bReady are, or when to change them.. I''m basically just wondering what is a simple way to do a message loop that could handle the app losing focus (minimized, whatever), and what WM_ message goes with that, and game pausing... What do you guys use? ----------------- The Goblin ----------------- "Oh, God..." "Yes?" <- My Response
- The Goblin (madgob@aol.com)
Advertisement
quote:
Basically, you don''t need to read any of that code, but I''m just wondering what bActive/bReady are, or when to change them..

I''m basically just wondering what is a simple way to do a message loop that could handle the app losing focus (minimized, whatever), and what WM_ message goes with that, and game pausing...

What do you guys use?


bActive and bReady determine the state of the program, logicly, ypu would set bActive when the game needs to updated. Then set it to false while it runs in the background, or is paused.

you would set bReady to true after the game has started, to assure that that deactivateing the reactivating the window doesn''t cause you to set the Active flag (assuring that the game is initialized before it can be run).

A simple message loop looks kinda like this:
 void MessageLoop(void) {   MSG msg;   while(1)   {     if(ProgramIsActive)       UpdateGame();     if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))     {       TranslateMessage(&msg);//You don''t really need this       DispatchMessage(&msg);     }   } }


Then, you would set ProgramIsActive after a, WM_ACTIVATEAPP message, a Pause command, or the games start.
If you look elsewhere in the sample code, you will see that bActive or whatever variable they use is being set by the WM_ACTIVATE_APP message or somethign similar. Basically it just stops performing logic and redrawing the screen if the application is not active.

Steve ''Sly'' Williams  Code Monkey  Krome Studios
Steve 'Sly' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers
To the AP: that would result in 100% CPU time - you ought to call WaitMessage() if the game isn''t active.

This topic is closed to new replies.

Advertisement