From 7b8127be763994e3bab22e3b191e8f5b50b1b8ef Mon Sep 17 00:00:00 2001 From: Olivier Leobal Date: Mon, 17 Jun 2019 17:09:28 +0200 Subject: [PATCH] Add FPS counter (--fps-counter) --- main.d | 78 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 11 deletions(-) diff --git a/main.d b/main.d index d34a0cc..aae6657 100644 --- a/main.d +++ b/main.d @@ -5,6 +5,7 @@ import std.process; import std.math; import std.random; Random rnd; +import std.container : DList; import std.algorithm.comparison; import std.concurrency; import core.thread; @@ -36,6 +37,30 @@ alias Dimensions=Tuple!(int, "cols", int, "lines"); ubyte[16] temperatures = [ 0, 52, 88, 124, 196, 202, 215, 220, 222, 226, 227, 228, 229, 230, 195, 231 ]; +/** + * take a queue of events (from earliest to latest) + * and returns the average number of such events per second + */ +long avgEventsPerSecond(DList!MonoTime times) +{ + auto nt = times.dup(); + MonoTime start = nt.front(); + nt.removeFront(); + + Duration total = dur!("seconds")(0); + long elements = 0; + + while (!nt.empty()) + { + total = total + (nt.front() - start) ; + nt.removeFront(); + elements++; + } + + return dur!("seconds")(1)/(total/elements); +} + + Dimensions getWindowSize() { // getting size should take a bit less than 10ms on most machines @@ -121,11 +146,11 @@ void updateCanvas(ref Canvas canvas, Dimensions area, bool keepSourceGoing, int } -void renderCanvas(Canvas canvas, Dimensions area) +void renderCanvas(Canvas canvas, Dimensions area, string topLeft="") { int offset = max(0, to!int(canvas.length)-to!int(area.lines)); - bugMsg("renderCanvas ",canvas.length, " ", area.lines, " ", offset); + bugMsg("renderCanvas ",canvas.length, " ", area.lines, " ", offset, " | ",topLeft); string result = ""; @@ -134,6 +159,14 @@ void renderCanvas(Canvas canvas, Dimensions area) result~=Keys.cursorAt(to!short(l),to!short(0)); for (int c=0 ; c : Flip the source on and off every seconds Switches: - --debug : Output debug info (while running) to stderr - --help, -h : Display this help and exit`); + --fps-counter : Displays a frames per second counter in the top left + --debug : Output debug info (while running) to stderr + --help, -h : Display this help and exit`); return 0; } if (args[i] == "--speed" || args[i] == "-s") @@ -184,6 +219,8 @@ Switches: if (args[i] == "--debug") bugMode = true; + if (args[i] == "--fps-counter") + displayFPScounter = true; } @@ -263,22 +300,41 @@ Switches: } }).start(); - // http://fabiensanglard.net/timer_and_framerate/ - - - - auto nextUpdate = MonoTime.currTime; + /// keeps the window size consistent within a loop Dimensions area; + + auto times = DList!MonoTime( + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime, + MonoTime.currTime, MonoTime.currTime); + auto nextUpdate = MonoTime.currTime; while (!gotSigint) { - area = dims; + area.lines = dims.lines; + area.cols = dims.cols; if (MonoTime.currTime >= nextUpdate) { nextUpdate+=dur!("msecs")(timeslice); updateCanvas(canvas, area, keepSourceGoing, decayMod, autoDecayMod); } - renderCanvas(canvas, area); + + if (displayFPScounter) + { + times.insertBack(MonoTime.currTime); + times.removeFront(); + renderCanvas(canvas, area, to!string(avgEventsPerSecond(times))); + } + else + renderCanvas(canvas, area); + } write(Keys.terminator,Keys.cursorAt(0,0), Keys.cursorNormal, Keys.alternateScreenOff);