-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
294 lines (232 loc) · 7.83 KB
/
main.cpp
File metadata and controls
294 lines (232 loc) · 7.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
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
286
287
288
289
290
291
292
293
294
#include <cstdint>
#include <memory>
#ifndef _WIN32
#include <unistd.h> // getopt
#else
// A very simple getopt replacement for Windows
static int optarg_idx = 1;
static char* optarg = nullptr;
static int getopt(int argc, char *const argv[], const char *optstring) {
if (optarg_idx >= argc || argv[optarg_idx][0] != '-' || argv[optarg_idx][1] == '\0') {
return -1;
}
char opt = argv[optarg_idx][1];
const char* p = strchr(optstring, opt);
if (p == nullptr) {
optarg_idx++;
return '?';
}
if (p[1] == ':') {
if (argv[optarg_idx][2] != '\0') {
optarg = &argv[optarg_idx][2];
optarg_idx++;
} else if (optarg_idx + 1 < argc) {
optarg = argv[optarg_idx + 1];
optarg_idx += 2;
} else {
optarg_idx++;
return '?';
}
} else {
optarg = nullptr;
optarg_idx++;
}
return opt;
}
#endif
#include "imgui.h"
#include "imgui_elements.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <GLES2/gl2.h>
#endif
#include <glad/glad.h>
#include <GLFW/glfw3.h> // Will drag system OpenGL headers
#include "artfactory.h"
#include "gl_debug.h"
#include "gl_state.h"
std::unique_ptr<Art> art;
static GLFWwindow* window;
static int sw = 1024, sh = 1024;
bool get_window_size() {
int _w, _h;
glfwGetFramebufferSize(window, &_w, &_h);
bool resized = (_w != sw || _h != sh);
sw = _w; sh = _h;
return resized;
}
static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
char info[1024*4] = "Initializing...";
int main(int argc, char *argv[])
{
unsigned frate = 0;
int opt;
int vsync = 1;
int artarg = -1;
bool shuffle_mode = false;
bool save_every_frame = false, save_frame = false;
bool hide_gui = false;
const char *title = "Dear ImGui screensaver";
while ((opt = getopt(argc, argv, "sa:St:wg")) != -1) {
switch (opt) {
case 's':
vsync = 0;
break;
case 'a':
artarg = atoi(optarg);
break;
case 'S':
shuffle_mode = true;
break;
case 't':
title = optarg;
break;
case 'w':
save_every_frame = true;
break;
case 'g':
hide_gui = true;
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-s] [-a art_number] [-S] [-w] [-g]\n\n"
"-s disable vsync\n"
"-a num select screensaver\n"
"-S enable perioding shuffling of parameters (screensaver mode)\n"
"-t title set window title\n"
"-w save every frame to PNG\n"
"-g hide GUI (screensaver mode)",
argv[0]);
exit(EXIT_FAILURE);
}
}
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit())
return 1;
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
const char* glsl_version = "#version 100";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
#elif defined(__APPLE__)
// GL 3.2 + GLSL 150
const char* glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
const char* glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
// Request debug context for OpenGL 4.3+ debug output
#ifndef NDEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
#endif
window = glfwCreateWindow(sw, sh, title, NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(vsync);
// Initialize GLAD
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
fprintf(stderr, "Failed to initialize GLAD\n");
return 1;
}
// Initialize OpenGL debug callbacks (only effective in debug builds with OpenGL 4.3+)
init_gl_debug();
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
//init_shaders();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable( GL_BLEND );
ImVec4 clear_color = ImVec4(0, 0, 0, 1.00f);
ArtFactory af;
if (artarg != -1)
af.set_art(artarg);
art = af.get_art();
get_window_size();
art->resized(sw, sh);
//make_pbos();
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
if (ImGui::IsKeyPressed(ImGuiKey_F1))
hide_gui = !hide_gui;
if (!hide_gui) {
ImGui::Begin(art->name());
if (af.render_gui())
{
art = af.get_art();
art->resized(sw, sh);
}
if (ImGui::CollapsingHeader("Clear Configuration"))
{
ScrollableSliderUInt("force clear every N frames", &art->clear_every, 0, 1024, "%d", 1);
ScrollableSliderUInt("Max 1k frames before reinit", &art->max_kframes, 0, 1024, "%d", 1);
ImGui::ColorEdit4("Clear color", (float*)&clear_color);
}
ImGui::Checkbox("##save_every_frame", &save_every_frame);
ImGui::SameLine();
save_frame = ImGui::Button("Save Frame");
art->gui();
if (art->frame_number % 120 == 0)
{
char *ti = info;
ti += cpu_load_text_now(info);
ti += sprintf(ti, "\n%.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate,
ImGui::GetIO().Framerate);
if (shuffle_mode)
art->check_shuffle(glfwGetTime());
}
ImGui::Text(info);
ImGui::End();
}
if (get_window_size()) {
art->resized(sw, sh);
glViewport(0, 0, sw, sh);
}
{
GL_ENABLE_FOR_SCOPE(GL_BLEND);
art->draw();
}
if (ImGui::IsKeyPressed(ImGuiKey_F12) || save_frame || save_every_frame) {
// TODO: works only with EaselVertex and width/height is always like fullscreen
art->save_frame();
}
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
if (art->clear_every != 0 && art->frame_number % art->clear_every == 0)
art->clear();
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}