I believe the proper way to do it in OpenTK would be to inherit from the GameWindow
class, and then override OnUpdateFrame
and OnRenderFrame
. There is a QuickStart solution included when you checkout the trunk, check the Game.cs
file.
[Edit] To clarify further, OpenTK.GameWindow
class provides a Keyboard
property (of type OpenTK.Input.KeyboardDevice), which should be read inside OnUpdateFrame
. There is no need to handle keyboard events separately, as this is already handled by the base class.
/// <summary>
/// Called when it is time to setup the next frame. Add you game logic here.
/// </summary>
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
TimeSlice();
// handle keyboard here
if (Keyboard[Key.Escape])
{
// escape key was pressed
Exit();
}
}
Also, for a more elaborate example, download Yet another starter kit from their webpage. The class should look something like this:
// Note: Taken from http://www.opentk.com
// Yet Another Starter Kit by Hortus Longus
// (http://www.opentk.com/project/Yask)
partial class Game : GameWindow
{
/// <summary>Init stuff.</summary>
public Game()
: base(800, 600, OpenTK.Graphics.GraphicsMode.Default, "My Game")
{ VSync = VSyncMode.On; }
/// <summary>Load resources here.</summary>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// load stuff
}
/// <summary>Called when your window is resized.
/// Set your viewport/projection matrix here.
/// </summary>
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
// do resize stuff
}
/// <summary>
/// Called when it is time to setup the next frame. Add you game logic here.
/// </summary>
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
TimeSlice();
// handle keyboard here
}
/// <summary>
/// Called when it is time to render the next frame. Add your rendering code here.
/// </summary>
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
// do your rendering here
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
using (Game game = new Game())
{
game.Run(30.0);
}
}
}
I would recommend that you download Yask and check how it's implemented there.