the Fiddler's picture

Investigate possible WinForms-based GameWindow implementation.

Project:The Open Toolkit library

A Windows.Forms implementation of GameWindow will significantly reduce the amount of code we have to maintain in OpenTK.Platform. It will also make new new features easier to implement and the code will be better tested.

I have spent some time implementing GameWindow2 using Windows.Forms, GLControl and Kamujin's InterleavedScheduler. The resulting code is, indeed, much simpler and supports several features missing from the current implementation:

  • Support for custom main loops.
  • Support for window icons.
  • Support for setting the window position.
  • Saner event interface (closer to the BCL conventions).

On the other hand, the new code is missing keyboard and mouse support, cannot run at unlimited update / render frequencies and is not compatible with GameWindow.

I'd love to hear your feedback:

  1. Does it run on your system? (which OS?) You should see a black 640x480 window with a fps counter on the top-left corner. I am especially interested in Mac OS X.
  2. Is your fps stable and close to 60?
  3. How is the CPU and memory usage compared to the old implementation?
  4. Try setting different pixel formats in the GameWindow2 constructor - does it still work?

Binaries and source code are attached. I won't be having much free time the next few weeks, so please feel free to make modifications and corrections (consider it released under the OpenTK license).

Edit: New version which suppresses WinForms Mouse & Keyboard messages and changes FrameEventArgs.Time from TimeSpan to double.

Edit 2: New version that allows you to choose between the old and new GameWindow implementation for comparison.

Edit 3: New version that forces VSync to off.

Edit 4: Please ignore the attachment below, it is outdated. Use the file located here.

GameWindow2.7z391.81 KB


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kanato's picture


I've run it on my Windows desktop with an ATI Radeon 4650 and on my Mac Mini with an Intel GMA 950, and on both it seems to run the same way, at 60 fps. I will play with it more tonight. Have you run it through the CLR profiler at all?

the Fiddler's picture


Nope, not yet - I've only tested on Linux.

kanato's picture


Well, I ran it through the CLR profiler on Windows, and while it was idle the memory profile was flat (with the fps counter rendering turned off), so that is good, but probably not surprising. There are the usual WinForms allocations of EventArgs structure during mouse move events, etc.

I tried profiling it on Mac OS X but it seems the profiling in mono 2.2 doesn't work on the Mac. I'm not well versed in the BSD top application, but the CPU usage reported in top is 4%, but when I run the OpenTK Examples.exe with the immediate mode it hovers around 2.2%. I don't know if the numbers themselves are significant but their difference probably is. Memory usage of GameWindow2 is slightly less than Examples, if I'm reading the output correctly, although it's obviously a smaller executable. Memory usages stays constant, but without the mono profiler I suspect that is not a very useful metric.

the Fiddler's picture


Interesting, thanks.

I haven't been able to get the Mono profiler to work on Linux either, where it crashes with a "thread finalized early" exception. However, I've added a message filter to suppress all WinForms keyboard & mouse events, which seems to get rid of the nasty EventArgs allocations.

It's not surprising that the new implementation is heavier on Mono, as it's WinForms implementation has to emulate the win32 API (generate WM_* messages, etc). Without a profiler it is difficult to tell, but I think WinForms are a viable solution - provided the emulation layer on Mono doesn't generate any garbage.

I have uploaded a new version to the opening post, which contains the message filter and changes the FrameEventArgs.Time property to double instead of TimeSpan (the latter is not granular enough).

Anyone else had a chance to test?

objarni's picture


I'm on a 32 bit Vista machine, HP Pavilion portable with an Intel Core Duo 2.00GHz processor. The gfx card is an NVidia Geforce 8400M GS, version 7.15 of the gfx drivers.

Results running the Release version of GameWindow2.exe:

1. First one or two seconds the FPS flickers around 30-35 fps
2. After that, flickers around 60-65 fps
3. Program exits after a few more seconds.

Is there an "old version" available to compare with, which uses the old GameWindow implementation?

the Fiddler's picture


Thanks. I've uploaded a new version that allows you to choose between the old and the new implementation for a direct comparison.

There are two fps counters:

  • The first is calculated as 1 / e.Time, where e.Time is the time that has passed since the previous render event.
  • The second is the average fps over one second.

It is normal for the second counter to start at 0 and make its way up to ~60 after a couple of seconds. Afterwards, it should stay at 60fps or so (for better results, try with vsync disabled).

JTalton's picture


Windows XP - Quad Xeon 2.6 - Quadro FX550
Both old and new run at 60 FPS and use 0% to 1% CPU.

the Fiddler's picture


Interesting. JTalton, can you please download and test the CPU usage again? I have attached a new version that forces VSync to off (unless your drivers override that).

Can you also take a look at memory consumption in the task manager? (it should stabilize after a few seconds - is it significantly different between the two implementations?)

martinsm's picture


Intel Core 2 Duo T9500 @ 2.6GHz, Nvidia 8400M GS, Windows 7 x64
old: fps=60, avg=61, 50% cpu usage, ~74-77 Mb
new: fps=58 or 62, avg=61, 0% cpu usage, ~78-82 Mb

Intel Quad Core Q6600 @ 2.4Ghz, ATI HD2400, Windows Vista SP1 32-bit
old: fps=60, avg=61, 25% cpu usage, ~39 Mb
new: fps=32 or 64, avg=58fps, 0% cpu usage, ~47Mb
Also on this computer new GameWindow at start stays on 32fps for about 4 seconds, only after that i goes up to 64fps and then jumps from ~32 to ~64 all the time.

the Fiddler's picture


Also on this computer new GameWindow at start stays on 32fps for about 4 seconds, only after that i goes up to 64fps and then jumps from ~32 to ~64 all the time.
Damn, that's not good at all.

Does this still occur if you set CPU affinity?