Class GameWindow
The GameWindow class contains cross-platform methods to create and render on an OpenGL window, handle input and load resources.
Inherited Members
Namespace: OpenTK
Assembly: cs.temp.dll.dll
Syntax
public class GameWindow : NativeWindow, INativeWindow, IGameWindow, 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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.Int32 | height | The height of the GameWindow in pixels. |
GraphicsMode | mode | The OpenTK.Graphics.GraphicsMode of the GameWindow. |
System.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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.Int32 | height | The height of the GameWindow in pixels. |
GraphicsMode | mode | The OpenTK.Graphics.GraphicsMode of the GameWindow. |
System.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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.Int32 | height | The height of the GameWindow in pixels. |
GraphicsMode | mode | The OpenTK.Graphics.GraphicsMode of the GameWindow. |
System.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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.Int32 | height | The height of the GameWindow in pixels. |
GraphicsMode | mode | The OpenTK.Graphics.GraphicsMode of the GameWindow. |
System.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. |
System.Int32 | major | The major version for the OpenGL GraphicsContext. |
System.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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.Int32 | height | The height of the GameWindow in pixels. |
GraphicsMode | mode | The OpenTK.Graphics.GraphicsMode of the GameWindow. |
System.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. |
System.Int32 | major | The major version for the OpenGL GraphicsContext. |
System.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 |
---|---|---|
System.Int32 | width | The width of the GameWindow in pixels. |
System.Int32 | height | The height of the GameWindow in pixels. |
GraphicsMode | mode | The OpenTK.Graphics.GraphicsMode of the GameWindow. |
System.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. |
System.Int32 | major | The major version for the OpenGL GraphicsContext. |
System.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. |
System.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 |
---|---|
System.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 |
---|---|
System.Double |
RenderPeriod
Gets a double representing the period of RenderFrame events, in seconds.
Declaration
public double RenderPeriod { get; }
Property Value
Type | Description |
---|---|
System.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 |
---|---|
System.Double |
TargetRenderFrequency
Gets or sets a double representing the target render frequency, in hertz.
Declaration
public double TargetRenderFrequency { get; set; }
Property Value
Type | Description |
---|---|
System.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 |
---|---|
System.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 |
---|---|
System.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 |
---|---|
System.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 |
---|---|
System.Double |
UpdatePeriod
Gets a double representing the period of UpdateFrame events, in seconds.
Declaration
public double UpdatePeriod { get; }
Property Value
Type | Description |
---|---|
System.Double |
UpdateTime
Gets a double representing the time spent in the UpdateFrame function, in seconds.
Declaration
public double UpdateTime { get; }
Property Value
Type | Description |
---|---|
System.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
Methods
Dispose()
Disposes of the GameWindow, releasing all resources consumed by it.
Declaration
public override void Dispose()
Overrides
Dispose(Boolean)
Override to add custom cleanup logic.
Declaration
protected virtual void Dispose(bool manual)
Parameters
Type | Name | Description |
---|---|---|
System.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(ComponentModel.CancelEventArgs)
Called when the NativeWindow is about to close.
Declaration
protected override void OnClosing(ComponentModel.CancelEventArgs e)
Parameters
Type | Name | Description |
---|---|---|
System.ComponentModel.CancelEventArgs | e | The |
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 |
---|---|---|
System.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 |
---|---|---|
System.EventArgs | e | Not used. |
Overrides
Remarks
You will typically wish to update your viewport whenever
the window is resized. See the
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 |
---|---|---|
System.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 |
---|---|---|
System.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)
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 |
---|---|---|
System.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 |
---|---|---|
System.Double | updates_per_second | The frequency of UpdateFrame events. |
System.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 |
---|---|
System.EventHandler<System.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 |
---|---|
System.EventHandler |
RenderFrame
Occurs when it is time to render a frame.
Declaration
public event EventHandler<FrameEventArgs> RenderFrame
Event Type
Type | Description |
---|---|
System.EventHandler<FrameEventArgs> |
Unload
Occurs before the window is destroyed.
Declaration
public event EventHandler<EventArgs> Unload
Event Type
Type | Description |
---|---|
System.EventHandler<System.EventArgs> |
UpdateFrame
Occurs when it is time to update a frame.
Declaration
public event EventHandler<FrameEventArgs> UpdateFrame
Event Type
Type | Description |
---|---|
System.EventHandler<FrameEventArgs> |