1 #ifndef _POSIX_C_SOURCE
2 #define _POSIX_C_SOURCE 199309L
5 #include "performance.h"
8 #include <stdio.h> // TODO remove
13 typedef struct timespec Timepoint;
15 static Timepoint lastDisplayTime;
16 static int frames = 0;
17 static bool meteringEnabled = false;
21 void initPerformanceMetering() {
22 if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
23 logWarning("Clock read failed, performance metering unavailable");
26 meteringEnabled = true;
30 void frameRendered() {
31 if (meteringEnabled) {
35 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
36 logWarning("Clock read failed, stopping performance metering");
37 meteringEnabled = false;
41 time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
42 if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
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;