From: Antonio Ospite Date: Tue, 17 Dec 2013 23:15:44 +0000 (+0100) Subject: Calculate the limit delay once and for all X-Git-Url: https://git.ao2.it/experiments/fps-limit.git/commitdiff_plain/refs/heads/master?ds=sidebyside Calculate the limit delay once and for all --- diff --git a/fps-limit.h b/fps-limit.h index 376fb77..d19df1c 100644 --- a/fps-limit.h +++ b/fps-limit.h @@ -50,7 +50,7 @@ struct fps_limit_stats { struct timespec time_start; struct timespec time_end; - unsigned int fps; + unsigned int delay; }; static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps) @@ -60,7 +60,7 @@ static void fps_limit_init(struct fps_limit_stats *stats, unsigned int fps) clock_gettime(CLOCK_MONOTONIC, &stats->time_start); fps_limit_dbg("Init time: s: %ld, ns: %ld", stats->time_start.tv_sec, stats->time_start.tv_nsec); - stats->fps = fps; + stats->delay = NSEC_PER_SEC / fps; } static void fps_limit_sleep(struct fps_limit_stats *stats) @@ -75,11 +75,11 @@ static void fps_limit_sleep(struct fps_limit_stats *stats) 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) { + if (elapsed.tv_sec == 0 && elapsed.tv_nsec < stats->delay) { struct timespec remaining = {0, 0}; /* remaining delay, _relative_ to time_end */ - remaining.tv_nsec = (NSEC_PER_SEC / stats->fps) - elapsed.tv_nsec; + remaining.tv_nsec = stats->delay - elapsed.tv_nsec; fps_limit_dbg("Sleeping for: ns: %ld", remaining.tv_nsec); clock_nanosleep(CLOCK_MONOTONIC, 0, &remaining, NULL);