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

feature/imgui-sdl points-test
Last change on this file since c1d9b2a 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
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
91void OpenGLGame::mainLoop() {
92 UIEvent e;
93 bool quit = false;
94
95 while (!quit) {
96 gui->processEvents();
97
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 }
114 }
115
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
123 glfwSwapBuffers(window);
124 }
125}
126
127void OpenGLGame::cleanup() {
128 gui->destroyWindow();
129 gui->shutdown();
130 delete gui;
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 }
231}
Note: See TracBrowser for help on using the repository browser.