-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
136 lines (111 loc) · 3.07 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#define CORE_IMPL
#include "core/core.h"
#undef CORE_IMPL
#include "game.h"
#ifndef __EMSCRIPTEN__
#include <pthread.h>
void *watchLoop(Watcher *w) {
while (true) {
if(!wait(w)) {
perror("poll");
}
}
}
typedef void *(*threadFunc)(void*);
#endif
#define WINDOW_WIDTH 600
#define WINDOW_HEIGHT 800
#define MAX_FRAME_RATE 120
#define MIN_FRAME_TIME (1000.0/MAX_FRAME_RATE)
Renderer r;
const char *title = "testris";
App app = {
.title = title,
.width = WINDOW_WIDTH,
.height = WINDOW_HEIGHT,
};
void resizeWindow(int w, int h) {
app.width = w;
app.height = h;
r.screenWidth = (float)w;
r.screenHeight = (float)h;
renderToScreen(&r);
}
GameState st = {};
bool step() {
clear(&r, black);
InputData in = step(&app);
bool result = updateGameState(&st, in);
clear(&r, black);
renderGameState(&r, &st);
swapWindow(&app.window);
if (in.dt < MIN_FRAME_TIME) SDL_Delay((uint32_t)(MIN_FRAME_TIME - in.dt));
return result;
}
void step_forever() {
step();
}
int main(int argc, char *argv[]) {
#ifdef __EMSCRIPTEN__
EM_ASM(
// Make a directory other than '/'
FS.mkdir('/offline');
// Then mount with IDBFS type
FS.mount(IDBFS, {}, '/offline');
// Then sync
FS.syncfs(true, function (err) {
if (err !== null)
console.error(err);
});
);
#endif
if (!initApp(&app, INIT_DEFAULT)) {
log("init failed, exiting");
return 1;
}
app.setWindowSize = resizeWindow;
// setup default 2d renderer
initRenderer(&r, app.width, app.height);
if (!setupDefaultShaders(&r)) {
// TODO: really we should be jumping to the cleanup routine here rather
// than returning, but for now this is fine.
return -1;
}
renderToScreen(&r);
if (!startGame(&st, &r, &app)) {
log("start game failed, exiting");
// TODO: really we should be jumping to the cleanup routine here rather
// than returning, but for now this is fine.
return 1;
}
#ifdef __EMSCRIPTEN__
#define REQUEST_ANIMATION_FRAME 0
emscripten_set_main_loop(step_forever, REQUEST_ANIMATION_FRAME, 1);
#else
// setup watched resources and watcher loop in thread
Watcher watcher = {};
int shaderWatch = watchPath(&watcher, "./assets/shaders");
pthread_t watchThread;
if (pthread_create(&watchThread, NULL, (threadFunc)watchLoop, (void*)&watcher)) {
log("pthread creation failed");
// TODO: really we should be jumping to the cleanup routine here rather
// than returning, but for now this is fine.
return 1;
}
while (step()) {
if (updated(&watcher, shaderWatch)) {
debug("SHADERS UPDATED");
if (!setupDefaultShaders(&r)) {
log("failed to update shaders");
}
if (!compileExtraShaders(&r)) {
log("failed to update shaders");
}
}
}
cleanupWatcher(&watcher);
cleanupRenderer(&r);
cleanupApp(&app);
#endif
return -1;
}