
GameWindow's internal loop
Posted Monday, 23 August, 2010 - 21:23 by Dr_Asik inHi, I started my project using SFML.NET and coded my own, fine-crafted game loop using Stopwatch for the best precision, lag detection etc. Now I've switched to OpenTK and haven't found a way to easily port my game loop. I've decided I could rely on GameWindow's integrated loop as long as it did what I wanted. The thing is, I'm not sure of that. First of all, what does OpenTK use internally to measure time? I hope it's not DateTime. :P Second, how does it cope with lag? Say we're behind on updates, does it stop drawing in order to catch up? If not, is there a way to implement that myself?
Thanks for any info.


Comments
Re: GameWindow's internal loop
It appears to skip drawing calls, depending on the run() parameters. I wrote a very slow raytracing example and, if I specified the target frame rate when calling run(), I would get two update calls for every draw call. This was occurring at around 6-7 FPS if I recall correctly.
This was not desirable in my test program, so I switched calling run without any parameters . That gave me update/draw calls with a 1-to-1 ratio.
I believe there is also an overloaded run() method that takes both a desired update_frequency and draw_frequency. That might give you more flexibility.
Re: GameWindow's internal loop
Few people know this, but GameWindow *does* let you use your own event loop. Example: (I figured it out by looking at the source of GameWindow.Run())
In fact, in my current project, I'm using Qt's eventloop to drive my OpenTK GameWindow.
As to what timer OpenTK uses internally - I suggest you look at the OpenTK sources. They're superbly written, easy to browse and even build with no hassle, if you ever want to make your changes to the source. They're installed in C:\Program Files\OpenTK\1.0\Source by default.
TheFiddler, are you reading? You should probably add this example to the OpenTK examples or tutorials. It's not good style by itself, but it helps when integrating with other frameworks like Qt and SDL.NET.
Re: GameWindow's internal loop
Oh. So you just never call window.Run(), and that's it. I feel dumb now. Thanks for pointing that out. As for what timer OpenTK uses internally, lemme check that out right now...
*looks at the source*
... it uses Stopwatch (yipee!). Actually, it is strikingly similar to the loop I had previously written. It does skip render frames to catch up, but even when doing that it will render a few frames to keep the game "alive" (one every 10 frame). This is precisely how I was doing it. :P But, of course, way better written and with additional logic I don't quite understand at a glance.
I love OpenTK right now.
Re: GameWindow's internal loop
The Run() method also raises the Load/Unload events, so keep in mind that those events won't be raised when supplying your own implementation.