/*
- * fps-meter - Example program about how to measure frames per seconds
+ * fps-meter - Some functions to measure frames per seconds
*
* Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
*
#include <time.h>
#ifdef DEBUG
-#define dbg(...) \
+#define fps_meter_dbg(...) \
do { \
printf(__VA_ARGS__); \
printf("\n"); \
fflush(stdout); \
} while(0)
#else
-#define dbg(...) do {} while(0)
+#define fps_meter_dbg(...) do {} while(0)
#endif
#define NSEC_PER_SEC 1000000000
-#define timespecsub(a, b, result) \
+#define fps_meter_timespecsub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
struct fps_meter_stats {
struct timespec time_start;
struct timespec time_end;
- struct timespec elapsed;
unsigned int frames;
double nsecs;
memset(stats, 0, sizeof(*stats));
clock_gettime(CLOCK_MONOTONIC, &stats->time_start);
- dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
+ fps_meter_dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
}
static void fps_meter_update(struct fps_meter_stats *stats)
{
- dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
+ struct timespec elapsed;
+
+ fps_meter_dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec);
clock_gettime(CLOCK_MONOTONIC, &stats->time_end);
- dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec);
+ fps_meter_dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec);
- timespecsub(&stats->time_end, &stats->time_start, &stats->elapsed);
- dbg("Elapsed s: %ld ns: %ld", stats->elapsed.tv_sec, stats->elapsed.tv_nsec);
+ fps_meter_timespecsub(&stats->time_end, &stats->time_start, &elapsed);
+ fps_meter_dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
stats->frames++;
- stats->nsecs += (stats->elapsed.tv_sec * NSEC_PER_SEC + stats->elapsed.tv_nsec);
+ stats->nsecs += (elapsed.tv_sec * NSEC_PER_SEC + elapsed.tv_nsec);
if (stats->nsecs >= NSEC_PER_SEC) {
/*
* if we were garanteed that each frame took less than