0

This is my current game loop in a normal Window:

while(running)
{
    while( PeekMessage( &msg, 0, 0, 0, PM_REMOVE ) )
    {
        if( msg.message == WM_QUIT )
        {
            running = false;
        }

        TranslateMessage( &msg );
        DrawGame(&game);
        DispatchMessage( &msg );
    }

    UpdateGame(&game);
    InvalidateRect(hwnd, NULL, 1);
    //Sleep(100);
}

... but should I actually place DrawGame between Translate and DispatchMessage? If I put DrawGame above/below UpdateGame nothing is ever drawn.

And after a while if I draw a simple rectangle they start overlapping eachother. It runs good for about a minute, draw a grid of rectangles (out of position due to: window size that you set isn't the correct size of the window. And after a while it draw another grid and sometimes it screws up totally and the window appears to have crashed and I must press "stop" button in Visual Studio.

So, where exactly do I place a Game loop and how do I know when to Draw the game? I need to draw rectangles primarly, although that isn't working so good either.

Community
  • 1
  • 1
Deukalion
  • 2,516
  • 9
  • 32
  • 50

1 Answers1

1

You should call DrawGame() from you Windows message callback when handling a WM_PAINT message.

In the message loop you are supposed to only fetch and dispatch Windows messages to the appropriate callback.

Additionally this is also the place to update the game world, but not for doing draw calls.

Paulo Pinto
  • 632
  • 4
  • 10