From 58488f8eabcc61089e0ae4297f38f10cc28d78c7 Mon Sep 17 00:00:00 2001 From: Matthew Kosarek Date: Sat, 27 Mar 2021 11:49:56 -0400 Subject: Working reusable simulation loop --- frontend/shared_cpp/MainLoop.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 frontend/shared_cpp/MainLoop.cpp (limited to 'frontend/shared_cpp/MainLoop.cpp') diff --git a/frontend/shared_cpp/MainLoop.cpp b/frontend/shared_cpp/MainLoop.cpp new file mode 100644 index 0000000..82a24b5 --- /dev/null +++ b/frontend/shared_cpp/MainLoop.cpp @@ -0,0 +1,30 @@ +#include "MainLoop.h" +#include +#include + +EM_BOOL loop(double time, void* loop) { + MainLoop* mainLoop = (MainLoop*) loop; + if (!mainLoop->isRunning) { + return false; + } + + if (mainLoop->lastTime == 0) { + mainLoop->lastTime = time; + return true; + } + + long deltaTime = time - mainLoop->lastTime; + mainLoop->lastTime = time; + mainLoop->elapsedTime += deltaTime; + mainLoop->numFrames++; + float deltaTimeSeconds = static_cast(deltaTime) / 1000.f; + + if (mainLoop->elapsedTime >= 1000.0) { + printf("FPS: %d\n", mainLoop->numFrames); + + mainLoop->elapsedTime = 0.0; + mainLoop->numFrames = 0; + } + + return mainLoop->updateFunc(deltaTimeSeconds, NULL); +} \ No newline at end of file -- cgit v1.2.1