mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-01-23 09:51:04 +00:00
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:
parent
bb2b3f8eae
commit
57b72e71e1
|
@ -87,7 +87,7 @@ namespace Examples.Tests
|
||||||
GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
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);
|
base.OnRenderFrame(e);
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ namespace Examples.Tests
|
||||||
}
|
}
|
||||||
|
|
||||||
float[] proj = new float[16];
|
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);
|
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ namespace Examples.Tutorial
|
||||||
/// will be notified of frame rendering events!
|
/// will be notified of frame rendering events!
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="e">Not used.</param>
|
/// <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);
|
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
|
@ -92,16 +92,8 @@ namespace Examples.Tutorial
|
||||||
Fullscreen = !Fullscreen;
|
Fullscreen = !Fullscreen;
|
||||||
}
|
}
|
||||||
|
|
||||||
GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW);
|
//angle += 180.0f * (float)e.Time;
|
||||||
GL.LoadIdentity();
|
angle += 3.0f;
|
||||||
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;
|
|
||||||
if (angle > 720.0f)
|
if (angle > 720.0f)
|
||||||
angle -= 720.0f;
|
angle -= 720.0f;
|
||||||
}
|
}
|
||||||
|
@ -113,21 +105,23 @@ namespace Examples.Tutorial
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Place your rendering code here.
|
/// Place your rendering code here.
|
||||||
/// </summary>
|
/// </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);
|
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
unsafe
|
GL.MatrixMode(GL.Enums.MatrixMode.MODELVIEW);
|
||||||
{
|
GL.LoadIdentity();
|
||||||
fixed (ushort* index_pointer = Shapes.Cube.Indices)
|
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.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();
|
Context.SwapBuffers();
|
||||||
Thread.Sleep(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
@ -142,7 +136,8 @@ namespace Examples.Tutorial
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public void Launch()
|
public void Launch()
|
||||||
{
|
{
|
||||||
Run();
|
// Run the main loop calling UpdateFrame and RenderFrame 60 times per second.
|
||||||
|
Run(60.0, 60.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
@ -118,7 +118,7 @@ namespace Examples.Tutorial
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Place your rendering code here.
|
/// Place your rendering code here.
|
||||||
/// </summary>
|
/// </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);
|
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT | GL.Enums.ClearBufferMask.DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
|
@ -137,7 +137,7 @@ namespace Examples.Tutorial
|
||||||
|
|
||||||
#region OnRenderFrame
|
#region OnRenderFrame
|
||||||
|
|
||||||
public override void OnRenderFrame(EventArgs e)
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnRenderFrame(e);
|
base.OnRenderFrame(e);
|
||||||
|
|
||||||
|
|
|
@ -153,7 +153,7 @@ namespace Examples.Tutorial
|
||||||
|
|
||||||
#region OnRenderFrame
|
#region OnRenderFrame
|
||||||
|
|
||||||
public override void OnRenderFrame(EventArgs e)
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnRenderFrame(e);
|
base.OnRenderFrame(e);
|
||||||
|
|
||||||
|
|
|
@ -172,7 +172,7 @@ namespace Examples.Tutorial
|
||||||
|
|
||||||
#region OnRenderFrame
|
#region OnRenderFrame
|
||||||
|
|
||||||
public override void OnRenderFrame(EventArgs e)
|
public override void OnRenderFrame(RenderFrameEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnRenderFrame(e);
|
base.OnRenderFrame(e);
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,8 @@ namespace OpenTK
|
||||||
private bool isExiting;
|
private bool isExiting;
|
||||||
private bool disposed;
|
private bool disposed;
|
||||||
|
|
||||||
|
private double updateTime, renderTime, eventTime, frameTime;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region --- Contructors ---
|
#region --- Contructors ---
|
||||||
|
@ -293,7 +295,7 @@ namespace OpenTK
|
||||||
#region public virtual void Run()
|
#region public virtual void Run()
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// <para>
|
/// <para>
|
||||||
|
@ -307,7 +309,18 @@ namespace OpenTK
|
||||||
/// to Operating System events.
|
/// to Operating System events.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </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);
|
this.OnLoad(EventArgs.Empty);
|
||||||
resizeEventArgs.Width = this.Width;
|
resizeEventArgs.Width = this.Width;
|
||||||
|
@ -316,20 +329,65 @@ namespace OpenTK
|
||||||
|
|
||||||
Debug.Print("Entering main loop");
|
Debug.Print("Entering main loop");
|
||||||
|
|
||||||
System.Diagnostics.Stopwatch watch = new Stopwatch();
|
Stopwatch watch = new Stopwatch();
|
||||||
watch.Reset();
|
|
||||||
UpdateFrameEventArgs updateArgs = new UpdateFrameEventArgs();
|
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)
|
while (this.Exists && !IsExiting)
|
||||||
{
|
{
|
||||||
this.ProcessEvents();
|
total_time = frameTime = watch.Elapsed.TotalSeconds;
|
||||||
if (!IsExiting)
|
if (total_time > 0.1)
|
||||||
{
|
total_time = 0.1;
|
||||||
updateArgs.Time = watch.ElapsedMilliseconds / 1000.0f;
|
updateArgs.Time = renderArgs.Time = total_time;
|
||||||
|
|
||||||
watch.Reset();
|
watch.Reset();
|
||||||
watch.Start();
|
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.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
|
#endregion
|
||||||
|
|
||||||
#region public virtual void OnRenderFrame(EventArgs e)
|
#region public virtual void OnRenderFrame(RenderFrameEventArgs e)
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Raises the RenderFrame event. Override in derived classes to render a frame.
|
/// 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
|
/// If overriden, the base.OnRenderFrame() function should be called, to ensure
|
||||||
/// listeners are notified of RenderFrame events.
|
/// listeners are notified of RenderFrame events.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
public virtual void OnRenderFrame(EventArgs e)
|
public virtual void OnRenderFrame(RenderFrameEventArgs e)
|
||||||
{
|
{
|
||||||
if (!this.Exists && !this.IsExiting)
|
if (!this.Exists && !this.IsExiting)
|
||||||
{
|
{
|
||||||
|
@ -626,12 +684,26 @@ namespace OpenTK
|
||||||
|
|
||||||
public class UpdateFrameEventArgs : EventArgs
|
public class UpdateFrameEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
private float time;
|
private double time;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the Time elapsed between frame updates, in seconds.
|
/// Gets the Time elapsed between frame updates, in seconds.
|
||||||
/// </summary>
|
/// </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; }
|
get { return time; }
|
||||||
internal set { time = value; }
|
internal set { time = value; }
|
||||||
|
|
|
@ -62,12 +62,12 @@ namespace OpenTK.Platform
|
||||||
void CreateContext(bool direct, IGLContext source);
|
void CreateContext(bool direct, IGLContext source);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Swaps buffers on a context. This presents the rendered scene to the user.
|
/// Swaps buffers, presenting the rendered scene to the user.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void SwapBuffers();
|
void SwapBuffers();
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
void MakeCurrent();
|
void MakeCurrent();
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,13 @@ namespace OpenTK.Platform
|
||||||
{
|
{
|
||||||
void Run();
|
void Run();
|
||||||
|
|
||||||
void OnRenderFrame(EventArgs e);
|
void OnRenderFrame(RenderFrameEventArgs e);
|
||||||
void OnUpdateFrame(UpdateFrameEventArgs e);
|
void OnUpdateFrame(UpdateFrameEventArgs e);
|
||||||
void OnLoad(EventArgs e);
|
void OnLoad(EventArgs e);
|
||||||
void Exit();
|
void Exit();
|
||||||
|
|
||||||
|
void SwapBuffers();
|
||||||
|
|
||||||
event UpdateFrameEvent UpdateFrame;
|
event UpdateFrameEvent UpdateFrame;
|
||||||
event RenderFrameEvent RenderFrame;
|
event RenderFrameEvent RenderFrame;
|
||||||
event LoadEvent Load;
|
event LoadEvent Load;
|
||||||
|
@ -28,6 +30,6 @@ namespace OpenTK.Platform
|
||||||
}
|
}
|
||||||
|
|
||||||
public delegate void UpdateFrameEvent(object sender, UpdateFrameEventArgs e);
|
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);
|
public delegate void LoadEvent(object sender, EventArgs e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue