1 | #include "opengl-game.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "consts.hpp"
|
---|
6 | #include "logger.hpp"
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | OpenGLGame::OpenGLGame() {
|
---|
11 | gui = nullptr;
|
---|
12 | window = nullptr;
|
---|
13 | }
|
---|
14 |
|
---|
15 | OpenGLGame::~OpenGLGame() {
|
---|
16 | }
|
---|
17 |
|
---|
18 | void OpenGLGame::run(int width, int height, unsigned char guiFlags) {
|
---|
19 | #ifdef NDEBUG
|
---|
20 | cout << "DEBUGGING IS OFF" << endl;
|
---|
21 | #else
|
---|
22 | cout << "DEBUGGING IS ON" << endl;
|
---|
23 | #endif
|
---|
24 |
|
---|
25 | cout << "OpenGL Game" << endl;
|
---|
26 |
|
---|
27 | // TODO: Refactor the logger api to be more flexible,
|
---|
28 | // esp. since gl_log() and gl_log_err() have issues printing anything besides stirngs
|
---|
29 | restart_gl_log();
|
---|
30 | gl_log("starting GLFW\n%s", glfwGetVersionString());
|
---|
31 |
|
---|
32 | open_log();
|
---|
33 | get_log() << "starting GLFW" << endl;
|
---|
34 | get_log() << glfwGetVersionString() << endl;
|
---|
35 |
|
---|
36 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
37 | return;
|
---|
38 | }
|
---|
39 |
|
---|
40 | initOpenGL();
|
---|
41 | mainLoop();
|
---|
42 | cleanup();
|
---|
43 |
|
---|
44 | close_log();
|
---|
45 | }
|
---|
46 |
|
---|
47 | // TODO: Make some more initi functions, or call this initUI if the
|
---|
48 | // amount of things initialized here keeps growing
|
---|
49 | bool OpenGLGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
50 | // TODO: Put all fonts, textures, and images in the assets folder
|
---|
51 | gui = new GameGui_GLFW();
|
---|
52 |
|
---|
53 | if (gui->init() == RTWO_ERROR) {
|
---|
54 | // TODO: Also print these sorts of errors to the log
|
---|
55 | cout << "UI library could not be initialized!" << endl;
|
---|
56 | cout << gui->getError() << endl;
|
---|
57 | return RTWO_ERROR;
|
---|
58 | }
|
---|
59 | cout << "GUI init succeeded" << endl;
|
---|
60 |
|
---|
61 | window = (GLFWwindow*) gui->createWindow("OpenGL Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
62 | if (window == nullptr) {
|
---|
63 | cout << "Window could not be created!" << endl;
|
---|
64 | cout << gui->getError() << endl;
|
---|
65 | return RTWO_ERROR;
|
---|
66 | }
|
---|
67 |
|
---|
68 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
69 | cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
|
---|
70 |
|
---|
71 | return RTWO_SUCCESS;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void OpenGLGame::initOpenGL() {
|
---|
75 | glfwMakeContextCurrent(window);
|
---|
76 | glViewport(0, 0, gui->getWindowWidth(), gui->getWindowHeight());
|
---|
77 |
|
---|
78 | glewExperimental = GL_TRUE;
|
---|
79 | glewInit();
|
---|
80 |
|
---|
81 | if (GLEW_KHR_debug) {
|
---|
82 | cout << "FOUND GLEW debug extension" << endl;
|
---|
83 | glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
---|
84 | glDebugMessageCallback((GLDEBUGPROC)opengl_debug_callback, nullptr);
|
---|
85 | cout << "Bound debug callback" << endl;
|
---|
86 | } else {
|
---|
87 | cout << "OpenGL debug message callback is not supported" << endl;
|
---|
88 | }
|
---|
89 |
|
---|
90 | // Setup Dear ImGui binding
|
---|
91 | IMGUI_CHECKVERSION();
|
---|
92 | ImGui::CreateContext();
|
---|
93 | ImGuiIO& io = ImGui::GetIO(); (void)io;
|
---|
94 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
---|
95 | //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
---|
96 | ImGui_ImplGlfwGL3_Init(window, true);
|
---|
97 |
|
---|
98 | // Setup style
|
---|
99 | ImGui::StyleColorsDark();
|
---|
100 | //ImGui::StyleColorsClassic();
|
---|
101 |
|
---|
102 | // The glfw event handlers have to be bound after ImGui is initialized.
|
---|
103 | // Otherwise, it seems they get overridden by ImGui
|
---|
104 | ((GameGui_GLFW*)gui)->bindEventHandlers();
|
---|
105 | }
|
---|
106 |
|
---|
107 | void OpenGLGame::mainLoop() {
|
---|
108 | UIEvent e;
|
---|
109 | bool quit = false;
|
---|
110 |
|
---|
111 | while (!quit) {
|
---|
112 | gui->processEvents();
|
---|
113 |
|
---|
114 | while (gui->pollEvent(&e)) {
|
---|
115 | switch (e.type) {
|
---|
116 | case UI_EVENT_QUIT:
|
---|
117 | cout << "Quit event detected" << endl;
|
---|
118 | quit = true;
|
---|
119 | break;
|
---|
120 | case UI_EVENT_KEY:
|
---|
121 | if (e.key.keycode == GLFW_KEY_ESCAPE) {
|
---|
122 | quit = true;
|
---|
123 | } else {
|
---|
124 | cout << "Key event detected" << endl;
|
---|
125 | }
|
---|
126 | break;
|
---|
127 | case UI_EVENT_WINDOWRESIZE:
|
---|
128 | cout << "Window resize event detected" << endl;
|
---|
129 | break;
|
---|
130 | default:
|
---|
131 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
136 |
|
---|
137 | // Anton's book suggests placing this here, after glClear(). Check it's impact on framerate
|
---|
138 | // TODO: This doesn't seem to work correctly when in the loop. DO some research
|
---|
139 | // max viewport dims are clamped to glGet(GL_MAX_VIEWPORT_DIMS)
|
---|
140 | //glViewport(0, 0, gui->getWindowWidth(), gui->getWindowHeight());
|
---|
141 |
|
---|
142 | renderScene();
|
---|
143 | renderUI();
|
---|
144 |
|
---|
145 | glfwSwapBuffers(window);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | void OpenGLGame::renderScene() {
|
---|
150 |
|
---|
151 | }
|
---|
152 |
|
---|
153 | void OpenGLGame::renderUI() {
|
---|
154 | ImGui_ImplGlfwGL3_NewFrame();
|
---|
155 |
|
---|
156 | {
|
---|
157 | int padding = 4;
|
---|
158 | ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
|
---|
159 | ImGui::SetNextWindowSize(ImVec2(gui->getWindowWidth() + 2 * padding, gui->getWindowHeight() + 2 * padding), ImGuiCond_Always);
|
---|
160 | ImGui::Begin("WndMain", NULL,
|
---|
161 | ImGuiWindowFlags_NoTitleBar |
|
---|
162 | ImGuiWindowFlags_NoResize |
|
---|
163 | ImGuiWindowFlags_NoMove);
|
---|
164 |
|
---|
165 | ImGui::InvisibleButton("", ImVec2(10, 80));
|
---|
166 | ImGui::InvisibleButton("", ImVec2(285, 18));
|
---|
167 | ImGui::SameLine();
|
---|
168 | ImGui::Button("New Game");
|
---|
169 |
|
---|
170 | ImGui::InvisibleButton("", ImVec2(10, 15));
|
---|
171 | ImGui::InvisibleButton("", ImVec2(300, 18));
|
---|
172 | ImGui::SameLine();
|
---|
173 | ImGui::Button("Quit");
|
---|
174 |
|
---|
175 | ImGui::End();
|
---|
176 | }
|
---|
177 |
|
---|
178 | ImGui::Render();
|
---|
179 | ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
|
---|
180 | }
|
---|
181 |
|
---|
182 | void OpenGLGame::cleanup() {
|
---|
183 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
184 | ImGui::DestroyContext();
|
---|
185 |
|
---|
186 | gui->destroyWindow();
|
---|
187 | gui->shutdown();
|
---|
188 | delete gui;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void APIENTRY opengl_debug_callback(
|
---|
192 | GLenum source,
|
---|
193 | GLenum type,
|
---|
194 | GLuint id,
|
---|
195 | GLenum severity,
|
---|
196 | GLsizei length,
|
---|
197 | const GLchar* message,
|
---|
198 | const void* userParam
|
---|
199 | ) {
|
---|
200 | string strMessage(message);
|
---|
201 |
|
---|
202 | // TODO: Use C++ strings directly and see if there are other ways to clean
|
---|
203 | // this function up
|
---|
204 | char source_str[2048];
|
---|
205 | char type_str[2048];
|
---|
206 | char severity_str[2048];
|
---|
207 |
|
---|
208 | switch (source) {
|
---|
209 | case 0x8246:
|
---|
210 | strcpy(source_str, "API");
|
---|
211 | break;
|
---|
212 | case 0x8247:
|
---|
213 | strcpy(source_str, "WINDOW_SYSTEM");
|
---|
214 | break;
|
---|
215 | case 0x8248:
|
---|
216 | strcpy(source_str, "SHADER_COMPILER");
|
---|
217 | break;
|
---|
218 | case 0x8249:
|
---|
219 | strcpy(source_str, "THIRD_PARTY");
|
---|
220 | break;
|
---|
221 | case 0x824A:
|
---|
222 | strcpy(source_str, "APPLICATION");
|
---|
223 | break;
|
---|
224 | case 0x824B:
|
---|
225 | strcpy(source_str, "OTHER");
|
---|
226 | break;
|
---|
227 | default:
|
---|
228 | strcpy(source_str, "undefined");
|
---|
229 | break;
|
---|
230 | }
|
---|
231 |
|
---|
232 | switch (type) {
|
---|
233 | case 0x824C:
|
---|
234 | strcpy(type_str, "ERROR");
|
---|
235 | break;
|
---|
236 | case 0x824D:
|
---|
237 | strcpy(type_str, "DEPRECATED_BEHAVIOR");
|
---|
238 | break;
|
---|
239 | case 0x824E:
|
---|
240 | strcpy(type_str, "UNDEFINED_BEHAVIOR");
|
---|
241 | break;
|
---|
242 | case 0x824F:
|
---|
243 | strcpy(type_str, "PORTABILITY");
|
---|
244 | break;
|
---|
245 | case 0x8250:
|
---|
246 | strcpy(type_str, "PERFORMANCE");
|
---|
247 | break;
|
---|
248 | case 0x8251:
|
---|
249 | strcpy(type_str, "OTHER");
|
---|
250 | break;
|
---|
251 | case 0x8268:
|
---|
252 | strcpy(type_str, "MARKER");
|
---|
253 | break;
|
---|
254 | case 0x8269:
|
---|
255 | strcpy(type_str, "PUSH_GROUP");
|
---|
256 | break;
|
---|
257 | case 0x826A:
|
---|
258 | strcpy(type_str, "POP_GROUP");
|
---|
259 | break;
|
---|
260 | default:
|
---|
261 | strcpy(type_str, "undefined");
|
---|
262 | break;
|
---|
263 | }
|
---|
264 | switch (severity) {
|
---|
265 | case 0x9146:
|
---|
266 | strcpy(severity_str, "HIGH");
|
---|
267 | break;
|
---|
268 | case 0x9147:
|
---|
269 | strcpy(severity_str, "MEDIUM");
|
---|
270 | break;
|
---|
271 | case 0x9148:
|
---|
272 | strcpy(severity_str, "LOW");
|
---|
273 | break;
|
---|
274 | case 0x826B:
|
---|
275 | strcpy(severity_str, "NOTIFICATION");
|
---|
276 | break;
|
---|
277 | default:
|
---|
278 | strcpy(severity_str, "undefined");
|
---|
279 | break;
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (string(severity_str) != "NOTIFICATION") {
|
---|
283 | cout << "OpenGL Error!!!" << endl;
|
---|
284 | cout << "Source: " << string(source_str) << endl;
|
---|
285 | cout << "Type: " << string(type_str) << endl;
|
---|
286 | cout << "Severity: " << string(severity_str) << endl;
|
---|
287 | cout << strMessage << endl;
|
---|
288 | }
|
---|
289 | }
|
---|