]> git.lizzy.rs Git - shadowclad.git/blob - performance.c
Goddamn clipping planes rendering my crap backwards
[shadowclad.git] / performance.c
1 #ifndef _POSIX_C_SOURCE
2 #define _POSIX_C_SOURCE 199309L
3 #endif
4
5 #include <stdbool.h>
6 #include <stdio.h> // TODO remove
7 #include <time.h>
8
9 #include "logger.h"
10
11 typedef struct timespec Timepoint;
12
13 static Timepoint lastDisplayTime;
14 static int frames = 0;
15 static bool meteringEnabled = false;
16
17 void initPerformanceMetering() {
18         if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
19                 logWarning("Clock read failed, performance metering unavailable");
20         }
21         else {
22                 meteringEnabled = true;
23         }
24 }
25
26 void frameRendered() {
27         if (meteringEnabled) {
28                 ++frames;
29                 Timepoint now;
30                 
31                 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
32                         logWarning("Clock read failed, stopping performance metering");
33                         meteringEnabled = false;
34                         return;
35                 }
36                 
37                 time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
38                 if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
39                 
40                 if (fullSeconds > 0) {
41                         float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
42                         seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
43                         // This goes to STDOUT because it's, uh, temporary
44                         printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
45                         lastDisplayTime = now;
46                         frames = 0;
47                 }
48         }
49 }