« Go back

Duration of your Application

Sometimes it could be helpful to measure how much time has elapsed or how long your application already runs. This can be done with the already known StopWatch struct. More specifically the getTime or getElapsedTime method.

Both return an instance to the Time struct, which holds the milliseconds and their seconds / minutes / hours representations.

Show Raw
import std.stdio;

import Dgame.Window.Window;
import Dgame.Window.Event;
import Dgame.System.StopWatch;

void main() {
	Window wnd = Window(640, 480, "Dgame Test");

	StopWatch clock;

	bool running = true;

	Event event;
	while (running) {
		wnd.clear();
		
		while (wnd.poll(&event)) {
			switch (event.type) {
				case Event.Type.Quit:
					writeln("Quit Event");
					running = false;
				break;
					
				case Event.Type.KeyDown:
					Time time = clock.getTime();
					writefln("Game Loop runs now for %d ms - %f secs - %f min",
					         time.msecs, time.seconds, time.minutes);
				break;
					
				default: break;
			}
		}
		
		wnd.display();
	}
}

You should see some output like: Game Loop runs now for 4314 ms - 4 secs - 0 min

« Go back