Class GameWindow
The GameWindow class contains cross-platform methods to create and render on an OpenGL window, handle input and load resources.
Implements
Inherited Members
Namespace: OpenTK.Windowing.Desktop
Assembly: OpenTK.Windowing.Desktop.dll
Syntax
public class GameWindow : NativeWindow, IDisposable
Remarks
GameWindow contains several events you can hook or override to add your custom logic:
Constructors
GameWindow(GameWindowSettings, NativeWindowSettings)
Initializes a new instance of the GameWindow class with sensible default attributes.
Declaration
public GameWindow(GameWindowSettings gameWindowSettings, NativeWindowSettings nativeWindowSettings)
Parameters
Type | Name | Description |
---|---|---|
GameWindowSettings | gameWindowSettings | The GameWindow related settings. |
NativeWindowSettings | nativeWindowSettings | The NativeWindow related settings. |
Remarks
Use GameWindowSettings.Default and NativeWindowSettings.Default to get some sensible default attributes.
Properties
ExpectedSchedulerPeriod
The expected scheduler period in milliseconds. Used to provide accurate sleep timings.
On Windows the scheduler period can be set using timeBeginPeriod()
, OpenTK sets this value to 8ms by default.
See Run() for more details.
On Linux we set this to 1 as it seems like Sleep(int) is able to accurately sleep 1ms.
On macos we set this to 1 aswell as tests imply Sleep(int) can accurately sleep 1ms.
Declaration
public int ExpectedSchedulerPeriod { get; set; }
Property Value
Type | Description |
---|---|
int |
IsMultiThreaded
Gets a value indicating whether or not the GameWindow should use a separate thread for rendering.
Declaration
[Obsolete("There is not one size fits all multithreading solution, especially for OpenGL. This feature has been removed and will not work.", true)]
public bool IsMultiThreaded { get; }
Property Value
Type | Description |
---|---|
bool |
Remarks
If this is true, render frames will be processed in a separate thread. Do not enable this unless your code is thread safe.
IsRunningSlowly
Gets a value indicating whether or not UpdatePeriod has consistently failed to reach TargetUpdatePeriod. This can be used to do things such as decreasing visual quality if the user's computer isn't powerful enough to handle the application.
Declaration
protected bool IsRunningSlowly { get; }
Property Value
Type | Description |
---|---|
bool |
RenderFrequency
Gets or sets a double representing the render frequency, in hertz.
Declaration
[Obsolete("Use UpdateFrequency instead. We no longer separate UpdateFrame and RenderFrame.", true)]
public double RenderFrequency { 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 500.0Hz.
RenderTime
Gets a double representing the time spent in the RenderFrame function, in seconds.
Declaration
[Obsolete("Use UpdateTime instead. We no longer separate UpdateFrame and RenderFrame.", true)]
public double RenderTime { get; protected set; }
Property Value
Type | Description |
---|---|
double |
UpdateFrequency
Gets or sets a double representing the update frequency, in hertz.
Declaration
public double UpdateFrequency { 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.
UpdateTime
Gets a double representing the time spent in the UpdateFrame function, in seconds.
Declaration
public double UpdateTime { get; protected set; }
Property Value
Type | Description |
---|---|
double |
Methods
Close()
Closes this window.
Declaration
public override void Close()
Overrides
Dispose()
Declaration
public override void Dispose()
Overrides
OnLoad()
Run immediately after Run() is called.
Declaration
protected virtual void OnLoad()
OnRenderFrame(FrameEventArgs)
Run when the window is ready to render. This is called after OnUpdateFrame(FrameEventArgs).
Declaration
protected virtual void OnRenderFrame(FrameEventArgs args)
Parameters
Type | Name | Description |
---|---|---|
FrameEventArgs | args | The event arguments for this frame. |
OnRenderThreadStarted()
Declaration
[Obsolete("There is no longer a separate render thread.")]
protected virtual void OnRenderThreadStarted()
OnUnload()
Run when the window is about to close.
Declaration
protected virtual void OnUnload()
OnUpdateFrame(FrameEventArgs)
Run when the window is ready to update. This is called before OnRenderFrame(FrameEventArgs).
Declaration
protected virtual void OnUpdateFrame(FrameEventArgs args)
Parameters
Type | Name | Description |
---|---|---|
FrameEventArgs | args | The event arguments for this frame. |
ResetTimeSinceLastUpdate()
Resets the time since the last update. This function is useful when implementing updates on resize using windows.
Declaration
public void ResetTimeSinceLastUpdate()
Run()
Initialize the update thread (if using a multi-threaded context, and enter the game loop of the GameWindow).
Declaration
public virtual void Run()
Remarks
On windows this function sets the thread affinity mask to 0x0001 to avoid the thread from changing cores.
On windows this function calls timeBeginPeriod(8)
to get better sleep timings, which can increase power usage.
This can be undone by calling timeEndPeriod(8)
in OnLoad() and timeBeginPeriod(8)
in OnUnload().
If the expected scheduler time is changed set ExpectedSchedulerPeriod to the appropriate value to keep the accuracy of the update loop.
SwapBuffers()
Swaps the front and back buffers of the current GraphicsContext, presenting the rendered scene to the user.
Declaration
public virtual void SwapBuffers()
TimeSinceLastUpdate()
The current time since the last update. This function is useful when implementing updates on resize using windows.
Declaration
public double TimeSinceLastUpdate()
Returns
Type | Description |
---|---|
double | The time since the last update. |
Remarks
Don't use this in OnUpdateFrame(FrameEventArgs) or OnRenderFrame(FrameEventArgs), instead use Time.
Events
Load
Occurs before the window is displayed for the first time.
Declaration
public event Action Load
Event Type
Type | Description |
---|---|
Action |
RenderFrame
Occurs when it is time to render a frame. This is invoked after UpdateFrequency.
Declaration
public event Action<FrameEventArgs> RenderFrame
Event Type
Type | Description |
---|---|
Action<FrameEventArgs> |
RenderThreadStarted
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
[Obsolete("There is no longer a separate render thread.")]
public event Action RenderThreadStarted
Event Type
Type | Description |
---|---|
Action |
Unload
Occurs before the window is destroyed.
Declaration
public event Action Unload
Event Type
Type | Description |
---|---|
Action |
UpdateFrame
Occurs when it is time to update a frame. This is invoked before RenderFrame.
Declaration
public event Action<FrameEventArgs> UpdateFrame
Event Type
Type | Description |
---|---|
Action<FrameEventArgs> |