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