From bf8e7d2259ff804799b7422669f19f380ae43f81 Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Sat, 4 Jan 2025 14:37:26 -0700 Subject: [PATCH] Multithread rendering --- 2dGameProject/Window.cpp | 18 ++++++-- 2dGameProject/Window.h | 2 + 2dGameProject/main.cpp | 88 +++++++++++++++++++++++----------------- 3 files changed, 67 insertions(+), 41 deletions(-) diff --git a/2dGameProject/Window.cpp b/2dGameProject/Window.cpp index 8b98b32..2d4073d 100644 --- a/2dGameProject/Window.cpp +++ b/2dGameProject/Window.cpp @@ -1,4 +1,5 @@ #include "Window.h" +#include Window::Window(int width, int height, const char* title) { glfw_window = glfwCreateWindow(width, height, title, NULL, NULL); @@ -7,7 +8,8 @@ Window::Window(int width, int height, const char* title) { setSize(width, height); setTitle(title); glfwSetFramebufferSizeCallback(glfw_window, onResize); - + glfwSetWindowUserPointer(glfw_window, this); + resized = false; } Window::~Window() { @@ -36,6 +38,12 @@ void Window::setSize(int width, int height) { glfwSetWindowSize(glfw_window, width, height); } +void Window::resizeViewport(int width, int height) { + this->width = width; + this->height = height; + resized = true; +} + void Window::setTitle(const char* title) { this->title = title; glfwSetWindowTitle(glfw_window, title); @@ -116,6 +124,11 @@ bool Window::isResizable() { void Window::beginRender() { glfwMakeContextCurrent(glfw_window); + if (resized) { + std::cout << "Resizing viewport\n"; + glViewport(0, 0, width, height); + resized = false; + } } void Window::endRender() { @@ -127,6 +140,5 @@ bool Window::isClosing() { } void Window::onResize(GLFWwindow* window, int width, int height) { - glfwMakeContextCurrent(window); - glViewport(0, 0, width, height); + ((Window*)glfwGetWindowUserPointer(window))->resizeViewport(width, height); } \ No newline at end of file diff --git a/2dGameProject/Window.h b/2dGameProject/Window.h index fdcc775..10af70c 100644 --- a/2dGameProject/Window.h +++ b/2dGameProject/Window.h @@ -10,6 +10,7 @@ private: bool is_fullscreen; bool is_resizable; int windowed_mode_x, windowed_mode_y; + bool resized; public: Window(int width, int height, const char* title); @@ -19,6 +20,7 @@ public: void setWidth(int width); void setHeight(int height); void setSize(int width, int height); + void resizeViewport(int width, int height); const char* getTitle(); void setTitle(const char* title); std::vector getVideoModes(); diff --git a/2dGameProject/main.cpp b/2dGameProject/main.cpp index 94ce199..d82eb9a 100644 --- a/2dGameProject/main.cpp +++ b/2dGameProject/main.cpp @@ -14,9 +14,11 @@ #include "Square.h" #include "deff.h" #include +#include int OpenGLVersion; spdlog::logger logger("none"); +std::mutex ready; float square[] = { 1.0f, 1.0f, 0.0f, // top right @@ -65,6 +67,19 @@ const char* fragmentShaderSource = "#version 330 core\n" #define ERR(...) logger.error(__VA_ARGS__) #define CRITICAL(...) logger.critical(__VA_ARGS__) +bool running = true; +Window* window; +unsigned int shaderProgram; + +void openGLContextInit() { + OpenGLVersion = gladLoadGL(); + if (OpenGLVersion == 0) { + ERR("Unable to load GLAD"); + throw ("Failed to load GLAD"); + } + INFO("GLAD Initialized"); +} + void init() { auto console = std::make_shared(); auto file = std::make_shared("log.txt", true); @@ -76,29 +91,14 @@ void init() { throw ("Unable to init GLFW"); } INFO("GLFW Initialized!"); + window = new Window(640, 480, "Test Window"); } -void openGLContextInit() { - OpenGLVersion = gladLoadGL(); - if (OpenGLVersion == 0) { - ERR("Unable to load GLAD"); - throw ("Failed to load GLAD"); - } - INFO("GLAD Initialized"); -} - -void deinit() { - glfwTerminate(); -} - -int main() { - - init(); - - Window* window = new Window(640, 480, "Test Window"); +void renderInit() { //window->setWindowedFullscreen(); window->beginRender(); - glfwSwapInterval(0); + //glfwSwapInterval(1); + window->setWindowedFullscreen(); openGLContextInit(); unsigned int vertexShader; @@ -112,7 +112,7 @@ int main() { if (!success) { glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); ERR("Vertex Shader Compile Failed: {}", infoLog); - return -1; + return; } unsigned int fragmentShader; @@ -124,10 +124,9 @@ int main() { if (!success) { glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); ERR("Fragment Shader Compile Failed: {}", infoLog); - return -1; + return; } - unsigned int shaderProgram; shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); @@ -137,7 +136,7 @@ int main() { if (!success) { glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); ERR("Program Link Failed: {}", infoLog); - return -1; + return; } glUseProgram(shaderProgram); @@ -147,26 +146,39 @@ int main() { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glUniformMatrix4fv(UNIFORM_PROJ_MAT, 1, GL_FALSE, glm::value_ptr(glm::ortho(0.0f, 800.0f, 600.0f, 0.0f))); - std::array squares; - for (int i = 0; i < 100 * 100; i++) { - int x = i % 100; - int y = i / 100; - squares[i] = new Square(glm::vec3(x*6, y*6, 0.0f), glm::vec3(6.0f, 6.0f, 1.0f), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f)); - } - //Square square({ 0, 0, 0 }, { 6, 6, 0 }, {0.0f, 1.0f, 0.0f, 1.0f}); - while (!window->isClosing()) { - //square.Draw(); - for (auto square : squares) { - square->Draw(); - } +} +void deinit() { + glDeleteProgram(shaderProgram); + glfwTerminate(); +} + +void render() { + INFO("Render thread starting"); + renderInit(); + while (running) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + window->beginRender(); window->endRender(); + } +} + +void update() { + INFO("Update Thread Starting"); + while (!window->isClosing()) { glfwPollEvents(); } + running = false; +} - for (auto square : squares) { - delete square; - } +int main() { + + init(); + + std::thread renderThread(render); + Sleep(10); + update(); + renderThread.join(); deinit();