feat: resizable window

This commit is contained in:
ktyl 2023-08-12 00:16:35 +02:00
parent 4795d88b91
commit 2b6444dfac
2 changed files with 15 additions and 10 deletions

View File

@ -7,9 +7,7 @@
#include "io.hpp" #include "io.hpp"
const int WIDTH = 640; float aspect_;
const int HEIGHT = 480;
const float ASPECT = (float)WIDTH / (float)HEIGHT;
// Initialize GLFW, OpenGL and GLEW, open a window and make it the current context. // Initialize GLFW, OpenGL and GLEW, open a window and make it the current context.
// Returns 0 for success, and -1 for any failure. // 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_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);
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) if (!window)
{ {
glfwTerminate(); glfwTerminate();
@ -34,6 +34,8 @@ int initGraphics(GLFWwindow** window, const std::string& title)
return -1; return -1;
} }
glfwSetWindowSizeCallback(*window, windowSizeCallback);
glfwMakeContextCurrent(*window); glfwMakeContextCurrent(*window);
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
@ -48,6 +50,12 @@ int initGraphics(GLFWwindow** window, const std::string& title)
return 0; 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 compileShader(const std::string& shaderPath, GLenum shaderType)
{ {
GLuint shader; GLuint shader;
@ -102,7 +110,7 @@ GLuint compileShaderProgram(const std::string& fragShaderPath)
void updateProjectionMatrix(GLuint shaderProgram) 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); glm::mat4 projection = glm::ortho(left, right, bottom, top, near, far);
GLint projectionLocation = getShaderUniformLocation(shaderProgram, "_Projection"); GLint projectionLocation = getShaderUniformLocation(shaderProgram, "_Projection");
glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection[0][0]); glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, &projection[0][0]);

View File

@ -5,11 +5,8 @@
#include "GL/glew.h" #include "GL/glew.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
extern const int WIDTH;
extern const int HEIGHT;
extern const float ASPECT;
int initGraphics(GLFWwindow** window, const std::string& title); int initGraphics(GLFWwindow** window, const std::string& title);
void windowSizeCallback(GLFWwindow* window, int width, int height);
GLuint compileShaderProgram(const std::string& fragShaderPath); GLuint compileShaderProgram(const std::string& fragShaderPath);
GLuint compileShader(const std::string& shaderPath, GLenum shaderType); GLuint compileShader(const std::string& shaderPath, GLenum shaderType);