Added FPS counter

Also removed multi window support
This commit is contained in:
Pasha Bibko
2025-11-16 21:19:17 +00:00
parent 4c1226f658
commit 5dc0974d50
3 changed files with 58 additions and 34 deletions

View File

@@ -21,11 +21,8 @@ int main()
{ {
using namespace PB::Renderer; // Project namespace using namespace PB::Renderer; // Project namespace
/* Initializes GLFW and creates window */ /* Initializes GLFW and creates a window */
if (!GLFWManager::Init()) GLFWwindow* window = GLFWManager::Init(800, 600, "Vulkan window");
return -1;
GLFWwindow* window = GLFWManager::CreateWindow(800, 600, "Vulkan window");
if (!window) if (!window)
return -1; return -1;
@@ -37,6 +34,7 @@ int main()
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
glfwPollEvents(); glfwPollEvents();
GLFWManager::UpdateWindowTitleFPSInfo();
if (!VulkanManager::RenderPass()) if (!VulkanManager::RenderPass())
CleanupAllAndExit(EXIT_FAILURE); CleanupAllAndExit(EXIT_FAILURE);

View File

@@ -2,53 +2,70 @@
namespace PB::Renderer namespace PB::Renderer
{ {
std::vector<GLFWwindow*> 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(); const bool success = glfwInit();
if (!success) if (!success)
{
std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl; std::cout << "PB::Renderer::GLFWManager::Init(): glfwInit() failed" << std::endl;
return nullptr;
}
/* Stops GLFW from creating openGL contexts */ /* Stops GLFW from creating openGL contexts */
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); 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 */ /* Checks for valid window size before creation */
if (width < 0 || height < 0) if (width < 0 || height < 0)
{ {
std::cout << "PB::Renderer::GLFWManager::CreateWindow(): width or height must be < 0" << std::endl; std::cout << "PB::Renderer::GLFWManager::CreateWindow(): width or height must be < 0" << std::endl;
return nullptr; return nullptr;
} }
GLFWwindow* window = glfwCreateWindow(width, height, title, nullptr, nullptr);
s_BaseWindowTitle = title;
/* Stores the window and returns it to the user */ /* Stores the window and returns it to the user */
s_Windows.push_back(window); s_Window = glfwCreateWindow(width, height, title, nullptr, nullptr);
return window; return s_Window;
} }
GLFWwindow* GLFWManager::GetWindow(int index) bool GLFWManager::Cleanup()
{ {
/* Checks input is within the bounds before fetching */ if (s_Window != nullptr)
if (index >= s_Windows.size() || index < 0) 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; s_FpsCounter = s_FramesInTheLastSecond / (currentTime - s_LastFPSUpdateTime);
return nullptr; 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());
} }
} }

View File

@@ -7,13 +7,22 @@ namespace PB::Renderer
class GLFWManager class GLFWManager
{ {
private: private:
static std::vector<GLFWwindow*> 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: 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 bool Cleanup();
static GLFWwindow* CreateWindow(int width, int height, const char* title = "Unnamed window"); static __forceinline void PollEvents() { glfwPollEvents(); }
static GLFWwindow* GetWindow(int index = 0); static void UpdateWindowTitleFPSInfo();
}; };
} }