From a5064ab4718aeadc035665a8a396ee709bb630f6 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 19 Dec 2022 21:02:46 +0100 Subject: [PATCH 1/5] Stuff --- src/renderer.cpp | 6 +++--- src/window.cpp | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/renderer.cpp b/src/renderer.cpp index 335a8be..e7ae4db 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -72,7 +72,7 @@ namespace Renderer { // Scene Scene::Scene(Window* win) { - window = win; + window = win; } Scene::Scene(Window* win, std::vector ROs) : Scene(win) { @@ -95,7 +95,7 @@ namespace Renderer { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - for ( RenderObject *ro: renderObjects ) + for ( RenderObject *ro: renderObjects ) ro->render(*camera); // Record the last frame @@ -125,7 +125,7 @@ namespace Renderer { } // RenderObject - RenderObject::RenderObject(std::vector verts, std::vector indices) + RenderObject::RenderObject(std::vector verts, std::vector indices) : shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE) { vertsVec = verts; indicesVec = indices; diff --git a/src/window.cpp b/src/window.cpp index 34e235b..53304ee 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -14,7 +14,7 @@ Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title } void Window::spawn() { - glfwInit(); + glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); @@ -31,8 +31,6 @@ void Window::spawn() { } void Window::updateSize(int w, int h) { - // int w, h; - // glfwGetWindowSize(win, &w, &h); _width = w; _height = h; glViewport(0, 0, w, h); From 218d6a7fe3a604331b07686fa29402916ea57a9e Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 20 Dec 2022 17:18:17 +0100 Subject: [PATCH 2/5] Minor refactor --- headers/window.hpp | 17 ++-- src/controller.cpp | 20 ++-- src/main.cpp | 222 ++++++++++++++++++++++----------------------- src/renderer.cpp | 2 +- src/textures.cpp | 4 +- src/window.cpp | 18 +++- 6 files changed, 150 insertions(+), 133 deletions(-) diff --git a/headers/window.hpp b/headers/window.hpp index cbc11e5..7316c2f 100644 --- a/headers/window.hpp +++ b/headers/window.hpp @@ -1,7 +1,7 @@ #pragma once -// #include "renderer.hpp" #include #include "GLFW/glfw3.h" +#include // Default window dimensions #define D_WINDOW_TITLE "Euclid Engine" @@ -11,19 +11,24 @@ class Window { public: - GLFWwindow* win; - // Renderer::Camera cam; - Window(const char* title); Window(const char* title, unsigned int w, unsigned int h); + ~Window(); + void spawn(); - unsigned int width() { return _width; } // getters and dumb and - unsigned int height() { return _height; } // cpp should have readonly fields... + unsigned int getWidth() { return _width; } // getters and dumb and + unsigned int getHeight() { return _height; } // cpp should have readonly fields... + GLFWwindow* getWindow() { return _win; } void updateSize(int w, int h); + void makeCurrent(); + void swapBuffers(); private: + GLFWwindow* _win; + static std::map windowMap; + const char* _title = D_WINDOW_TITLE; unsigned int _width = D_WINDOW_WIDTH; unsigned int _height = D_WINDOW_HEIGHT; diff --git a/src/controller.cpp b/src/controller.cpp index c19b231..d0f36eb 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -10,15 +10,15 @@ void Controller::processMouseInput(float deltaTime) { double x, y; - glfwGetCursorPos(window->win, &x, &y); + glfwGetCursorPos(window->getWindow(), &x, &y); if (firstMouseInput) { lastX = x; lastY = y; firstMouseInput = false; - } + } - float xOffset = x - lastX; + float xOffset = x - lastX; float yOffset = y - lastY; lastX = x; lastY = y; @@ -49,24 +49,24 @@ void Controller::processMouseInput(float deltaTime) { void Controller::processInput(float deltaTime) { processMouseInput(deltaTime); - float cameraSpeed = deltaTime * 2.5f; + float cameraSpeed = deltaTime * 2.5f; - if (glfwGetKey(window->win, GLFW_KEY_W) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_W) == GLFW_PRESS) { translate(cameraSpeed * front); } - if (glfwGetKey(window->win, GLFW_KEY_S) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_S) == GLFW_PRESS) { translate(-cameraSpeed * front); } - if (glfwGetKey(window->win, GLFW_KEY_A) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_A) == GLFW_PRESS) { translate(-glm::normalize(glm::cross(front, up)) * cameraSpeed); } - if (glfwGetKey(window->win, GLFW_KEY_D) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_D) == GLFW_PRESS) { translate(glm::normalize(glm::cross(front, up)) * cameraSpeed); } - if (glfwGetKey(window->win, GLFW_KEY_SPACE) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_SPACE) == GLFW_PRESS) { translate(cameraSpeed * up); } - if (glfwGetKey(window->win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { + if (glfwGetKey(window->getWindow(), GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { translate(cameraSpeed * -up); } } diff --git a/src/main.cpp b/src/main.cpp index c9b9466..f002dc7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,92 +14,92 @@ #define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg" std::vector verts({ - -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, - 0.5f, 0.5f, -0.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); + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f + }); // Vert struc: x y z r g b tx ty std::vector indices({ - 0, 1, 3, - 1, 2, 3, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, + 0, 1, 3, + 1, 2, 3, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, }); @@ -108,68 +108,68 @@ Window win("Euclid Engine: Demo"); Renderer::Scene scene(&win); void framebuffer_size_callback(GLFWwindow* glfwWindow, int w, int h) { - win.updateSize(w, h); + win.updateSize(w, h); } void processInput(GLFWwindow *win) { - int action = glfwGetKey(win, GLFW_KEY_ESCAPE); - if (action == GLFW_PRESS) { - glfwSetWindowShouldClose(win, true); - } + int action = glfwGetKey(win, GLFW_KEY_ESCAPE); + if (action == GLFW_PRESS) { + glfwSetWindowShouldClose(win, true); + } } int main() { - // Spawn window - win.spawn(); + // Spawn window + win.spawn(); - if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { - printf("Failed to init GLAD.\n"); - return 1; - } + if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { + printf("Failed to init GLAD.\n"); + return 1; + } - glViewport(0, 0, win.width(), win.height()); - glfwSetFramebufferSizeCallback(win.win, framebuffer_size_callback); // Framebuffer + glViewport(0, 0, win.getWidth(), win.getHeight()); + glfwSetFramebufferSizeCallback(win.getWindow(), framebuffer_size_callback); // Framebuffer - glEnable(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); - // Input - glfwSetInputMode(win.win, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor + // Input + glfwSetInputMode(win.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor - float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); + float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - // Create scene - Renderer::TexturedObject ro(verts, indices); - Renderer::TexturedObject ro2(verts, indices); + // Create scene + Renderer::TexturedObject ro(verts, indices); + Renderer::TexturedObject ro2(verts, indices); - ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug - ro.setTexture(RUSTY_METAL_TEXTURE); + ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug + ro.setTexture(RUSTY_METAL_TEXTURE); - scene.spawnObject(&ro); - scene.spawnObject(&ro2); + scene.spawnObject(&ro); + scene.spawnObject(&ro2); - // Controller test - Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f)); - scene.setCamera(&player); + // Controller test + Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f)); + scene.setCamera(&player); - while (!glfwWindowShouldClose(win.win)) { - // Handle input - player.processInput(scene.deltaTime); - processInput(win.win); + while (!glfwWindowShouldClose(win.getWindow())) { + // Handle input + player.processInput(scene.deltaTime); + processInput(win.getWindow()); - ro.translate(glm::vec3(0.0f, 0.0f, 0.001f)); - // ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f)); - ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f)); - ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f)); + ro.translate(glm::vec3(0.0f, 0.0f, 0.001f)); + // ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f)); + // ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f)); + ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f)); - // Render new frame - scene.render(); + // Render new frame + scene.render(); - // glfw - glfwSwapBuffers(win.win); - glfwPollEvents(); - } + // glfw + glfwSwapBuffers(win.getWindow()); + glfwPollEvents(); + } - glfwTerminate(); - return 0; + glfwTerminate(); + return 0; } diff --git a/src/renderer.cpp b/src/renderer.cpp index 30e6f0c..d1008d2 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -120,7 +120,7 @@ namespace Renderer { } glm::mat4 Camera::getProjection() { - projection = glm::perspective(glm::radians(FOV), (float)window->width() / (float)window->height(), NEAR_PLANE, FAR_PLANE); + projection = glm::perspective(glm::radians(FOV), (float)window->getWidth() / (float)window->getHeight(), NEAR_PLANE, FAR_PLANE); return projection; } diff --git a/src/textures.cpp b/src/textures.cpp index 8841ed5..64551ce 100644 --- a/src/textures.cpp +++ b/src/textures.cpp @@ -1,5 +1,5 @@ #include -#include "../headers/textures.hpp" +#include "textures.hpp" #include #define STB_IMAGE_IMPLEMENTATION @@ -30,7 +30,7 @@ namespace Textures { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); unsigned char* data = stbi_load(texture_src, &width, &height, &nrChannels, 0); - + if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); diff --git a/src/window.cpp b/src/window.cpp index 5b7ea0f..3b13421 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -13,6 +13,10 @@ Window::Window(const char* title, unsigned int w, unsigned int h) : Window(title this->_height = h; } +Window::~Window() { + glfwDestroyWindow(_win); +} + void Window::spawn() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -20,14 +24,14 @@ void Window::spawn() { glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_FLOATING, GL_TRUE); - win = glfwCreateWindow(_width, _height, _title, NULL, NULL); - if (win == NULL) { + _win = glfwCreateWindow(_width, _height, _title, NULL, NULL); + if (_win == NULL) { printf("[ERROR] Failed to create a window.\n"); glfwTerminate(); exit(1); } - glfwMakeContextCurrent(win); + glfwMakeContextCurrent(_win); } void Window::updateSize(int w, int h) { @@ -35,3 +39,11 @@ void Window::updateSize(int w, int h) { _height = h; glViewport(0, 0, w, h); } + +void Window::makeCurrent() { + glfwMakeContextCurrent(_win); +} + +void Window::swapBuffers() { + glfwSwapBuffers(_win); +} From 477f72e139e514a9406fc9da56e3bfa6e76206c6 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 20 Dec 2022 17:20:54 +0100 Subject: [PATCH 3/5] Fixed formatting --- src/main.cpp | 222 +++++++++++++++++++++++++-------------------------- 1 file changed, 111 insertions(+), 111 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f002dc7..31443b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,92 +14,92 @@ #define RUSTY_METAL_TEXTURE "assets/textures/rusty_metal.jpg" std::vector verts({ - -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0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1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, - -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f - }); + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f +}); // Vert struc: x y z r g b tx ty std::vector indices({ - 0, 1, 3, - 1, 2, 3, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, - 0, 1, 3, - 3, 4, 5, - 5, 6, 7, - 7, 8, 9, - 9, 10, 11, - 11, 12, 13, + 0, 1, 3, + 1, 2, 3, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, + 0, 1, 3, + 3, 4, 5, + 5, 6, 7, + 7, 8, 9, + 9, 10, 11, + 11, 12, 13, }); @@ -108,68 +108,68 @@ Window win("Euclid Engine: Demo"); Renderer::Scene scene(&win); void framebuffer_size_callback(GLFWwindow* glfwWindow, int w, int h) { - win.updateSize(w, h); + win.updateSize(w, h); } void processInput(GLFWwindow *win) { - int action = glfwGetKey(win, GLFW_KEY_ESCAPE); - if (action == GLFW_PRESS) { - glfwSetWindowShouldClose(win, true); - } + int action = glfwGetKey(win, GLFW_KEY_ESCAPE); + if (action == GLFW_PRESS) { + glfwSetWindowShouldClose(win, true); + } } int main() { - // Spawn window - win.spawn(); + // Spawn window + win.spawn(); - if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { - printf("Failed to init GLAD.\n"); - return 1; - } + if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { + printf("Failed to init GLAD.\n"); + 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); + glEnable(GL_DEPTH_TEST); - // Input - glfwSetInputMode(win.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor + // Input + glfwSetInputMode(win.getWindow(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); // Disable cursor - float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); + float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - // Create scene - Renderer::TexturedObject ro(verts, indices); - Renderer::TexturedObject ro2(verts, indices); + // Create scene + Renderer::TexturedObject ro(verts, indices); + Renderer::TexturedObject ro2(verts, indices); - ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug - ro.setTexture(RUSTY_METAL_TEXTURE); + ro2.setTexture("assets/textures/meep.jpg"); // TODO: fix texture bug + ro.setTexture(RUSTY_METAL_TEXTURE); - scene.spawnObject(&ro); - scene.spawnObject(&ro2); + scene.spawnObject(&ro); + scene.spawnObject(&ro2); - // Controller test - Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f)); - scene.setCamera(&player); + // Controller test + Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f)); + scene.setCamera(&player); - while (!glfwWindowShouldClose(win.getWindow())) { - // Handle input - player.processInput(scene.deltaTime); - processInput(win.getWindow()); + while (!glfwWindowShouldClose(win.getWindow())) { + // Handle input + player.processInput(scene.deltaTime); + processInput(win.getWindow()); - ro.translate(glm::vec3(0.0f, 0.0f, 0.001f)); - // ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f)); - // ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f)); - ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f)); + ro.translate(glm::vec3(0.0f, 0.0f, 0.001f)); + // ro2.translate(glm::vec3(0.0f, -0.01f, 0.01f)); + // ro2.translate(glm::vec3(0.0f, 0.0f, -0.001f)); + ro2.rotate(glm::vec3(1.01f, 1.0f, 1.0f)); - // Render new frame - scene.render(); + // Render new frame + scene.render(); - // glfw - glfwSwapBuffers(win.getWindow()); - glfwPollEvents(); - } + // glfw + glfwSwapBuffers(win.getWindow()); + glfwPollEvents(); + } - glfwTerminate(); - return 0; + glfwTerminate(); + return 0; } From 5e88f63273706a268c7523ef3efd7d76bb6ae9cd Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 23 Dec 2022 19:59:23 +0100 Subject: [PATCH 4/5] 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); +} From ce061b91ae64377061ff5bf216270f3a11ed183e Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 23 Dec 2022 20:03:33 +0100 Subject: [PATCH 5/5] Cleanup the main.cpp file --- headers/window.hpp | 1 + src/main.cpp | 8 ++------ src/window.cpp | 5 +++++ 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/headers/window.hpp b/headers/window.hpp index 62bf0a7..fdb0fa4 100644 --- a/headers/window.hpp +++ b/headers/window.hpp @@ -25,6 +25,7 @@ class Window { void updateSize(int w, int h); void makeCurrent(); void swapBuffers(); + bool shouldClose(); private: GLFWwindow* _win; static std::map windowMap; diff --git a/src/main.cpp b/src/main.cpp index 4b1050e..25a341e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -123,9 +123,6 @@ int main() { return 1; } - // glViewport(0, 0, win.getWidth(), win.getHeight()); - // glfwSetFramebufferSizeCallback(win.getWindow(), framebuffer_size_callback); // Framebuffer - glEnable(GL_DEPTH_TEST); // Input @@ -148,7 +145,7 @@ int main() { Controller player(&win, glm::vec3(0.0f, 0.0f, 8.0f)); scene.setCamera(&player); - while (!glfwWindowShouldClose(win.getWindow())) { + while (!win.shouldClose()) { // Handle input player.processInput(scene.deltaTime); processInput(win.getWindow()); @@ -162,10 +159,9 @@ int main() { scene.render(); // glfw - glfwSwapBuffers(win.getWindow()); + win.swapBuffers(); glfwPollEvents(); } - glfwTerminate(); return 0; } diff --git a/src/window.cpp b/src/window.cpp index b432e3e..576de07 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -55,6 +55,11 @@ void Window::swapBuffers() { glfwSwapBuffers(_win); } +bool Window::shouldClose() { + return glfwWindowShouldClose(_win); +} + +// Framebuffer Size Callback, called each time the window "updates" void Window::framebufferSizeCallback(GLFWwindow* win, int width, int height) { windowMap[win]->updateSize(width, height); }