fire/draw.c

108 lines
2.1 KiB
C
Raw Normal View History

2019-12-12 19:27:13 -08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2019-12-13 13:15:29 -08:00
#include "args.h"
2019-12-12 19:27:13 -08:00
#include "termbox.h"
#include "draw.h"
#include "colors.h"
2019-12-13 13:15:29 -08:00
#include "output.h"
#ifdef __OpenBSD__
#include "sys/types.h"
#else
2020-09-17 04:42:48 -07:00
#include "stdint.h"
#endif
#define MAX(VAL1, VAL2) ((VAL1) > (VAL2) ? (VAL1) : (VAL2))
#define MIN(VAL1, VAL2) ((VAL1) < (VAL2) ? (VAL1) : (VAL2))
2019-12-13 13:15:29 -08:00
// arguments
extern struct Options *opts;
2019-12-12 19:27:13 -08:00
// initialize the framebuffer
void
2020-09-17 05:04:12 -07:00
init(struct buffer *buf)
2019-12-12 19:27:13 -08:00
{
// initialize width/height of terminal
buf->width = tb_width();
buf->height = tb_height();
size_t len = buf->width * buf->height;
2020-09-17 05:06:16 -07:00
buf->buf = (uint8_t*) calloc(len, sizeof(uint8_t));
2019-12-12 19:27:13 -08:00
len -= buf->width;
if (buf->buf == NULL) {
2020-09-17 05:07:24 -07:00
EPRINT("fire: cannot ");
2019-12-12 19:27:13 -08:00
perror("calloc()");
exit(1);
}
2020-09-17 05:07:24 -07:00
// calloc sets the entire screen to black
2019-12-12 19:27:13 -08:00
// ...except for the last row, which is white.
// this is the 'base' of the fire.
2020-09-17 11:40:38 -07:00
memset(buf->buf + len, opts->truecolor ? 34 : 12, buf->width);
2019-12-12 19:27:13 -08:00
}
// update the framebuffer
void
2020-09-17 05:04:12 -07:00
dofire(struct buffer *buf)
2019-12-12 19:27:13 -08:00
{
size_t src;
size_t random = (rand() % 7) & 3;
size_t dest;
2019-12-12 19:27:13 -08:00
struct tb_cell *realbuf = tb_cell_buffer();
2020-09-17 05:10:27 -07:00
for (size_t x = 0; x < buf->width; ++x) {
for (size_t y = 1; y < buf->height; ++y) {
if ((rand() % opts->random_factor) == 0) {
random = (rand() % 7) & 3;
2020-09-17 05:10:27 -07:00
}
2019-12-12 19:27:13 -08:00
src = y * buf->width + x;
2019-12-12 19:27:13 -08:00
if (opts->random_wind) {
dest = src - random + opts->wind;
} else {
dest = src + opts->wind;
2020-09-17 05:10:27 -07:00
}
2019-12-12 19:27:13 -08:00
size_t max_value;
2019-12-13 13:35:13 -08:00
struct tb_cell *colors;
2020-09-17 05:10:27 -07:00
if (opts->truecolor == TRUE) {
2019-12-13 13:35:13 -08:00
colors = (struct tb_cell*) &truecolors;
max_value = 35;
2020-09-17 05:10:27 -07:00
} else {
colors = (struct tb_cell*) &normcolors;
max_value = 12;
}
if (buf->width > dest) {
dest = 0;
} else {
dest -= buf->width;
}
size_t loss = MIN(opts->max_heat_loss, 3);
buf->buf[dest] = MAX(buf->buf[src] - (random & loss), 0);
if (buf->buf[dest] > max_value) {
buf->buf[dest] = 0;
2020-09-17 05:10:27 -07:00
}
2019-12-13 13:35:13 -08:00
realbuf[dest] = colors[buf->buf[dest]];
realbuf[src] = colors[buf->buf[src]];
2019-12-12 19:27:13 -08:00
}
}
}
2019-12-12 20:03:58 -08:00
// free framebuffer and shutdown termbox
void
2020-09-17 05:04:12 -07:00
cleanup(struct buffer *buf)
2019-12-12 20:03:58 -08:00
{
free(buf->buf);
tb_shutdown();
}