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

View File

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

39
draw.c
View File

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

38
main.c
View File

@ -19,9 +19,9 @@ char *argv0;
struct Options *opts;
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) {
perror("fire: error: calloc()");
}
@ -30,6 +30,10 @@ main ( int argc, char *argv[] )
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;
// argument parsing
argv0 = argv[0];
@ -42,18 +46,44 @@ main ( int argc, char *argv[] )
case 'r':
opts->refresh_rate = atoi(ARGF());
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':
printf("%s %s\n", argv0, VERSION);
return 0;
case 'h':
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("ARGUMENTS:\n");
printf(" -r [rate] Change refresh rate. (default: 5)\n");
printf(" -r [speed] Change refresh rate. (default: 5)\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");
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("https://github.com/lptstr/fire\n");

View File

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