Added constant and variable time-step update logic to GameWindow. Updated examples to use the new GameWindow UpdateFrame and RenderFrame events.

This commit is contained in:
the_fiddler 2007-09-23 12:09:42 +00:00
parent bb2b3f8eae
commit 57b72e71e1
11 changed files with 114 additions and 45 deletions

View file

@ -87,7 +87,7 @@ namespace Examples.Tests
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
base.OnRenderFrame(e);

View file

@ -30,7 +30,7 @@ namespace Examples.Tests
}
float[] proj = new float[16];
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT);

View file

@ -51,7 +51,7 @@ namespace Examples.Tutorial
/// will be notified of frame rendering events!
/// </summary>
/// <param name="e">Not used.</param>
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT);

View file

@ -92,16 +92,8 @@ namespace Examples.Tutorial
Fullscreen = !Fullscreen;
}
GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW);
GL.LoadIdentity();
Glu.LookAt(
0.0, 5.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
angle += 180.0f * e.Time;
//angle += 180.0f * (float)e.Time;
angle += 3.0f;
if (angle > 720.0f)
angle -= 720.0f;
}
@ -113,21 +105,23 @@ namespace Examples.Tutorial
/// <summary>
/// Place your rendering code here.
/// </summary>
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
unsafe
{
fixed (ushort* index_pointer = Shapes.Cube.Indices)
{
GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW);
GL.LoadIdentity();
Glu.LookAt(
0.0, 5.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0
);
GL.Rotate(angle, 0.0f, 1.0f, 0.0f);
GL.DrawElements(GL.Enums.BeginMode.TRIANGLES, Shapes.Cube.Indices.Length,
GL.Enums.All.UNSIGNED_SHORT, index_pointer);
}
}
GL.Enums.All.UNSIGNED_SHORT, Shapes.Cube.Indices);
Context.SwapBuffers();
Thread.Sleep(0);
}
#endregion
@ -142,7 +136,8 @@ namespace Examples.Tutorial
/// </remarks>
public void Launch()
{
Run();
// Run the main loop calling UpdateFrame and RenderFrame 60 times per second.
Run(60.0, 60.0);
}
#endregion

View file

@ -118,7 +118,7 @@ namespace Examples.Tutorial
/// <summary>
/// Place your rendering code here.
/// </summary>
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);

View file

@ -137,7 +137,7 @@ namespace Examples.Tutorial
#region OnRenderFrame
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
base.OnRenderFrame(e);

View file

@ -153,7 +153,7 @@ namespace Examples.Tutorial
#region OnRenderFrame
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
base.OnRenderFrame(e);

View file

@ -172,7 +172,7 @@ namespace Examples.Tutorial
#region OnRenderFrame
public override void OnRenderFrame(EventArgs e)
public override void OnRenderFrame(RenderFrameEventArgs e)
{
base.OnRenderFrame(e);

View file

@ -29,6 +29,8 @@ namespace OpenTK
private bool isExiting;
private bool disposed;
private double updateTime, renderTime, eventTime, frameTime;
#endregion
#region --- Contructors ---
@ -293,7 +295,7 @@ namespace OpenTK
#region public virtual void Run()
/// <summary>
/// Runs the default game loop on GameWindow (process event->update frame->render frame).
/// Runs the default game loop on GameWindow at the maximum possible update and render speed.
/// </summary>
/// <remarks>
/// <para>
@ -307,7 +309,18 @@ namespace OpenTK
/// to Operating System events.
/// </para>
/// </remarks>
public virtual void Run()
/// <see cref="public virtual void Run(float update_frequency, float render_frequency)"/>
public void Run()
{
Run(0.0f, 0.0f);
}
/// <summary>
/// Runs the default game loop on GameWindow at the specified update and render speed.
/// </summary>
/// <param name="update_frequency">Indicates how many times UpdateFrame will be called per second.</param>
/// <param name="render_frequency">Indicates how many times RenderFrame will be called per second.</param>
public virtual void Run(double update_frequency, double render_frequency)
{
this.OnLoad(EventArgs.Empty);
resizeEventArgs.Width = this.Width;
@ -316,20 +329,65 @@ namespace OpenTK
Debug.Print("Entering main loop");
System.Diagnostics.Stopwatch watch = new Stopwatch();
watch.Reset();
Stopwatch watch = new Stopwatch();
UpdateFrameEventArgs updateArgs = new UpdateFrameEventArgs();
RenderFrameEventArgs renderArgs = new RenderFrameEventArgs();
double update_target = 0.0, render_target = 0.0, next_update = 0.0, next_render = 0.0;
double time, total_time;
if (update_frequency > 0.0)
{
next_update = update_target = 1.0 / update_frequency;
}
if (render_frequency > 0.0)
{
next_render = render_target = 1.0 / render_frequency;
}
while (this.Exists && !IsExiting)
{
this.ProcessEvents();
if (!IsExiting)
{
updateArgs.Time = watch.ElapsedMilliseconds / 1000.0f;
total_time = frameTime = watch.Elapsed.TotalSeconds;
if (total_time > 0.1)
total_time = 0.1;
updateArgs.Time = renderArgs.Time = total_time;
watch.Reset();
watch.Start();
time = watch.Elapsed.TotalSeconds;
this.ProcessEvents();
eventTime = watch.Elapsed.TotalSeconds - time;
if (!IsExiting)
{
time = watch.Elapsed.TotalSeconds;
next_update -= (total_time + time);
while (next_update <= 0.0)
{
updateArgs.Time += watch.Elapsed.TotalSeconds;
this.OnUpdateFrame(updateArgs);
this.OnRenderFrame(EventArgs.Empty);
if (update_target == 0.0)
break;
next_update += update_target;
}
updateTime = watch.Elapsed.TotalSeconds - time;
time = watch.Elapsed.TotalSeconds;
next_render -= (total_time + time);
if (next_render <= 0.0)
{
renderArgs.Time += time;
this.OnRenderFrame(renderArgs);
next_render += render_target;
}
renderTime = watch.Elapsed.TotalSeconds - time;
if (renderTime < render_target && updateTime < update_target)
{
Thread.Sleep((int)(1000.0 * System.Math.Min(
render_target - renderTime, update_target - updateTime)));
}
}
}
@ -367,7 +425,7 @@ namespace OpenTK
#endregion
#region public virtual void OnRenderFrame(EventArgs e)
#region public virtual void OnRenderFrame(RenderFrameEventArgs e)
/// <summary>
/// Raises the RenderFrame event. Override in derived classes to render a frame.
@ -376,7 +434,7 @@ namespace OpenTK
/// If overriden, the base.OnRenderFrame() function should be called, to ensure
/// listeners are notified of RenderFrame events.
/// </remarks>
public virtual void OnRenderFrame(EventArgs e)
public virtual void OnRenderFrame(RenderFrameEventArgs e)
{
if (!this.Exists && !this.IsExiting)
{
@ -626,12 +684,26 @@ namespace OpenTK
public class UpdateFrameEventArgs : EventArgs
{
private float time;
private double time;
/// <summary>
/// Gets the Time elapsed between frame updates, in seconds.
/// </summary>
public float Time
public double Time
{
get { return time; }
internal set { time = value; }
}
}
public class RenderFrameEventArgs : EventArgs
{
private double time;
/// <summary>
/// Gets the Time elapsed between frame updates, in seconds.
/// </summary>
public double Time
{
get { return time; }
internal set { time = value; }

View file

@ -62,12 +62,12 @@ namespace OpenTK.Platform
void CreateContext(bool direct, IGLContext source);
/// <summary>
/// Swaps buffers on a context. This presents the rendered scene to the user.
/// Swaps buffers, presenting the rendered scene to the user.
/// </summary>
void SwapBuffers();
/// <summary>
/// Makes this context the current rendering target.
/// Makes the Context current in the calling thread, i.e. future OpenGL commands will affect this Context.
/// </summary>
void MakeCurrent();

View file

@ -14,11 +14,13 @@ namespace OpenTK.Platform
{
void Run();
void OnRenderFrame(EventArgs e);
void OnRenderFrame(RenderFrameEventArgs e);
void OnUpdateFrame(UpdateFrameEventArgs e);
void OnLoad(EventArgs e);
void Exit();
void SwapBuffers();
event UpdateFrameEvent UpdateFrame;
event RenderFrameEvent RenderFrame;
event LoadEvent Load;
@ -28,6 +30,6 @@ namespace OpenTK.Platform
}
public delegate void UpdateFrameEvent(object sender, UpdateFrameEventArgs e);
public delegate void RenderFrameEvent(object sender, EventArgs e);
public delegate void RenderFrameEvent(object sender, RenderFrameEventArgs e);
public delegate void LoadEvent(object sender, EventArgs e);
}