summaryrefslogtreecommitdiff
path: root/themes/src/main_loop.cpp
blob: e5397ca35cf9f49aab86d2e7159248b260310899 (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 "main_loop.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;
}