Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Goblin

Game Loops & Message Pumps

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

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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?
[/QUOTE]

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.

Share this post


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

Share this post


Link to post
Share on other sites

  • 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!