2019-12-13 03:27:13 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-12-13 21:15:29 +00:00
|
|
|
#include "args.h"
|
2019-12-13 03:27:13 +00:00
|
|
|
#include "termbox.h"
|
|
|
|
#include "draw.h"
|
|
|
|
#include "colors.h"
|
2019-12-13 21:15:29 +00:00
|
|
|
#include "output.h"
|
|
|
|
|
2020-03-09 16:29:38 +00:00
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include "sys/types.h"
|
|
|
|
#else
|
2020-09-17 11:42:48 +00:00
|
|
|
#include "stdint.h"
|
2020-03-09 16:29:38 +00:00
|
|
|
#endif
|
|
|
|
|
2019-12-13 21:15:29 +00:00
|
|
|
// arguments
|
|
|
|
extern struct Options *opts;
|
2019-12-13 03:27:13 +00:00
|
|
|
|
|
|
|
// initialize the framebuffer
|
|
|
|
void
|
2020-09-17 12:04:12 +00:00
|
|
|
init(struct buffer *buf)
|
2019-12-13 03:27:13 +00:00
|
|
|
{
|
|
|
|
// initialize width/height of terminal
|
|
|
|
buf->width = tb_width();
|
|
|
|
buf->height = tb_height();
|
|
|
|
|
2020-03-09 16:29:38 +00:00
|
|
|
size_t len = buf->width * buf->height;
|
|
|
|
buf->buf = (uint8_t*) malloc(len);
|
2019-12-13 03:27:13 +00:00
|
|
|
len -= buf->width;
|
|
|
|
|
|
|
|
if (buf->buf == NULL) {
|
|
|
|
PRINT("fire: cannot ");
|
|
|
|
perror("calloc()");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize the entire screen to black...
|
|
|
|
memset(buf->buf, 0, len);
|
|
|
|
|
|
|
|
// ...except for the last row, which is white.
|
|
|
|
// this is the 'base' of the fire.
|
|
|
|
memset(buf->buf + len, 12, buf->width);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the framebuffer
|
|
|
|
void
|
2020-09-17 12:04:12 +00:00
|
|
|
dofire(struct buffer *buf)
|
2019-12-13 03:27:13 +00:00
|
|
|
{
|
2020-03-09 16:29:38 +00:00
|
|
|
size_t src;
|
|
|
|
size_t random;
|
|
|
|
size_t dest;
|
2019-12-13 03:27:13 +00:00
|
|
|
|
|
|
|
struct tb_cell *realbuf = tb_cell_buffer();
|
|
|
|
|
2020-03-09 16:29:38 +00:00
|
|
|
for (size_t x = 0; x < buf->width; ++x)
|
2019-12-13 03:27:13 +00:00
|
|
|
{
|
2020-03-09 16:29:38 +00:00
|
|
|
for (size_t y = 1; y < buf->height; ++y)
|
2019-12-13 03:27:13 +00:00
|
|
|
{
|
|
|
|
src = y * buf->width + x;
|
|
|
|
random = (rand() % 7) & 3;
|
|
|
|
dest = src - random + 1;
|
|
|
|
|
|
|
|
if (buf->width > dest)
|
|
|
|
dest = 0;
|
|
|
|
else
|
|
|
|
dest -= buf->width;
|
|
|
|
|
|
|
|
buf->buf[dest] = buf->buf[src] - (random & 1);
|
|
|
|
|
|
|
|
if (buf->buf[dest] > 12)
|
|
|
|
buf->buf[dest] = 0;
|
|
|
|
|
2019-12-13 21:35:13 +00:00
|
|
|
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]];
|
2019-12-13 03:27:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 04:03:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
// free framebuffer and shutdown termbox
|
|
|
|
void
|
2020-09-17 12:04:12 +00:00
|
|
|
cleanup(struct buffer *buf)
|
2019-12-13 04:03:58 +00:00
|
|
|
{
|
|
|
|
free(buf->buf);
|
|
|
|
tb_shutdown();
|
|
|
|
}
|