feat: added FPS counter to terminal

This commit is contained in:
2025-09-03 13:55:51 +02:00
parent a6cad08505
commit 4a57bb1357
2 changed files with 53 additions and 1 deletions

40
PerformanceAnalyser.hpp Normal file
View File

@@ -0,0 +1,40 @@
#include <GL/glew.h>
#include <cmath>
#include <iostream>
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;
}
}
};

View File

@@ -1,7 +1,9 @@
#include "FullScreenQuad.hpp"
#include "PerformanceAnalyser.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstdio>
#include <iostream>
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);