Created
April 3, 2025 02:22
-
-
Save lucasteles/84739da2b3ff6d102cad3ef5b53e555a to your computer and use it in GitHub Desktop.
FPS Counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
public sealed class FpsCounter | |
{ | |
double currentFps; | |
long lastFrameTime; | |
const float SmoothFactor = 0.1f; | |
public FpsCounter() => Reset(); | |
public double Current => Math.Round(currentFps, 2); | |
public void Update() | |
{ | |
var delta = Stopwatch.GetElapsedTime(lastFrameTime); | |
Update(delta.TotalSeconds); | |
lastFrameTime = Stopwatch.GetTimestamp(); | |
} | |
void Update(double deltaTime) | |
{ | |
var newFps = 1 / deltaTime; | |
currentFps = ((1 - SmoothFactor) * currentFps) + (SmoothFactor * newFps); | |
} | |
public void Reset() | |
{ | |
currentFps = FrameRate.ExpectedFps; | |
lastFrameTime = Stopwatch.GetTimestamp(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment