2 #include <stdio.h> // TODO remove
5 typedef struct timespec Timepoint;
7 static Timepoint lastDisplayTime;
9 static bool meteringEnabled = false;
11 void initPerformanceMetering() {
12 if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
13 fprintf(stderr, "Clock read failed, performance metering unavailable\n");
16 meteringEnabled = true;
20 void frameRendered() {
21 if (meteringEnabled) {
25 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
26 fprintf(stderr, "Clock read failed, stopping performance metering\n");
27 meteringEnabled = false;
31 time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
32 if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
34 if (fullSeconds > 0) {
35 float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
36 seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
37 printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
38 lastDisplayTime = now;