1 #ifndef _POSIX_C_SOURCE
2 #define _POSIX_C_SOURCE 199309L
6 #include <stdio.h> // TODO remove
11 typedef struct timespec Timepoint;
13 static Timepoint lastDisplayTime;
14 static int frames = 0;
15 static bool meteringEnabled = false;
19 void initPerformanceMetering() {
20 if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
21 logWarning("Clock read failed, performance metering unavailable");
24 meteringEnabled = true;
28 void frameRendered() {
29 if (meteringEnabled) {
33 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
34 logWarning("Clock read failed, stopping performance metering");
35 meteringEnabled = false;
39 time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
40 if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
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;