source: opengl-game/opengl-game.cpp@ 5b02676

feature/imgui-sdl points-test
Last change on this file since 5b02676 was 3de31cf, checked in by Dmitry Portnoy <dmp1488@…>, 5 years ago

Add and begin implementing an OpenGL version of the GraphicsPipeline class

  • Property mode set to 100644
File size: 8.3 KB
Line 
1#include "opengl-game.hpp"
2
3#include <iostream>
4
5#include "consts.hpp"
6#include "logger.hpp"
7
8using namespace std;
9
10OpenGLGame::OpenGLGame() {
11 gui = nullptr;
12 window = nullptr;
13}
14
15OpenGLGame::~OpenGLGame() {
16}
17
18void 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
49bool 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
74void 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 graphicsPipelines.push_back(GraphicsPipeline_OpenGL());
107 graphicsPipelines.back().createPipeline("gl-shaders/ship.vert", "gl-shaders/ship.frag");
108
109 graphicsPipelines.push_back(GraphicsPipeline_OpenGL());
110 graphicsPipelines.back().createPipeline("gl-shaders/asteroid.vert", "gl-shaders/asteroid.frag");
111
112 graphicsPipelines.push_back(GraphicsPipeline_OpenGL());
113 graphicsPipelines.back().createPipeline("gl-shaders/laser.vert", "gl-shaders/laser.frag");
114
115 graphicsPipelines.push_back(GraphicsPipeline_OpenGL());
116 graphicsPipelines.back().createPipeline("gl-shaders/explosion.vert", "gl-shaders/explosion.frag");
117
118 cout << "Created " << graphicsPipelines.size() << " graphics pipelines" << endl;
119}
120
121void OpenGLGame::mainLoop() {
122 UIEvent e;
123 bool quit = false;
124
125 while (!quit) {
126 gui->processEvents();
127
128 while (gui->pollEvent(&e)) {
129 switch (e.type) {
130 case UI_EVENT_QUIT:
131 cout << "Quit event detected" << endl;
132 quit = true;
133 break;
134 case UI_EVENT_KEY:
135 if (e.key.keycode == GLFW_KEY_ESCAPE) {
136 quit = true;
137 } else {
138 cout << "Key event detected" << endl;
139 }
140 break;
141 case UI_EVENT_WINDOWRESIZE:
142 cout << "Window resize event detected" << endl;
143 break;
144 default:
145 cout << "Unhandled UI event: " << e.type << endl;
146 }
147 }
148
149 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
150
151 // Anton's book suggests placing this here, after glClear(). Check it's impact on framerate
152 // TODO: This doesn't seem to work correctly when in the loop. DO some research
153 // max viewport dims are clamped to glGet(GL_MAX_VIEWPORT_DIMS)
154 //glViewport(0, 0, gui->getWindowWidth(), gui->getWindowHeight());
155
156 renderScene();
157 renderUI();
158
159 glfwSwapBuffers(window);
160 }
161}
162
163void OpenGLGame::renderScene() {
164
165}
166
167void OpenGLGame::renderUI() {
168 ImGui_ImplGlfwGL3_NewFrame();
169
170 {
171 int padding = 4;
172 ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
173 ImGui::SetNextWindowSize(ImVec2(gui->getWindowWidth() + 2 * padding, gui->getWindowHeight() + 2 * padding), ImGuiCond_Always);
174 ImGui::Begin("WndMain", NULL,
175 ImGuiWindowFlags_NoTitleBar |
176 ImGuiWindowFlags_NoResize |
177 ImGuiWindowFlags_NoMove);
178
179 ImGui::InvisibleButton("", ImVec2(10, 80));
180 ImGui::InvisibleButton("", ImVec2(285, 18));
181 ImGui::SameLine();
182 ImGui::Button("New Game");
183
184 ImGui::InvisibleButton("", ImVec2(10, 15));
185 ImGui::InvisibleButton("", ImVec2(300, 18));
186 ImGui::SameLine();
187 ImGui::Button("Quit");
188
189 ImGui::End();
190 }
191
192 ImGui::Render();
193 ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
194}
195
196void OpenGLGame::cleanup() {
197 ImGui_ImplGlfwGL3_Shutdown();
198 ImGui::DestroyContext();
199
200 gui->destroyWindow();
201 gui->shutdown();
202 delete gui;
203}
204
205void APIENTRY opengl_debug_callback(
206 GLenum source,
207 GLenum type,
208 GLuint id,
209 GLenum severity,
210 GLsizei length,
211 const GLchar* message,
212 const void* userParam
213) {
214 string strMessage(message);
215
216 // TODO: Use C++ strings directly and see if there are other ways to clean
217 // this function up
218 char source_str[2048];
219 char type_str[2048];
220 char severity_str[2048];
221
222 switch (source) {
223 case 0x8246:
224 strcpy(source_str, "API");
225 break;
226 case 0x8247:
227 strcpy(source_str, "WINDOW_SYSTEM");
228 break;
229 case 0x8248:
230 strcpy(source_str, "SHADER_COMPILER");
231 break;
232 case 0x8249:
233 strcpy(source_str, "THIRD_PARTY");
234 break;
235 case 0x824A:
236 strcpy(source_str, "APPLICATION");
237 break;
238 case 0x824B:
239 strcpy(source_str, "OTHER");
240 break;
241 default:
242 strcpy(source_str, "undefined");
243 break;
244 }
245
246 switch (type) {
247 case 0x824C:
248 strcpy(type_str, "ERROR");
249 break;
250 case 0x824D:
251 strcpy(type_str, "DEPRECATED_BEHAVIOR");
252 break;
253 case 0x824E:
254 strcpy(type_str, "UNDEFINED_BEHAVIOR");
255 break;
256 case 0x824F:
257 strcpy(type_str, "PORTABILITY");
258 break;
259 case 0x8250:
260 strcpy(type_str, "PERFORMANCE");
261 break;
262 case 0x8251:
263 strcpy(type_str, "OTHER");
264 break;
265 case 0x8268:
266 strcpy(type_str, "MARKER");
267 break;
268 case 0x8269:
269 strcpy(type_str, "PUSH_GROUP");
270 break;
271 case 0x826A:
272 strcpy(type_str, "POP_GROUP");
273 break;
274 default:
275 strcpy(type_str, "undefined");
276 break;
277 }
278 switch (severity) {
279 case 0x9146:
280 strcpy(severity_str, "HIGH");
281 break;
282 case 0x9147:
283 strcpy(severity_str, "MEDIUM");
284 break;
285 case 0x9148:
286 strcpy(severity_str, "LOW");
287 break;
288 case 0x826B:
289 strcpy(severity_str, "NOTIFICATION");
290 break;
291 default:
292 strcpy(severity_str, "undefined");
293 break;
294 }
295
296 if (string(severity_str) != "NOTIFICATION") {
297 cout << "OpenGL Error!!!" << endl;
298 cout << "Source: " << string(source_str) << endl;
299 cout << "Type: " << string(type_str) << endl;
300 cout << "Severity: " << string(severity_str) << endl;
301 cout << strMessage << endl;
302 }
303}
Note: See TracBrowser for help on using the repository browser.