Game window stays up until ESC is pressed

This commit is contained in:
ATLAS_Moon 2020-08-15 16:27:24 +00:00
parent 0efe16a204
commit fd605024da
3 changed files with 107 additions and 27 deletions

View File

@ -2,18 +2,47 @@
#define OUR_DICK_RENDER_HPP #define OUR_DICK_RENDER_HPP
#include <stdint.h> #include <stdint.h>
#include <GLFW/glfw3.h>
#define OPENFL_VERSION 400 #define OPENFL_VERSION 400
#define GLSL_VERSION 400 #define GLSL_VERSION 400
class RenderManager class GLFWwindow;
{
public:
RenderManager (RenderManager&) = delete;
RenderManager (RenderManager&&) = delete;
RenderManager(); class render_manager
bool Init(uint16_t width, uint16_t height, const char* title); // Sets up the OpenGL environment {
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 <typename T>
void handle_window_close_event(T handle){
m_windowCloseCallback = handle;
}
template <typename T>
void handle_keypress_event(T handle){
m_inputHandler = handle;
glfwSetKeyCallback(m_mainWindow, m_inputHandler);
}
}; };

View File

@ -12,6 +12,14 @@
// --------- // ---------
// 6 | 7 | 8 // 6 | 7 | 8
#define TILE_COUNT 9
namespace
{
bool running = true;
render_manager manager;
}
int get_player_input(){ int get_player_input(){
//TODO get player input //TODO get player input
return rand()%9; return rand()%9;
@ -77,12 +85,28 @@ int exists_empty_tile(const game_state& gs){
return 0; 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(){ int main(){
srand(time(NULL)); srand(time(NULL));
game_state gs = {}; 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){ while(exists_empty_tile(gs) && gs.turn != -1){
game_turn(gs, get_player_input()); game_turn(gs, get_player_input());

View File

@ -1,33 +1,60 @@
#include <gl3w/GL/gl3w.h> // Must be included first
#include "render.hpp" #include "render.hpp"
#include <gl3w/GL/gl3w.h>
#include <GLFW/glfw3.h>
#include <stdio.h> #include <stdio.h>
RenderManager::RenderManager(){} namespace
{
bool RenderManager::Init (uint16_t width, uint16_t height, const char* title){ void handle_resize_event(GLFWwindow*, int width, int height){
if (!glfwInit()) { printf("Window resized\n");
printf("[EE] failed to initialize GLFW.\n"); glViewport(0, 0, width, height);
return false;
} }
}
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_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
auto window = glfwCreateWindow(width, height, title, nullptr, nullptr); m_mainWindow = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!window) { if (!m_mainWindow) {
printf ("[EE] Could not create window\n"); fprintf (stdout, "[EE] Could not create window\n");
return false; return GLFW_WINDOW_ERROR;
} }
glfwMakeContextCurrent(window); glfwMakeContextCurrent(m_mainWindow);
if (gl3wInit()) { if (gl3wInit()) {
printf("[EE] failed to initialize OpenGL\n"); fprintf(stdout, "[EE] failed to initialize OpenGL\n");
return false; return GLFW_CONTEXT_ERROR;
} }
glViewport(0, 0, width, height);
printf("[DD] OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION)); 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);
} }