From: Antonio Ospite Date: Tue, 17 Dec 2013 23:05:29 +0000 (+0100) Subject: Add a fps_limit prefix to macros X-Git-Url: https://git.ao2.it/experiments/fps-limit.git/commitdiff_plain/5d2f835b14ca786dc7c390aecb2e6c4b604c8be4?ds=sidebyside Add a fps_limit prefix to macros --- diff --git a/fps-limit.c b/fps-limit.c index 101f126..bccb8a5 100644 --- a/fps-limit.c +++ b/fps-limit.c @@ -28,7 +28,7 @@ int main(void) fps_limit_init(&stats, FPS); while (1) { - dbg("Doing some work, max: %d", NSEC_PER_SEC / FPS); + fps_limit_dbg("Doing some work, max: %d", NSEC_PER_SEC / FPS); tmp.tv_sec = 0; tmp.tv_nsec = (NSEC_PER_SEC / FPS) * 2 / 3; nanosleep(&tmp, NULL); diff --git a/fps-limit.h b/fps-limit.h index 101cf1d..376fb77 100644 --- a/fps-limit.h +++ b/fps-limit.h @@ -25,19 +25,19 @@ #include #ifdef DEBUG -#define dbg(...) \ +#define fps_limit_dbg(...) \ do { \ printf(__VA_ARGS__); \ printf("\n"); \ fflush(stdout); \ } while(0) #else -#define dbg(...) do {} while(0) +#define fps_limit_dbg(...) do {} while(0) #endif #define NSEC_PER_SEC 1000000000 -#define timespecsub(a, b, result) \ +#define fps_limit_timespecsub(a, b, result) \ do { \ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \ @@ -58,7 +58,7 @@ static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps) 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_limit_dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec); stats->fps = fps; } @@ -67,20 +67,20 @@ static void fps_limit_sleep(struct fps_limit_stats *stats) { struct timespec elapsed; - dbg("Start time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec); + fps_limit_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_limit_dbg("End time: s: %ld, ns: %ld", stats->time_end.tv_sec, stats->time_end.tv_nsec); - timespecsub(&stats->time_end, &stats->time_start, &elapsed); - dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec); + fps_limit_timespecsub(&stats->time_end, &stats->time_start, &elapsed); + fps_limit_dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec); if (elapsed.tv_sec == 0 && elapsed.tv_nsec < NSEC_PER_SEC / stats->fps) { struct timespec remaining = {0, 0}; /* remaining delay, _relative_ to time_end */ remaining.tv_nsec = (NSEC_PER_SEC / stats->fps) - elapsed.tv_nsec; - dbg("Sleeping for: ns: %ld", remaining.tv_nsec); + fps_limit_dbg("Sleeping for: ns: %ld", remaining.tv_nsec); clock_nanosleep(CLOCK_MONOTONIC, 0, &remaining, NULL); }