From 5e88f63273706a268c7523ef3efd7d76bb6ae9cd Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 23 Dec 2022 19:59:23 +0100 Subject: [PATCH] Fixed wrapper class for windows --- headers/window.hpp | 4 +++- src/main.cpp | 14 +++++--------- src/window.cpp | 11 +++++++++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/headers/window.hpp b/headers/window.hpp index 7316c2f..62bf0a7 100644 --- a/headers/window.hpp +++ b/headers/window.hpp @@ -20,7 +20,7 @@ class Window { unsigned int getWidth() { return _width; } // getters and dumb and unsigned int getHeight() { return _height; } // cpp should have readonly fields... - GLFWwindow* getWindow() { return _win; } + GLFWwindow* getWindow() { return _win; } void updateSize(int w, int h); void makeCurrent(); @@ -35,4 +35,6 @@ class Window { void setWidth(unsigned int w) { _width = w; } void setHeight(unsigned int h) { _height = h; } + + static void framebufferSizeCallback(GLFWwindow* win, int width, int height); }; diff --git a/src/main.cpp b/src/main.cpp index 31443b5..4b1050e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -104,13 +104,6 @@ std::vector indices({ // Window for the game -Window win("Euclid Engine: Demo"); -Renderer::Scene scene(&win); - -void framebuffer_size_callback(GLFWwindow* glfwWindow, int w, int h) { - win.updateSize(w, h); -} - void processInput(GLFWwindow *win) { int action = glfwGetKey(win, GLFW_KEY_ESCAPE); if (action == GLFW_PRESS) { @@ -120,6 +113,9 @@ void processInput(GLFWwindow *win) { int main() { // Spawn window + Window win("Euclid Engine: Demo"); + Renderer::Scene scene(&win); + win.spawn(); if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { @@ -127,8 +123,8 @@ int main() { return 1; } - glViewport(0, 0, win.getWidth(), win.getHeight()); - glfwSetFramebufferSizeCallback(win.getWindow(), framebuffer_size_callback); // Framebuffer + // glViewport(0, 0, win.getWidth(), win.getHeight()); + // glfwSetFramebufferSizeCallback(win.getWindow(), framebuffer_size_callback); // Framebuffer glEnable(GL_DEPTH_TEST); diff --git a/src/window.cpp b/src/window.cpp index 3b13421..b432e3e 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -4,6 +4,8 @@ #include #include +std::map Window::windowMap; + Window::Window(const char* title) { this->_title = title; } @@ -15,6 +17,7 @@ Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title Window::~Window() { glfwDestroyWindow(_win); + windowMap.erase(_win); } void Window::spawn() { @@ -31,6 +34,10 @@ void Window::spawn() { exit(1); } + // Register window in the std::map + windowMap[_win] = this; + + glfwSetFramebufferSizeCallback(_win, framebufferSizeCallback); glfwMakeContextCurrent(_win); } @@ -47,3 +54,7 @@ void Window::makeCurrent() { void Window::swapBuffers() { glfwSwapBuffers(_win); } + +void Window::framebufferSizeCallback(GLFWwindow* win, int width, int height) { + windowMap[win]->updateSize(width, height); +}