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

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

Implement processEvents() and pollEvent() for GameGui_GLFW and re-implement the main loop in opengl-game using those functions

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[d02c25f]1#include "opengl-game.hpp"
2
3#include <iostream>
4
[5edbd58]5#include "consts.hpp"
6
[d8cb15e]7#include "game-gui-glfw.hpp"
8
[d02c25f]9using namespace std;
10
11OpenGLGame::OpenGLGame() {
[d8cb15e]12 gui = nullptr;
13 window = nullptr;
[d02c25f]14}
15
16OpenGLGame::~OpenGLGame() {
17}
18
[b6e60b4]19void OpenGLGame::run(int width, int height, unsigned char guiFlags) {
[5edbd58]20 if (initWindow(width, height, guiFlags) == RTWO_ERROR) {
[d8cb15e]21 return;
22 }
[b6e60b4]23
[d8cb15e]24 initOpenGL();
25 mainLoop();
26 cleanup();
27}
28
[b6e60b4]29bool OpenGLGame::initWindow(int width, int height, unsigned char guiFlags) {
[d8cb15e]30 gui = new GameGui_GLFW();
31
[b6e60b4]32 if (gui->init() == RTWO_ERROR) {
[d8cb15e]33 cout << "UI library could not be initialized!" << endl;
[b6e60b4]34 cout << gui->getError() << endl;
[d8cb15e]35 return RTWO_ERROR;
36 }
37 cout << "GUI init succeeded" << endl;
38
[b6e60b4]39 window = (GLFWwindow*) gui->createWindow("OpenGL Game", width, height, guiFlags & GUI_FLAGS_WINDOW_FULLSCREEN);
[d8cb15e]40 if (window == nullptr) {
41 cout << "Window could not be created!" << endl;
[ed7c953]42 cout << gui->getError() << endl;
[d8cb15e]43 return RTWO_ERROR;
44 }
45
[b6e60b4]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
[d8cb15e]52 return RTWO_SUCCESS;
53}
54
55void OpenGLGame::initOpenGL() {
56}
57
58void OpenGLGame::mainLoop() {
[c61323a]59 UIEvent e;
60 bool quit = false;
[7bf5433]61
[c61323a]62 while (!quit) {
[7bf5433]63 gui->processEvents();
64
[c61323a]65 while (gui->pollEvent(&e)) {
66 switch (e.type) {
67 case UI_EVENT_QUIT:
68 cout << "Quit event detected" << endl;
69 quit = true;
70 break;
71 case UI_EVENT_KEY:
72 if (e.key.keycode == GLFW_KEY_ESCAPE) {
73 quit = true;
74 } else {
75 cout << "Key event detected" << endl;
76 }
77 break;
78 default:
79 cout << "Unhandled UI event: " << e.type << endl;
80 }
[1ce9afe]81 }
82
[d8cb15e]83 glfwSwapBuffers(window);
84 }
85}
86
87void OpenGLGame::cleanup() {
[b6e60b4]88 gui->destroyWindow();
89 gui->shutdown();
[d8cb15e]90 delete gui;
[d02c25f]91}
Note: See TracBrowser for help on using the repository browser.