1 | #include "opengl-game.hpp"
|
---|
2 |
|
---|
3 | #include <iostream>
|
---|
4 |
|
---|
5 | #include "consts.hpp"
|
---|
6 |
|
---|
7 | #include "game-gui-glfw.hpp"
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | OpenGLGame::OpenGLGame() {
|
---|
12 | gui = nullptr;
|
---|
13 | window = nullptr;
|
---|
14 | }
|
---|
15 |
|
---|
16 | OpenGLGame::~OpenGLGame() {
|
---|
17 | }
|
---|
18 |
|
---|
19 | void OpenGLGame::run(int width, int height, unsigned char guiFlags) {
|
---|
20 | if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
|
---|
21 | return;
|
---|
22 | }
|
---|
23 |
|
---|
24 | initOpenGL();
|
---|
25 | mainLoop();
|
---|
26 | cleanup();
|
---|
27 | }
|
---|
28 |
|
---|
29 | bool OpenGLGame::initWindow(int width, int height, unsigned char guiFlags) {
|
---|
30 | gui = new GameGui_GLFW();
|
---|
31 |
|
---|
32 | if (gui->init() == RTWO_ERROR) {
|
---|
33 | cout << "UI library could not be initialized!" << endl;
|
---|
34 | cout << gui->getError() << endl;
|
---|
35 | return RTWO_ERROR;
|
---|
36 | }
|
---|
37 | cout << "GUI init succeeded" << endl;
|
---|
38 |
|
---|
39 | window = (GLFWwindow*) gui->createWindow("OpenGL Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
|
---|
40 | if (window == nullptr) {
|
---|
41 | cout << "Window could not be created!" << endl;
|
---|
42 | cout << gui->getError() << endl;
|
---|
43 | return RTWO_ERROR;
|
---|
44 | }
|
---|
45 |
|
---|
46 | int actualWidth=0, actualHeight=0;
|
---|
47 | gui->getWindowSize(&actualWidth, &actualHeight);
|
---|
48 |
|
---|
49 | cout << "Target window size: (" << width << ", " << height << ")" << endl;
|
---|
50 | cout << "Actual window size: (" << actualWidth << ", " << actualHeight << ")" << endl;
|
---|
51 |
|
---|
52 | return RTWO_SUCCESS;
|
---|
53 | }
|
---|
54 |
|
---|
55 | void OpenGLGame::initOpenGL() {
|
---|
56 | }
|
---|
57 |
|
---|
58 | void OpenGLGame::mainLoop() {
|
---|
59 | while (!glfwWindowShouldClose(window)) {
|
---|
60 | glfwPollEvents();
|
---|
61 |
|
---|
62 | if (GameGui_GLFW::s_keyState[GLFW_KEY_ESCAPE] == GLFW_PRESS) {
|
---|
63 | glfwSetWindowShouldClose(window, 1);
|
---|
64 | }
|
---|
65 |
|
---|
66 | glfwSwapBuffers(window);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | void OpenGLGame::cleanup() {
|
---|
71 | gui->destroyWindow();
|
---|
72 | gui->shutdown();
|
---|
73 | delete gui;
|
---|
74 | }
|
---|