]> git.lizzy.rs Git - shadowclad.git/blob - src/performance.c
Overhaul Makefile and directory structure
[shadowclad.git] / src / 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
18
19 void initPerformanceMetering() {
20         if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
21                 logWarning("Clock read failed, performance metering unavailable");
22         }
23         else {
24                 meteringEnabled = true;
25         }
26 }
27
28 void frameRendered() {
29         if (meteringEnabled) {
30                 ++frames;
31                 Timepoint now;
32                 
33                 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
34                         logWarning("Clock read failed, stopping performance metering");
35                         meteringEnabled = false;
36                         return;
37                 }
38                 
39                 time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
40                 if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
41                 
42                 if (fullSeconds > 0) {
43                         float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
44                         seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
45                         // This goes to STDOUT because it's, uh, temporary
46                         printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
47                         lastDisplayTime = now;
48                         frames = 0;
49                 }
50         }
51 }