1 | #include "opengl-game.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "consts.hpp"
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | OpenGLGame::OpenGLGame() {
|
---|
10 | gui = nullptr;
|
---|
11 | window = nullptr;
|
---|
12 | }
|
---|
13 |
|
---|
14 | OpenGLGame::~OpenGLGame() {
|
---|
15 | }
|
---|
16 |
|
---|
17 | void OpenGLGame::run(int width, int height, unsigned char guiFlags) {
|
---|
18 | #ifdef NDEBUG
|
---|
19 | cout << "DEBUGGING IS OFF" << endl;
|
---|
20 | #else
|
---|
21 | cout << "DEBUGGING IS ON" << endl;
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | cout << "OpenGL Game" << endl;
|
---|
25 |
|
---|
26 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
27 | return;
|
---|
28 | }
|
---|
29 |
|
---|
30 | initOpenGL();
|
---|
31 | mainLoop();
|
---|
32 | cleanup();
|
---|
33 | }
|
---|
34 |
|
---|
35 | bool OpenGLGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
36 | gui = new GameGui_GLFW();
|
---|
37 |
|
---|
38 | if (gui->init() == RTWO_ERROR) {
|
---|
39 | cout << "UI library could not be initialized!" << endl;
|
---|
40 | cout << gui->getError() << endl;
|
---|
41 | return RTWO_ERROR;
|
---|
42 | }
|
---|
43 | cout << "GUI init succeeded" << endl;
|
---|
44 |
|
---|
45 | window = (GLFWwindow*) gui->createWindow("OpenGL Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
46 | if (window == nullptr) {
|
---|
47 | cout << "Window could not be created!" << endl;
|
---|
48 | cout << gui->getError() << endl;
|
---|
49 | return RTWO_ERROR;
|
---|
50 | }
|
---|
51 |
|
---|
52 | int actualWidth=0, actualHeight=0;
|
---|
53 | gui->getWindowSize(&actualWidth, &actualHeight);
|
---|
54 |
|
---|
55 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
56 | cout << "Actual window size: (" << actualWidth << ", " << actualHeight << ")" << endl;
|
---|
57 |
|
---|
58 | return RTWO_SUCCESS;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void OpenGLGame::initOpenGL() {
|
---|
62 | }
|
---|
63 |
|
---|
64 | void OpenGLGame::mainLoop() {
|
---|
65 | UIEvent e;
|
---|
66 | bool quit = false;
|
---|
67 |
|
---|
68 | while (!quit) {
|
---|
69 | gui->processEvents();
|
---|
70 |
|
---|
71 | while (gui->pollEvent(&e)) {
|
---|
72 | switch (e.type) {
|
---|
73 | case UI_EVENT_QUIT:
|
---|
74 | cout << "Quit event detected" << endl;
|
---|
75 | quit = true;
|
---|
76 | break;
|
---|
77 | case UI_EVENT_KEY:
|
---|
78 | if (e.key.keycode == GLFW_KEY_ESCAPE) {
|
---|
79 | quit = true;
|
---|
80 | } else {
|
---|
81 | cout << "Key event detected" << endl;
|
---|
82 | }
|
---|
83 | break;
|
---|
84 | default:
|
---|
85 | cout << "Unhandled UI event: " << e.type << endl;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | glfwSwapBuffers(window);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | void OpenGLGame::cleanup() {
|
---|
94 | gui->destroyWindow();
|
---|
95 | gui->shutdown();
|
---|
96 | delete gui;
|
---|
97 | } |
---|