-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.cpp
195 lines (162 loc) · 5.05 KB
/
App.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdinc.h>
#include <deferrable.h>
#include "App.h"
#include "Shader.h"
#include "Geom.h"
map<string, vector<string>> shaders = {
{"triangle", {"shaders/triangle.frag", "shaders/triangle.vert"}},
{"triangle_combined", {"shaders/triangle_combined.glsl"}},
};
chrono::time_point<chrono::high_resolution_clock> startTime;
chrono::time_point<chrono::high_resolution_clock> lastFixedUpdate;
chrono::time_point<chrono::high_resolution_clock> lastDrawTick;
chrono::milliseconds fixedUpdateDelta =
chrono::milliseconds(FIXED_UPDATE_DELTA);
chrono::milliseconds maxFps = chrono::milliseconds(1000 / MAX_FPS);
bool reloadDown = false;
App::App() {}
App::~App() {
D("APP DECONST")
entities.clear();
// Shader::shaders.clear();
glfwSetWindowShouldClose(window->window, GL_TRUE);
}
void App::init() {
D("app - init")
window = make_unique<Window>(EWindowMode_Windowed, 800, 600);
window->create();
// setup buffers and GL things
glGenBuffers(1, &vertexBuffer);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.f, 0.f, 0.f, 1.f);
// setup shaders, entities
// auto es = make_shared<Shader>();
// es->name = "@internal/error";
// es->loadFileCombined("shader/err.glsl");
// Shader::errorShader = es;
// deferrable::defer p([] { glClearColor(1.f, 0.f, 0.5f, 1.f); }, 5s);
reloadShaders();
createEntities();
}
void App::reloadShaders() {
D("app - reload shaders")
for (auto const &s : shaders) {
shared_ptr<Shader> sh;
string n = s.first;
if (Shader::shaders.find(n) != Shader::shaders.end()) {
D("reload -- found existing")
sh = Shader::shaders[n];
sh->reset();
} else {
D("reload -- creating new")
sh = make_shared<Shader>();
sh->name = n;
}
if (s.second.size() == 1) {
sh->loadFileCombined(s.second[0]);
} else {
sh->loadFiles(s.second);
}
sh->link();
Shader::shaders.insert_or_assign(n, sh);
}
}
void App::createEntities() {
D("app - create entities")
for (unsigned int i = 0; i < 3; i++) {
float o = 0.1 * i;
auto p = make_shared<Geom>(
vector<float>{
-0.5f + o, 0.5f + o, 1.0f + o, // Top-left
0.5f + o, 0.5f + o, 0.0f + o, // Top-right
0.5f + o, -0.5f + o, 0.0f + o, // Bottom-right
-0.5f + o, -0.5f + o, 1.0f + o, // Bottom-left
},
vector<unsigned int>{0, 1, 2, 2, 3, 0},
vector<float>{
0.0f, 0.0f, // TL
1.0f, 0.0f, // TR
0.0f, 1.0f, // BR
0.0f, 0.0f // BL
});
p->shaderName = "triangle_combined";
p->setMaterialCallback([o](auto shader) { shader->set("u_Depth", o * 3); });
entities.push_back(p);
}
}
void App::mainLoop() {
D("main loop fired")
chrono::time_point<chrono::high_resolution_clock> currentClock =
chrono::high_resolution_clock::now();
startTime = currentClock;
lastDrawTick = currentClock;
lastFixedUpdate = currentClock;
cout << "TIMINGS: max fps = " << MAX_FPS << " (" << 1000 / MAX_FPS
<< "ms); fixed delta time = " << FIXED_UPDATE_DELTA << "ms" << endl;
auto fixedUpdateDiff = currentClock - lastFixedUpdate;
auto drawUpdateDiff = currentClock - lastDrawTick;
// D("main loop prewarmed")
while (!glfwWindowShouldClose(window->window)) {
glClear(GL_COLOR_BUFFER_BIT);
// D("loop")
currentClock = chrono::high_resolution_clock::now(); //
// fixed updates
fixedUpdateDiff = currentClock - lastFixedUpdate;
if (fixedUpdateDiff >= fixedUpdateDelta) {
// D("FIXED UPDATE")
lastFixedUpdate = currentClock;
setWindowFixedUpdate();
fixedUpdate();
}
// regular draws
drawUpdateDiff = currentClock - lastDrawTick;
if (drawUpdateDiff >= maxFps) {
// D("DRAWING")
lastDrawTick = currentClock;
setWindowFPS();
earlyUpdate();
update();
lateUpdate();
// shims
fix_render_on_mac(window->window);
} else {
std::chrono::duration<float, std::milli> a = maxFps - drawUpdateDiff;
std::chrono::duration<float, std::milli> b =
fixedUpdateDelta - fixedUpdateDiff;
float waitTime = min(a.count(), b.count());
// if there wasn't a draw, let's tap the thread for a ms to prevent
this_thread::sleep_for(
std::chrono::duration<float, std::milli>(waitTime));
}
updateWindowTitle(window->window);
}
}
void App::earlyUpdate() {
deferrable::tick();
Shader::updateAllUniformTimes(startTime, lastDrawTick);
}
void App::update() {
// D("app - update")
for (auto const &poly : entities) {
poly->draw();
}
}
void App::lateUpdate() {
// D("app - lateUpdate")
glfwSwapBuffers(window->window);
glfwPollEvents();
if (glfwGetKey(window->window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window->window, GL_TRUE);
if (glfwGetKey(window->window, GLFW_KEY_PAGE_UP) == GLFW_PRESS) {
if (reloadDown == false) {
reloadShaders();
reloadDown = true;
cout << "Reloaded shaders." << endl;
}
} else {
reloadDown = false;
}
}
void App::fixedUpdate() {
// D("app - fixed update")
}