Show / Hide Table of Contents

Class GameWindow

The GameWindow class contains cross-platform methods to create and render on an OpenGL window, handle input and load resources.

Inheritance
Object
NativeWindow
GameWindow
Implements
IGameWindow
INativeWindow
IDisposable
Inherited Members
NativeWindow.Close()
NativeWindow.PointToClient(Point)
NativeWindow.PointToScreen(Point)
NativeWindow.ProcessEvents()
NativeWindow.Bounds
NativeWindow.ClientRectangle
NativeWindow.ClientSize
NativeWindow.Cursor
NativeWindow.Exists
NativeWindow.Focused
NativeWindow.Height
NativeWindow.Icon
NativeWindow.Location
NativeWindow.Size
NativeWindow.Title
NativeWindow.Visible
NativeWindow.Width
NativeWindow.WindowBorder
NativeWindow.WindowInfo
NativeWindow.X
NativeWindow.Y
NativeWindow.CursorVisible
NativeWindow.CursorGrabbed
NativeWindow.Closed
NativeWindow.Closing
NativeWindow.Disposed
NativeWindow.FocusedChanged
NativeWindow.IconChanged
NativeWindow.KeyDown
NativeWindow.KeyPress
NativeWindow.KeyUp
NativeWindow.Move
NativeWindow.MouseEnter
NativeWindow.MouseLeave
NativeWindow.Resize
NativeWindow.TitleChanged
NativeWindow.VisibleChanged
NativeWindow.WindowBorderChanged
NativeWindow.WindowStateChanged
NativeWindow.MouseDown
NativeWindow.MouseUp
NativeWindow.MouseMove
NativeWindow.MouseWheel
NativeWindow.FileDrop
NativeWindow.EnsureUndisposed()
NativeWindow.IsDisposed
NativeWindow.OnClosed(EventArgs)
NativeWindow.OnDisposed(EventArgs)
NativeWindow.OnFocusedChanged(EventArgs)
NativeWindow.OnIconChanged(EventArgs)
NativeWindow.OnKeyDown(KeyboardKeyEventArgs)
NativeWindow.OnKeyPress(KeyPressEventArgs)
NativeWindow.OnKeyUp(KeyboardKeyEventArgs)
NativeWindow.OnMove(EventArgs)
NativeWindow.OnMouseEnter(EventArgs)
NativeWindow.OnMouseLeave(EventArgs)
NativeWindow.OnMouseDown(MouseButtonEventArgs)
NativeWindow.OnMouseUp(MouseButtonEventArgs)
NativeWindow.OnMouseMove(MouseMoveEventArgs)
NativeWindow.OnMouseWheel(MouseWheelEventArgs)
NativeWindow.OnFileDrop(FileDropEventArgs)
NativeWindow.OnTitleChanged(EventArgs)
NativeWindow.OnVisibleChanged(EventArgs)
NativeWindow.OnWindowBorderChanged(EventArgs)
NativeWindow.OnWindowStateChanged(EventArgs)
NativeWindow.ProcessEvents(Boolean)
Object.ToString()
Object.Equals(Object)
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Namespace: OpenTK
Assembly: OpenTK.dll
Syntax
public class GameWindow : NativeWindow, IGameWindow, INativeWindow, IDisposable
Remarks

GameWindow contains several events you can hook or override to add your custom logic:

Call the Run() method to start the application's main loop. Run(double, double) takes two parameters that specify the logic update rate, and the render update rate.

Constructors

GameWindow()

Constructs a new GameWindow with sensible default attributes.

Declaration
public GameWindow()

GameWindow(Int32, Int32)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GameWindow(Int32, Int32, GraphicsMode)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height, GraphicsMode mode)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GameWindow.

GameWindow(Int32, Int32, GraphicsMode, String)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height, GraphicsMode mode, string title)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GameWindow.

String title

The title of the GameWindow.

GameWindow(Int32, Int32, GraphicsMode, String, GameWindowFlags)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height, GraphicsMode mode, string title, GameWindowFlags options)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GameWindow.

String title

The title of the GameWindow.

GameWindowFlags options

GameWindow options regarding window appearance and behavior.

GameWindow(Int32, Int32, GraphicsMode, String, GameWindowFlags, DisplayDevice)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height, GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GameWindow.

String title

The title of the GameWindow.

GameWindowFlags options

GameWindow options regarding window appearance and behavior.

DisplayDevice device

The OpenTK.Graphics.DisplayDevice to construct the GameWindow in.

GameWindow(Int32, Int32, GraphicsMode, String, GameWindowFlags, DisplayDevice, Int32, Int32, GraphicsContextFlags)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height, GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device, int major, int minor, GraphicsContextFlags flags)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GameWindow.

String title

The title of the GameWindow.

GameWindowFlags options

GameWindow options regarding window appearance and behavior.

DisplayDevice device

The OpenTK.Graphics.DisplayDevice to construct the GameWindow in.

Int32 major

The major version for the OpenGL GraphicsContext.

Int32 minor

The minor version for the OpenGL GraphicsContext.

GraphicsContextFlags flags

The GraphicsContextFlags version for the OpenGL GraphicsContext.

GameWindow(Int32, Int32, GraphicsMode, String, GameWindowFlags, DisplayDevice, Int32, Int32, GraphicsContextFlags, IGraphicsContext)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height, GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device, int major, int minor, GraphicsContextFlags flags, IGraphicsContext sharedContext)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GameWindow.

String title

The title of the GameWindow.

GameWindowFlags options

GameWindow options regarding window appearance and behavior.

DisplayDevice device

The OpenTK.Graphics.DisplayDevice to construct the GameWindow in.

Int32 major

The major version for the OpenGL GraphicsContext.

Int32 minor

The minor version for the OpenGL GraphicsContext.

GraphicsContextFlags flags

The GraphicsContextFlags version for the OpenGL GraphicsContext.

IGraphicsContext sharedContext

An IGraphicsContext to share resources with.

GameWindow(Int32, Int32, GraphicsMode, String, GameWindowFlags, DisplayDevice, Int32, Int32, GraphicsContextFlags, IGraphicsContext, Boolean)

Constructs a new GameWindow with the specified attributes.

Declaration
public GameWindow(int width, int height, GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device, int major, int minor, GraphicsContextFlags flags, IGraphicsContext sharedContext, bool isSingleThreaded)
Parameters
Type Name Description
Int32 width

The width of the GameWindow in pixels.

Int32 height

The height of the GameWindow in pixels.

GraphicsMode mode

The OpenTK.Graphics.GraphicsMode of the GameWindow.

String title

The title of the GameWindow.

GameWindowFlags options

GameWindow options regarding window appearance and behavior.

DisplayDevice device

The OpenTK.Graphics.DisplayDevice to construct the GameWindow in.

Int32 major

The major version for the OpenGL GraphicsContext.

Int32 minor

The minor version for the OpenGL GraphicsContext.

GraphicsContextFlags flags

The GraphicsContextFlags version for the OpenGL GraphicsContext.

IGraphicsContext sharedContext

An IGraphicsContext to share resources with.

Boolean isSingleThreaded

Should the update and render frames be fired on the same thread? If false, render and update events will be fired from separate threads.

Properties

Context

Returns the opengl IGraphicsContext associated with the current GameWindow.

Declaration
public IGraphicsContext Context { get; }
Property Value
Type Description
IGraphicsContext

IsExiting

Gets a value indicating whether the shutdown sequence has been initiated for this window, by calling GameWindow.Exit() or hitting the 'close' button. If this property is true, it is no longer safe to use any OpenTK.Input or OpenTK.Graphics.OpenGL functions or properties.

Declaration
public bool IsExiting { get; }
Property Value
Type Description
Boolean

RenderFrequency

Gets a double representing the actual frequency of RenderFrame events, in hertz (i.e. fps or frames per second).

Declaration
public double RenderFrequency { get; }
Property Value
Type Description
Double

RenderPeriod

Gets a double representing the period of RenderFrame events, in seconds.

Declaration
public double RenderPeriod { get; }
Property Value
Type Description
Double

RenderTime

Gets a double representing the time spent in the RenderFrame function, in seconds.

Declaration
public double RenderTime { get; protected set; }
Property Value
Type Description
Double

TargetRenderFrequency

Gets or sets a double representing the target render frequency, in hertz.

Declaration
public double TargetRenderFrequency { get; set; }
Property Value
Type Description
Double
Remarks

A value of 0.0 indicates that RenderFrame events are generated at the maximum possible frequency (i.e. only limited by the hardware's capabilities).

Values lower than 1.0Hz are clamped to 0.0. Values higher than 500.0Hz are clamped to 200.0Hz.

TargetRenderPeriod

Gets or sets a double representing the target render period, in seconds.

Declaration
public double TargetRenderPeriod { get; set; }
Property Value
Type Description
Double
Remarks

A value of 0.0 indicates that RenderFrame events are generated at the maximum possible frequency (i.e. only limited by the hardware's capabilities).

Values lower than 0.002 seconds (500Hz) are clamped to 0.0. Values higher than 1.0 seconds (1Hz) are clamped to 1.0.

TargetUpdateFrequency

Gets or sets a double representing the target update frequency, in hertz.

Declaration
public double TargetUpdateFrequency { get; set; }
Property Value
Type Description
Double
Remarks

A value of 0.0 indicates that UpdateFrame events are generated at the maximum possible frequency (i.e. only limited by the hardware's capabilities).

Values lower than 1.0Hz are clamped to 0.0. Values higher than 500.0Hz are clamped to 500.0Hz.

TargetUpdatePeriod

Gets or sets a double representing the target update period, in seconds.

Declaration
public double TargetUpdatePeriod { get; set; }
Property Value
Type Description
Double
Remarks

A value of 0.0 indicates that UpdateFrame events are generated at the maximum possible frequency (i.e. only limited by the hardware's capabilities).

Values lower than 0.002 seconds (500Hz) are clamped to 0.0. Values higher than 1.0 seconds (1Hz) are clamped to 1.0.

UpdateFrequency

Gets a double representing the frequency of UpdateFrame events, in hertz.

Declaration
public double UpdateFrequency { get; }
Property Value
Type Description
Double

UpdatePeriod

Gets a double representing the period of UpdateFrame events, in seconds.

Declaration
public double UpdatePeriod { get; }
Property Value
Type Description
Double

UpdateTime

Gets a double representing the time spent in the UpdateFrame function, in seconds.

Declaration
public double UpdateTime { get; }
Property Value
Type Description
Double

VSync

Gets or sets the VSyncMode.

Declaration
public VSyncMode VSync { get; set; }
Property Value
Type Description
VSyncMode

WindowState

Gets or states the state of the NativeWindow.

Declaration
public override WindowState WindowState { get; set; }
Property Value
Type Description
WindowState
Overrides
NativeWindow.WindowState

Methods

Dispose()

Disposes of the GameWindow, releasing all resources consumed by it.

Declaration
public override void Dispose()
Overrides
NativeWindow.Dispose()

Dispose(Boolean)

Override to add custom cleanup logic.

Declaration
protected virtual void Dispose(bool manual)
Parameters
Type Name Description
Boolean manual

True, if this method was called by the application; false if this was called by the finalizer thread.

Exit()

Closes the GameWindow. Equivalent to Close() method.

Declaration
public virtual void Exit()
Remarks

Override if you are not using Run().

If you override this method, place a call to base.Exit(), to ensure proper OpenTK shutdown.

MakeCurrent()

Makes the GraphicsContext current on the calling thread.

Declaration
public void MakeCurrent()

OnClosing(CancelEventArgs)

Called when the NativeWindow is about to close.

Declaration
protected override void OnClosing(CancelEventArgs e)
Parameters
Type Name Description
CancelEventArgs e

The CancelEventArgs for this event. Set e.Cancel to true in order to stop the GameWindow from closing.

Overrides
NativeWindow.OnClosing(CancelEventArgs)

OnLoad(EventArgs)

Called after an OpenGL context has been established, but before entering the main loop.

Declaration
protected virtual void OnLoad(EventArgs e)
Parameters
Type Name Description
EventArgs e

Not used.

OnRenderFrame(FrameEventArgs)

Called when the frame is rendered.

Declaration
protected virtual void OnRenderFrame(FrameEventArgs e)
Parameters
Type Name Description
FrameEventArgs e

Contains information necessary for frame rendering.

Remarks

Subscribe to the RenderFrame event instead of overriding this method.

OnResize(EventArgs)

Called when this window is resized.

Declaration
protected override void OnResize(EventArgs e)
Parameters
Type Name Description
EventArgs e

Not used.

Overrides
NativeWindow.OnResize(EventArgs)
Remarks

You will typically wish to update your viewport whenever the window is resized. See the Viewport(Int32, Int32, Int32, Int32) method.

OnUnload(EventArgs)

Called after GameWindow.Exit was called, but before destroying the OpenGL context.

Declaration
protected virtual void OnUnload(EventArgs e)
Parameters
Type Name Description
EventArgs e

Not used.

OnUpdateFrame(FrameEventArgs)

Called when the frame is updated.

Declaration
protected virtual void OnUpdateFrame(FrameEventArgs e)
Parameters
Type Name Description
FrameEventArgs e

Contains information necessary for frame updating.

Remarks

Subscribe to the UpdateFrame event instead of overriding this method.

OnWindowInfoChanged(EventArgs)

Called when the WindowInfo for this GameWindow has changed.

Declaration
protected virtual void OnWindowInfoChanged(EventArgs e)
Parameters
Type Name Description
EventArgs e

Not used.

Run()

Enters the game loop of the GameWindow using the maximum update rate.

Declaration
public void Run()
See Also
Run(Double)

Run(Double)

Enters the game loop of the GameWindow using the specified update rate. maximum possible render frequency.

Declaration
public void Run(double updateRate)
Parameters
Type Name Description
Double updateRate

Run(Double, Double)

Enters the game loop of the GameWindow updating and rendering at the specified frequency.

Declaration
public void Run(double updates_per_second, double frames_per_second)
Parameters
Type Name Description
Double updates_per_second

The frequency of UpdateFrame events.

Double frames_per_second

The frequency of RenderFrame events.

Remarks

When overriding the default game loop you should call ProcessEvents() to ensure that your GameWindow responds to operating system events.

Once ProcessEvents() returns, it is time to call update and render the next frame.

SwapBuffers()

Swaps the front and back buffer, presenting the rendered scene to the user.

Declaration
public void SwapBuffers()

Events

Load

Occurs before the window is displayed for the first time.

Declaration
public event EventHandler<EventArgs> Load
Event Type
Type Description
EventHandler<EventArgs>

OnUpdateThreadStarted

If game window is configured to run with a dedicated update thread (by passing isSingleThreaded = false in the constructor), occurs when the update thread has started. This would be a good place to initialize thread specific stuff (like setting a synchronization context).

Declaration
public event EventHandler OnUpdateThreadStarted
Event Type
Type Description
EventHandler

RenderFrame

Occurs when it is time to render a frame.

Declaration
public event EventHandler<FrameEventArgs> RenderFrame
Event Type
Type Description
EventHandler<FrameEventArgs>

Unload

Occurs before the window is destroyed.

Declaration
public event EventHandler<EventArgs> Unload
Event Type
Type Description
EventHandler<EventArgs>

UpdateFrame

Occurs when it is time to update a frame.

Declaration
public event EventHandler<FrameEventArgs> UpdateFrame
Event Type
Type Description
EventHandler<FrameEventArgs>

Implements

IGameWindow
INativeWindow
System.IDisposable
In This Article
Back to top Generated by DocFX