1 | #include "opengl-game.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "consts.hpp"
|
---|
6 | #include "logger.hpp"
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | OpenGLGame::OpenGLGame() {
|
---|
11 | gui = nullptr;
|
---|
12 | window = nullptr;
|
---|
13 | }
|
---|
14 |
|
---|
15 | OpenGLGame::~OpenGLGame() {
|
---|
16 | }
|
---|
17 |
|
---|
18 | void 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
|
---|
49 | bool 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 |
|
---|
74 | void OpenGLGame::initOpenGL() {
|
---|
75 | }
|
---|
76 |
|
---|
77 | void OpenGLGame::mainLoop() {
|
---|
78 | UIEvent e;
|
---|
79 | bool quit = false;
|
---|
80 |
|
---|
81 | while (!quit) {
|
---|
82 | gui->processEvents();
|
---|
83 |
|
---|
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 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | glfwSwapBuffers(window);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | void OpenGLGame::cleanup() {
|
---|
107 | gui->destroyWindow();
|
---|
108 | gui->shutdown();
|
---|
109 | delete gui;
|
---|
110 | }
|
---|