views:

514

answers:

2

I'm creating a game engine using wxWidgets and OpenGL. I'm trying to set up a timer so the game can be updated regularly. I don't want to use wxTimer, because it's probably not accurate enough for what I need. I'm using a while (true) and a wxStopWatch:

while (true) {
 stopWatch.Start();
 <handle events> // I need a function for this
 game->OnUpdate();
 game->Refresh();
 if (stopWatch.Time() < 1000 / 60)
  wxMilliSleep(1000 / 60 - stopWatch.Time());
}

What I need is a function that will handle all the wxWidgets events, because right now my app just freezes.

UPDATE: It doesn't. It's slightly jerky on Windows, and when tested on a Mac, it was extremely jerky. Apparently EVT_IDLE doesn't get called consistently on Windows, and even less on a Mac.

UPDATE2: It actually mostly does. It's fine on a Mac; I misunderstood my Mac tester's reply.

+1  A: 

Instead of using a while (true) loop, I'm using EVT_IDLE, and it works perfectly.

UPDATE: It doesn't. It's slightly jerky on Windows, and when tested on a Mac, it was extremely jerky. Apparently EVT_IDLE doesn't get called consistently on Windows, and even less on a Mac.

UPDATE2: It actually mostly does. It's fine on a Mac; I misunderstood my Mac tester's reply.

Micah
Mark this as the solution. :-) (If that's allowed ... I'm new here.)
GreenReign
You can accept your own answer by clicking the big check mark under the vote count. No need to annotate the title.
John Sheehan
Oh, sorry.I have to wait 3 more hours before doing so, though :P
Micah
A: 
  • Have you requested idle events to be generated at the maximum rate? You have to call RequestMore() on the event, if you don't you will get the next idle event only after some other event has been processed. Note that constant idle processing will cause 100% CPU load on one core.
  • Even if you request more idle events you can't be sure how long it will take for the next one to arrive. Therefore to get smooth animation you will need to calculate the elapsed time since the last event, and update the display accordingly.
mghie
I am using a StopWatch to ensure that the display is being refreshed at a constant rate.I'd rather not cause 100% CPU load, and there was actually far less of a problem than I originally thought, so I'll probably just leave things as they are.
Micah