From e13392916639c945980506333db679ce3d0e45f9 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sat, 15 Nov 2025 17:27:08 +0000 Subject: [PATCH] Added GLFWManager --- .idea/vcs.xml | 7 ++++++ CMakeLists.txt | 4 +++- main.cpp | 29 +++++++---------------- src/GLFWManager.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/GLFWManager.h | 21 +++++++++++++++++ 5 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 src/GLFWManager.cpp create mode 100644 src/GLFWManager.h diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..259a06e --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index eb9cd20..0526612 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,9 @@ FetchContent_MakeAvailable(glfw) # Creates the output binary project(VulkanRenderer LANGUAGES CXX) add_executable(VulkanRenderer - main.cpp + main.cpp + src/GLFWManager.h + src/GLFWManager.cpp ) # Links the libraries to the binary # diff --git a/main.cpp b/main.cpp index 0a32d62..fcd209d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,29 +1,16 @@ -#include -#include - -#include +#include "src/GLFWManager.h" int main() { + using namespace PB::Renderer; // Project namespace + /* Initializes GLFW and creates window */ - if (!glfwInit()) - { - std::cout << "Failed to initialize GLFW" << std::endl; + if (!GLFWManager::Init()) return -1; - } - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - - const int WIDTH = 800; - const int HEIGHT = 600; - - GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan Window", nullptr, nullptr); + GLFWwindow* window = GLFWManager::CreateWindow(800, 600, "Vulkan window"); if (!window) - { - std::cout << "Failed to create GLFW window" << std::endl; - glfwTerminate(); return -1; - } /* Polls window events whilst it is still open */ while (!glfwWindowShouldClose(window)) @@ -32,6 +19,6 @@ int main() } /* Cleans up GLFW */ - glfwDestroyWindow(window); - glfwTerminate(); -} \ No newline at end of file + if (!GLFWManager::Cleanup()) + return -1; +} diff --git a/src/GLFWManager.cpp b/src/GLFWManager.cpp new file mode 100644 index 0000000..6a0fa17 --- /dev/null +++ b/src/GLFWManager.cpp @@ -0,0 +1,57 @@ +#include "GLFWManager.h" + +#include + +namespace PB::Renderer +{ + std::vector GLFWManager::s_Windows; + + bool GLFWManager::Init() + { + const bool success = glfwInit(); + if (!success) + std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl; + + /* Stops GLFW from creating openGL contexts */ + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + + return success; + } + + bool GLFWManager::Cleanup() + { + /* Closes all windows */ + for (GLFWwindow* window : s_Windows) + glfwDestroyWindow(window); + + glfwTerminate(); + return true; // Cannot fail (yet) + } + + GLFWwindow* GLFWManager::CreateWindow(int width, int height, const char* title) + { + /* Checks for valid window size before creation */ + if (width < 0 || height < 0) + { + std::cout << "PB::Renderer::GLFWManager::CreateWindow(): width or height must be < 0" << std::endl; + return nullptr; + } + GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr); + + /* Stores the window and returns it to the user */ + s_Windows.push_back(window); + return window; + } + + GLFWwindow* GLFWManager::GetWindow(int index) + { + /* Checks input is within the bounds before fetching */ + if (index >= s_Windows.size() || index < 0) + { + std::cout << "PB::Renderer::GLFWManager::GetWindow(): index out of bounds" << std::endl; + return nullptr; + } + + return s_Windows[index]; + } +} diff --git a/src/GLFWManager.h b/src/GLFWManager.h new file mode 100644 index 0000000..5f9a6a0 --- /dev/null +++ b/src/GLFWManager.h @@ -0,0 +1,21 @@ +#pragma once + +#include "GLFW/glfw3.h" + +#include + +namespace PB::Renderer +{ + class GLFWManager + { + private: + static std::vector s_Windows; + + public: + static bool Init(); + static bool Cleanup(); + + static GLFWwindow* CreateWindow(int width, int height, const char* title = "Unnamed window"); + static GLFWwindow* GetWindow(int index = 0); + }; +}