From 3f6c99cf21c75ee1be1db9a9897e94613a86e625 Mon Sep 17 00:00:00 2001 From: Luca Conte Date: Thu, 25 Apr 2024 16:52:19 +0200 Subject: [PATCH] frame rate counter --- u06-2/main.c | 27 ++++++++++++++++++++++++++- u06-2/wavefrontobj.c | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/u06-2/main.c b/u06-2/main.c index 3100b00..f0289b6 100644 --- a/u06-2/main.c +++ b/u06-2/main.c @@ -28,6 +28,11 @@ GLFWwindow* window; GLfloat aspectRatio = 1.0f; +double timeBetweenUpdates = 0.2f; + +double timeSinceUpdate = 0.0f; +int framesSinceUpdate = 0; + GLfloat step = 0.0f; const GLfloat pi = 3.14159f; @@ -179,8 +184,25 @@ void init(void) { glClearColor(0.1f, 0.1f, 0.3f, 1.0f); } +void updateStats() { + printf("\rFPS: %.1f", framesSinceUpdate / timeSinceUpdate); + fflush(stdout); +} + void draw(void) { + // FPS Counter + framesSinceUpdate++; + double deltaTime = glfwGetTime(); + timeSinceUpdate += deltaTime; + glfwSetTime(0.0f); + + if (timeSinceUpdate >= timeBetweenUpdates) { + updateStats(); + timeSinceUpdate = 0.0f; + framesSinceUpdate = 0; + } + // camera movement handleInputs(); @@ -190,7 +212,7 @@ void draw(void) { // step for rotations // counts up to 1.0 and then resets back to 0.0 forever - step += 0.002f; + step += deltaTime / 5; if (step > 1.0f) step -= 1.0f; // step multiplied by pi * 2 for use in rotation and trig functions @@ -272,6 +294,9 @@ int main(void) { glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwMakeContextCurrent(window); + // disable framerate cap + glfwSwapInterval(0); + // register keyboard event handler glfwSetKeyCallback(window, keyboardHandler); diff --git a/u06-2/wavefrontobj.c b/u06-2/wavefrontobj.c index 256451b..ef2b74b 100644 --- a/u06-2/wavefrontobj.c +++ b/u06-2/wavefrontobj.c @@ -53,7 +53,7 @@ ParsedObjFile readObjFile(char* path) { } } - printf("Vertices: %d\nFaces: %d\nNormals:%d\nTextures:%d\n", numVertices, numFaces, numVertexNormals, numTextureCoords); + // printf("Vertices: %d\nFaces: %d\nNormals:%d\nTextures:%d\n", numVertices, numFaces, numVertexNormals, numTextureCoords); vec3* vertices = (vec3*) malloc(sizeof(vec3) * numVertices); vec3* normals = (vec3*) malloc(sizeof(vec3) * numVertexNormals);