source: opengl-game/opengl-game.cpp@ f94eea9

feature/imgui-sdl points-test
Last change on this file since f94eea9 was c6fec84, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Update the makefile to correctly compile openglgame

  • Property mode set to 100644
File size: 7.5 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
107void 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 default:
128 cout << "Unhandled UI event: " << e.type << endl;
129 }
130 }
131
132 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133
134 // Anton's book suggests placing this here, after glClear(). Check it's impact on framerate
135 // TODO: This doesn't seem to work correctly when in the loop. DO some research
136 // max viewport dims are clamped to glGet(GL_MAX_VIEWPORT_DIMS)
137 //glViewport(0, 0, gui->getWindowWidth(), gui->getWindowHeight());
138
139 renderScene();
140 renderUI();
141
142 glfwSwapBuffers(window);
143 }
144}
145
146void OpenGLGame::renderScene() {
147
148}
149
150void OpenGLGame::renderUI() {
151 ImGui_ImplGlfwGL3_NewFrame();
152
153 {
154 int padding = 4;
155 ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
156 ImGui::SetNextWindowSize(ImVec2(gui->getWindowWidth() + 2 * padding, gui->getWindowHeight() + 2 * padding), ImGuiCond_Always);
157 ImGui::Begin("WndMain", NULL,
158 ImGuiWindowFlags_NoTitleBar |
159 ImGuiWindowFlags_NoResize |
160 ImGuiWindowFlags_NoMove);
161
162 ImGui::InvisibleButton("", ImVec2(10, 80));
163 ImGui::InvisibleButton("", ImVec2(285, 18));
164 ImGui::SameLine();
165 ImGui::Button("New Game");
166
167 ImGui::InvisibleButton("", ImVec2(10, 15));
168 ImGui::InvisibleButton("", ImVec2(300, 18));
169 ImGui::SameLine();
170 ImGui::Button("Quit");
171
172 ImGui::End();
173 }
174
175 ImGui::Render();
176 ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
177}
178
179void OpenGLGame::cleanup() {
180 ImGui_ImplGlfwGL3_Shutdown();
181 ImGui::DestroyContext();
182
183 gui->destroyWindow();
184 gui->shutdown();
185 delete gui;
186}
187
188void APIENTRY opengl_debug_callback(
189 GLenum source,
190 GLenum type,
191 GLuint id,
192 GLenum severity,
193 GLsizei length,
194 const GLchar* message,
195 const void* userParam
196) {
197 string strMessage(message);
198
199 // TODO: Use C++ strings directly and see if there are other ways to clean
200 // this function up
201 char source_str[2048];
202 char type_str[2048];
203 char severity_str[2048];
204
205 switch (source) {
206 case 0x8246:
207 strcpy(source_str, "API");
208 break;
209 case 0x8247:
210 strcpy(source_str, "WINDOW_SYSTEM");
211 break;
212 case 0x8248:
213 strcpy(source_str, "SHADER_COMPILER");
214 break;
215 case 0x8249:
216 strcpy(source_str, "THIRD_PARTY");
217 break;
218 case 0x824A:
219 strcpy(source_str, "APPLICATION");
220 break;
221 case 0x824B:
222 strcpy(source_str, "OTHER");
223 break;
224 default:
225 strcpy(source_str, "undefined");
226 break;
227 }
228
229 switch (type) {
230 case 0x824C:
231 strcpy(type_str, "ERROR");
232 break;
233 case 0x824D:
234 strcpy(type_str, "DEPRECATED_BEHAVIOR");
235 break;
236 case 0x824E:
237 strcpy(type_str, "UNDEFINED_BEHAVIOR");
238 break;
239 case 0x824F:
240 strcpy(type_str, "PORTABILITY");
241 break;
242 case 0x8250:
243 strcpy(type_str, "PERFORMANCE");
244 break;
245 case 0x8251:
246 strcpy(type_str, "OTHER");
247 break;
248 case 0x8268:
249 strcpy(type_str, "MARKER");
250 break;
251 case 0x8269:
252 strcpy(type_str, "PUSH_GROUP");
253 break;
254 case 0x826A:
255 strcpy(type_str, "POP_GROUP");
256 break;
257 default:
258 strcpy(type_str, "undefined");
259 break;
260 }
261 switch (severity) {
262 case 0x9146:
263 strcpy(severity_str, "HIGH");
264 break;
265 case 0x9147:
266 strcpy(severity_str, "MEDIUM");
267 break;
268 case 0x9148:
269 strcpy(severity_str, "LOW");
270 break;
271 case 0x826B:
272 strcpy(severity_str, "NOTIFICATION");
273 break;
274 default:
275 strcpy(severity_str, "undefined");
276 break;
277 }
278
279 if (string(severity_str) != "NOTIFICATION") {
280 cout << "OpenGL Error!!!" << endl;
281 cout << "Source: " << string(source_str) << endl;
282 cout << "Type: " << string(type_str) << endl;
283 cout << "Severity: " << string(severity_str) << endl;
284 cout << strMessage << endl;
285 }
286}
Note: See TracBrowser for help on using the repository browser.