fire/main.c

144 lines
2.4 KiB
C
Raw Normal View History

2019-12-12 22:08:35 +00:00
#include <math.h>
#include <stdio.h>
2019-12-13 02:18:56 +00:00
#include <string.h>
2019-12-12 22:08:35 +00:00
#include <stdlib.h>
2019-12-12 23:46:24 +00:00
#include "termbox.h"
#include "bool.h"
#include "outp.h"
#include "type.h"
2019-12-12 22:08:35 +00:00
2019-12-12 23:46:24 +00:00
#define STEPS 13
2019-12-12 22:08:35 +00:00
2019-12-13 02:18:56 +00:00
typedef struct term_buf
{
usize width;
usize height;
usize init_width;
usize init_height;
u8* tmp_buf;
} term_buf;
void init ( struct term_buf *buf );
void dofire ( struct term_buf *term_buf );
2019-12-12 22:08:35 +00:00
int
main ( void )
{
2019-12-12 23:46:24 +00:00
// initialize termbox
tb_init();
tb_select_output_mode(TB_OUTPUT_NORMAL);
tb_clear();
struct term_buf buf;
2019-12-13 02:18:56 +00:00
// initialize drawing
buf.width = tb_width();
buf.height = tb_height();
init(&buf);
2019-12-12 22:08:35 +00:00
2019-12-13 02:18:56 +00:00
// animate
while (TRUE)
{
tb_clear();
dofire(&buf);
tb_present();
}
2019-12-12 23:46:24 +00:00
// cleanup termbox
tb_shutdown();
2019-12-13 02:18:56 +00:00
free(buf.tmp_buf);
2019-12-12 23:46:24 +00:00
2019-12-12 22:08:35 +00:00
return 0;
}
2019-12-12 23:46:24 +00:00
void
2019-12-13 02:18:56 +00:00
init ( struct term_buf *buf )
2019-12-12 23:46:24 +00:00
{
buf->init_width = buf->width;
buf->init_height = buf->height;
usize len = buf->width * buf->height;
2019-12-13 02:18:56 +00:00
buf->tmp_buf = (u8*) malloc(len);
2019-12-12 23:46:24 +00:00
len -= buf->width;
2019-12-13 02:18:56 +00:00
if (buf->tmp_buf == NULL) {
PRINT("fire: cannot ");
perror("calloc()");
exit(1);
}
2019-12-12 23:46:24 +00:00
memset(buf->tmp_buf, 0, len);
memset(buf->tmp_buf + len, STEPS - 1, buf->width);
}
2019-12-12 22:08:35 +00:00
void
2019-12-13 02:18:56 +00:00
dofire ( struct term_buf *term_buf )
2019-12-12 22:08:35 +00:00
{
2019-12-13 02:18:56 +00:00
static struct tb_cell fire[STEPS] =
2019-12-12 22:08:35 +00:00
{
2019-12-13 02:18:56 +00:00
{' ', 9, 0}, // default
{0x2591, 2, 0}, // red
{0x2592, 2, 0}, // red
{0x2593, 2, 0}, // red
{0x2588, 2, 0}, // red
{0x2591, 4, 2}, // yellow
{0x2592, 4, 2}, // yellow
{0x2593, 4, 2}, // yellow
{0x2588, 4, 2}, // yellow
{0x2591, 8, 4}, // white
{0x2592, 8, 4}, // white
{0x2593, 8, 4}, // white
{0x2588, 8, 4}, // white
};
usize src;
usize random;
usize dst;
usize w = term_buf->init_width;
u8* tmp = term_buf->tmp_buf;
if ((term_buf->width != term_buf->init_width) || (term_buf->height != term_buf->init_height))
{
return;
2019-12-12 22:08:35 +00:00
}
2019-12-13 02:18:56 +00:00
struct tb_cell* buf = tb_cell_buffer();
2019-12-12 22:08:35 +00:00
2019-12-13 02:18:56 +00:00
for (usize x = 0; x < w; ++x)
2019-12-12 22:08:35 +00:00
{
2019-12-13 02:18:56 +00:00
for (usize y = 1; y < term_buf->init_height; ++y)
2019-12-12 22:08:35 +00:00
{
2019-12-13 02:18:56 +00:00
src = y * w + x;
random = ((rand() % 7) & 3);
dst = src - random + 1;
if (w > dst)
{
dst = 0;
}
else
{
dst -= w;
}
2019-12-12 23:46:24 +00:00
2019-12-13 02:18:56 +00:00
tmp[dst] = tmp[src] - (random & 1);
if (tmp[dst] > 12)
{
tmp[dst] = 0;
}
2019-12-12 22:08:35 +00:00
2019-12-13 02:18:56 +00:00
buf[dst] = fire[tmp[dst]];
buf[src] = fire[tmp[src]];
2019-12-12 22:08:35 +00:00
}
}
2019-12-13 02:18:56 +00:00
2019-12-12 22:08:35 +00:00
}
2019-12-13 02:18:56 +00:00
// extract RGB value from color
//u8 blue = color % 256;
//u8 green = ((color - blue) / 256) % 256;
//u8 red = ((color - blue) / (int)pow((double)256, (double)2))-(green / 256);