OpenTK.GameWindow Class Reference

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

Inheritance diagram for OpenTK.GameWindow:
OpenTK.NativeWindow OpenTK.Platform.IGameWindow OpenTK.INativeWindow OpenTK.INativeWindow

List of all members.

Public Member Functions

 GameWindow ()
 Constructs a new GameWindow with sensible default attributes.
 GameWindow (int width, int height)
 Constructs a new GameWindow with the specified attributes.
 GameWindow (int width, int height, GraphicsMode mode)
 Constructs a new GameWindow with the specified attributes.
 GameWindow (int width, int height, GraphicsMode mode, string title)
 Constructs a new GameWindow with the specified attributes.
 GameWindow (int width, int height, GraphicsMode mode, string title, GameWindowFlags options)
 Constructs a new GameWindow with the specified attributes.
 GameWindow (int width, int height, GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device)
 Constructs a new GameWindow with the specified attributes.
 GameWindow (int width, int height, GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device, int major, int minor, GraphicsContextFlags flags)
 Constructs a new GameWindow with the specified attributes.
 GameWindow (int width, int height, GraphicsMode mode, string title, GameWindowFlags options, DisplayDevice device, int major, int minor, GraphicsContextFlags flags, IGraphicsContext sharedContext)
 Constructs a new GameWindow with the specified attributes.
override void Dispose ()
 Disposes of the GameWindow, releasing all resources consumed by it.
virtual void Exit ()
 Closes the GameWindow. Equivalent to NativeWindow.Close method.
void MakeCurrent ()
 Makes the GraphicsContext current on the calling thread.
void Run ()
 Enters the game loop of the GameWindow using the maximum update rate.
void Run (double updateRate)
 Enters the game loop of the GameWindow using the specified update rate. maximum possible render frequency.
void Run (double updates_per_second, double frames_per_second)
 Enters the game loop of the GameWindow updating and rendering at the specified frequency.
void SwapBuffers ()
 Swaps the front and back buffer, presenting the rendered scene to the user.

Protected Member Functions

override void OnClosing (System.ComponentModel.CancelEventArgs e)
 Called when the NativeWindow is about to close.
virtual void OnLoad (EventArgs e)
 Called after an OpenGL context has been established, but before entering the main loop.
virtual void OnUnload (EventArgs e)
 Called after GameWindow.Exit was called, but before destroying the OpenGL context.
virtual void Dispose (bool manual)
 Override to add custom cleanup logic.
virtual void OnRenderFrame (FrameEventArgs e)
 Called when the frame is rendered.
virtual void OnUpdateFrame (FrameEventArgs e)
 Called when the frame is updated.
virtual void OnWindowInfoChanged (EventArgs e)
 Called when the WindowInfo for this GameWindow has changed.
override void OnResize (EventArgs e)
 Called when the NativeWindow is resized.

Properties

IGraphicsContext Context [get]
 Returns the opengl IGraphicsContext associated with the current GameWindow.
bool IsExiting [get]
 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.
IList< JoystickDeviceJoysticks [get]
 Gets a readonly IList containing all available OpenTK.Input.JoystickDevices.
KeyboardDevice Keyboard [get]
 Gets the primary Keyboard device, or null if no Keyboard exists.
MouseDevice Mouse [get]
 Gets the primary Mouse device, or null if no Mouse exists.
double RenderFrequency [get]
 Gets a double representing the actual frequency of RenderFrame events, in hertz (i.e. fps or frames per second).
double RenderPeriod [get]
 Gets a double representing the period of RenderFrame events, in seconds.
double RenderTime [get, set]
 Gets a double representing the time spent in the RenderFrame function, in seconds.
double TargetRenderFrequency [get, set]
 Gets or sets a double representing the target render frequency, in hertz.
double TargetRenderPeriod [get, set]
 Gets or sets a double representing the target render period, in seconds.
double TargetUpdateFrequency [get, set]
 Gets or sets a double representing the target update frequency, in hertz.
double TargetUpdatePeriod [get, set]
 Gets or sets a double representing the target update period, in seconds.
double UpdateFrequency [get]
 Gets a double representing the frequency of UpdateFrame events, in hertz.
double UpdatePeriod [get]
 Gets a double representing the period of UpdateFrame events, in seconds.
double UpdateTime [get]
 Gets a double representing the time spent in the UpdateFrame function, in seconds.
VSyncMode VSync [get, set]
 Gets or sets the VSyncMode.
override WindowState WindowState [get, set]
 Gets or states the state of the NativeWindow.

Events

EventHandler< EventArgs > Load
 Occurs before the window is displayed for the first time.
EventHandler< FrameEventArgsRenderFrame
 Occurs when it is time to render a frame.
EventHandler< EventArgs > Unload
 Occurs before the window is destroyed.
EventHandler< FrameEventArgsUpdateFrame
 Occurs when it is time to update a frame.

Detailed Description

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

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.

Definition at line 72 of file GameWindow.cs.


Constructor & Destructor Documentation

OpenTK.GameWindow.GameWindow (  ) 

Constructs a new GameWindow with sensible default attributes.

Definition at line 100 of file GameWindow.cs.

00101             : this(640, 480, GraphicsMode.Default, "OpenTK Game Window", 0, DisplayDevice.Default) { }

OpenTK.GameWindow.GameWindow ( int  width,
int  height 
)

Constructs a new GameWindow with the specified attributes.

Parameters:
width The width of the GameWindow in pixels.
height The height of the GameWindow in pixels.

Definition at line 110 of file GameWindow.cs.

00111             : this(width, height, GraphicsMode.Default, "OpenTK Game Window", 0, DisplayDevice.Default) { }

OpenTK.GameWindow.GameWindow ( int  width,
int  height,
GraphicsMode  mode 
)

Constructs a new GameWindow with the specified attributes.

Parameters:
width The width of the GameWindow in pixels.
height The height of the GameWindow in pixels.
mode The OpenTK.Graphics.GraphicsMode of the GameWindow.

Definition at line 121 of file GameWindow.cs.

00122             : this(width, height, mode, "OpenTK Game Window", 0, DisplayDevice.Default) { }

OpenTK.GameWindow.GameWindow ( int  width,
int  height,
GraphicsMode  mode,
string  title 
)

Constructs a new GameWindow with the specified attributes.

Parameters:
width The width of the GameWindow in pixels.
height The height of the GameWindow in pixels.
mode The OpenTK.Graphics.GraphicsMode of the GameWindow.
title The title of the GameWindow.

Definition at line 133 of file GameWindow.cs.

00134             : this(width, height, mode, title, 0, DisplayDevice.Default) { }

OpenTK.GameWindow.GameWindow ( int  width,
int  height,
GraphicsMode  mode,
string  title,
GameWindowFlags  options 
)

Constructs a new GameWindow with the specified attributes.

Parameters:
width The width of the GameWindow in pixels.
height The height of the GameWindow in pixels.
mode The OpenTK.Graphics.GraphicsMode of the GameWindow.
title The title of the GameWindow.
options GameWindow options regarding window appearance and behavior.

Definition at line 146 of file GameWindow.cs.

00147             : this(width, height, mode, title, options, DisplayDevice.Default) { }

OpenTK.GameWindow.GameWindow ( int  width,
int  height,
GraphicsMode  mode,
string  title,
GameWindowFlags  options,
DisplayDevice  device 
)

Constructs a new GameWindow with the specified attributes.

Parameters:
width The width of the GameWindow in pixels.
height The height of the GameWindow in pixels.
mode The OpenTK.Graphics.GraphicsMode of the GameWindow.
title The title of the GameWindow.
options GameWindow options regarding window appearance and behavior.
device The OpenTK.Graphics.DisplayDevice to construct the GameWindow in.

Definition at line 160 of file GameWindow.cs.

00161             : this(width, height, mode, title, options, device, 1, 0, GraphicsContextFlags.Default)
00162         { }

OpenTK.GameWindow.GameWindow ( int  width,
int  height,
GraphicsMode  mode,
string  title,
GameWindowFlags  options,
DisplayDevice  device,
int  major,
int  minor,
GraphicsContextFlags  flags 
)

Constructs a new GameWindow with the specified attributes.

Parameters:
width The width of the GameWindow in pixels.
height The height of the GameWindow in pixels.
mode The OpenTK.Graphics.GraphicsMode of the GameWindow.
title The title of the GameWindow.
options GameWindow options regarding window appearance and behavior.
device The OpenTK.Graphics.DisplayDevice to construct the GameWindow in.
major The major version for the OpenGL GraphicsContext.
minor The minor version for the OpenGL GraphicsContext.
flags The GraphicsContextFlags version for the OpenGL GraphicsContext.

Definition at line 178 of file GameWindow.cs.

00180             : this(width, height, mode, title, options, device, major, minor, flags, null)
00181         { }

OpenTK.GameWindow.GameWindow ( int  width,
int  height,
GraphicsMode  mode,
string  title,
GameWindowFlags  options,
DisplayDevice  device,
int  major,
int  minor,
GraphicsContextFlags  flags,
IGraphicsContext  sharedContext 
)

Constructs a new GameWindow with the specified attributes.

Parameters:
width The width of the GameWindow in pixels.
height The height of the GameWindow in pixels.
mode The OpenTK.Graphics.GraphicsMode of the GameWindow.
title The title of the GameWindow.
options GameWindow options regarding window appearance and behavior.
device The OpenTK.Graphics.DisplayDevice to construct the GameWindow in.
major The major version for the OpenGL GraphicsContext.
minor The minor version for the OpenGL GraphicsContext.
flags The GraphicsContextFlags version for the OpenGL GraphicsContext.
sharedContext An IGraphicsContext to share resources with.

Definition at line 198 of file GameWindow.cs.

00200             : base(width, height, title, options,
00201                    mode == null ? GraphicsMode.Default : mode,
00202                    device == null ? DisplayDevice.Default : device)
00203         {
00204             try
00205             {
00206                 glContext = new GraphicsContext(mode == null ? GraphicsMode.Default : mode, WindowInfo, major, minor, flags);
00207                 glContext.MakeCurrent(WindowInfo);
00208                 (glContext as IGraphicsContextInternal).LoadAll();
00209 
00210                 VSync = VSyncMode.On;
00211 
00212                 //glWindow.WindowInfoChanged += delegate(object sender, EventArgs e) { OnWindowInfoChangedInternal(e); };
00213             }
00214             catch (Exception e)
00215             {
00216                 Debug.Print(e.ToString());
00217                 base.Dispose();
00218                 throw;
00219             }
00220         }


Member Function Documentation

virtual void OpenTK.GameWindow.Dispose ( bool  manual  )  [protected, virtual]

Override to add custom cleanup logic.

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

Definition at line 960 of file GameWindow.cs.

00960 { }

override void OpenTK.GameWindow.Dispose (  )  [virtual]

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

Reimplemented from OpenTK.NativeWindow.

Definition at line 235 of file GameWindow.cs.

00236         {
00237             try
00238             {
00239                 Dispose(true);
00240             }
00241             finally
00242             {
00243                 try
00244                 {
00245                     if (glContext != null)
00246                     {
00247                         glContext.Dispose();
00248                         glContext = null;
00249                     }
00250                 }
00251                 finally
00252                 {
00253                     base.Dispose();
00254                 }
00255             }
00256             GC.SuppressFinalize(this);
00257         }

virtual void OpenTK.GameWindow.Exit (  )  [virtual]

Closes the GameWindow. Equivalent to NativeWindow.Close method.

Override if you are not using GameWindow.Run().

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

Definition at line 270 of file GameWindow.cs.

00271         {
00272             Close();
00273         }

void OpenTK.GameWindow.MakeCurrent (  ) 

Makes the GraphicsContext current on the calling thread.

Implements OpenTK.Platform.IGameWindow.

Definition at line 282 of file GameWindow.cs.

00283         {
00284             EnsureUndisposed();
00285             Context.MakeCurrent(WindowInfo);
00286         }

override void OpenTK.GameWindow.OnClosing ( System.ComponentModel.CancelEventArgs  e  )  [protected]

Called when the NativeWindow is about to close.

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

Definition at line 298 of file GameWindow.cs.

00299         {
00300             base.OnClosing(e);
00301             if (!e.Cancel)
00302             {
00303                 isExiting = true;
00304                 OnUnloadInternal(EventArgs.Empty);
00305             }
00306         }

virtual void OpenTK.GameWindow.OnLoad ( EventArgs  e  )  [protected, virtual]

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

Parameters:
e Not used.

Definition at line 317 of file GameWindow.cs.

00318         {
00319             if (Load != null) Load(this, e);
00320         }

virtual void OpenTK.GameWindow.OnRenderFrame ( FrameEventArgs  e  )  [protected, virtual]

Called when the frame is rendered.

Parameters:
e Contains information necessary for frame rendering.

Subscribe to the RenderFrame event instead of overriding this method.

Definition at line 973 of file GameWindow.cs.

00974         {
00975             if (RenderFrame != null) RenderFrame(this, e);
00976         }

override void OpenTK.GameWindow.OnResize ( EventArgs  e  )  [protected, virtual]

Called when the NativeWindow is resized.

Parameters:
e Not used.

Reimplemented from OpenTK.NativeWindow.

Definition at line 1008 of file GameWindow.cs.

01009         {
01010             base.OnResize(e);
01011             glContext.Update(base.WindowInfo);
01012         }

virtual void OpenTK.GameWindow.OnUnload ( EventArgs  e  )  [protected, virtual]

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

Parameters:
e Not used.

Definition at line 330 of file GameWindow.cs.

00331         {
00332             if (Unload != null) Unload(this, e);
00333         }

virtual void OpenTK.GameWindow.OnUpdateFrame ( FrameEventArgs  e  )  [protected, virtual]

Called when the frame is updated.

Parameters:
e Contains information necessary for frame updating.

Subscribe to the UpdateFrame event instead of overriding this method.

Definition at line 989 of file GameWindow.cs.

00990         {
00991             if (UpdateFrame != null) UpdateFrame(this, e);
00992         }

virtual void OpenTK.GameWindow.OnWindowInfoChanged ( EventArgs  e  )  [protected, virtual]

Called when the WindowInfo for this GameWindow has changed.

Parameters:
e Not used.

Definition at line 1002 of file GameWindow.cs.

01002 { }

void OpenTK.GameWindow.Run ( double  updates_per_second,
double  frames_per_second 
)

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

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.

Parameters:
updates_per_second The frequency of UpdateFrame events.
frames_per_second The frequency of RenderFrame events.

Definition at line 377 of file GameWindow.cs.

00378         {
00379             EnsureUndisposed();
00380 
00381             try
00382             {
00383                 if (updates_per_second < 0.0 || updates_per_second > 200.0)
00384                     throw new ArgumentOutOfRangeException("updates_per_second", updates_per_second,
00385                                                           "Parameter should be inside the range [0.0, 200.0]");
00386                 if (frames_per_second < 0.0 || frames_per_second > 200.0)
00387                     throw new ArgumentOutOfRangeException("frames_per_second", frames_per_second,
00388                                                           "Parameter should be inside the range [0.0, 200.0]");
00389 
00390                 TargetUpdateFrequency = updates_per_second;
00391                 TargetRenderFrequency = frames_per_second;
00392 
00393                 Visible = true;   // Make sure the GameWindow is visible.
00394                 OnLoadInternal(EventArgs.Empty);
00395                 OnResize(EventArgs.Empty);
00396 
00397                 // On some platforms, ProcessEvents() does not return while the user is resizing or moving
00398                 // the window. We can avoid this issue by raising UpdateFrame and RenderFrame events
00399                 // whenever we encounter a size or move event.
00400                 Move += DispatchUpdateAndRenderFrame;
00401                 Resize += DispatchUpdateAndRenderFrame;
00402 
00403                 Debug.Print("Entering main loop.");
00404                 update_watch.Start();
00405                 render_watch.Start();
00406                 while (true)
00407                 {
00408                     ProcessEvents();
00409                     if (Exists && !IsExiting)
00410                         DispatchUpdateAndRenderFrame(this, EventArgs.Empty);
00411                     else
00412                         return;
00413                 }
00414             }
00415             finally
00416             {
00417                 Move -= DispatchUpdateAndRenderFrame;
00418                 Resize -= DispatchUpdateAndRenderFrame;
00419 
00420                 if (Exists)
00421                 {
00422                     // TODO: Should similar behaviour be retained, possibly on native window level?
00423                     //while (this.Exists)
00424                     //    ProcessEvents(false);
00425                 }
00426             }
00427         }

void OpenTK.GameWindow.Run ( double  updateRate  ) 

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

Implements OpenTK.Platform.IGameWindow.

Definition at line 356 of file GameWindow.cs.

00357         {
00358             Run(updateRate, 0.0);
00359         }

void OpenTK.GameWindow.Run (  ) 

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

See also:
Run(double)

Implements OpenTK.Platform.IGameWindow.

Definition at line 343 of file GameWindow.cs.

00344         {
00345             Run(0.0, 0.0);
00346         }

void OpenTK.GameWindow.SwapBuffers (  ) 

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

Implements OpenTK.Platform.IGameWindow.

Definition at line 539 of file GameWindow.cs.

00540         {
00541             EnsureUndisposed();
00542             this.Context.SwapBuffers();
00543         }


Property Documentation

IGraphicsContext OpenTK.GameWindow.Context [get]

Returns the opengl IGraphicsContext associated with the current GameWindow.

Definition at line 557 of file GameWindow.cs.

bool OpenTK.GameWindow.IsExiting [get]

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.

Definition at line 576 of file GameWindow.cs.

IList<JoystickDevice> OpenTK.GameWindow.Joysticks [get]

Gets a readonly IList containing all available OpenTK.Input.JoystickDevices.

Definition at line 592 of file GameWindow.cs.

KeyboardDevice OpenTK.GameWindow.Keyboard [get]

Gets the primary Keyboard device, or null if no Keyboard exists.

Definition at line 604 of file GameWindow.cs.

MouseDevice OpenTK.GameWindow.Mouse [get]

Gets the primary Mouse device, or null if no Mouse exists.

Definition at line 616 of file GameWindow.cs.

double OpenTK.GameWindow.RenderFrequency [get]

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

Definition at line 642 of file GameWindow.cs.

double OpenTK.GameWindow.RenderPeriod [get]

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

Definition at line 660 of file GameWindow.cs.

double OpenTK.GameWindow.RenderTime [get, set]

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

Definition at line 676 of file GameWindow.cs.

double OpenTK.GameWindow.TargetRenderFrequency [get, set]

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

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 1.0Hz. Values higher than 200.0Hz are clamped to 200.0Hz.

Definition at line 701 of file GameWindow.cs.

double OpenTK.GameWindow.TargetRenderPeriod [get, set]

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

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.005 seconds (200Hz) are clamped to 0.0. Values higher than 1.0 seconds (1Hz) are clamped to 1.0.

Definition at line 736 of file GameWindow.cs.

double OpenTK.GameWindow.TargetUpdateFrequency [get, set]

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

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 1.0Hz. Values higher than 200.0Hz are clamped to 200.0Hz.

Definition at line 769 of file GameWindow.cs.

double OpenTK.GameWindow.TargetUpdatePeriod [get, set]

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

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.005 seconds (200Hz) are clamped to 0.0. Values higher than 1.0 seconds (1Hz) are clamped to 1.0.

Definition at line 804 of file GameWindow.cs.

double OpenTK.GameWindow.UpdateFrequency [get]

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

Definition at line 833 of file GameWindow.cs.

double OpenTK.GameWindow.UpdatePeriod [get]

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

Definition at line 851 of file GameWindow.cs.

double OpenTK.GameWindow.UpdateTime [get]

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

Definition at line 867 of file GameWindow.cs.

VSyncMode OpenTK.GameWindow.VSync [get, set]

Gets or sets the VSyncMode.

Definition at line 885 of file GameWindow.cs.

override WindowState OpenTK.GameWindow.WindowState [get, set]

Gets or states the state of the NativeWindow.

Reimplemented from OpenTK.NativeWindow.

Definition at line 908 of file GameWindow.cs.


Event Documentation

EventHandler<EventArgs> OpenTK.GameWindow.Load

Occurs before the window is displayed for the first time.

Implements OpenTK.Platform.IGameWindow.

Definition at line 931 of file GameWindow.cs.

EventHandler<FrameEventArgs> OpenTK.GameWindow.RenderFrame

Occurs when it is time to render a frame.

Implements OpenTK.Platform.IGameWindow.

Definition at line 936 of file GameWindow.cs.

EventHandler<EventArgs> OpenTK.GameWindow.Unload

Occurs before the window is destroyed.

Implements OpenTK.Platform.IGameWindow.

Definition at line 941 of file GameWindow.cs.

EventHandler<FrameEventArgs> OpenTK.GameWindow.UpdateFrame

Occurs when it is time to update a frame.

Implements OpenTK.Platform.IGameWindow.

Definition at line 946 of file GameWindow.cs.

 All Classes Functions Variables Enumerations Properties Events

Generated on Tue Mar 9 14:59:13 2010 for The Open Toolkit library by  doxygen 1.6.1