mirror of
https://github.com/Ryujinx/Opentk.git
synced 2025-01-11 11:35:26 +00:00
Merge branch 'timing' into develop
This commit is contained in:
commit
eacc896605
|
@ -25,8 +25,8 @@ namespace Examples.Tests
|
||||||
int texture;
|
int texture;
|
||||||
bool mouse_in_window = false;
|
bool mouse_in_window = false;
|
||||||
bool viewport_changed = true;
|
bool viewport_changed = true;
|
||||||
MouseState mouse;
|
Stopwatch watch = new Stopwatch();
|
||||||
KeyboardState keyboard;
|
double update_time, render_time;
|
||||||
|
|
||||||
public GameWindowStates()
|
public GameWindowStates()
|
||||||
: base(800, 600, GraphicsMode.Default)
|
: base(800, 600, GraphicsMode.Default)
|
||||||
|
@ -82,6 +82,15 @@ namespace Examples.Tests
|
||||||
|
|
||||||
case Key.KeypadMinus:
|
case Key.KeypadMinus:
|
||||||
case Key.Minus: Size -= new Size(16, 16); break;
|
case Key.Minus: Size -= new Size(16, 16); break;
|
||||||
|
|
||||||
|
case Key.V:
|
||||||
|
VSync = VSync == VSyncMode.On ? VSyncMode.Off : VSyncMode.On;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Key.BracketLeft: TargetUpdateFrequency--; break;
|
||||||
|
case Key.BracketRight: TargetUpdateFrequency++; break;
|
||||||
|
case Key.Comma: TargetRenderFrequency--; break;
|
||||||
|
case Key.Period: TargetRenderFrequency++; break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,80 +111,103 @@ namespace Examples.Tests
|
||||||
return val > max ? max : val < min ? min : val;
|
return val > max ? max : val < min ? min : val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DrawString(Graphics gfx, string str, int line)
|
static float DrawString(Graphics gfx, string str, int line)
|
||||||
{
|
{
|
||||||
gfx.DrawString(str, TextFont, Brushes.White, new PointF(0, line * TextFont.Height));
|
return DrawString(gfx, str, line, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DrawString(Graphics gfx, string str, int line, float offset)
|
static float DrawString(Graphics gfx, string str, int line, float offset)
|
||||||
{
|
{
|
||||||
gfx.DrawString(str, TextFont, Brushes.White, new PointF(offset, line * TextFont.Height));
|
gfx.DrawString(str, TextFont, Brushes.White, new PointF(offset, line * TextFont.Height));
|
||||||
|
return offset + gfx.MeasureString(str, TextFont).Width;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DrawKeyboard(Graphics gfx, KeyboardState keyboard, int line)
|
static int DrawKeyboards(Graphics gfx, int line)
|
||||||
{
|
{
|
||||||
const string str = "Keys pressed:";
|
line++;
|
||||||
float space = gfx.MeasureString(" ", TextFont).Width;
|
DrawString(gfx, "Keyboard:", line++);
|
||||||
float offset = gfx.MeasureString(str, TextFont).Width + space;
|
for (int i = 0; i < 4; i++)
|
||||||
DrawString(gfx, str, line);
|
|
||||||
for (int i = 0; i < (int)Key.LastKey; i++)
|
|
||||||
{
|
{
|
||||||
Key k = (Key)i;
|
var state = OpenTK.Input.Keyboard.GetState(i);
|
||||||
if (keyboard[k])
|
if (state.IsConnected)
|
||||||
{
|
{
|
||||||
string key = k.ToString();
|
StringBuilder sb = new StringBuilder();
|
||||||
DrawString(gfx, key, line, offset);
|
sb.Append(i);
|
||||||
offset += gfx.MeasureString(key, TextFont).Width + space;
|
sb.Append(": ");
|
||||||
|
for (int key_index = 0; key_index < (int)Key.LastKey; key_index++)
|
||||||
|
{
|
||||||
|
Key k = (Key)key_index;
|
||||||
|
if (state[k])
|
||||||
|
{
|
||||||
|
sb.Append(k);
|
||||||
|
sb.Append(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DrawString(gfx, sb.ToString(), line++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DrawMouse(Graphics gfx, MouseState mouse, int line)
|
static int DrawMice(Graphics gfx, int line)
|
||||||
{
|
{
|
||||||
const string str = "Buttons pressed:";
|
line++;
|
||||||
float space = gfx.MeasureString(" ", TextFont).Width;
|
DrawString(gfx, "Mouse:", line++);
|
||||||
float offset = gfx.MeasureString(str, TextFont).Width + space;
|
for (int i = 0; i < 4; i++)
|
||||||
DrawString(gfx, str, line);
|
|
||||||
for (int i = 0; i < (int)MouseButton.LastButton; i++)
|
|
||||||
{
|
{
|
||||||
MouseButton b = (MouseButton)i;
|
var state = OpenTK.Input.Mouse.GetState(i);
|
||||||
if (mouse[b])
|
if (state.IsConnected)
|
||||||
{
|
{
|
||||||
string button = b.ToString();
|
StringBuilder sb = new StringBuilder();
|
||||||
DrawString(gfx, button, line, offset);
|
Vector3 pos = new Vector3(state.X, state.Y, state.WheelPrecise);
|
||||||
offset += gfx.MeasureString(button, TextFont).Width + space;
|
sb.Append(i);
|
||||||
|
sb.Append(": ");
|
||||||
|
sb.Append(pos);
|
||||||
|
for (int button_index = 0; button_index < (int)MouseButton.LastButton; button_index++)
|
||||||
|
{
|
||||||
|
MouseButton b = (MouseButton)button_index;
|
||||||
|
if (state[b])
|
||||||
|
{
|
||||||
|
sb.Append(b);
|
||||||
|
sb.Append(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DrawString(gfx, sb.ToString(), line++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int DrawJoysticks(Graphics gfx, IList<JoystickDevice> joysticks, int line)
|
static int DrawLegacyJoysticks(Graphics gfx, IList<JoystickDevice> joysticks, int line)
|
||||||
{
|
{
|
||||||
float space = gfx.MeasureString(" ", TextFont).Width;
|
line++;
|
||||||
|
DrawString(gfx, "Legacy Joystick:", line++);
|
||||||
|
|
||||||
|
int joy_index = -1;
|
||||||
foreach (var joy in joysticks)
|
foreach (var joy in joysticks)
|
||||||
{
|
{
|
||||||
string str = String.Format("Joystick '{0}': ", joy.Description);
|
joy_index++;
|
||||||
DrawString(gfx, str, line);
|
if (!String.IsNullOrEmpty(joy.Description))
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.Append(joy_index);
|
||||||
|
sb.Append(": '");
|
||||||
|
sb.Append(joy.Description);
|
||||||
|
sb.Append("' ");
|
||||||
|
|
||||||
float offset = 0;
|
|
||||||
line++;
|
|
||||||
for (int i = 0; i < joy.Axis.Count; i++)
|
for (int i = 0; i < joy.Axis.Count; i++)
|
||||||
{
|
{
|
||||||
string axis = joy.Axis[i].ToString();
|
sb.Append(joy.Axis[i]);
|
||||||
DrawString(gfx, axis, line, offset);
|
sb.Append(" ");
|
||||||
offset += gfx.MeasureString(axis, TextFont).Width + space;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
offset = 0;
|
|
||||||
line++;
|
|
||||||
for (int i = 0; i < joy.Button.Count; i++)
|
for (int i = 0; i < joy.Button.Count; i++)
|
||||||
{
|
{
|
||||||
string button = joy.Button[i].ToString();
|
sb.Append(joy.Button[i]);
|
||||||
DrawString(gfx, button, line, offset);
|
sb.Append(" ");
|
||||||
offset += gfx.MeasureString(button, TextFont).Width + space;
|
}
|
||||||
|
DrawString(gfx, sb.ToString(), line++);
|
||||||
}
|
}
|
||||||
|
|
||||||
line++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
|
@ -183,10 +215,8 @@ namespace Examples.Tests
|
||||||
|
|
||||||
protected override void OnUpdateFrame(FrameEventArgs e)
|
protected override void OnUpdateFrame(FrameEventArgs e)
|
||||||
{
|
{
|
||||||
InputDriver.Poll();
|
double clock_time = watch.Elapsed.TotalSeconds;
|
||||||
|
update_time += e.Time;
|
||||||
mouse = OpenTK.Input.Mouse.GetState();
|
|
||||||
keyboard = OpenTK.Input.Keyboard.GetState();
|
|
||||||
|
|
||||||
using (Graphics gfx = Graphics.FromImage(TextBitmap))
|
using (Graphics gfx = Graphics.FromImage(TextBitmap))
|
||||||
{
|
{
|
||||||
|
@ -195,41 +225,50 @@ namespace Examples.Tests
|
||||||
gfx.Clear(Color.Black);
|
gfx.Clear(Color.Black);
|
||||||
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
|
||||||
|
|
||||||
DrawString(gfx, GL.GetString(StringName.Vendor), line++);
|
// OpenGL information
|
||||||
DrawString(gfx, GL.GetString(StringName.Version), line++);
|
|
||||||
DrawString(gfx, GL.GetString(StringName.Renderer), line++);
|
DrawString(gfx, GL.GetString(StringName.Renderer), line++);
|
||||||
|
DrawString(gfx, GL.GetString(StringName.Version), line++);
|
||||||
DrawString(gfx, Context.GraphicsMode.ToString(), line++);
|
DrawString(gfx, Context.GraphicsMode.ToString(), line++);
|
||||||
|
|
||||||
DrawString(gfx, String.Format("[1 - 4]: change WindowState (current: {0}).", this.WindowState), line++);
|
// GameWindow information
|
||||||
DrawString(gfx, String.Format("[5 - 7]: change WindowBorder (current: {0}).", this.WindowBorder), line++);
|
line++;
|
||||||
DrawString(gfx, String.Format("Focused: {0}.", this.Focused), line++);
|
DrawString(gfx, "GameWindow:", line++);
|
||||||
DrawString(gfx, String.Format("Mouse {0} window.", mouse_in_window ? "inside" : "outside of"), line++);
|
DrawString(gfx, String.Format("[1 - 4]:[5 - 7]: WindowState.{0}:WindowBorder.{1}",
|
||||||
DrawString(gfx, String.Format("Mouse visible: {0}", CursorVisible), line++);
|
this.WindowState, this.WindowBorder), line++);
|
||||||
DrawString(gfx, String.Format("Mouse position (absolute): {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.Wheel)), line++);
|
DrawString(gfx, String.Format("[V]: VSync.{0}.", VSync), line++);
|
||||||
DrawString(gfx, String.Format("Mouse position (relative): {0}", new Vector3(mouse.X, mouse.Y, mouse.WheelPrecise)), line++);
|
DrawString(gfx, String.Format("Bounds: {0}", Bounds), line++);
|
||||||
DrawString(gfx, String.Format("Window.Bounds: {0}", Bounds), line++);
|
DrawString(gfx, String.Format("ClientRectangle: {0}", ClientRectangle), line++);
|
||||||
DrawString(gfx, String.Format("Window.Location: {0}, Size: {1}", Location, Size), line++);
|
DrawString(gfx, String.Format("Mouse {0} and {1}. {2}.",
|
||||||
DrawString(gfx, String.Format("Window: {{X={0},Y={1},Width={2},Height={3}}}", X, Y, Width, Height), line++);
|
mouse_in_window ? "inside" : "outside",
|
||||||
DrawString(gfx, String.Format("Window.ClientRectangle: {0}", ClientRectangle), line++);
|
CursorVisible ? "visible" : "hidden",
|
||||||
DrawString(gfx, TypedText.ToString(), line++);
|
Focused ? "Focused" : "Not focused"), line++);
|
||||||
DrawKeyboard(gfx, keyboard, line++);
|
DrawString(gfx, String.Format("Mouse coordinates: {0}", new Vector3(Mouse.X, Mouse.Y, Mouse.WheelPrecise)), line++);
|
||||||
DrawMouse(gfx, mouse, line++);
|
|
||||||
line = DrawJoysticks(gfx, Joysticks, line++);
|
// Timing information
|
||||||
line = DrawGamePads(gfx, line++);
|
line++;
|
||||||
|
DrawString(gfx, "Timing:", line++);
|
||||||
|
DrawString(gfx, String.Format("Frequency: update ({0:f2}/{1:f2}); render ({2:f2}/{3:f2})",
|
||||||
|
UpdateFrequency, TargetUpdateFrequency, RenderFrequency, TargetRenderFrequency), line++);
|
||||||
|
DrawString(gfx, String.Format("Period: update ({0:f4}/{1:f4}); render ({2:f4}/{3:f4})",
|
||||||
|
UpdatePeriod, TargetUpdatePeriod, RenderPeriod, TargetRenderPeriod), line++);
|
||||||
|
DrawString(gfx, String.Format("Time: update {0:f4}; render {1:f4}",
|
||||||
|
UpdateTime, RenderTime), line++);
|
||||||
|
DrawString(gfx, String.Format("Drift: clock {0:f4}; update {1:f4}; render {2:f4}",
|
||||||
|
clock_time, clock_time - update_time, clock_time - render_time), line++);
|
||||||
|
DrawString(gfx, String.Format("Text: {0}", TypedText.ToString()), line++);
|
||||||
|
|
||||||
|
// Input information
|
||||||
|
line = DrawKeyboards(gfx, line);
|
||||||
|
line = DrawMice(gfx, line);
|
||||||
|
line = DrawJoysticks(gfx, line);
|
||||||
|
line = DrawLegacyJoysticks(gfx, Joysticks, line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits(
|
int DrawJoysticks(Graphics gfx, int line)
|
||||||
new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height),
|
|
||||||
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
|
||||||
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra,
|
|
||||||
PixelType.UnsignedByte, data.Scan0);
|
|
||||||
TextBitmap.UnlockBits(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
int DrawGamePads(Graphics gfx, int line)
|
|
||||||
{
|
{
|
||||||
line++;
|
line++;
|
||||||
DrawString(gfx, "GamePads:", line++);
|
DrawString(gfx, "GamePad:", line++);
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
GamePadCapabilities caps = GamePad.GetCapabilities(i);
|
GamePadCapabilities caps = GamePad.GetCapabilities(i);
|
||||||
|
@ -240,8 +279,9 @@ namespace Examples.Tests
|
||||||
DrawString(gfx, state.ToString(), line++);
|
DrawString(gfx, state.ToString(), line++);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
line++;
|
line++;
|
||||||
DrawString(gfx, "Joysticks:", line++);
|
DrawString(gfx, "Joystick:", line++);
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
JoystickCapabilities caps = Joystick.GetCapabilities(i);
|
JoystickCapabilities caps = Joystick.GetCapabilities(i);
|
||||||
|
@ -258,7 +298,7 @@ namespace Examples.Tests
|
||||||
|
|
||||||
protected override void OnLoad(EventArgs e)
|
protected override void OnLoad(EventArgs e)
|
||||||
{
|
{
|
||||||
base.OnLoad(e);
|
watch.Start();
|
||||||
|
|
||||||
GL.ClearColor(Color.MidnightBlue);
|
GL.ClearColor(Color.MidnightBlue);
|
||||||
|
|
||||||
|
@ -282,7 +322,14 @@ namespace Examples.Tests
|
||||||
|
|
||||||
protected override void OnRenderFrame(FrameEventArgs e)
|
protected override void OnRenderFrame(FrameEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnRenderFrame(e);
|
render_time += e.Time;
|
||||||
|
|
||||||
|
System.Drawing.Imaging.BitmapData data = TextBitmap.LockBits(
|
||||||
|
new System.Drawing.Rectangle(0, 0, TextBitmap.Width, TextBitmap.Height),
|
||||||
|
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
|
||||||
|
GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, TextBitmap.Width, TextBitmap.Height, PixelFormat.Bgra,
|
||||||
|
PixelType.UnsignedByte, data.Scan0);
|
||||||
|
TextBitmap.UnlockBits(data);
|
||||||
|
|
||||||
if (viewport_changed)
|
if (viewport_changed)
|
||||||
{
|
{
|
||||||
|
@ -297,7 +344,7 @@ namespace Examples.Tests
|
||||||
|
|
||||||
GL.Clear(ClearBufferMask.ColorBufferBit);
|
GL.Clear(ClearBufferMask.ColorBufferBit);
|
||||||
|
|
||||||
GL.Begin(BeginMode.Quads);
|
GL.Begin(PrimitiveType.Quads);
|
||||||
|
|
||||||
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
|
GL.TexCoord2(0, 0); GL.Vertex2(0, 0);
|
||||||
GL.TexCoord2(1, 0); GL.Vertex2(TextBitmap.Width, 0);
|
GL.TexCoord2(1, 0); GL.Vertex2(TextBitmap.Width, 0);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#region License
|
#region License
|
||||||
//
|
//
|
||||||
// The Open Toolkit Library License
|
// The Open Toolkit Library License
|
||||||
//
|
//
|
||||||
|
@ -75,7 +75,7 @@ namespace OpenTK
|
||||||
{
|
{
|
||||||
#region --- Fields ---
|
#region --- Fields ---
|
||||||
|
|
||||||
object exit_lock = new object();
|
readonly Stopwatch watch = new Stopwatch();
|
||||||
|
|
||||||
IGraphicsContext glContext;
|
IGraphicsContext glContext;
|
||||||
|
|
||||||
|
@ -83,12 +83,15 @@ namespace OpenTK
|
||||||
|
|
||||||
double update_period, render_period;
|
double update_period, render_period;
|
||||||
double target_update_period, target_render_period;
|
double target_update_period, target_render_period;
|
||||||
// TODO: Implement these:
|
|
||||||
double update_time, render_time;
|
double update_time; // length of last UpdateFrame event
|
||||||
|
double render_time; // length of last RenderFrame event
|
||||||
|
|
||||||
|
double update_timestamp; // timestamp of last UpdateFrame event
|
||||||
|
double render_timestamp; // timestamp of last RenderFrame event
|
||||||
|
|
||||||
VSyncMode vsync;
|
VSyncMode vsync;
|
||||||
|
|
||||||
Stopwatch update_watch = new Stopwatch(), render_watch = new Stopwatch();
|
|
||||||
double next_render = 0.0, next_update = 0.0;
|
|
||||||
FrameEventArgs update_args = new FrameEventArgs();
|
FrameEventArgs update_args = new FrameEventArgs();
|
||||||
FrameEventArgs render_args = new FrameEventArgs();
|
FrameEventArgs render_args = new FrameEventArgs();
|
||||||
|
|
||||||
|
@ -404,8 +407,7 @@ namespace OpenTK
|
||||||
//Resize += DispatchUpdateAndRenderFrame;
|
//Resize += DispatchUpdateAndRenderFrame;
|
||||||
|
|
||||||
Debug.Print("Entering main loop.");
|
Debug.Print("Entering main loop.");
|
||||||
update_watch.Start();
|
watch.Start();
|
||||||
render_watch.Start();
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
ProcessEvents();
|
ProcessEvents();
|
||||||
|
@ -431,99 +433,61 @@ namespace OpenTK
|
||||||
|
|
||||||
void DispatchUpdateAndRenderFrame(object sender, EventArgs e)
|
void DispatchUpdateAndRenderFrame(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
RaiseUpdateFrame(update_watch, ref next_update, update_args);
|
const int max_frameskip = 10;
|
||||||
RaiseRenderFrame(render_watch, ref next_render, render_args);
|
int frameskip = 0;
|
||||||
}
|
double timestamp = watch.Elapsed.TotalSeconds;
|
||||||
|
|
||||||
void RaiseUpdateFrame(Stopwatch update_watch, ref double next_update, FrameEventArgs update_args)
|
do
|
||||||
{
|
{
|
||||||
int num_updates = 0;
|
|
||||||
double total_update_time = 0;
|
|
||||||
|
|
||||||
// Cap the maximum time drift to 1 second (e.g. when the process is suspended).
|
|
||||||
double time = update_watch.Elapsed.TotalSeconds;
|
|
||||||
if (time <= 0)
|
|
||||||
{
|
|
||||||
// Protect against negative Stopwatch.Elapsed values.
|
|
||||||
// See http://connect.microsoft.com/VisualStudio/feedback/details/94083/stopwatch-returns-negative-elapsed-time
|
|
||||||
update_watch.Reset();
|
|
||||||
update_watch.Start();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (time > 1.0)
|
|
||||||
time = 1.0;
|
|
||||||
|
|
||||||
// Raise UpdateFrame events until we catch up with our target update rate.
|
// Raise UpdateFrame events until we catch up with our target update rate.
|
||||||
while (next_update - time <= 0 && time > 0)
|
double update_elapsed = MathHelper.Clamp(timestamp - update_timestamp, 0.0, 1.0);
|
||||||
|
if (RaiseUpdateFrame(update_elapsed))
|
||||||
{
|
{
|
||||||
next_update -= time;
|
update_period = update_elapsed;
|
||||||
update_args.Time = time;
|
update_timestamp = timestamp;
|
||||||
OnUpdateFrameInternal(update_args);
|
timestamp = watch.Elapsed.TotalSeconds;
|
||||||
time = update_time = Math.Max(update_watch.Elapsed.TotalSeconds, 0) - time;
|
update_time = timestamp - update_timestamp;
|
||||||
// Stopwatches are not accurate over long time periods.
|
}
|
||||||
// We accumulate the total elapsed time into the time variable
|
else
|
||||||
// while reseting the Stopwatch frequently.
|
{
|
||||||
update_watch.Reset();
|
// We have executed enough UpdateFrame events to catch up.
|
||||||
update_watch.Start();
|
// Break and issue a RenderFrame event.
|
||||||
|
|
||||||
// Don't schedule a new update more than 1 second in the future.
|
|
||||||
// Sometimes the hardware cannot keep up with updates
|
|
||||||
// (e.g. when the update rate is too high, or the UpdateFrame processing
|
|
||||||
// is too costly). This cap ensures we can catch up in a reasonable time
|
|
||||||
// once the load becomes lighter.
|
|
||||||
next_update += TargetUpdatePeriod;
|
|
||||||
next_update = Math.Max(next_update, -1.0);
|
|
||||||
|
|
||||||
total_update_time += update_time;
|
|
||||||
|
|
||||||
// Allow up to 10 consecutive UpdateFrame events to prevent the
|
|
||||||
// application from "hanging" when the hardware cannot keep up
|
|
||||||
// with the requested update rate.
|
|
||||||
if (++num_updates >= 10 || TargetUpdateFrequency == 0.0)
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
} while (++frameskip < max_frameskip);
|
||||||
|
|
||||||
// Calculate statistics
|
timestamp = watch.Elapsed.TotalSeconds;
|
||||||
if (num_updates > 0)
|
double render_elapsed = MathHelper.Clamp(timestamp - render_timestamp, 0.0, 1.0);
|
||||||
|
if (RaiseRenderFrame(render_elapsed))
|
||||||
{
|
{
|
||||||
update_period = total_update_time / (double)num_updates;
|
render_period = render_elapsed;
|
||||||
|
render_timestamp = timestamp;
|
||||||
|
timestamp = watch.Elapsed.TotalSeconds;
|
||||||
|
render_time = timestamp - render_timestamp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RaiseRenderFrame(Stopwatch render_watch, ref double next_render, FrameEventArgs render_args)
|
bool RaiseUpdateFrame(double time)
|
||||||
{
|
{
|
||||||
// Cap the maximum time drift to 1 second (e.g. when the process is suspended).
|
if (time >= TargetUpdatePeriod)
|
||||||
double time = render_watch.Elapsed.TotalSeconds;
|
|
||||||
if (time <= 0)
|
|
||||||
{
|
{
|
||||||
// Protect against negative Stopwatch.Elapsed values.
|
update_args.Time = time;
|
||||||
// See http://connect.microsoft.com/VisualStudio/feedback/details/94083/stopwatch-returns-negative-elapsed-time
|
OnUpdateFrameInternal(update_args);
|
||||||
render_watch.Reset();
|
return true;
|
||||||
render_watch.Start();
|
}
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
if (time > 1.0)
|
|
||||||
time = 1.0;
|
|
||||||
double time_left = next_render - time;
|
|
||||||
|
|
||||||
if (time_left <= 0.0 && time > 0)
|
|
||||||
|
bool RaiseRenderFrame(double time)
|
||||||
{
|
{
|
||||||
// Schedule next render event. The 1 second cap ensures
|
if (time >= TargetRenderPeriod)
|
||||||
// the process does not appear to hang.
|
|
||||||
next_render = time_left + TargetRenderPeriod;
|
|
||||||
if (next_render < -1.0)
|
|
||||||
next_render = -1.0;
|
|
||||||
|
|
||||||
render_watch.Reset();
|
|
||||||
render_watch.Start();
|
|
||||||
|
|
||||||
if (time > 0)
|
|
||||||
{
|
{
|
||||||
render_period = render_args.Time = time;
|
render_args.Time = time;
|
||||||
OnRenderFrameInternal(render_args);
|
OnRenderFrameInternal(render_args);
|
||||||
render_time = render_watch.Elapsed.TotalSeconds;
|
return true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
Loading…
Reference in a new issue