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 | 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 |
|
---|
74 | return RTWO_SUCCESS;
|
---|
75 | }
|
---|
76 |
|
---|
77 | void OpenGLGame::initOpenGL() {
|
---|
78 | }
|
---|
79 |
|
---|
80 | void OpenGLGame::mainLoop() {
|
---|
81 | UIEvent e;
|
---|
82 | bool quit = false;
|
---|
83 |
|
---|
84 | while (!quit) {
|
---|
85 | gui->processEvents();
|
---|
86 |
|
---|
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 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | glfwSwapBuffers(window);
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | void OpenGLGame::cleanup() {
|
---|
110 | gui->destroyWindow();
|
---|
111 | gui->shutdown();
|
---|
112 | delete gui;
|
---|
113 | }
|
---|