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

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

Add renderScene and renderUI functions to openglgame and use IMGUI to render the main menu

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