Skip to content

Commit

Permalink
Better update accuracy on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
NogginBops committed Feb 22, 2023
1 parent efb6ab7 commit dbd3a12
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/OpenTK.Windowing.Desktop/GameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicsLibraryFramework;
Expand Down Expand Up @@ -201,6 +202,12 @@ public GameWindow(GameWindowSettings gameWindowSettings, NativeWindowSettings na
UpdateFrequency = gameWindowSettings.UpdateFrequency;
}

[DllImport("winmm")]
private static extern uint timeBeginPeriod(uint uPeriod);

[DllImport("winmm")]
private static extern uint timeEndPeriod(uint uPeriod);

/// <summary>
/// Initialize the update thread (if using a multi-threaded context, and enter the game loop of the GameWindow).
/// </summary>
Expand All @@ -209,6 +216,11 @@ public virtual unsafe void Run()
// Make sure that the gl contexts is current for OnLoad and the initial OnResize
Context?.MakeCurrent();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
timeBeginPeriod(1);
}

// Send the OnLoad event, to load all user code.
OnLoad();

Expand Down Expand Up @@ -246,6 +258,8 @@ public virtual unsafe void Run()
}

OnUnload();

timeEndPeriod(1);
}

private unsafe void StartRenderThread()
Expand Down
8 changes: 5 additions & 3 deletions tests/LocalTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ static void Main(string[] args)
{
GameWindowSettings gwSettings = new GameWindowSettings()
{
//UpdateFrequency = 10,
//RenderFrequency = 10,
// UpdateFrequency = 60,
// RenderFrequency = 60,
};

NativeWindowSettings nwSettings = new NativeWindowSettings()
Expand All @@ -28,7 +28,7 @@ static void Main(string[] args)
Profile = ContextProfile.Core,
Size = (800, 600),
StartFocused = true,
StartVisible = false,
StartVisible = true,
Title = "Local OpenTK Test",
WindowBorder = WindowBorder.Resizable,
WindowState = WindowState.Normal,
Expand Down Expand Up @@ -58,6 +58,8 @@ protected override void OnRenderFrame(FrameEventArgs args)
GL.ClearColor(Color4.Coral);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

Console.WriteLine($"{1/args.Time:0.000}");

SwapBuffers();
}

Expand Down

0 comments on commit dbd3a12

Please sign in to comment.