-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
75 lines (57 loc) · 1.83 KB
/
engine.cpp
File metadata and controls
75 lines (57 loc) · 1.83 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <ChaosEngine/engine.hpp>
#include "ChaosInfo.hpp"
#include <thread>
Engine::Engine(int argc, char *argv[], string gameName, int gameWidth, int gameHeight) {
log("Starting ChaosEngine version: " + string(CHAOS_VERSION));
game = gameName;
width = gameWidth;
height = gameHeight;
string args;
for (int i = 1; i < argc; i++) {
args += " ";
args += argv[i];
}
log("Command line:" + args);
log("Game: " + game);
log("Resolution: " + to_string(width) + "x" + to_string(height));
log("Connecting to X11.");
cnn = xcb_connect(NULL, NULL);
// Get screen data
const xcb_setup_t * setup = xcb_get_setup(cnn);
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
scrn = iter.data;
log("Creating window.");
win = xcb_generate_id(cnn);
xcb_create_window(cnn, XCB_COPY_FROM_PARENT, win, scrn->root, 0, 0, width, height, 10, XCB_WINDOW_CLASS_INPUT_OUTPUT, scrn->root_visual, 0, NULL);
log("Showing window.");
xcb_map_window(cnn, win);
xcb_flush(cnn);
lastFrameTime = steady_clock::now();
}
Engine::~Engine() {
log("Disconnecting from X11");
xcb_disconnect(cnn);
log("Stopping ChaosEngine with exit code: " + to_string(code));
}
void Engine::close(int exitCode) {
code = exitCode;
running = false;
}
void Engine::limitFPS() {
//double idealFrameDuration = 1 / fpsLimit;
duration<double> frameDuration = steady_clock::now() - lastFrameTime;
deltaTime = frameDuration.count();
double timeToSleep = (1 / fpsLimit) - deltaTime;
if (timeToSleep > 0) {
auto sleepDuration = duration<double>(timeToSleep);
std::this_thread::sleep_for(duration_cast<nanoseconds>(sleepDuration));
auto afterSleep = steady_clock::now();
duration<double> realFrameDuration = afterSleep - lastFrameTime;
deltaTime = realFrameDuration.count();
}
lastFrameTime = steady_clock::now();
}
bool Engine::run() {
limitFPS();
return running;
}