diff --git a/main.cpp b/main.cpp index 2f4b354..03100ff 100644 --- a/main.cpp +++ b/main.cpp @@ -21,11 +21,8 @@ int main() { using namespace PB::Renderer; // Project namespace - /* Initializes GLFW and creates window */ - if (!GLFWManager::Init()) - return -1; - - GLFWwindow* window = GLFWManager::CreateWindow(800, 600, "Vulkan window"); + /* Initializes GLFW and creates a window */ + GLFWwindow* window = GLFWManager::Init(800, 600, "Vulkan window"); if (!window) return -1; @@ -37,6 +34,7 @@ int main() while (!glfwWindowShouldClose(window)) { glfwPollEvents(); + GLFWManager::UpdateWindowTitleFPSInfo(); if (!VulkanManager::RenderPass()) CleanupAllAndExit(EXIT_FAILURE); diff --git a/src/managers/GLFWManager.cpp b/src/managers/GLFWManager.cpp index a25a096..12abfdc 100644 --- a/src/managers/GLFWManager.cpp +++ b/src/managers/GLFWManager.cpp @@ -2,53 +2,70 @@ namespace PB::Renderer { - std::vector GLFWManager::s_Windows; + GLFWwindow* GLFWManager::s_Window = nullptr; - bool GLFWManager::Init() + int GLFWManager::s_FramesInTheLastSecond = 0; + int GLFWManager::s_FpsCounter = 0; + + double GLFWManager::s_LastDeltaUpdateTime = 0.0; + double GLFWManager::s_LastFPSUpdateTime = 0.0; + + std::string GLFWManager::s_BaseWindowTitle = {}; + + GLFWwindow* GLFWManager::Init(const int width, const int height, const char* title) { const bool success = glfwInit(); if (!success) + { std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl; + return nullptr; + } /* 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); + + s_BaseWindowTitle = title; /* Stores the window and returns it to the user */ - s_Windows.push_back(window); - return window; + s_Window = glfwCreateWindow(width, height, title, nullptr, nullptr); + return s_Window; } - GLFWwindow* GLFWManager::GetWindow(int index) + bool GLFWManager::Cleanup() { - /* Checks input is within the bounds before fetching */ - if (index >= s_Windows.size() || index < 0) + if (s_Window != nullptr) + glfwDestroyWindow(s_Window); + + glfwTerminate(); + return true; // Cannot fail (yet) + } + + void GLFWManager::UpdateWindowTitleFPSInfo() + { + const double currentTime = glfwGetTime(); + s_FramesInTheLastSecond++; + + if (currentTime - s_LastFPSUpdateTime > 1.0f) { - std::cout << "PB::Renderer::GLFWManager::GetWindow(): index out of bounds" << std::endl; - return nullptr; + s_FpsCounter = s_FramesInTheLastSecond / (currentTime - s_LastFPSUpdateTime); + s_FramesInTheLastSecond = 0; + s_LastFPSUpdateTime = currentTime; } - return s_Windows[index]; + double deltaTime = currentTime - s_LastDeltaUpdateTime; + s_LastDeltaUpdateTime = currentTime; + + std::string fullTitle = s_BaseWindowTitle + '(' + + std::to_string(s_FpsCounter) + " FPS | " + + std::to_string(deltaTime) + " ms)"; + + glfwSetWindowTitle(s_Window, fullTitle.c_str()); } } diff --git a/src/managers/GLFWManager.h b/src/managers/GLFWManager.h index 2d4b7d0..664024d 100644 --- a/src/managers/GLFWManager.h +++ b/src/managers/GLFWManager.h @@ -7,13 +7,22 @@ namespace PB::Renderer class GLFWManager { private: - static std::vector s_Windows; + static GLFWwindow* s_Window; + + static int s_FramesInTheLastSecond; + static int s_FpsCounter; + static double s_LastDeltaUpdateTime; + static double s_LastFPSUpdateTime; + + static std::string s_BaseWindowTitle; public: - static bool Init(); + static GLFWwindow* Init(int width, int height, const char* title = "Unnamed window"); + __forceinline static GLFWwindow* GetWindow() { return s_Window; } + static bool Cleanup(); - static GLFWwindow* CreateWindow(int width, int height, const char* title = "Unnamed window"); - static GLFWwindow* GetWindow(int index = 0); + static __forceinline void PollEvents() { glfwPollEvents(); } + static void UpdateWindowTitleFPSInfo(); }; }