diff options
| author | Matthew Kosarek <mattkae@protonmail.com> | 2021-03-27 11:49:56 -0400 |
|---|---|---|
| committer | Matthew Kosarek <mattkae@protonmail.com> | 2021-03-27 11:49:56 -0400 |
| commit | 58488f8eabcc61089e0ae4297f38f10cc28d78c7 (patch) | |
| tree | 81ac504e61c57c7228024ad40710f782a3d8c884 /frontend/shared_cpp/MainLoop.cpp | |
| parent | 26ea81c5893778a7fa4ecbb2e8f561c9edabd0b3 (diff) | |
Working reusable simulation loop
Diffstat (limited to 'frontend/shared_cpp/MainLoop.cpp')
| -rw-r--r-- | frontend/shared_cpp/MainLoop.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
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 <cstdio> +#include <cstdlib> + +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<float>(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 |
