From 57e6968f3c5b1f707978b05b1a8253195584d770 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 25 Jul 2023 22:19:18 -0400 Subject: [PATCH] add fps counter --- Program.cs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Program.cs b/Program.cs index 7723201..3a45e43 100644 --- a/Program.cs +++ b/Program.cs @@ -10,12 +10,33 @@ using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Metadata.Profiles.Xmp; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.Drawing; +using System; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Xml.Linq; namespace SemiColinGames; +public class FpsCounter { + private readonly int[] frameTimes = new int[60]; + private double fps = 0; + private int idx = 0; + + public int Fps { + get => (int) Math.Ceiling(fps); + } + + public void Update() { + var now = Environment.TickCount; // ms + if (frameTimes[idx] != 0) { + var timeElapsed = now - frameTimes[idx]; + fps = 1000.0 * frameTimes.Length / timeElapsed; + } + frameTimes[idx] = now; + idx = (idx + 1) % frameTimes.Length; + } +} + public class CameraInfo { public readonly Vector2i Resolution; @@ -499,6 +520,7 @@ public class Game : GameWindow { private static Texture TEXTURE_BLACK = new(new Image(1, 1, new Rgba32(0, 0, 0))); UiGeometry geometry = new(); + FpsCounter fpsCounter = new(); // Input handling. long downTimer = Int64.MaxValue; @@ -526,6 +548,7 @@ public class Game : GameWindow { protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); + Console.WriteLine("update"); long now = DateTime.Now.Ticks; KeyboardState input = KeyboardState; @@ -726,6 +749,8 @@ public class Game : GameWindow { protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); + Console.WriteLine("render"); + fpsCounter.Update(); LoadAndUnloadImagesAsync(); @@ -767,6 +792,7 @@ public class Game : GameWindow { // Draw status box. DrawFilledBox(geometry.StatusBox, Color4.Black); DrawText(activePhoto.Description(), geometry.StatusBox.Min.X + 80, geometry.StatusBox.Min.Y); + DrawText($" FPS: {fpsCounter.Fps}", geometry.StatusBox.Max.X - 76, geometry.StatusBox.Min.Y); if (activePhoto.Loaded) { DrawText($"{(scale * 100):F1}%", geometry.StatusBox.Min.X, geometry.StatusBox.Min.Y); } @@ -860,7 +886,8 @@ static class Program { } Console.WriteLine($"best monitor: {bestMonitor.HorizontalResolution}x{bestMonitor.VerticalResolution}"); GameWindowSettings gwSettings = new(); - gwSettings.RenderFrequency = 60.0; + gwSettings.UpdateFrequency = 30.0; + gwSettings.RenderFrequency = 30.0; NativeWindowSettings nwSettings = new(); nwSettings.WindowState = WindowState.Normal;