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
/* 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);

View File

@@ -2,53 +2,70 @@
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();
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)
{
std::cout << "PB::Renderer::GLFWManager::GetWindow(): index out of bounds" << std::endl;
return nullptr;
if (s_Window != nullptr)
glfwDestroyWindow(s_Window);
glfwTerminate();
return true; // Cannot fail (yet)
}
return s_Windows[index];
void GLFWManager::UpdateWindowTitleFPSInfo()
{
const double currentTime = glfwGetTime();
s_FramesInTheLastSecond++;
if (currentTime - s_LastFPSUpdateTime > 1.0f)
{
s_FpsCounter = s_FramesInTheLastSecond / (currentTime - s_LastFPSUpdateTime);
s_FramesInTheLastSecond = 0;
s_LastFPSUpdateTime = currentTime;
}
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
{
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:
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();
};
}