[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 | int actualWidth=0, actualHeight=0;
|
---|
| 69 | gui->getWindowSize(&actualWidth, &actualHeight);
|
---|
| 70 |
|
---|
| 71 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
| 72 | cout << "Actual window size: (" << actualWidth << ", " << actualHeight << ")" << endl;
|
---|
| 73 |
|
---|
[d8cb15e] | 74 | return RTWO_SUCCESS;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void OpenGLGame::initOpenGL() {
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void OpenGLGame::mainLoop() {
|
---|
[c61323a] | 81 | UIEvent e;
|
---|
| 82 | bool quit = false;
|
---|
[7bf5433] | 83 |
|
---|
[c61323a] | 84 | while (!quit) {
|
---|
[7bf5433] | 85 | gui->processEvents();
|
---|
| 86 |
|
---|
[c61323a] | 87 | while (gui->pollEvent(&e)) {
|
---|
| 88 | switch (e.type) {
|
---|
| 89 | case UI_EVENT_QUIT:
|
---|
| 90 | cout << "Quit event detected" << endl;
|
---|
| 91 | quit = true;
|
---|
| 92 | break;
|
---|
| 93 | case UI_EVENT_KEY:
|
---|
| 94 | if (e.key.keycode == GLFW_KEY_ESCAPE) {
|
---|
| 95 | quit = true;
|
---|
| 96 | } else {
|
---|
| 97 | cout << "Key event detected" << endl;
|
---|
| 98 | }
|
---|
| 99 | break;
|
---|
| 100 | default:
|
---|
| 101 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
| 102 | }
|
---|
[1ce9afe] | 103 | }
|
---|
| 104 |
|
---|
[d8cb15e] | 105 | glfwSwapBuffers(window);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void OpenGLGame::cleanup() {
|
---|
[b6e60b4] | 110 | gui->destroyWindow();
|
---|
| 111 | gui->shutdown();
|
---|
[d8cb15e] | 112 | delete gui;
|
---|
[d02c25f] | 113 | }
|
---|