AutoBotAM
AutoBotAM

Reputation: 1555

Window focus issue

I am currently programming a graphics application with OpenGL and the Windows API in C++. Unfortunately the image freezes under certain conditions, such as when I'm resizing the window, and/or when my mouse isn't moving. Is there some sort of mechanism I can use in Win32 to ensure that the frames are constantly being processed?

Here's some pseudocode describing the basic flow of my program

Main Loop

while(running)
{
    if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))   
    {
        if (Msg.message==WM_QUIT)               
        {
            SetRunning(false);                  
        }
        else                                    
        {
            TranslateMessage(&Msg);             
            DispatchMessage(&Msg);              
        }
    }
    else
    {
        SwapBuffers(deviceContext);
    }
}

WndProc

switch(msg)
{

case WM_CLOSE:
{
    PostQuitMessage(0);
    break;
}

case WM_SIZE:
{
    ResizeScreen(LOWORD(lParam),HIWORD(lParam));
    break;
}

}

return DefWindowProc(hwnd, msg, wParam, lParam);

EDIT: I read the tutorial Kol linked to and made some edits, and now the frame rate is consistent even when the mouse is not moving. However the image still freezes when I'm moving or resizing the window, so I'd appreciate help on that.

Upvotes: 0

Views: 434

Answers (1)

kol
kol

Reputation: 28728

Read the NeHe site to learn the basics of OpenGL with Win32. There are detailed explanations about how the message loop should look like, what the WM_SIZE handler should do etc.

EDIT

The code which draws the scene and the buffer swapping should be put into the message loop, in an else branch after the if (PeekMessage(...)) branch. See where the DrawGLScene() call is in the above mentioned NeHe example.

EDIT2

The problems were the followings:

  • The scene renderer function was not called in the WM_SIZE and WM_MOVE handlers.
  • The scene was drawn only once a second.

Upvotes: 1

Related Questions