-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
54 lines (43 loc) · 1.31 KB
/
main.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <thread>
#include <chrono>
#include "src/consts.h"
#include "src/contra/game.h"
#include "src/kernel/avancezlib.h"
float game_speed = 1.f;
int main (int argc, char *argv[]) {
AvancezLib engine{};
engine.init(WINDOW_WIDTH, WINDOW_HEIGHT);
Game game;
game.Create(&engine);
game.Init();
float lastTime = engine.getElapsedTime();
#ifndef NDEBUG
char fps[100];
#endif
float smoothedDt = 0.004;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
while (true) {
float newTime = engine.getElapsedTime();
float dt = newTime - lastTime;
// Cap to 240 FPS, because my laptop goes very noisy otherwise and it embarrasses me
int dtMillis = (int) (dt * 1000.f);
if (dtMillis < 4) {
std::this_thread::sleep_for(std::chrono::milliseconds(4 - dtMillis));
newTime = engine.getElapsedTime();
dt = newTime - lastTime;
smoothedDt = smoothedDt * 0.99f + dt * 0.01f;
}
dt = dt * game_speed;
lastTime = newTime;
engine.processInput();
game.Update(dt);
#ifndef NDEBUG
sprintf(fps, "FPS: %d", (int) round(1 / smoothedDt));
engine.drawText(0, 0, fps);
#endif
game.Draw();
}
#pragma clang diagnostic pop
}