4
\$\begingroup\$

I've seen people who've written in code where the graphics of a scene are rendered in response to WM_PAINT. I only ever rendered the scene once all Windows messages were done translating, and then rendered the scene. Is this the wrong way to go about it, or should I only use DirectX rendering in response to WM_PAINT?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ I believe WM_PAINT is used so that the scene is rendered on all special updates (like maximize, resize, etc.) although I'm not sure. \$\endgroup\$ – Jonathan Connell May 30 '11 at 11:15
6
\$\begingroup\$

WM_PAINT response code is a convenient place to put rendering routines, especially when rendering in a window, but I believe - it is not the best.

WM_PAINT message is recieved by window message loop when system wants to update its contents, eg. during moving or resising window or when window content is showed again due to closing other overlapping window. Rendering in response to WM_PAINT assures that rendered content stays accurate. It is up to programmer to assure that WM_PAINT is posted to window's message queue often enough, to keep up to the desired rendering framerate. It can be achieved by calling

BOOL InvalidateRect(__in  HWND hWnd, __in  const RECT *lpRect, __in  BOOL bErase);

function.

It is not necessary to do it that way, as in fact- posting and processing window messages can introduce a significant delay to rendering pipeline.

Better way is to call rendering routines from the message loop itself, something like this:

BOOL done = false;
while (!done)
{
    if (PeekMessage(&msg, wnd, 0, 0, false))
    {
        int result = GetMessage(&msg, wnd, 0, 0);

        if (result == -1)
        {
             // process GetMessage() error
        }
        else
        {
             if (msg.message == WM_QUIT)
                 done = true;

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

    DoTheUpdatingHere();
    DoTheRenderingHere();
}
\$\endgroup\$
0

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.