From 8479b0b67114a3946e36d50ec522ae6ab1c36fdd Mon Sep 17 00:00:00 2001 From: Eric Myllyoja Date: Thu, 27 Jan 2022 22:20:38 -0500 Subject: [PATCH] A memory usage counter ingame to complement the FPS counter --- source/Main.hx | 6 +++++- source/MemoryCounter.hx | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 source/MemoryCounter.hx diff --git a/source/Main.hx b/source/Main.hx index 2d0120387..6e9117a0d 100644 --- a/source/Main.hx +++ b/source/Main.hx @@ -71,6 +71,7 @@ class Main extends Sprite private var overlay:Sprite; public static var fpsCounter:FPS; + public static var memoryCounter:MemoryCounter; private function setupGame():Void { @@ -94,10 +95,13 @@ class Main extends Sprite addChild(new FlxGame(gameWidth, gameHeight, initialState, zoom, framerate, framerate, skipSplash, startFullscreen)); - #if !mobile + #if debug fpsCounter = new FPS(10, 3, 0xFFFFFF); addChild(fpsCounter); + memoryCounter = new MemoryCounter(10, 13, 0xFFFFFF); + addChild(memoryCounter); #end + /* video = new Video(); addChild(video); diff --git a/source/MemoryCounter.hx b/source/MemoryCounter.hx new file mode 100644 index 000000000..2772ada77 --- /dev/null +++ b/source/MemoryCounter.hx @@ -0,0 +1,44 @@ +import openfl.text.TextFormat; +import openfl.system.System; +import openfl.text.TextField; + +class MemoryCounter extends TextField +{ + private var memPeak:Float = 0; + + static final BYTES_PER_MEG:Float = 1024 * 1024; + static final ROUND_TO:Float = 1 / 100; + + public function new(x:Float = 10, y:Float = 10, color:Int = 0x000000) + { + super(); + + this.x = x; + this.y = y; + this.width = 500; + this.selectable = false; + this.mouseEnabled = false; + defaultTextFormat = new TextFormat("_sans", 12, color); + text = "RAM: "; + + #if flash + addEventListener(Event.ENTER_FRAME, function(e) + { + var time = Lib.getTimer(); + __enterFrame(time - currentTime); + }); + #end + } + + // Event Handlers + @:noCompletion + private #if !flash override #end function __enterFrame(deltaTime:Float):Void + { + var mem:Float = Math.round(System.totalMemory / BYTES_PER_MEG / ROUND_TO) * ROUND_TO; + + if (mem > memPeak) + memPeak = mem; + + text = 'RAM: ${mem}mb / ${memPeak}mb'; + } +}