Allow user to have finer control on fire params

Plus numerous fixes
This commit is contained in:
Kiëd Llaentenn 2020-09-17 18:01:40 +00:00
parent d3e4ed96d6
commit 7a417130c9
5 changed files with 111 additions and 51 deletions

11
args.h
View File

@ -3,9 +3,10 @@
// by 20h // by 20h
// //
// This file is was proudly stol^Hborrowed // This file was proudly stol^Hborrowed
// from the st project and is copyright all // from the st project.
// st contributors. View the LICENSE at: //
// (c) all st contributors. View the LICENSE at:
// https://git.suckless.org/st/file/LICENSE.html // https://git.suckless.org/st/file/LICENSE.html
@ -24,6 +25,10 @@ extern char *argv0;
typedef struct Options { typedef struct Options {
size_t refresh_rate; size_t refresh_rate;
size_t max_heat_loss;
size_t wind;
size_t random_factor;
bool random_wind;
bool truecolor; bool truecolor;
} Options; } Options;

View File

@ -2,7 +2,6 @@
#define COLORS_INCLUDED #define COLORS_INCLUDED
#include "termbox.h" #include "termbox.h"
#define CLRS_LEN 13
#ifdef __OpenBSD__ #ifdef __OpenBSD__
#include "sys/types.h" #include "sys/types.h"
@ -10,12 +9,9 @@
#include "stdint.h" #include "stdint.h"
#endif #endif
#define RED 0xdd1111 #define BLACK 0x000000
#define BLACK 0x000000
#define YELLOW 0xff7700
#define WHITE 0xffffff
struct tb_cell normcolors[CLRS_LEN] = struct tb_cell normcolors[13] =
{ {
// default // default
{ ' ', 9, 0 }, { ' ', 9, 0 },
@ -39,28 +35,42 @@ struct tb_cell normcolors[CLRS_LEN] =
{ 0x2588, 8, 2 }, { 0x2588, 8, 2 },
}; };
struct tb_cell truecolors[CLRS_LEN] = struct tb_cell truecolors[36] = {
{ { ' ', BLACK, 0x070707 },
// default { ' ', BLACK, 0x1F0707 },
{ ' ', 9, 0 }, { ' ', BLACK, 0x2F0F07 },
{ ' ', BLACK, 0x470F07 },
// red/black { ' ', BLACK, 0x571707 },
{ 0x2591, RED, BLACK }, { ' ', BLACK, 0x671F07 },
{ 0x2592, RED, BLACK }, { ' ', BLACK, 0x771F07 },
{ 0x2593, RED, BLACK }, { ' ', BLACK, 0x8F2707 },
{ 0x2588, RED, BLACK }, { ' ', BLACK, 0x9F2F07 },
{ ' ', BLACK, 0xAF3F07 },
// yellow/red { ' ', BLACK, 0xBF4707 },
{ 0x2591, YELLOW, RED }, { ' ', BLACK, 0xC74707 },
{ 0x2592, YELLOW, RED }, { ' ', BLACK, 0xDF4F07 },
{ 0x2593, YELLOW, RED }, { ' ', BLACK, 0xDF5707 },
{ 0x2588, YELLOW, RED }, { ' ', BLACK, 0xDF5707 },
{ ' ', BLACK, 0xD75F07 },
// white/red { ' ', BLACK, 0xD7670F },
{ 0x2591, WHITE, RED }, { ' ', BLACK, 0xCF6F0F },
{ 0x2592, WHITE, RED }, { ' ', BLACK, 0xCF770F },
{ 0x2593, WHITE, RED }, { ' ', BLACK, 0xCF7F0F },
{ 0x2588, WHITE, RED }, { ' ', BLACK, 0xCF8717 },
{ ' ', BLACK, 0xC78717 },
{ ' ', BLACK, 0xC78F17 },
{ ' ', BLACK, 0xC7971F },
{ ' ', BLACK, 0xBF9F1F },
{ ' ', BLACK, 0xBF9F1F },
{ ' ', BLACK, 0xBFA727 },
{ ' ', BLACK, 0xBFA727 },
{ ' ', BLACK, 0xBFAF2F },
{ ' ', BLACK, 0xB7AF2F },
{ ' ', BLACK, 0xB7B737 },
{ ' ', BLACK, 0xCFCF6F },
{ ' ', BLACK, 0xDFDF9F },
{ ' ', BLACK, 0xEFEFC7 },
{ ' ', BLACK, 0xFFFFFF }
}; };
#endif #endif

39
draw.c
View File

@ -14,6 +14,9 @@
#include "stdint.h" #include "stdint.h"
#endif #endif
#define MAX(VAL1, VAL2) ((VAL1) > (VAL2) ? (VAL1) : (VAL2))
#define MIN(VAL1, VAL2) ((VAL1) < (VAL2) ? (VAL1) : (VAL2))
// arguments // arguments
extern struct Options *opts; extern struct Options *opts;
@ -46,16 +49,34 @@ void
dofire(struct buffer *buf) dofire(struct buffer *buf)
{ {
size_t src; size_t src;
size_t random; size_t random = (rand() % 7) & 3;
size_t dest; size_t dest;
struct tb_cell *realbuf = tb_cell_buffer(); struct tb_cell *realbuf = tb_cell_buffer();
for (size_t x = 0; x < buf->width; ++x) { for (size_t x = 0; x < buf->width; ++x) {
for (size_t y = 1; y < buf->height; ++y) { for (size_t y = 1; y < buf->height; ++y) {
if ((rand() % opts->random_factor) == 0) {
random = (rand() % 7) & 3;
}
src = y * buf->width + x; src = y * buf->width + x;
random = (rand() % 7) & 3;
dest = src - random + 1; if (opts->random_wind) {
dest = src - random + opts->wind;
} else {
dest = src + opts->wind;
}
size_t max_value;
struct tb_cell *colors;
if (opts->truecolor == TRUE) {
colors = (struct tb_cell*) &truecolors;
max_value = 35;
} else {
colors = (struct tb_cell*) &normcolors;
max_value = 12;
}
if (buf->width > dest) { if (buf->width > dest) {
dest = 0; dest = 0;
@ -63,19 +84,13 @@ dofire(struct buffer *buf)
dest -= buf->width; dest -= buf->width;
} }
buf->buf[dest] = buf->buf[src] - (random & 1); size_t loss = MIN(opts->max_heat_loss, 3);
buf->buf[dest] = MAX(buf->buf[src] - (random & loss), 0);
if (buf->buf[dest] > 12) { if (buf->buf[dest] > max_value) {
buf->buf[dest] = 0; buf->buf[dest] = 0;
} }
struct tb_cell *colors;
if (opts->truecolor == TRUE) {
colors = (struct tb_cell*) &truecolors;
} else {
colors = (struct tb_cell*) &normcolors;
}
realbuf[dest] = colors[buf->buf[dest]]; realbuf[dest] = colors[buf->buf[dest]];
realbuf[src] = colors[buf->buf[src]]; realbuf[src] = colors[buf->buf[src]];
} }

44
main.c
View File

@ -19,9 +19,9 @@ char *argv0;
struct Options *opts; struct Options *opts;
int int
main ( int argc, char *argv[] ) main(int argc, char *argv[])
{ {
opts = (struct Options*) calloc(1, sizeof(struct Options*)); opts = (struct Options*) calloc(1, sizeof(struct Options));
if (opts == NULL) { if (opts == NULL) {
perror("fire: error: calloc()"); perror("fire: error: calloc()");
} }
@ -30,6 +30,10 @@ main ( int argc, char *argv[] )
opts->refresh_rate = 5; opts->refresh_rate = 5;
size_t output_mode = TB_OUTPUT_NORMAL; size_t output_mode = TB_OUTPUT_NORMAL;
opts->truecolor = FALSE; opts->truecolor = FALSE;
opts->max_heat_loss = 1;
opts->wind = 1;
opts->random_wind = TRUE;
opts->random_factor = 4;
// argument parsing // argument parsing
argv0 = argv[0]; argv0 = argv[0];
@ -42,18 +46,44 @@ main ( int argc, char *argv[] )
case 'r': case 'r':
opts->refresh_rate = atoi(ARGF()); opts->refresh_rate = atoi(ARGF());
break; break;
case 'l':
opts->max_heat_loss = atoi(ARGF());
break;
case 'w':
opts->wind = atoi(ARGF());
break;
case 'R':
opts->random_wind = FALSE;
break;
case 'f':
opts->random_factor = atoi(ARGF());
break;
case 'V': case 'V':
printf("%s %s\n", argv0, VERSION); printf("%s %s\n", argv0, VERSION);
return 0; return 0;
case 'h': case 'h':
default: default:
printf("Usage: %s [-tVh] [-r rate]\n", argv0); printf("Usage: %s [-RtVh] [-r speed] [-l loss] [-w wind] [-f fact]\n", argv0);
printf("Display a nice fiery animation.\n\n"); printf("Display a nice fiery animation.\n\n");
printf("ARGUMENTS:\n"); printf("ARGUMENTS:\n");
printf(" -r [rate] Change refresh rate. (default: 5)\n"); printf(" -r [speed] Change refresh rate. (default: 5)\n");
printf(" -t Enable truecolor. (Will not work on *rxvt)\n"); printf(" -l [loss] Maximum heat loss for each row upward. (default: 1)\n");
printf(" -h Display this help message and exit.\n"); printf(" Higher values will lead to a smaller fire.\n");
printf(" -V Display version and exit.\n\n"); printf(" -w [wind] Wind. Negative values, or values less than one will\n");
printf(" cause the fire to be blown west. (default: 1)\n");
printf(" To disable wind, set this value to 0 and use the -R flag.\n");
printf(" -f [fact] Set the chance of the random value being refreshed\n");
printf(" for each tile in the fire animation. (default: 4)\n");
printf(" High values will cause a matrix-like effect.\n");
printf(" -R Disable random wind factor.\n");
printf(" -t Enable truecolor. (Will not work on *rxvt)\n");
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("(c) Kiëd Llaentenn, nullgemm\n"); printf("(c) Kiëd Llaentenn, nullgemm\n");
printf("https://github.com/lptstr/fire\n"); printf("https://github.com/lptstr/fire\n");

View File

@ -11,7 +11,7 @@ WARNING = -Wall -Wextra -pedantic -Wmissing-prototypes \
INC = -Isub/termbox_next/src INC = -Isub/termbox_next/src
CC = gcc CC = gcc
CFLAGS = -std=c99 -O3 $(WARNING) $(INC) CFLAGS = -std=c99 $(WARNING) $(INC) -fsanitize=address -Og -ggdb
LDFLAGS = LDFLAGS =
TRMBOX = sub/termbox_next/bin/termbox.a TRMBOX = sub/termbox_next/bin/termbox.a