From 210122454cb4baf7eb1c68c7337b84ec3c993dad Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Fri, 26 Oct 2007 15:54:35 +0000 Subject: [PATCH] Cleaned up warnings. Updated documentation. Cleaned up some functions. --- Source/OpenTK/GameWindow.cs | 149 +++++++++++++++++++++--------------- 1 file changed, 89 insertions(+), 60 deletions(-) diff --git a/Source/OpenTK/GameWindow.cs b/Source/OpenTK/GameWindow.cs index ada9c6cd..17f0d3c3 100644 --- a/Source/OpenTK/GameWindow.cs +++ b/Source/OpenTK/GameWindow.cs @@ -119,46 +119,6 @@ namespace OpenTK #endregion - #region --- Functions --- - - #region public void CreateWindow(DisplayMode mode, string title) - - /// - /// Creates a render window for the calling GameWindow, with the specified DisplayMode and Title. - /// - /// The DisplayMode of the render window. - /// The Title of the render window. - /// - /// It is an error to call this function when a render window already exists. - /// Call DestroyWindow to close the render window. - /// - /// Occurs when a render window already exists. - private void CreateWindow(DisplayMode mode, string title) - { - if (!Exists) - { - try - { - glWindow.CreateWindow(mode); - this.Title = title; - input_driver = new InputDriver(this); - } - catch (ApplicationException expt) - { - Debug.Print(expt.ToString()); - throw; - } - } - else - { - throw new InvalidOperationException("A render window already exists for this GameWindow."); - } - } - - #endregion - - #endregion - #region --- INativeGLWindow Members --- #region public void Exit() @@ -172,6 +132,7 @@ namespace OpenTK public virtual void Exit() { isExiting = true; + //throw new GameWindowExitException(); //glWindow.Exit(); //this.Dispose(); } @@ -406,6 +367,42 @@ namespace OpenTK #region --- GameWindow Methods --- + #region public void CreateWindow(DisplayMode mode, string title) + + /// + /// Creates a render window for the calling GameWindow, with the specified DisplayMode and Title. + /// + /// The DisplayMode of the render window. + /// The Title of the render window. + /// + /// It is an error to call this function when a render window already exists. + /// Call DestroyWindow to close the render window. + /// + /// Occurs when a render window already exists. + private void CreateWindow(DisplayMode mode, string title) + { + if (!Exists) + { + try + { + glWindow.CreateWindow(mode); + this.Title = title; + input_driver = new InputDriver(this); + } + catch (ApplicationException expt) + { + Debug.Print(expt.ToString()); + throw; + } + } + else + { + throw new InvalidOperationException("A render window already exists for this GameWindow."); + } + } + + #endregion + #region void Run() /// @@ -450,7 +447,7 @@ namespace OpenTK UpdateFrameEventArgs update_args = new UpdateFrameEventArgs(); RenderFrameEventArgs render_args = new RenderFrameEventArgs(); - double sleep_granularity; // In seconds. + //double sleep_granularity; // In seconds. //GC.Collect(2); //GC.WaitForPendingFinalizers(); @@ -482,6 +479,8 @@ namespace OpenTK while (next_update - time <= 0.0) { next_update = next_update - time + TargetUpdatePeriod; + if (next_update < -1.0) // Cap the maximum time drift, to avoid lengthy catch-up games. + next_update = -1.0; update_time_counter += time; ++num_updates; @@ -491,6 +490,7 @@ namespace OpenTK update_args.Time = time; OnUpdateFrameInternal(update_args); + update_time = update_watch.Elapsed.TotalSeconds; if (TargetUpdateFrequency == 0.0) break; @@ -499,8 +499,8 @@ namespace OpenTK next_update -= time; update_time_counter += time; - // Allow up to 10 frames to be dropped. - // Prevent the application from hanging with very high update frequencies. + // Allow up to 10 frames to be dropped. Prevents the application from hanging + // with very high update frequencies. if (num_updates >= 10) break; } @@ -511,6 +511,9 @@ namespace OpenTK update_time_counter = 0.0; } + if (isExiting) + break; + // Raise RenderFrame event time = render_watch.Elapsed.TotalSeconds; if (time > 1.0) @@ -519,7 +522,7 @@ namespace OpenTK if (VSync == VSyncMode.Adaptive) { // Check if we have enough time for a vsync - if (time_left <= 0.5 * TargetRenderFrequency) + if (RenderTime > 2.0 * TargetRenderPeriod) Context.VSync = false; else Context.VSync = true; @@ -528,12 +531,16 @@ namespace OpenTK if (time_left <= 0.0) { next_render = time_left + TargetRenderPeriod; + if (next_render < -1.0) // Cap the maximum time drift, to avoid lengthy catch-up games. + next_render = -1.0; + render_watch.Reset(); render_watch.Start(); render_period = render_args.Time = time; render_args.ScaleFactor = RenderPeriod / UpdatePeriod; OnRenderFrameInternal(render_args); + render_time = render_watch.Elapsed.TotalSeconds; } // Yield CPU time, if the Thread.Sleep() granularity allows it. @@ -545,10 +552,10 @@ namespace OpenTK //} } } - catch (GameWindowExitException e) - { + //catch (GameWindowExitException e) + //{ - } + //} finally { Thread.CurrentThread.Priority = ThreadPriority.Normal; @@ -858,7 +865,7 @@ namespace OpenTK #region public double TargetRenderPeriod /// - /// Gets or sets the target render period in seconds. + /// 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. @@ -889,7 +896,7 @@ namespace OpenTK #region public double TargetRenderFrequency /// - /// Gets or sets the target render frequency in Herz. + /// Gets or sets a double representing the target render frequency, in Herz. /// /// /// 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). @@ -922,7 +929,7 @@ namespace OpenTK #region public double TargetUpdatePeriod /// - /// Gets or sets the target update period in seconds. + /// 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). @@ -953,7 +960,7 @@ namespace OpenTK #region public double TargetUpdateFrequency /// - /// Gets or sets the target update frequency in Herz. + /// Gets or sets a double representing the target update frequency, in Herz. /// /// /// 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). @@ -986,7 +993,7 @@ namespace OpenTK #region public double RenderFrequency /// - /// Gets the actual frequency of RenderFrame events in Herz (i.e. FPS or Frames Per Second). + /// Gets a double representing the actual frequency of RenderFrame events, in Herz (i.e. FPS or Frames Per Second). /// public double RenderFrequency { @@ -1003,7 +1010,7 @@ namespace OpenTK #region public double RenderPeriod /// - /// Gets the period of RenderFrame events in seconds. + /// Gets a double representing the period of RenderFrame events, in seconds. /// public double RenderPeriod { @@ -1018,7 +1025,7 @@ namespace OpenTK #region public double UpdateFrequency /// - /// Gets the frequency of UpdateFrame events in Herz. + /// Gets a double representing the frequency of UpdateFrame events, in Herz. /// public double UpdateFrequency { @@ -1035,7 +1042,7 @@ namespace OpenTK #region public double UpdatePeriod /// - /// Gets the period of UpdateFrame events in seconds. + /// Gets a double representing the period of UpdateFrame events, in seconds. /// public double UpdatePeriod { @@ -1047,6 +1054,31 @@ namespace OpenTK #endregion + #region public double RenderTime + + /// + /// Gets a double representing the time spent in the RenderFrame function, in seconds. + /// + public double RenderTime + { + get { return render_time; } + protected set { render_time = value; } + } + + #endregion + + #region public double RenderTime + + /// + /// Gets a double representing the time spent in the UpdateFrame function, in seconds. + /// + public double UpdateTime + { + get { return update_time; } + } + + #endregion + #endregion #region --- IResizable Members --- @@ -1193,7 +1225,7 @@ namespace OpenTK /// public virtual void Dispose() { - Dispose(true); // Real Dispose method. + Dispose(true); GC.SuppressFinalize(this); } @@ -1201,9 +1233,6 @@ namespace OpenTK { if (!disposed) { - // Is this safe? Maybe 'Debug' has been disposed, too... - //Debug.Print("{0} disposing GameWindow.", manual ? "Manually" : "Automatically"); - if (manual) { if (glWindow != null) @@ -1218,7 +1247,7 @@ namespace OpenTK ~GameWindow() { - this.Dispose(false); + Dispose(false); } #endregion