Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
2  
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. –  Jonathan Connell May 30 '11 at 11:15
add comment

1 Answer

up vote 5 down vote accepted

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();
}
share|improve this answer
    
Thanks! This clarifies a lot. –  DeadMG May 30 '11 at 20:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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