// This code was written for the OpenTK library and has been released
// to the Public Domain.
// It is provided "as is" without express or implied warranty of any kind.
using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
namespace Examples.Tutorial
{
///
/// Demonstrates the GameWindow class.
///
[Example("GameWindow Simple", ExampleCategory.OpenTK, "GameWindow", 1, Documentation = "GameWindowSimple")]
public class SimpleWindow : GameWindow
{
public SimpleWindow() : base(800, 600)
{
Keyboard.KeyDown += Keyboard_KeyDown;
}
#region Keyboard_KeyDown
///
/// Occurs when a key is pressed.
///
/// The KeyboardDevice which generated this event.
/// The key that was pressed.
void Keyboard_KeyDown(object sender, KeyboardKeyEventArgs e)
{
if (e.Key == Key.Escape)
this.Exit();
if (e.Key == Key.F11)
if (this.WindowState == WindowState.Fullscreen)
this.WindowState = WindowState.Normal;
else
this.WindowState = WindowState.Fullscreen;
}
#endregion
#region OnLoad
///
/// Setup OpenGL and load resources here.
///
/// Not used.
protected override void OnLoad(EventArgs e)
{
GL.ClearColor(Color.MidnightBlue);
}
#endregion
#region OnResize
///
/// Respond to resize events here.
///
/// Contains information on the new GameWindow size.
/// There is no need to call the base implementation.
protected override void OnResize(EventArgs e)
{
GL.Viewport(0, 0, Width, Height);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
}
#endregion
#region OnUpdateFrame
///
/// Add your game logic here.
///
/// Contains timing information.
/// There is no need to call the base implementation.
protected override void OnUpdateFrame(FrameEventArgs e)
{
// Nothing to do!
}
#endregion
#region OnRenderFrame
///
/// Add your game rendering code here.
///
/// Contains timing information.
/// There is no need to call the base implementation.
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Begin(BeginMode.Triangles);
GL.Color3(Color.MidnightBlue);
GL.Vertex2(-1.0f, 1.0f);
GL.Color3(Color.SpringGreen);
GL.Vertex2(0.0f, -1.0f);
GL.Color3(Color.Ivory);
GL.Vertex2(1.0f, 1.0f);
GL.End();
this.SwapBuffers();
}
#endregion
#region public static void Main()
///
/// Entry point of this example.
///
[STAThread]
public static void Main()
{
using (SimpleWindow example = new SimpleWindow())
{
// Get the title and category of this example using reflection.
Utilities.SetWindowTitle(example);
example.Run(30.0, 0.0);
}
}
#endregion
}
}