views:

146

answers:

3

I'm writing a 3D application for Windows, using OpenGL. For now it renders a simple test scene with one model of about 50000 polygons and it renders smoothly at 60FPS.

However, the framerate gets very uneven whenever the mouse is moved over the application window. It fluctuates from 400 FPS to 20 FPS randomly. Is there any reason for this? Is this caused by mouse events the app is forced to handle? Can I disable them and just poll the mouse state?

My app loop is very simple, something more or less like:

if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
  if (msg.message==WM_QUIT)
  {
    quit();
  }
  else
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
}
else
{
  draw();
  Sleep(1);
}

and the window procedure is basically

return DefWindowProc(hWnd,uMsg,wParam,lParam);
A: 

Sounds like a thread problem. What about using a library like glut to handle the opengl main loop for you?

John Ellinwood
I can't, it has to be using pure WinAPI.
Kronikarz
+1  A: 

In addition to the mouse move messages, moving over a window will generate WM_SETCURSOR messages. You might try discarding those and the WM_MOUSEMOVE messages to see if the app speeds up. To discard, return 1 instead of calling DefWindowProc.

There's no way to turn off the mouse messages, but throwing them away should be quick. You can use GetCursorPos to poll for the mouse coordinates.

Mark Ransom
That's the thing, I'm not handling any messages except WM_CLOSE. Returning 0 or 1 on WM_MOUSEMOVE doesn't change a thing.
Kronikarz
You may not be handling them, but DefWindowProc is.
Mark Ransom
A: 

A bit of a shot in the dark, but will the WM_PAINT message trigger your app to redraw? If that is where you rendering is happening, then it sounds like the mouse is generating InvalidateRects and forcing the window to update.

Generally I don't let windows poll the mouse at all, but the app itself polls as needed.

Brandorf
Nah, I don't handle the WM_PAINT message.
Kronikarz
Like everyone is saying DefWindowProc is handling them.
thing2k