From 4a57bb1357128868259b63d7a3775e179af8cb68 Mon Sep 17 00:00:00 2001 From: Amy Retzerau Date: Wed, 3 Sep 2025 13:55:51 +0200 Subject: [PATCH] feat: added FPS counter to terminal --- PerformanceAnalyser.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ main.cpp | 14 +++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 PerformanceAnalyser.hpp diff --git a/PerformanceAnalyser.hpp b/PerformanceAnalyser.hpp new file mode 100644 index 0000000..ac8fa76 --- /dev/null +++ b/PerformanceAnalyser.hpp @@ -0,0 +1,40 @@ +#include +#include +#include + +class PerformanceAnalyser { + private: + double lastFrameTime = 0; + int frameCount = 0; + double timeAcc = 0; + double lastFps = 0; + + public: + PerformanceAnalyser(int framesToAcc = 10, bool autoPrint = true) + : framesToAcc(framesToAcc), autoPrint(autoPrint) {}; + + int framesToAcc; + bool autoPrint; + + int getFPS() { return lastFps; } + + void update(float time) { + + timeAcc += (time - lastFrameTime); + lastFrameTime = time; + frameCount++; + + if (frameCount == framesToAcc) { + + lastFps = double(framesToAcc) / timeAcc; + + if (autoPrint) { + std::cout << "Fps: " << std::round(lastFps * 10.0) * 0.1 << "\n" + << std::flush; + } + + frameCount = 0; + timeAcc = 0; + } + } +}; diff --git a/main.cpp b/main.cpp index c4f93fb..7303e76 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,9 @@ #include "FullScreenQuad.hpp" +#include "PerformanceAnalyser.hpp" #include #include #include +#include const char *vertexShaderSource = #include "fsq.glsl" @@ -75,6 +77,8 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int height) { } int main(int argc, char **argv) { + std::cout << "test"; + std::cout << std::flush; GLFWwindow *window; @@ -104,7 +108,15 @@ int main(int argc, char **argv) { FullScreenQuad fsq{}; + PerformanceAnalyser perf{}; + + double time = glfwGetTime(); + while (!glfwWindowShouldClose(window)) { + + time = glfwGetTime(); + perf.update(time); + processInput(window); /* Render here */ @@ -116,7 +128,7 @@ int main(int argc, char **argv) { int aspectLocation = glGetUniformLocation(shaderProgram, "aspect"); glUniform1f(aspectLocation, aspect); int timeLocation = glGetUniformLocation(shaderProgram, "t"); - glUniform1f(timeLocation, 2. * glfwGetTime()); + glUniform1f(timeLocation, 2. * time); glBindVertexArray(fsq.getVAO()); glDrawArrays(GL_TRIANGLES, 0, 6);