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.
objarni's picture


Looking good and this does make OpenTK both simpler and more flexible :) Will be fun to try out one of these days!..

Kamujin's picture


Do you have plans to allow for a pluggable scheduler?

the Fiddler's picture


Yes, overriding the OnIdle method gives you complete control of the scheduler.

the Fiddler's picture


A WIP implementation of the INativeWindow and IGameWindow is now available at the 'gw-next' branch. It should be mostly compatible with the original GameWindow (still working on that).

Implemented as of this point:

  1. Everything that was in the original GameWindow.
  2. Icons, window placement and other functionality missing from the original GameWindow.
  3. Ability to change the scheduler.
  4. Relatively minor API cleanup (OnLoad, OnUpdateFrame etc are now protected instead of public. Parameters are more sane.)

It is untested on Linux. Unicode input is missing and keyboard / mouse input may be broken or behave abnormally. Some constructors are missing; the same with inline docs.

This is still a WIP and the API will undergo some changes, so take care if you decide to test this code.

Edit 1.a: Two of the examples have been converted to the new GameWindow (tutorials 1 and 2).

Edit 1.b: If you happen have 2 monitors, can you please test the aforementioned tutorials? Add a key to make the window go fullscreen (e.g. F11) and try to use it on each monitor: Does it fill the correct monitor?

The original implementation would always use the primary monitor when going fullscreen.

kanato's picture


I tested fullscreen on tutorial 1, and it seems to go to fullscreen on whatever monitor has most of the window. However if I go back to normal by doing this.WindowState = WindowState.Normal; I get a window that has no border or title bar.

objarni's picture


Could someone compile & upload the example-binaries so I can try them out on Ubuntu?

the Fiddler's picture


@kanato: thanks, sounds good. The borders-not-restored bug should be easy to fix.

@objarni: Here you are.

Examples-gw-next.7z1.07 MB
objarni's picture


My Ubuntu complains about the .7z format. .zip or .tar.gz is fine.

Edit: 7zip was available in the package manager. Sweet!

Now testing...

the Fiddler's picture


sudo apt-get install p7zip

It's necessary for day to day work.

objarni's picture


Wow 2nd cross-posting today ;) (first one about DL's)