Skip to content

Commit 54d42da

Browse files
committed
Better FPS Calculation!
Signed-off-by: TechnikTil <techniktil@tilnotdrip.org>
1 parent 481f34c commit 54d42da

5 files changed

Lines changed: 104 additions & 95 deletions

File tree

Project.hxp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Project extends HXProject
7070
meta.company = 'TilNotDrip';
7171

7272
app.file = 'TechNotDrip';
73-
app.main = 'Main';
73+
app.main = 'funkin.FunkinGame';
7474
app.preloader = 'flixel.system.FlxPreloader';
7575
}
7676

src/Main.hx renamed to src/funkin/FunkinGame.hx

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
1-
package;
1+
package funkin;
22

3-
import flixel.FlxG;
3+
import flixel.FlxBasic;
44
import flixel.FlxGame;
5-
import flixel.FlxSprite;
65
import flixel.util.typeLimit.NextState;
7-
import funkin.data.save.Save;
86
import funkin.objects.ui.PerformanceStats;
97
import funkin.states.ui.TitleState;
108
import lime.utils.Assets as LimeAssets;
11-
import openfl.display.Sprite;
129
import openfl.utils.Assets as OpenFlAssets;
1310
#if FUNKIN_DISCORD_RPC
1411
import funkin.api.DiscordRPC;
1512
#end
1613

17-
class Main extends Sprite
14+
class FunkinGame extends FlxGame
1815
{
16+
/**
17+
* The current instance of `FunkinGame`.
18+
*/
19+
public static var instance:FunkinGame;
20+
1921
/**
2022
* The FPS and Memory overlay at the top left of the screen.
2123
*/
22-
public static var performanceStats:PerformanceStats;
24+
public var performanceStats:PerformanceStats;
2325

2426
final flxGameData:FlxGameInit = {
2527
width: 1280,
@@ -32,16 +34,15 @@ class Main extends Sprite
3234

3335
public function new()
3436
{
35-
super();
37+
super(flxGameData.width, flxGameData.height, flxGameData.initState, flxGameData.framerate, flxGameData.framerate, !flxGameData.showSplash,
38+
flxGameData.startFullscreen);
3639

37-
initGame();
40+
instance = this;
3841
}
3942

40-
function initGame():Void
43+
override function create(_):Void
4144
{
42-
var flxGame:FlxGame = new FlxGame(flxGameData.width, flxGameData.height, flxGameData.initState, flxGameData.framerate, flxGameData.framerate,
43-
!flxGameData.showSplash, flxGameData.startFullscreen);
44-
addChild(flxGame);
45+
super.create(_);
4546

4647
performanceStats = new PerformanceStats();
4748
addChild(performanceStats);
@@ -69,6 +70,58 @@ class Main extends Sprite
6970
stage.window.onClose.add(closeWindow);
7071
}
7172

73+
override function onEnterFrame(_):Void
74+
{
75+
ticks = getTicks();
76+
_elapsedMS = ticks - _total;
77+
_total = ticks;
78+
79+
if (soundTray != null && soundTray.active)
80+
soundTray.update(_elapsedMS);
81+
82+
if (performanceStats != null)
83+
performanceStats.update(_elapsedMS / 1000);
84+
85+
if (_lostFocus && FlxG.autoPause)
86+
return;
87+
88+
if (FlxG.vcr.paused)
89+
{
90+
if (FlxG.vcr.stepRequested)
91+
{
92+
FlxG.vcr.stepRequested = false;
93+
}
94+
else if (_nextState == null)
95+
{
96+
#if FLX_DEBUG
97+
debugger.update();
98+
// If the interactive debug is active, the screen must
99+
// be rendered because the user might be doing changes
100+
// to game objects (e.g. moving things around).
101+
if (debugger.interaction.isActive())
102+
{
103+
draw();
104+
}
105+
#end
106+
107+
return;
108+
}
109+
}
110+
111+
step();
112+
113+
#if FLX_DEBUG
114+
FlxBasic.visibleCount = 0;
115+
#end
116+
117+
draw();
118+
119+
#if FLX_DEBUG
120+
debugger.stats.visibleObjects(FlxBasic.visibleCount);
121+
debugger.update();
122+
#end
123+
}
124+
72125
/**
73126
* Called when the game gets closed.
74127
*/

src/funkin/data/save/Save.hx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ class Save
121121
FlxG.updateFramerate = options.fps;
122122
}
123123

124-
if (Main.performanceStats != null)
125-
Main.performanceStats.visible = options.showFps;
124+
if (FunkinGame.instance?.performanceStats != null)
125+
{
126+
FunkinGame.instance.performanceStats.visible = options.showFps;
127+
}
126128

127129
FlxG.fullscreen = options.fullscreen;
128130

src/funkin/objects/ui/PerformanceStats.hx

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ import openfl.text.TextFormat;
1111
*/
1212
class PerformanceStats extends Sprite
1313
{
14+
/**
15+
* How long we should wait until we update the text.
16+
*/
17+
public static final TEXT_UPDATE_INTERVAL:Float = 0.1;
18+
1419
/**
1520
* How many frames have passed since the last second.
1621
*/
17-
public var framesPerSecond(default, null):Int;
22+
public var framesPerSecond(default, null):Float;
1823

1924
/**
2025
* The amount of RAM the application is currently using.
@@ -26,8 +31,7 @@ class PerformanceStats extends Sprite
2631
*/
2732
var mainText:TextField;
2833

29-
var fpsCount:Int = 0;
30-
var elapsed:Float = 0;
34+
var textUpdateTimer:Float;
3135

3236
public function new(x:Float = 5, y:Float = 5)
3337
{
@@ -51,31 +55,43 @@ class PerformanceStats extends Sprite
5155
];
5256

5357
addChild(mainText);
58+
59+
textUpdateTimer = TEXT_UPDATE_INTERVAL;
5460
}
5561

56-
override function __enterFrame(deltaTime:Float):Void
62+
/**
63+
* Update the counter.
64+
* @param elapsed The seconds elapsed since last call.
65+
*/
66+
public function update(elapsed:Float):Void
5767
{
58-
fpsCount++;
59-
elapsed += deltaTime;
68+
if (elapsed <= 0)
69+
return;
6070

61-
if (elapsed >= 1000)
71+
textUpdateTimer -= elapsed;
72+
73+
var currentFPS:Float = 1 / elapsed;
74+
var smoothMult:Float = FlxMath.bound(elapsed / 0.5, 0.05, 1);
75+
76+
framesPerSecond = framesPerSecond * (1 - smoothMult) + currentFPS * smoothMult;
77+
framesPerSecond = Math.min(framesPerSecond, FlxG.game.stage.frameRate);
78+
79+
if (textUpdateTimer <= 0)
6280
{
63-
framesPerSecond = fpsCount;
64-
fpsCount = 0;
65-
elapsed = 0;
66-
mainText.text = "FPS: " + framesPerSecond + "\n" + getMemory();
81+
mainText.text = "FPS: " + FlxMath.roundDecimal(framesPerSecond, 2) + "\n" + getMemory();
82+
textUpdateTimer = TEXT_UPDATE_INTERVAL;
6783
}
6884
}
6985

7086
function getMemory():String
7187
{
72-
if (randomAccessMemory != null)
73-
{
74-
var formatted:String = FlxStringUtil.formatBytes(randomAccessMemory);
75-
return (formatted == "0MB") ? "" : "MEM: " + formatted;
76-
}
88+
var ram:Float = randomAccessMemory ?? 0;
89+
var formatted:String = FlxStringUtil.formatBytes(ram);
90+
91+
if (ram > 0)
92+
formatted = 'MEM: ${formatted}';
7793

78-
return "";
94+
return formatted;
7995
}
8096

8197
function get_randomAccessMemory():Null<Float>

src/funkin/states/ui/CreditsState.hx

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -342,68 +342,6 @@ class CreditsState extends FunkinState
342342
}
343343
}
344344
}
345-
346-
#if FUNKIN_GIT_DETAILS
347-
/**
348-
* Gets contributor percentage for the current branch.
349-
* @return Author => Percentage
350-
*/
351-
public static function getGithubContributors():Map<String, Float>
352-
{
353-
var numerators:Map<String, Int> = new Map<String, Int>();
354-
var denominator:Int = 0;
355-
356-
var commitsGet:Http = new Http('https://api.github.com/repos/TilNotDrip/TechNotDrip-Engine/commits?sha=' + Constants.GIT_BRANCH);
357-
commitsGet.setHeader("User-Agent", "request");
358-
359-
var statusCode:Int = 400;
360-
commitsGet.onStatus = (code:Int) ->
361-
{
362-
statusCode = code;
363-
};
364-
365-
commitsGet.request();
366-
367-
var commitList:Array<Dynamic> = [];
368-
369-
try
370-
{
371-
if (statusCode < 200 || statusCode >= 400)
372-
throw "HTTP ERROR";
373-
374-
// when my wifi down it be returning nothing and try catching the actual request didnt work so
375-
commitList = cast Json.parse(commitsGet.responseData);
376-
}
377-
catch (e:Exception)
378-
{
379-
trace('[ERROR] Could not parse the commit list! Message: ${e.message}');
380-
}
381-
382-
for (commit in commitList)
383-
{
384-
denominator++;
385-
386-
var authorName:String = commit?.author?.login;
387-
var numerator:Null<Int> = numerators.get(authorName);
388-
389-
if (numerator != null)
390-
numerator++;
391-
else
392-
numerator = 1;
393-
394-
numerators.set(authorName, numerator);
395-
}
396-
397-
var toReturn:Map<String, Float> = new Map<String, Float>();
398-
for (author in numerators.keys())
399-
{
400-
var percentage:Float = ((numerators.get(author) ?? 0) / denominator);
401-
toReturn.set(author, percentage);
402-
}
403-
404-
return toReturn;
405-
}
406-
#end
407345
}
408346

409347
private class CreditsBubble extends FlxSpriteGroup

0 commit comments

Comments
 (0)