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

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

In openglgame, initialize OpenGL using glew, register the OpenGL debug callback, and clear the screen to black

  • Property mode set to 100644
File size: 5.9 KB
RevLine 
[d02c25f]1#include "opengl-game.hpp"
2
3#include <iostream>
4
[5edbd58]5#include "consts.hpp"
[c559904]6#include "logger.hpp"
[5edbd58]7
[d02c25f]8using namespace std;
9
10OpenGLGame::OpenGLGame() {
[d8cb15e]11 gui = nullptr;
12 window = nullptr;
[d02c25f]13}
14
15OpenGLGame::~OpenGLGame() {
16}
17
[b6e60b4]18void OpenGLGame::run(int width, int height, unsigned char guiFlags) {
[2e77b3f]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
[c559904]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
[5edbd58]36 if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
[d8cb15e]37 return;
38 }
[b6e60b4]39
[d8cb15e]40 initOpenGL();
41 mainLoop();
42 cleanup();
[c559904]43
44 close_log();
[d8cb15e]45}
46
[c559904]47// TODO: Make some more initi functions, or call this initUI if the
48// amount of things initialized here keeps growing
[b6e60b4]49bool OpenGLGame::initWindow(int width, int height, unsigned char guiFlags) {
[c559904]50 // TODO: Put all fonts, textures, and images in the assets folder
[d8cb15e]51 gui = new GameGui_GLFW();
52
[b6e60b4]53 if (gui->init() == RTWO_ERROR) {
[c559904]54 // TODO: Also print these sorts of errors to the log
[d8cb15e]55 cout << "UI library could not be initialized!" << endl;
[b6e60b4]56 cout << gui->getError() << endl;
[d8cb15e]57 return RTWO_ERROR;
58 }
59 cout << "GUI init succeeded" << endl;
60
[b6e60b4]61 window = (GLFWwindow*) gui->createWindow("OpenGL Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
[d8cb15e]62 if (window == nullptr) {
63 cout << "Window could not be created!" << endl;
[ed7c953]64 cout << gui->getError() << endl;
[d8cb15e]65 return RTWO_ERROR;
66 }
67
[b6e60b4]68 cout << "Target window size: (" << width << ", " << height << ")" << endl;
[a6f6833]69 cout << "Actual window size: (" << gui->getWindowWidth() << ", " << gui->getWindowHeight() << ")" << endl;
[b6e60b4]70
[d8cb15e]71 return RTWO_SUCCESS;
72}
73
74void OpenGLGame::initOpenGL() {
[92cbc6a]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 }
[d8cb15e]89}
90
91void OpenGLGame::mainLoop() {
[c61323a]92 UIEvent e;
93 bool quit = false;
[7bf5433]94
[c61323a]95 while (!quit) {
[7bf5433]96 gui->processEvents();
97
[c61323a]98 while (gui->pollEvent(&e)) {
99 switch (e.type) {
100 case UI_EVENT_QUIT:
101 cout << "Quit event detected" << endl;
102 quit = true;
103 break;
104 case UI_EVENT_KEY:
105 if (e.key.keycode == GLFW_KEY_ESCAPE) {
106 quit = true;
107 } else {
108 cout << "Key event detected" << endl;
109 }
110 break;
111 default:
112 cout << "Unhandled UI event: " << e.type << endl;
113 }
[1ce9afe]114 }
115
[92cbc6a]116 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
117
118 // Anton's book suggests placing this here, after glClear(). Check it's impact on framerate
119 // TODO: This doesn't seem to work correctly when in the loop. DO some research
120 // max viewport dims are clamped to glGet(GL_MAX_VIEWPORT_DIMS)
121 //glViewport(0, 0, gui->getWindowWidth(), gui->getWindowHeight());
122
[d8cb15e]123 glfwSwapBuffers(window);
124 }
125}
126
127void OpenGLGame::cleanup() {
[b6e60b4]128 gui->destroyWindow();
129 gui->shutdown();
[d8cb15e]130 delete gui;
[92cbc6a]131}
132
133void APIENTRY opengl_debug_callback(
134 GLenum source,
135 GLenum type,
136 GLuint id,
137 GLenum severity,
138 GLsizei length,
139 const GLchar* message,
140 const void* userParam
141) {
142 string strMessage(message);
143
144 // TODO: Use C++ strings directly and see if there are other ways to clean
145 // this function up
146 char source_str[2048];
147 char type_str[2048];
148 char severity_str[2048];
149
150 switch (source) {
151 case 0x8246:
152 strcpy(source_str, "API");
153 break;
154 case 0x8247:
155 strcpy(source_str, "WINDOW_SYSTEM");
156 break;
157 case 0x8248:
158 strcpy(source_str, "SHADER_COMPILER");
159 break;
160 case 0x8249:
161 strcpy(source_str, "THIRD_PARTY");
162 break;
163 case 0x824A:
164 strcpy(source_str, "APPLICATION");
165 break;
166 case 0x824B:
167 strcpy(source_str, "OTHER");
168 break;
169 default:
170 strcpy(source_str, "undefined");
171 break;
172 }
173
174 switch (type) {
175 case 0x824C:
176 strcpy(type_str, "ERROR");
177 break;
178 case 0x824D:
179 strcpy(type_str, "DEPRECATED_BEHAVIOR");
180 break;
181 case 0x824E:
182 strcpy(type_str, "UNDEFINED_BEHAVIOR");
183 break;
184 case 0x824F:
185 strcpy(type_str, "PORTABILITY");
186 break;
187 case 0x8250:
188 strcpy(type_str, "PERFORMANCE");
189 break;
190 case 0x8251:
191 strcpy(type_str, "OTHER");
192 break;
193 case 0x8268:
194 strcpy(type_str, "MARKER");
195 break;
196 case 0x8269:
197 strcpy(type_str, "PUSH_GROUP");
198 break;
199 case 0x826A:
200 strcpy(type_str, "POP_GROUP");
201 break;
202 default:
203 strcpy(type_str, "undefined");
204 break;
205 }
206 switch (severity) {
207 case 0x9146:
208 strcpy(severity_str, "HIGH");
209 break;
210 case 0x9147:
211 strcpy(severity_str, "MEDIUM");
212 break;
213 case 0x9148:
214 strcpy(severity_str, "LOW");
215 break;
216 case 0x826B:
217 strcpy(severity_str, "NOTIFICATION");
218 break;
219 default:
220 strcpy(severity_str, "undefined");
221 break;
222 }
223
224 if (string(severity_str) != "NOTIFICATION") {
225 cout << "OpenGL Error!!!" << endl;
226 cout << "Source: " << string(source_str) << endl;
227 cout << "Type: " << string(type_str) << endl;
228 cout << "Severity: " << string(severity_str) << endl;
229 cout << strMessage << endl;
230 }
[d02c25f]231}
Note: See TracBrowser for help on using the repository browser.