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

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

Remove getWindowSize() from game-gui and instead add getWindowWidth(), getWindowHeight(), and refreshWindowSize() (which updates the internal variables returned by the first two functions)

  • Property mode set to 100644
File size: 2.7 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() {
75}
76
77void OpenGLGame::mainLoop() {
[c61323a]78 UIEvent e;
79 bool quit = false;
[7bf5433]80
[c61323a]81 while (!quit) {
[7bf5433]82 gui->processEvents();
83
[c61323a]84 while (gui->pollEvent(&e)) {
85 switch (e.type) {
86 case UI_EVENT_QUIT:
87 cout << "Quit event detected" << endl;
88 quit = true;
89 break;
90 case UI_EVENT_KEY:
91 if (e.key.keycode == GLFW_KEY_ESCAPE) {
92 quit = true;
93 } else {
94 cout << "Key event detected" << endl;
95 }
96 break;
97 default:
98 cout << "Unhandled UI event: " << e.type << endl;
99 }
[1ce9afe]100 }
101
[d8cb15e]102 glfwSwapBuffers(window);
103 }
104}
105
106void OpenGLGame::cleanup() {
[b6e60b4]107 gui->destroyWindow();
108 gui->shutdown();
[d8cb15e]109 delete gui;
[d02c25f]110}
Note: See TracBrowser for help on using the repository browser.