2
\$\begingroup\$

I'm pretty confused by what's happening. Now that I've finally put something moving on my screen, I notice it doesn't update unless I move the mouse, or press a key, or trigger other events.

Using PIX, the "Frame: #" counter only goes up when I fire these events. This is all that happens in my game loop:

while(GetMessage(&msg, NULL, 0, 0) > 0 && m_isRunning)
{
        TranslateMessage(&msg);
        DispatchMessage(&msg);

        m_gameGraphics->BeginRender();
        m_gameGraphics->EndRender();
}

BeginRender clears the screen, and EndRender presents to the swap chain. I thought maybe it was a problem with WndProc, but comparing it to other DirectX 11 game WndProcs, I don't see any major differences. I'm pretty confused, never seen this problem before, and I have no idea what causes it. I'm just hoping maybe someone will have some insight on why this might be happening.

\$\endgroup\$
9
\$\begingroup\$

Actually that "while loop" that you wrote there is the source of your problem. GetMessage puts your application to sleep until a message arrives. This is good for GUI applications but obviously is not good for games.

The right way of doing the game loop is using PeekMessage instead so that your application is not put to sleep and can just keep spinning.

Here is the loop that you can see in DXUT and in most DirectX samples:

while( WM_QUIT != msg.message )
{
    // Use PeekMessage() so we can use idle time to render the scene. 
    bGotMsg = ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) != 0 );

    if( bGotMsg )
    {
        // Translate and dispatch the message
        if( hAccel == NULL || hWnd == NULL ||
            0 == TranslateAccelerator( hWnd, hAccel, &msg ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }
    else
    {
        // Render a frame during idle time (no messages are waiting)
        DXUTRender3DEnvironment();
    }
}

Here is a link that explains it in more detail.

\$\endgroup\$
1
  • \$\begingroup\$ Interesting, I must have read somewhere that PeekMessage was no longer needed, and that's why I didn't use it. I guess whatever I read wasn't about games. \$\endgroup\$ – smoth190 Mar 4 '12 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.