From 669f4f37edc002355f3b6dd79531a503db263660 Mon Sep 17 00:00:00 2001 From: Ne_Eo Date: Thu, 11 Jul 2024 18:17:48 +0200 Subject: [PATCH] Updated Fps counter --- .../system/framerate/FramerateCounter.hx | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/source/funkin/backend/system/framerate/FramerateCounter.hx b/source/funkin/backend/system/framerate/FramerateCounter.hx index 0b3ed07b..931ba9d6 100644 --- a/source/funkin/backend/system/framerate/FramerateCounter.hx +++ b/source/funkin/backend/system/framerate/FramerateCounter.hx @@ -1,17 +1,26 @@ package funkin.backend.system.framerate; +import haxe.ds.Vector; import openfl.display.Sprite; import openfl.text.TextField; import openfl.text.TextFormat; +inline final FRAME_TIME_HISTORY = 20; + class FramerateCounter extends Sprite { public var fpsNum:TextField; public var fpsLabel:TextField; - public var lastFPS:Float = 0; + + public var fpsHistoryIndex:Int = 0; + public var fpsHistory:Vector; public function new() { super(); + fpsHistory = new Vector(FRAME_TIME_HISTORY); + // Initialize to 60 FPS, so that the initial estimation until we get enough data is always reasonable. + fpsHistory.fill(1000.0 / 60.0); + fpsNum = new TextField(); fpsLabel = new TextField(); @@ -30,8 +39,20 @@ class FramerateCounter extends Sprite { if (alpha <= 0.05) return; super.__enterFrame(t); - lastFPS = CoolUtil.fpsLerp(lastFPS, FlxG.elapsed == 0 ? 0 : (1 / FlxG.elapsed), 0.25); - fpsNum.text = Std.string(Math.floor(lastFPS)); + // todo use gpu time + fpsHistory[fpsHistoryIndex] = FlxG.elapsed * 1000; + fpsHistoryIndex = (fpsHistoryIndex + 1) % FRAME_TIME_HISTORY; + + // Calculate average CPU time. + // Code based on Godot's FPS counter. + var cpuTime = 0.0; + for(i in 0...FRAME_TIME_HISTORY) { + cpuTime += fpsHistory[i]; + } + cpuTime /= FRAME_TIME_HISTORY; + cpuTime = Math.max(0.01, cpuTime); // Prevent unrealistically low values. + + fpsNum.text = Std.string(Math.floor(1000.0 / cpuTime)); fpsLabel.x = fpsNum.x + fpsNum.width; fpsLabel.y = (fpsNum.y + fpsNum.height) - fpsLabel.height; }