]> git.lizzy.rs Git - shadowclad.git/commitdiff
Add frametime & fps metering
authoroutfrost <kotlet.bahn@gmail.com>
Sun, 6 Jan 2019 05:25:55 +0000 (06:25 +0100)
committeroutfrost <kotlet.bahn@gmail.com>
Sun, 6 Jan 2019 05:25:55 +0000 (06:25 +0100)
It runs at 2400 fps. My PC might need help later.

Makefile
main.c
performance.c [new file with mode: 0644]
performance.h [new file with mode: 0644]
render.c

index a296fac521c9f6c7ae72a86b354b46903753f550..0490851c3b93f52a7cb2adf1293e8f537298f2dc 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ libraries ::= -L/usr/local/lib -lGL -lglut -lassimp
 # Prefix all object file names with the compilation directory
 objects ::= $(addprefix out/, \
               main.o debugutil.o glut_janitor.o render.o \
-              tga.o level.o)
+              tga.o level.o performance.o)
 
 # Set executable extension for the platform
 ifeq ($(OS),Windows_NT)
@@ -56,3 +56,6 @@ out/tga.o : tga.c tga.h | out
 
 out/level.o : level.c level.h tga.h | out
        $(CC) $(compileargs) -c -o out/level.o level.c
+
+out/performance.o : performance.c | out
+       $(CC) $(compileargs) -c -o out/performance.o performance.c
diff --git a/main.c b/main.c
index 22e6831d82005afab0dffaa210c9c51a280407d3..34d7ec6f18d521a471e18baa31d8175dea0149ed 100644 (file)
--- a/main.c
+++ b/main.c
@@ -4,6 +4,7 @@
 #include "glut_janitor.h"
 #include "render.h"
 #include "level.h"
+#include "performance.h"
 
 int main(int argc, char** argv) {
        glutInit(&argc, argv);
@@ -22,6 +23,7 @@ int main(int argc, char** argv) {
        //glutMotionFunc(mouse_motion_event);
        
        initRender();
+       initPerformanceMetering();
        
        model = importModel("out/assets/wall01.3ds");
        /*
diff --git a/performance.c b/performance.c
new file mode 100644 (file)
index 0000000..cb3709f
--- /dev/null
@@ -0,0 +1,42 @@
+#include <time.h>
+#include <stdio.h> // TODO remove
+#include <stdbool.h>
+
+typedef struct timespec Timepoint;
+
+static Timepoint lastDisplayTime;
+static int frames = 0;
+static bool meteringEnabled = false;
+
+void initPerformanceMetering() {
+       if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
+               fprintf(stderr, "Clock read failed, performance metering unavailable\n");
+       }
+       else {
+               meteringEnabled = true;
+       }
+}
+
+void frameRendered() {
+       if (meteringEnabled) {
+               ++frames;
+               Timepoint now;
+               
+               if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
+                       fprintf(stderr, "Clock read failed, stopping performance metering\n");
+                       meteringEnabled = false;
+                       return;
+               }
+               
+               time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
+               if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
+               
+               if (fullSeconds > 0) {
+                       float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
+                       seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
+                       printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
+                       lastDisplayTime = now;
+                       frames = 0;
+               }
+       }
+}
diff --git a/performance.h b/performance.h
new file mode 100644 (file)
index 0000000..b4ded6b
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef PERFORMANCE_H_
+#define PERFORMANCE_H_
+
+void initPerformanceMetering();
+void frameRendered();
+
+#endif
index 3e29339d966cf93f600555f1ac76ffeaddfc82fb..a1f5a3070edbf2c26fe7e7a049b32d041631a70f 100644 (file)
--- a/render.c
+++ b/render.c
@@ -3,6 +3,7 @@
 
 #include "render.h"
 #include "typedefs.h"
+#include "performance.h"
 
 const float AXIS_RADIUS = 5.0f;
 
@@ -15,6 +16,8 @@ void renderScene() {
        
        glFlush();
        glutSwapBuffers();
+       frameRendered();
+       glutPostRedisplay();
 }
 
 void drawAxes() {