[d02c25f] | 1 | #include "opengl-game.hpp"
|
---|
| 2 |
|
---|
| 3 | #include <iostream>
|
---|
| 4 |
|
---|
[5edbd58] | 5 | #include "consts.hpp"
|
---|
[c559904] | 6 | #include "logger.hpp"
|
---|
[5edbd58] | 7 |
|
---|
[d02c25f] | 8 | using namespace std;
|
---|
| 9 |
|
---|
| 10 | OpenGLGame::OpenGLGame() {
|
---|
[d8cb15e] | 11 | gui = nullptr;
|
---|
| 12 | window = nullptr;
|
---|
[d02c25f] | 13 | }
|
---|
| 14 |
|
---|
| 15 | OpenGLGame::~OpenGLGame() {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[b6e60b4] | 18 | void 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] | 49 | bool 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 |
|
---|
| 74 | void 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 | }
|
---|
[f133da0] | 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();
|
---|
[d8cb15e] | 105 | }
|
---|
| 106 |
|
---|
| 107 | void OpenGLGame::mainLoop() {
|
---|
[c61323a] | 108 | UIEvent e;
|
---|
| 109 | bool quit = false;
|
---|
[7bf5433] | 110 |
|
---|
[c61323a] | 111 | while (!quit) {
|
---|
[7bf5433] | 112 | gui->processEvents();
|
---|
| 113 |
|
---|
[c61323a] | 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 | }
|
---|
[1ce9afe] | 130 | }
|
---|
| 131 |
|
---|
[92cbc6a] | 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 |
|
---|
[f133da0] | 139 | renderScene();
|
---|
| 140 | renderUI();
|
---|
| 141 |
|
---|
[d8cb15e] | 142 | glfwSwapBuffers(window);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[f133da0] | 146 | void OpenGLGame::renderScene() {
|
---|
| 147 |
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void 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 |
|
---|
[d8cb15e] | 179 | void OpenGLGame::cleanup() {
|
---|
[f133da0] | 180 | ImGui_ImplGlfwGL3_Shutdown();
|
---|
| 181 | ImGui::DestroyContext();
|
---|
| 182 |
|
---|
[b6e60b4] | 183 | gui->destroyWindow();
|
---|
| 184 | gui->shutdown();
|
---|
[d8cb15e] | 185 | delete gui;
|
---|
[92cbc6a] | 186 | }
|
---|
| 187 |
|
---|
| 188 | void 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 | }
|
---|
[d02c25f] | 286 | } |
---|