From 2b6444dfac9c13c86f028c048ff154a206422d86 Mon Sep 17 00:00:00 2001 From: ktyl Date: Sat, 12 Aug 2023 00:16:35 +0200 Subject: [PATCH] feat: resizable window --- src/gfx.cpp | 18 +++++++++++++----- src/gfx.hpp | 7 ++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index ef9f854..fa3bed1 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -7,9 +7,7 @@ #include "io.hpp" -const int WIDTH = 640; -const int HEIGHT = 480; -const float ASPECT = (float)WIDTH / (float)HEIGHT; +float aspect_; // Initialize GLFW, OpenGL and GLEW, open a window and make it the current context. // Returns 0 for success, and -1 for any failure. @@ -25,8 +23,10 @@ int initGraphics(GLFWwindow** window, const std::string& title) glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + + *window = glfwCreateWindow(640, 480, title.c_str(), NULL, NULL); - *window = glfwCreateWindow(WIDTH, HEIGHT, title.c_str(), NULL, NULL); if (!window) { glfwTerminate(); @@ -34,6 +34,8 @@ int initGraphics(GLFWwindow** window, const std::string& title) return -1; } + glfwSetWindowSizeCallback(*window, windowSizeCallback); + glfwMakeContextCurrent(*window); glewExperimental = GL_TRUE; @@ -48,6 +50,12 @@ int initGraphics(GLFWwindow** window, const std::string& title) return 0; } +void windowSizeCallback(GLFWwindow* window, int width, int height) +{ + aspect_ = (float)width / (float)height; + glViewport(0, 0, width, height); +} + GLuint compileShader(const std::string& shaderPath, GLenum shaderType) { GLuint shader; @@ -102,7 +110,7 @@ GLuint compileShaderProgram(const std::string& fragShaderPath) void updateProjectionMatrix(GLuint shaderProgram) { - float left = -ASPECT, right = ASPECT, bottom = -1.0, top = 1.0, near = -1.0, far = 1.0; + float left = -aspect_, right = aspect_, bottom = -1.0, top = 1.0, near = -1.0, far = 1.0; glm::mat4 projection = glm::ortho(left, right, bottom, top, near, far); GLint projectionLocation = getShaderUniformLocation(shaderProgram, "_Projection"); glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection[0][0]); diff --git a/src/gfx.hpp b/src/gfx.hpp index f15bbda..1de9fac 100644 --- a/src/gfx.hpp +++ b/src/gfx.hpp @@ -5,11 +5,8 @@ #include "GL/glew.h" #include -extern const int WIDTH; -extern const int HEIGHT; -extern const float ASPECT; - int initGraphics(GLFWwindow** window, const std::string& title); +void windowSizeCallback(GLFWwindow* window, int width, int height); GLuint compileShaderProgram(const std::string& fragShaderPath); GLuint compileShader(const std::string& shaderPath, GLenum shaderType); @@ -18,4 +15,4 @@ GLint getShaderUniformLocation(GLuint shaderProgram, const std::string& uniformN void updateProjectionMatrix(GLuint shaderProgram); void updateModelMatrix(GLuint shaderProgram, float time); void updateViewMatrix(GLuint shaderProgram); -void updateModelViewProjectionMatrix(GLuint shaderProgram, float time); \ No newline at end of file +void updateModelViewProjectionMatrix(GLuint shaderProgram, float time);