-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
323 lines (252 loc) · 8.56 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#define STB_IMAGE_IMPLEMENTATION
#include "Common.h"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include "Demos/Demo.hpp"
#include "Demos/ImageLab/ImageLab.hpp"
#include "Demos/AudioLab/AudioLab.hpp"
struct DemoManager {
Demo *demo;
DemoManager(){
demo=nullptr;
}
void ClearDemo() {
if(demo!=nullptr) {
demo->Unload();
delete demo;
}
}
void LoadDemo(int demoInx) {
switch (demoInx)
{
case 0:
demo = new ImageLab;
demo->windowWidth = windowWidth;
demo->windowHeight = windowHeight;
demo->Load();
break;
case 1:
demo = new AudioLab;
demo->windowWidth = windowWidth;
demo->windowHeight = windowHeight;
demo->Load();
break;
default:
break;
}
}
void MouseMove(float xpos, float ypos ) {
if(demo!=nullptr) {
demo->MouseMove(xpos, ypos);
}
}
void MouseAction(int button, int action, int mods) {
if(demo!=nullptr) {
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS){
demo->RightClickDown();
} else if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_RELEASE){
demo->RightClickUp();
}
else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
demo->LeftClickDown();
}
else if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
demo->LeftClickUp();
}
}
}
void Scroll(float offset) {
if(demo!=nullptr) {
demo->Scroll(offset);
}
}
void Key(int keyCode, int action)
{
if(demo !=nullptr)
{
demo->Key(keyCode, action);
}
}
void Render() {
if(demo!=nullptr) {
demo->Render();
}
}
void RenderGUI()
{
if(demo!=nullptr) {
demo->RenderGUI();
}
}
int windowWidth, windowHeight;
};
static void ErrorCallback(int error, const char* description)
{
fprintf(stderr, "Glfw Error %d: %s\n", error, description);
}
void CursorPositionCallback(GLFWwindow* window, double xpos, double ypos);
void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset);
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
struct App {
public:
App(){}
int InitWindow() {
// Setup window
glfwSetErrorCallback(ErrorCallback);
if (!glfwInit())
return 1;
// GL 3.0 + GLSL 130
glsl_version = "#version 430";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT , GL_TRUE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT , GLFW_FALSE);
GLFWmonitor* primary = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(primary);
window = glfwCreateWindow(mode->width, mode->height, "Testing", NULL, NULL);
if (window == NULL)
return 1;
glfwMakeContextCurrent(window);
glfwSwapInterval(1); // Enable vsync
int version = gladLoadGL(glfwGetProcAddress);
if (version == 0) {
printf("Failed to initialize OpenGL context\n");
exit(0);
}
glfwSetCursorPosCallback(window, CursorPositionCallback);
glfwSetMouseButtonCallback(window, MouseButtonCallback);
glfwSetScrollCallback(window, ScrollCallback);
glfwSetKeyCallback(window, KeyCallback);
// glDisable(GL_CULL_FACE);
// glEnable(GL_STENCIL_TEST);
// glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
// glStencilFunc(GL_ALWAYS, 1, 0xFF); // all fragments should update the stencil buffer
// glStencilMask(0xFF); // enable writing to the stencil buffer
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
// glEnable(GL_MULTISAMPLE);
return 0;
}
void InitImGUI() {
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
// Setup Dear ImGui style
ImGui::StyleColorsDark();
ImGuiIO &_io = ImGui::GetIO();
_io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
//ImGui::StyleColorsClassic();
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
}
void Start() {
demoManager={};
ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.00f);
// Main loop
while (!glfwWindowShouldClose(window))
{
GLenum err;
while((err = glGetError()) != GL_NO_ERROR)
{
if(err==GL_INVALID_ENUM) std::cout << "GL_INVALID_ENUM"<<std::endl;
if(err==GL_INVALID_VALUE) std::cout << "GL_INVALID_VALUE"<<std::endl;
if(err==GL_INVALID_OPERATION) std::cout << "GL_INVALID_OPERATION"<<std::endl;
if(err==GL_STACK_OVERFLOW) std::cout << "GL_STACK_OVERFLOW"<<std::endl;
if(err==GL_STACK_UNDERFLOW) std::cout << "GL_STACK_UNDERFLOW"<<std::endl;
if(err==GL_OUT_OF_MEMORY) std::cout << "GL_OUT_OF_MEMORY"<<std::endl;
if(err==GL_INVALID_FRAMEBUFFER_OPERATION) std::cout << "GL_INVALID_FRAMEBUFFER_OPERATION"<<std::endl;
if(err==GL_CONTEXT_LOST) std::cout << "GL_CONTEXT_LOST"<<std::endl;
}
glfwPollEvents();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
RenderGUI();
// Rendering
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
demoManager.windowWidth = display_w;
demoManager.windowHeight = display_h;
glDepthMask(GL_TRUE);
demoManager.Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
demoManager.ClearDemo();
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
}
void MouseMove(float x, float y) {
demoManager.MouseMove(x, y);
}
void MouseAction(int button, int action, int mods) {
demoManager.MouseAction(button, action, mods);
}
void Scroll(float yOffset) {
demoManager.Scroll(yOffset);
}
void Key(int keyCode, int action)
{
demoManager.Key(keyCode, action);
}
GLFWwindow* window;
private:
const char* glsl_version;
DemoManager demoManager;
void RenderGUI() {
{
ImGui::Begin("Demos : ");
if(ImGui::Button("Image Lab", ImVec2(100, 20))) {
demoManager.ClearDemo();
demoManager.LoadDemo(0);
}
if(ImGui::Button("Audio Lab", ImVec2(100, 20))) {
demoManager.ClearDemo();
demoManager.LoadDemo(1);
}
demoManager.RenderGUI();
ImGui::End();
}
}
};
App app;
void CursorPositionCallback(GLFWwindow* window, double xpos, double ypos)
{
app.MouseMove((float)xpos, (float)ypos);
}
void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
app.MouseAction(button, action, mods);
}
void ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
app.Scroll((float)yoffset);
}
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
app.Key(key, action);
}
int main(int, char**)
{
app.InitWindow();
app.InitImGUI();
app.Start();
return 0;
}