# Why does my game not update unless I'm moving the mouse?

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.

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.

• 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. Mar 4 '12 at 16:14