Updated Fps counter
This commit is contained in:
@@ -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<Float>;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user