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;
17 void initPerformanceMetering() {
18 if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
19 logWarning("Clock read failed, performance metering unavailable");
22 meteringEnabled = true;
26 void frameRendered() {
27 if (meteringEnabled) {
31 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
32 logWarning("Clock read failed, stopping performance metering");
33 meteringEnabled = false;
37 time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
38 if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
40 if (fullSeconds > 0) {
41 float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
42 seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
43 // This goes to STDOUT because it's, uh, temporary
44 printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
45 lastDisplayTime = now;