Skip to content

Commit a8808be

Browse files
authored
Fix FramerateCounter and VRAM Counter (#820)
* Update FramerateCounter.hx * Update FramerateCounter.hx * Update FramerateCounter.hx * Update FramerateCounter.hx * Update FramerateCounter.hx * Update FramerateCounter.hx * Update FramerateCounter.hx * Update FramerateCounter.hx * Update FramerateCounter.hx * Optimize memory label update in MemoryCounter * Optimize frame update logic in AssetTreeInfo and MemoryCounter * Fixed VRAM display showing -2147483648B * Update SystemInfo.hx * VRAM: 6MB?
1 parent 53aa9cc commit a8808be

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

source/funkin/backend/system/framerate/AssetTreeInfo.hx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import funkin.backend.assets.IModsAssetLibrary;
88
import funkin.backend.assets.ScriptedAssetLibrary;
99

1010
class AssetTreeInfo extends FramerateCategory {
11+
private var lastUpdateTime:Float = 1;
12+
1113
public function new() {
1214
super("Asset Libraries Tree Info");
1315
}
1416

1517
public override function __enterFrame(t:Int) {
1618
if (alpha <= 0.05) return;
19+
20+
if ((lastUpdateTime += FlxG.rawElapsed) < 1)
21+
return;
22+
23+
lastUpdateTime = 0;
24+
1725
var text = 'Not initialized yet\n';
1826
if (Paths.assetsTree != null){
1927
text = "";

source/funkin/backend/system/framerate/FramerateCounter.hx

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ class FramerateCounter extends Sprite {
99
public var fpsLabel:TextField;
1010
public var lastFPS:Float = 0;
1111

12+
private var frameCount:Int = 0;
13+
14+
private var accumulatedTime:Float = openfl.Lib.getTimer();
15+
16+
private final updateInterval:Float = 1 / 15;
17+
private var lastUpdateTime:Float = 0;
18+
1219
public function new() {
1320
super();
1421

@@ -27,15 +34,40 @@ class FramerateCounter extends Sprite {
2734
}
2835
}
2936

30-
public function reload() {}
37+
public function reload() {
38+
lastUpdateTime = 0;
39+
}
3140

3241
public override function __enterFrame(t:Int) {
3342
if (alpha <= 0.05) return;
43+
3444
super.__enterFrame(t);
3545

36-
lastFPS = CoolUtil.fpsLerp(lastFPS, FlxG.rawElapsed == 0 ? 0 : (1 / FlxG.rawElapsed), 0.25);
37-
fpsNum.text = Std.string(Math.floor(lastFPS));
46+
frameCount++;
47+
48+
if ((lastUpdateTime += FlxG.rawElapsed) < updateInterval)
49+
{
50+
updateLabelPosition();
51+
return;
52+
}
53+
54+
final timer = openfl.Lib.getTimer();
55+
56+
final time = timer - accumulatedTime;
57+
58+
accumulatedTime = timer;
59+
60+
lastFPS = FlxMath.lerp(lastFPS, time <= 0 ? 0 : (1000 / time * frameCount), 1.0 - Math.pow(0.75, time * 0.06));
61+
62+
fpsNum.text = Std.string(Math.round(lastFPS));
63+
lastUpdateTime = frameCount = 0;
64+
65+
updateLabelPosition();
66+
}
67+
68+
private inline function updateLabelPosition():Void
69+
{
3870
fpsLabel.x = fpsNum.x + fpsNum.width;
3971
fpsLabel.y = (fpsNum.y + fpsNum.height) - fpsLabel.height;
4072
}
41-
}
73+
}

source/funkin/backend/system/framerate/MemoryCounter.hx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,21 @@ class MemoryCounter extends Sprite {
3636
if (alpha <= 0.05) return;
3737
super.__enterFrame(t);
3838

39-
memory = MemoryUtil.currentMemUsage();
39+
final mem = MemoryUtil.currentMemUsage();
40+
41+
if (mem == memory) {
42+
updateLabelPosition();
43+
return;
44+
}
45+
46+
memory = mem;
4047
if (memoryPeak < memory) memoryPeak = memory;
4148
memoryText.text = CoolUtil.getSizeString(memory);
4249
memoryPeakText.text = ' / ${CoolUtil.getSizeString(memoryPeak)}';
4350

44-
memoryPeakText.x = memoryText.x + memoryText.width;
51+
updateLabelPosition();
4552
}
53+
54+
private inline function updateLabelPosition():Void
55+
memoryPeakText.x = memoryText.x + memoryText.width;
4656
}

source/funkin/backend/system/framerate/SystemInfo.hx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ class SystemInfo extends FramerateCategory {
115115
if (vRAMBytes == 1000 || vRAMBytes == 1 || vRAMBytes <= 0)
116116
Logs.trace('Unable to grab GPU VRAM', ERROR, RED);
117117
else {
118-
var vRAMBytesFloat:#if cpp Float64 #else Float #end = vRAMBytes*1024;
119-
vRAM = CoolUtil.getSizeString64(vRAMBytesFloat);
118+
vRAM = getSizeString(vRAMBytes / 1024);
120119
}
121120
}
122121
} else
@@ -155,6 +154,17 @@ class SystemInfo extends FramerateCategory {
155154
if (totalMem != "Unknown" && memType != "Unknown") __formattedSysText += '\nTotal MEM: $totalMem $memType';
156155
}
157156

157+
static function getSizeString(size:Float):String {
158+
if (size < 1024)
159+
return Std.int(size) + " MB";
160+
else if (size < 1024 * 1024)
161+
return Std.int(size / 1024) + " GB";
162+
else {
163+
var tb = size / (1024 * 1024);
164+
return Std.int(tb) + "." + CoolUtil.addZeros(Std.string(Std.int((tb % 1) * 100)), 2) + " TB";
165+
}
166+
}
167+
158168
public function new() {
159169
super("System Info");
160170
}

0 commit comments

Comments
 (0)