summaryrefslogtreecommitdiff
path: root/themes/src/main_loop.cpp
blob: 743892ef0c6c906624a59250338f3c3d8c7677eb (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
32
33
34
35
36
37
#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;
  }

  // Ignore any update with a greater than 0.1 change. We were
  // probably tabbed away, so this is uninteresting to us.
  if (deltaTimeSeconds > 0.1) {
    return true;
  }

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