add fps counter

This commit is contained in:
Colin McMillen 2023-07-25 22:19:18 -04:00
parent 32cb03ab15
commit 57e6968f3c

View File

@ -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<Rgba32>(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;