Skip to content

Commit

Permalink
Add integration with windowing to TimeTirals
Browse files Browse the repository at this point in the history
  • Loading branch information
Perksey committed Aug 20, 2023
1 parent 1a7a5c7 commit 3e222b0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Core/Silk.NET.Core/Miscellaneous/TimeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ public bool BeginWindow()
{
#if NET7_0_OR_GREATER
var ct = CurrentTime;
if (Sleeping && ct < NextWindowStart)
var nw = NextWindowStart;
if (Sleeping && ct < nw)
{
_sleep.Sleep(NextWindowStart - ct);
_sleep.Sleep(nw - ct);
}
#endif
// ct local var intentionally not used here to ensure we are up-to-date
return CurrentTime >= NextWindowStart;
}

Expand Down
58 changes: 57 additions & 1 deletion src/Lab/Experiments/TimeTrials/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,68 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using Silk.NET.Core;
using Silk.NET.Windowing;

var timeWindow = new TimeWindow();
var timeWindow = new TimeWindow(false);
var bag = new ConcurrentBag<double>();
var sw = new Stopwatch();
Console.Write("Enter frames per second: ");
timeWindow.Window = TimeSpan.FromSeconds(1 / double.Parse(Console.ReadLine()!));
Console.Write("Open a window? (Y/N) ");
ConsoleKey k;
while ((k = Console.ReadKey().Key) is not ConsoleKey.Y and not ConsoleKey.Y) {}

if (k == ConsoleKey.Y)
{
Console.Write("\nOpening window...\n");
var realWindow = Window.Create
(
WindowOptions.Default with
{
VSync = false, FramesPerSecond = 1 / timeWindow.Window.TotalSeconds,
UpdatesPerSecond = 1 / timeWindow.Window.TotalSeconds
}
);
realWindow.Initialize();
realWindow.Title += " | 0 FPS | 0 UPS";
realWindow.Render += d =>
{
var cur = realWindow.Title;
realWindow.Title = cur.Replace
(cur[cur.IndexOf('|')..(cur.IndexOf(" FPS", StringComparison.Ordinal) + 4)], $"| {1 / d:0000.000} FPS");
Console.WriteLine($"{sw.Elapsed.TotalSeconds},{d}");
};
realWindow.Update += d =>
{
var cur = realWindow.Title;
realWindow.Title = cur.Replace
(cur[cur.LastIndexOf('|')..(cur.IndexOf(" UPS", StringComparison.Ordinal) + 4)], $"| {1 / d:0000.000} UPS");
};
Console.WriteLine("Recording Time,Frame Time");
sw.Start();
while (!realWindow.IsClosing)
{
if (timeWindow.BeginWindow())
{
if (!realWindow.IsClosing)
{
realWindow.DoUpdate();
}

if (!realWindow.IsClosing)
{
realWindow.DoRender();
}
timeWindow.EndWindow();
}
realWindow.DoEvents();
}

realWindow.DoEvents();
realWindow.Reset();
return;
}

Console.WriteLine("Press Ctrl+C to measure statistics.");
Console.CancelKeyPress += (_, _) =>
{
Expand Down
1 change: 1 addition & 0 deletions src/Lab/Experiments/TimeTrials/TimeTrials.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\Core\Silk.NET.Core\Silk.NET.Core.csproj" />
<ProjectReference Include="..\..\..\Windowing\Silk.NET.Windowing.Glfw\Silk.NET.Windowing.Glfw.csproj" />
</ItemGroup>

</Project>

0 comments on commit 3e222b0

Please sign in to comment.