From fd605024dab2c1fdb77016e1247f0c24c0e2398d Mon Sep 17 00:00:00 2001 From: ATLAS_Moon Date: Sat, 15 Aug 2020 16:27:24 +0000 Subject: [PATCH] Game window stays up until ESC is pressed --- include/render.hpp | 43 +++++++++++++++++++++++++------ src/main.cpp | 28 +++++++++++++++++++-- src/render.cpp | 63 +++++++++++++++++++++++++++++++++------------- 3 files changed, 107 insertions(+), 27 deletions(-) diff --git a/include/render.hpp b/include/render.hpp index 23c50d3..36a9355 100644 --- a/include/render.hpp +++ b/include/render.hpp @@ -2,18 +2,47 @@ #define OUR_DICK_RENDER_HPP #include +#include #define OPENFL_VERSION 400 #define GLSL_VERSION 400 -class RenderManager -{ -public: - RenderManager (RenderManager&) = delete; - RenderManager (RenderManager&&) = delete; +class GLFWwindow; - RenderManager(); - bool Init(uint16_t width, uint16_t height, const char* title); // Sets up the OpenGL environment +class render_manager +{ +private: + void (*m_windowCloseCallback)(void); + void (*m_inputHandler)(GLFWwindow* window, int key, int scancode, int action, int mods); + GLFWwindow* m_mainWindow; + +public: + enum GLFW_ERROR + { + GLFW_OK, + GLFW_CONTEXT_ERROR, + GLFW_WINDOW_ERROR, + GLFW_INIT_ERROR, + }; + + render_manager (render_manager&) = delete; + render_manager (render_manager&&) = delete; + + render_manager(); + GLFW_ERROR init(int width, int height, const char* title); // Sets up the OpenGL environment + void update(); // Update the GL context and draw new frame + void request_exit(); + + template + void handle_window_close_event(T handle){ + m_windowCloseCallback = handle; + } + + template + void handle_keypress_event(T handle){ + m_inputHandler = handle; + glfwSetKeyCallback(m_mainWindow, m_inputHandler); + } }; diff --git a/src/main.cpp b/src/main.cpp index 6aecbc4..0bc2b8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,14 @@ // --------- // 6 | 7 | 8 +#define TILE_COUNT 9 + +namespace +{ + bool running = true; + render_manager manager; +} + int get_player_input(){ //TODO get player input return rand()%9; @@ -77,12 +85,28 @@ int exists_empty_tile(const game_state& gs){ return 0; } +void handle_window_close(){ + printf ("[II] Window close event triggered\n"); + running = false; +} + +void handle_input_events(GLFWwindow*, int key, int, int, int) +{ + printf ("[II] Recieved keycode %d\n", key); + if (key == 256) + manager.request_exit (); +} + int main(){ srand(time(NULL)); game_state gs = {}; - RenderManager manager; - manager.Init(640, 480, "Tic-Tac-Gugh"); + manager.init(640, 480, "Tic-Tac-Gugh"); + manager.handle_window_close_event(handle_window_close); + manager.handle_keypress_event (handle_input_events); + + while (running) + manager.update(); while(exists_empty_tile(gs) && gs.turn != -1){ game_turn(gs, get_player_input()); diff --git a/src/render.cpp b/src/render.cpp index ee4fd7c..78a0608 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -1,33 +1,60 @@ +#include // Must be included first #include "render.hpp" - -#include -#include #include -RenderManager::RenderManager(){} - -bool RenderManager::Init (uint16_t width, uint16_t height, const char* title){ - if (!glfwInit()) { - printf("[EE] failed to initialize GLFW.\n"); - return false; +namespace +{ + void handle_resize_event(GLFWwindow*, int width, int height){ + printf("Window resized\n"); + glViewport(0, 0, width, height); } +} + + +render_manager::render_manager() : + m_windowCloseCallback (nullptr), + m_inputHandler (nullptr), + m_mainWindow (nullptr) +{} + +render_manager::GLFW_ERROR render_manager::init (int width, int height, const char* title){ + if (!glfwInit()) { + fprintf(stdout, "[EE] failed to initialize GLFW.\n"); + return GLFW_INIT_ERROR; + } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - auto window = glfwCreateWindow(width, height, title, nullptr, nullptr); - if (!window) { - printf ("[EE] Could not create window\n"); - return false; + m_mainWindow = glfwCreateWindow(width, height, title, nullptr, nullptr); + if (!m_mainWindow) { + fprintf (stdout, "[EE] Could not create window\n"); + return GLFW_WINDOW_ERROR; } - glfwMakeContextCurrent(window); + glfwMakeContextCurrent(m_mainWindow); if (gl3wInit()) { - printf("[EE] failed to initialize OpenGL\n"); - return false; + fprintf(stdout, "[EE] failed to initialize OpenGL\n"); + return GLFW_CONTEXT_ERROR; } + glViewport(0, 0, width, height); + printf("[DD] OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION)); - return true; + + glfwSetFramebufferSizeCallback(m_mainWindow, handle_resize_event); + + return GLFW_OK; +} + +void render_manager::update(){ + glfwSwapBuffers(m_mainWindow); + glfwPollEvents(); +} + +void render_manager::request_exit() +{ + if (m_windowCloseCallback) m_windowCloseCallback(); + glfwSetWindowShouldClose(m_mainWindow, true); }