From 85794e888f349997300be24456b9d984e179c127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ki=C3=ABd=20Llaentenn?= Date: Thu, 17 Sep 2020 18:34:19 +0000 Subject: [PATCH] add animation_speed control --- args.h | 1 + draw.c | 2 +- main.c | 43 +++++++++++++++++++++++++++++-------------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/args.h b/args.h index 166fb9a..9752a13 100644 --- a/args.h +++ b/args.h @@ -25,6 +25,7 @@ extern char *argv0; typedef struct Options { size_t refresh_rate; + size_t animation_speed; size_t max_heat_loss; size_t wind; size_t random_factor; diff --git a/draw.c b/draw.c index ad4276e..a639798 100644 --- a/draw.c +++ b/draw.c @@ -41,7 +41,7 @@ init(struct buffer *buf) // calloc sets the entire screen to black // ...except for the last row, which is white. // this is the 'base' of the fire. - memset(buf->buf + len, 12, buf->width); + memset(buf->buf + len, opts->truecolor ? 35 : 12, buf->width); } // update the framebuffer diff --git a/main.c b/main.c index b64d4a1..26ae780 100644 --- a/main.c +++ b/main.c @@ -27,13 +27,14 @@ main(int argc, char *argv[]) } // default args - opts->refresh_rate = 5; - size_t output_mode = TB_OUTPUT_NORMAL; - opts->truecolor = FALSE; - opts->max_heat_loss = 1; - opts->wind = 1; - opts->random_wind = TRUE; - opts->random_factor = 4; + opts->animation_speed = 5; + opts->refresh_rate = 1; + size_t output_mode = TB_OUTPUT_NORMAL; + opts->truecolor = FALSE; + opts->max_heat_loss = 1; + opts->wind = 1; + opts->random_wind = TRUE; + opts->random_factor = 4; // argument parsing argv0 = argv[0]; @@ -43,6 +44,9 @@ main(int argc, char *argv[]) output_mode = TB_OUTPUT_TRUECOLOR; opts->truecolor = TRUE; break; + case 's': + opts->animation_speed = atoi(ARGF()); + break; case 'r': opts->refresh_rate = atoi(ARGF()); break; @@ -63,10 +67,13 @@ main(int argc, char *argv[]) return 0; case 'h': default: - printf("Usage: %s [-RtVh] [-r speed] [-l loss] [-w wind] [-f fact]\n", argv0); + printf("Usage: %s [-RtVh] [-r rate] [-s speed] [-l loss] [-w wind] [-f fact]\n", argv0); printf("Display a nice fiery animation.\n\n"); printf("ARGUMENTS:\n"); - printf(" -r [speed] Change refresh rate. (default: 5)\n"); + printf(" -r [rate] Change refresh rate. (default: 1)\n"); + printf(" Higher values == slower refresh rate.\n"); + printf(" -s [speed] Change animation speed. (default: 5)\n"); + printf(" Higher values == slower animation.\n"); printf(" -l [loss] Maximum heat loss for each row upward. (default: 1)\n"); printf(" Higher values will lead to a smaller fire.\n"); printf(" -w [wind] Wind. Negative values, or values less than one will\n"); @@ -80,10 +87,10 @@ main(int argc, char *argv[]) printf(" -h Display this help message and exit.\n"); printf(" -V Display version and exit.\n\n"); printf("EXAMPLES:\n"); - printf(" %s 'Normal' fire.\n"); - printf(" %s -Rw0 -f100 Cmatrix-esque effect.\n"); - printf(" %s -l2 -w2 Small fire with wind blowing east.\n"); - printf(" %s -Rw0 -f10000000 Heatwaves!\n"); + printf(" %s 'Normal' fire.\n", argv0); + printf(" %s -Rw0 -f100 Cmatrix-esque effect.\n", argv0); + printf(" %s -l2 -w2 Small fire with wind blowing east.\n", argv0); + printf(" %s -Rw0 -f10000000 Heatwaves!\n", argv0); printf("(c) Kiƫd Llaentenn, nullgemm\n"); printf("https://github.com/lptstr/fire\n"); @@ -100,16 +107,24 @@ main(int argc, char *argv[]) // initialize drawing init(&buf); + uint64_t ctr = 0; + // animate while (TRUE) { + ctr += 1; + // update framebuffer dofire(&buf); + if ((ctr % opts->refresh_rate) != 0) { + continue; + } + // draw framebuffer to terminal tb_present(); // event handling - int err = (size_t) tb_peek_event(&e, opts->refresh_rate); + int err = (size_t) tb_peek_event(&e, opts->animation_speed); if (err < 0) { continue;