-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
285 lines (212 loc) · 9.17 KB
/
app.cpp
File metadata and controls
285 lines (212 loc) · 9.17 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "app.h"
#include "imgui.h"
#include "serializer.h"
#include <GLFW/glfw3.h>
#include <cstring>
App::App(){
Serializer::loadData();
// GLFW window setup
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
glfwWindowHint(GLFW_MOUSE_PASSTHROUGH, GLFW_TRUE);
GLFWmonitor* primary_monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(primary_monitor);
Global::aspect_ratio = (float)mode->height / (float)mode->width;
this->window = glfwCreateWindow(mode->width, mode->height, "Stevica :D", NULL, NULL);
try{
if(this->window == NULL){
glfwTerminate();
throw std::runtime_error("Failed to create GLFW window");
}
} catch(const std::exception& e){
std::cerr << e.what() << std::endl;
}
glfwMakeContextCurrent(window);
try{
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
glfwTerminate();
throw std::runtime_error("Failed to initialize GLAD");
}
} catch(const std::exception& e){
std::cerr << e.what() << std::endl;
}
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(0xFFFF);
glfwSetWindowAttrib(window, GLFW_FLOATING, GLFW_TRUE);
this->update_limit_framerate();
glLineWidth(3.0f);
this->renderer = new FishRenderer();
this->handler = new FishHandler();
this->handler->createFish();
this->last_frame = 0.0f;
this->o_first_touch = false;
this->inputing_text = false;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(this->window, true);
const char* glsl_version = "#version 450";
ImGui_ImplOpenGL3_Init(glsl_version);
}
App::~App(){
if(Serializer::store_on_exit) Serializer::storeData();
delete this->handler;
delete this->renderer;
glfwTerminate();
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
void App::Run(){
float current_frame = glfwGetTime();
float delta_time = current_frame - this->last_frame;
this->last_frame = current_frame;
this->process_input();
this->handler->resetBoxSizes();
this->handler->boxTheFish();
this->handler->handleFishMovement(delta_time);
this->renderer->renderFish(this->handler->allFish, this->handler->number_of_fish);
this->handle_imgui();
glfwSwapBuffers(window);
glfwPollEvents();
}
void App::handle_imgui(){
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if(Serializer::show_gui)
{
ImGui::Begin("Options menu", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
int num_of_fish = static_cast<int>(Serializer::number_of_fish);
if(ImGui::InputInt("Number of fish", &num_of_fish)){
Serializer::number_of_fish = std::min(std::max(1, num_of_fish), MAX_NUM_OF_FISH);
this->handler->update_num_of_fish();
}
if(ImGui::ColorEdit3("Fish eye color", &Serializer::fish_eye_color[0])){
this->renderer->update_fish_eye_color();
}
if(ImGui::ColorEdit3("Fish fin color", &Serializer::fish_fin_color[0])){
this->renderer->update_fish_fin_color();
}
if(ImGui::ColorEdit3("Fish body color", &Serializer::fish_body_color[0])){
this->renderer->update_fish_body_color();
}
if(ImGui::Checkbox("Use solid color", &Serializer::use_solid_color)){
this->renderer->update_use_solid_color();
}
if(ImGui::Checkbox("Use pixelation shader", &Serializer::use_pixelation)){
this->renderer->update_use_pixelation();
}
if(Serializer::use_pixelation){
if(ImGui::InputFloat("Pixelation amout", &Serializer::pixelation_amount)){
this->renderer->update_pixelation_amount();
}
}
char buffer[256];
std::strcpy(buffer, Serializer::fish_tex_path.c_str());
buffer[std::max((int)Serializer::fish_tex_path.size(), 256)] = '\0';
if(ImGui::InputText("Fish texture path", buffer, 256)){
Serializer::fish_tex_path.assign(buffer);
this->renderer->update_texture_path();
}
this->inputing_text = ImGui::IsItemActive();
if(ImGui::InputFloat("Cohesion intensity", &Serializer::cohesion_intensity)){
this->handler->update_cohesion_intensity();
}
if(ImGui::InputFloat("Alignment intensity", &Serializer::alignment_intensity)){
this->handler->update_alignment_intensity();
}
if(ImGui::InputFloat("Separation intensity", &Serializer::separation_intensity)){
this->handler->update_separation_intensity();
}
if(ImGui::InputFloat("Edge Evasion intensity", &Serializer::edge_evasion_intensity)){
this->handler->update_edge_evasion_intensity();
}
if(ImGui::Checkbox("Limit framerate", &Serializer::is_framerate_limited)){
this->update_limit_framerate();
}
ImGui::Checkbox("Store changes on exit", &Serializer::store_on_exit);
if(ImGui::Button("Store changes")){
Serializer::storeData();
}
ImGui::SameLine();
ImGui::SetCursorPosX(250.0f);
if(ImGui::Button("Reset to default")){
Serializer::fish_eye_color = DEFAULT_EYE_COLOR;
Serializer::fish_fin_color = DEFAULT_FIN_COLOR;
Serializer::fish_body_color = DEFAULT_BODY_COLOR;
Serializer::fish_tex_path = (std::filesystem::path)TEXTURES_PATH / "koi.jpg";
Serializer::number_of_fish = DEFAULT_NUM_OF_FISH;
Serializer::cohesion_intensity = DEFAULT_COHESION_INTENSITY;
Serializer::alignment_intensity = DEFAULT_ALIGNMENT_INTENSITY;
Serializer::separation_intensity = DEFAULT_SEPARATION_INTENSITY;
Serializer::edge_evasion_intensity = DEFAULT_EDGE_EVASION_INTENSITY;
Serializer::pixelation_amount = DEFAULT_PIXELATION_AMOUNT;
Serializer::is_framerate_limited = DEFAULT_IS_FRAMERATE_LIMITED;
Serializer::use_solid_color = DEFAULT_USE_SOLID_COLOR;
Serializer::use_pixelation = DEFAULT_USE_PIXELATION;
Serializer::store_on_exit = DEFAULT_STORE_ON_EXIT;
// not updating show gui because if the button is clicked, the gui is already on
this->handler->update_num_of_fish();
this->renderer->update_fish_eye_color();
this->renderer->update_fish_fin_color();
this->renderer->update_fish_body_color();
this->renderer->update_use_solid_color();
this->renderer->update_texture_path();
this->handler->update_cohesion_intensity();
this->handler->update_alignment_intensity();
this->handler->update_separation_intensity();
this->handler->update_edge_evasion_intensity();
this->renderer->update_pixelation_amount();
this->renderer->update_use_pixelation();
this->update_limit_framerate();
}
ImGuiIO& io = ImGui::GetIO();
ImGui::Text("Toggle the options menu on and off with 'O' while the app is in focus");
ImGui::Text("Exit the application with Esc while the app is in focus");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
}
// Check if mouse is interacting with ImGui windows
bool mouseOverImGui = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow) || ImGui::IsPopupOpen(nullptr, ImGuiPopupFlags_AnyPopup);
// Enable/Disable mouse passthrough accordingly
glfwSetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH, mouseOverImGui ? GLFW_FALSE : GLFW_TRUE);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
bool App::isRunning(){
return !glfwWindowShouldClose(this->window);
}
void App::process_input(){
if(glfwGetKey(this->window, GLFW_KEY_ESCAPE) == GLFW_PRESS){
glfwSetWindowShouldClose(window, true);
}
if(!this->inputing_text){
if(glfwGetKey(this->window, GLFW_KEY_O) == GLFW_PRESS){
if(!o_first_touch){
o_first_touch = true;
Serializer::show_gui = !Serializer::show_gui;
}
}
if(glfwGetKey(this->window, GLFW_KEY_O) == GLFW_RELEASE){
o_first_touch = false;
}
}
}
void App::update_limit_framerate(){
glfwSwapInterval(Serializer::is_framerate_limited ? 1 : 0);
}