2 #include <stdio.h> // TODO remove
7 typedef struct timespec Timepoint;
9 static Timepoint lastDisplayTime;
10 static int frames = 0;
11 static bool meteringEnabled = false;
13 void initPerformanceMetering() {
14 if (clock_gettime(CLOCK_MONOTONIC, &lastDisplayTime) != 0) {
15 logWarning("Clock read failed, performance metering unavailable");
18 meteringEnabled = true;
22 void frameRendered() {
23 if (meteringEnabled) {
27 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
28 logWarning("Clock read failed, stopping performance metering");
29 meteringEnabled = false;
33 time_t fullSeconds = now.tv_sec - lastDisplayTime.tv_sec;
34 if (now.tv_nsec < lastDisplayTime.tv_nsec) --fullSeconds;
36 if (fullSeconds > 0) {
37 float seconds = (now.tv_nsec - lastDisplayTime.tv_nsec) / 1000000000.0f;
38 seconds += (float) (now.tv_sec - lastDisplayTime.tv_sec);
39 // This goes to STDOUT because it's, uh, temporary
40 printf("frametime avg %.1f ms; fps avg %.f\n", (seconds / frames) * 1000.0f, (frames / seconds));
41 lastDisplayTime = now;