summaryrefslogtreecommitdiff
path: root/shared_cpp/MainLoop.cpp
blob: 09aa64306a9ef75648e085a8b432144fdf3bb216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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;
    }

    mainLoop->updateFunc(deltaTimeSeconds, NULL);
    return true;
}