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