tags:

views:

124

answers:

2

I'm making a Win32 application with OpenGL in the main window (not using GLUT). I have my drawing code in WM_PAINT right now when I call swapBuffers it must be invalidating itself because it is constantly rerendering and using lots of cpu resources. How can I make it only render when it honestly receives WM_PAINT like when using GDI?

Thanks

A: 

I suspect you don't return 0 from your WM_PAINT handler. This means that WM_PAINTs just keep getting generated. If you return 0 then, I believe, the problem will go away.

Goz
+2  A: 

WM_PAINT messages keep on being sent until Windows has validated the dirty region of the window. The API that resets the dirty region is 'EndPaint'.

Calling SwapBuffers should not effect the invalid window region at all.

Your WM_PAINT handler should be something like the following:

case WM_PAINT:
  HDC hdc;
  PAINTSTRUCT ps;
  hdc = BeginPaint(hwnd,&ps);
  wglMakeCurrent(hdc,scene.m_oglContext);
  scene.Render(); //
  wglSwapBuffers(hdc);
  wglMakeCurrent(hdc,0);
  EndPaint(hwnd,&ps);
  return 0;

Lots of sample code for Open GL programming has a single HDC and OpenGL context setup at application start. While it makes sample code simpler, it does mean that the code cannot correctly deal with multiple OpenGL contexts. This WM_PAINT handler assumes that the OpenGL context is created for a scene, and then made current as necessary.

The side effect of swapping the OpenGL context as necessary is that, the hdc retrieved from BeginPaint is used as the render (and SwapBuffer) target, which means the OpenGL application will now paint synchronously if and when other windows are dragged over the App's window.

Chris Becke