Add memory usage indicator and add GC toggle

This commit is contained in:
AirDog46 2025-05-18 00:54:25 +03:00
parent e03cd7d1c3
commit 903970c542
9 changed files with 108 additions and 13 deletions

View File

@ -23,6 +23,10 @@ class GameOverState extends FlxTransitionableState
override function create()
{
if (!FlxG.save.data.GC) {
Gc.enable(true);
trace("Player died. We can reenable the garbage collector");
}
var loser:FlxSprite = new FlxSprite(100, 100);
var loseTex = Paths.getSparrowAtlas('lose');
loser.frames = loseTex;

View File

@ -46,10 +46,10 @@ class KadeEngineData
if (FlxG.save.data.fpsRain == null)
FlxG.save.data.fpsRain = false;
if (FlxG.save.data.fpsCap == null)
FlxG.save.data.fpsCap = 120;
/*if (FlxG.save.data.fpsCap == null)
FlxG.save.data.fpsCap = 120;*/
if (FlxG.save.data.fpsCap > 285 || FlxG.save.data.fpsCap < 60)
if (FlxG.save.data.fpsCap > 1000 || FlxG.save.data.fpsCap < 60)
FlxG.save.data.fpsCap = 120; // baby proof so you can't hard lock ur copy of kade engine
if (FlxG.save.data.scrollSpeed == null)
@ -118,6 +118,9 @@ class KadeEngineData
if (FlxG.save.data.zoom == null)
FlxG.save.data.zoom = 1;
if (FlxG.save.data.GC == null)
FlxG.save.data.GC == true;
var gamepad:FlxGamepad = FlxG.gamepads.lastActive;
KeyBinds.gamepad = gamepad != null;

View File

@ -1,5 +1,6 @@
package;
import cpp.vm.Gc;
import lime.app.Application;
#if windows
import Discord.DiscordClient;
@ -102,6 +103,8 @@ class Main extends Sprite
fpsCounter = new FPS(10, 3, 0xFFFFFF);
addChild(fpsCounter);
toggleFPS(FlxG.save.data.fps);
memory = new Memory(10, 15, 0xFFFFFF);
addChild(memory);
#end
}
@ -109,6 +112,8 @@ class Main extends Sprite
var fpsCounter:FPS;
var memory:Memory;
public function toggleFPS(fpsEnabled:Bool):Void {
fpsCounter.visible = fpsEnabled;
}

45
source/Memory.hx Normal file
View File

@ -0,0 +1,45 @@
package;
import flixel.util.FlxStringUtil;
import openfl.events.Event;
import openfl.text.TextFormat;
import cpp.vm.Gc;
import openfl.text.TextField;
import openfl.Lib;
class Memory extends TextField {
private var overCounter = 0;
private var isOver = false;
@:noCompletion private var cacheCount:Int;
@:noCompletion private var currentTime:Float;
@:noCompletion private var times:Array<Float>;
public function new(x:Float = 15, y:Float = 20, color:Int = 0x000000) {
super();
this.x = x;
this.y = y;
defaultTextFormat = new TextFormat("_sans", 12, color);
text = "Memory: ";
addEventListener(Event.ENTER_FRAME, function(e)
{
var usedMemory:Float = Gc.memUsage();
if (Gc.memUsage() < 0 && !isOver)
{
isOver = true;
}
if (Gc.memUsage() > 0 && isOver)
{
overCounter++;
isOver = false;
}
if (isOver) {
usedMemory = 2147483647+(2147483647-(Math.abs(Gc.memUsage()))); // funny math to represent values over 2GB
}
text = "Memory: " + FlxStringUtil.formatBytes(usedMemory + 4294967296*overCounter);
});
}
}

View File

@ -530,10 +530,10 @@ class FPSCapOption extends Option
}
override function right():Bool {
if (FlxG.save.data.fpsCap >= 290)
if (FlxG.save.data.fpsCap >= 1000)
{
FlxG.save.data.fpsCap = 290;
(cast (Lib.current.getChildAt(0), Main)).setFPSCap(290);
FlxG.save.data.fpsCap = 1000;
(cast (Lib.current.getChildAt(0), Main)).setFPSCap(1000);
}
else
FlxG.save.data.fpsCap = FlxG.save.data.fpsCap + 10;
@ -543,8 +543,8 @@ class FPSCapOption extends Option
}
override function left():Bool {
if (FlxG.save.data.fpsCap > 290)
FlxG.save.data.fpsCap = 290;
if (FlxG.save.data.fpsCap > 1000)
FlxG.save.data.fpsCap = 1000;
else if (FlxG.save.data.fpsCap < 60)
FlxG.save.data.fpsCap = Application.current.window.displayMode.refreshRate;
else
@ -631,6 +631,27 @@ class RainbowFPSOption extends Option
}
}
class GarbageCollection extends Option
{
public function new(desc:String)
{
super();
description = desc;
}
public override function press():Bool
{
FlxG.save.data.GC = !FlxG.save.data.GC;
display = updateDisplay();
return true;
}
private override function updateDisplay():String
{
return "Garbage Collection " + ((FlxG.save.data.GC) ? " ON " : "OFF") ;
}
}
class Optimization extends Option
{
public function new(desc:String)
@ -946,6 +967,7 @@ class ResetSettings extends Option
FlxG.save.data.optimize = null;
FlxG.save.data.cacheImages = null;
FlxG.save.data.editor = null;
FlxG.save.data.GC = null;
KadeEngineData.initSave();
confirm = false;

View File

@ -60,6 +60,7 @@ class OptionsMenu extends MusicBeatState
new MissSoundsOption("Toggle miss sounds playing when you don't hit a note."),
new ScoreScreen("Show the score screen after the end of a song"),
new ShowInput("Display every single input on the score screen."),
new GarbageCollection("Reduces lag at the cost of immense RAM usage."),
new Optimization("No characters or backgrounds. Just a usual rhythm game layout."),
new GraphicLoading("On startup, cache every character. Significantly decrease load times. (HIGH MEMORY)"),
new BotPlay("Showcase your charts and mods with autoplay.")

View File

@ -33,6 +33,8 @@ class PauseSubState extends MusicBeatSubstate
public function new(x:Float, y:Float)
{
super();
cpp.vm.Gc.run(true);
trace("Game paused. We can run the garbage collector");
if (PlayState.instance.useVideo)
{
@ -225,6 +227,10 @@ class PauseSubState extends MusicBeatSubstate
PlayState.instance.clean();
FlxG.resetState();
case "Exit to menu":
if (!FlxG.save.data.GC) {
cpp.vm.Gc.enable(true);
trace("Exiting the song. We can reenable the garbage collector");
}
PlayState.startTime = 0;
if (PlayState.instance.useVideo)
{
@ -246,8 +252,8 @@ class PauseSubState extends MusicBeatSubstate
PlayState.luaModchart = null;
}
#end
if (FlxG.save.data.fpsCap > 290)
(cast (Lib.current.getChildAt(0), Main)).setFPSCap(290);
if (FlxG.save.data.fpsCap > 1000)
(cast (Lib.current.getChildAt(0), Main)).setFPSCap(1000);
PlayState.instance.clean();

View File

@ -1,6 +1,7 @@
package;
import cpp.vm.Gc;
import Song.Event;
import openfl.media.Sound;
#if sys
@ -1692,6 +1693,10 @@ class PlayState extends MusicBeatState
function startSong():Void
{
if (!FlxG.save.data.GC) {
Gc.enable(false);
trace("Song started. Garbage collection has been disabled");
}
startingSong = false;
songStarted = true;
previousFrameTime = FlxG.game.ticks;
@ -3313,6 +3318,10 @@ class PlayState extends MusicBeatState
function endSong():Void
{
if (!FlxG.save.data.GC) {
Gc.enable(true);
trace("Song ended. We can reenable the garbage collector");
}
endingSong = true;
FlxG.stage.removeEventListener(KeyboardEvent.KEY_DOWN, handleInput);
FlxG.stage.removeEventListener(KeyboardEvent.KEY_UP, releaseInput);
@ -3334,8 +3343,8 @@ class PlayState extends MusicBeatState
PlayStateChangeables.useDownscroll = false;
}
if (FlxG.save.data.fpsCap > 290)
(cast(Lib.current.getChildAt(0), Main)).setFPSCap(290);
if (FlxG.save.data.fpsCap > 1000)
(cast(Lib.current.getChildAt(0), Main)).setFPSCap(1000);
#if cpp
if (luaModchart != null)

View File

@ -40,7 +40,7 @@ class TimingStruct
if (msTime >= i.startTime * 1000 && msTime < (i.startTime + i.length) * 1000)
return i;
}
trace('Apparently ' + msTime + ' is out of any segs');
/*trace('Apparently ' + msTime + ' is out of any segs');*/
return null;
}