From 93a8575f087afd1d908c9326d0fc6be3144f6430 Mon Sep 17 00:00:00 2001 From: Kied Llaentenn <32681240+Kiedtl@users.noreply.github.com> Date: Thu, 12 Dec 2019 21:18:56 -0500 Subject: [PATCH] it's working! :) --- src/bool.h => bool.h | 2 +- main.c | 146 +++++++++++++++++++++++++++---------------- makefile | 11 ++-- src/outp.h => outp.h | 0 src/main.c | 109 -------------------------------- src/type.h => type.h | 1 + 6 files changed, 100 insertions(+), 169 deletions(-) rename src/bool.h => bool.h (93%) rename src/outp.h => outp.h (100%) delete mode 100644 src/main.c rename src/type.h => type.h (97%) diff --git a/src/bool.h b/bool.h similarity index 93% rename from src/bool.h rename to bool.h index 9a93430..026cd79 100644 --- a/src/bool.h +++ b/bool.h @@ -11,6 +11,6 @@ #define TRUE 1 #define FALSE 0 -typedef char bool +typedef char bool; #endif diff --git a/main.c b/main.c index 2f39815..dfe7bff 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,6 @@ #include #include -#include -#include +#include #include #include "termbox.h" @@ -11,9 +10,18 @@ #define STEPS 13 -void init ( struct term_buf buf ); -void dofire ( void ); -void draw ( void ); +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 ); int main ( void ) @@ -22,86 +30,114 @@ main ( void ) tb_init(); tb_select_output_mode(TB_OUTPUT_NORMAL); tb_clear(); - struct term_buf buf; - draw_init(&buf); - - // initialize drawing + // initialize drawing + buf.width = tb_width(); + buf.height = tb_height(); + init(&buf); - draw(); - dofire(); + // animate + while (TRUE) + { + tb_clear(); + dofire(&buf); + tb_present(); + } // cleanup termbox tb_shutdown(); + free(buf.tmp_buf); return 0; } void -init ( struct term_buf buf ) +init ( struct term_buf *buf ) { buf->init_width = buf->width; buf->init_height = buf->height; usize len = buf->width * buf->height; - buf->tmp_buf = malloc(len); + buf->tmp_buf = (u8*) malloc(len); len -= buf->width; - if (buf->tmp_buf == NULL) - PRINT("fire: error: unable to malloc() memory for buffer\n"); - + if (buf->tmp_buf == NULL) { + PRINT("fire: cannot "); + perror("calloc()"); + exit(1); + } + memset(buf->tmp_buf, 0, len); memset(buf->tmp_buf + len, STEPS - 1, buf->width); } void -dofire ( void ) +dofire ( struct term_buf *term_buf ) { - for (usize x = 0; x < width; x++) + static struct tb_cell fire[STEPS] = { - for (usize y = 0; y < height; y++) + {' ', 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; + } + + struct tb_cell* buf = tb_cell_buffer(); + + for (usize x = 0; x < w; ++x) + { + for (usize y = 1; y < term_buf->init_height; ++y) { - usize src = y * width + x; - usize random = 0;//(rand() % 7) & 3; - usize dest = src - random + 1; - usize color; + src = y * w + x; + random = ((rand() % 7) & 3); + dst = src - random + 1; - if (width > dest ) dest = 0; - else dest -= width; - - color = pixels[src]; - - if (color == 0 || color == (usize) NULL) - pixels[(src - width) % ((width * height) - 1)] = palette[0]; + if (w > dst) + { + dst = 0; + } else - pixels[(dest - width) % ((width * height) - 1)] = pixels[src] - (random & 1); - draw(); + { + dst -= w; + } + + tmp[dst] = tmp[src] - (random & 1); + + if (tmp[dst] > 12) + { + tmp[dst] = 0; + } + + buf[dst] = fire[tmp[dst]]; + buf[src] = fire[tmp[src]]; } } + } -void -draw ( void ) -{ - // clear screen - PRINT("%c[2J", esc); - - // draw pixels onto screen - for (usize y = 0; y < height; y++) - { - for (usize x = 0; x < width; x++) - { - usize color = pixels[y * width + x]; - if (color == (usize) NULL) continue; - // 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); - - PRINT("%c[48;2;%i;%i;%im %c[0m", esc, red, blue, green, esc); - } - PRINT("\n"); - } -} + //u8 blue = color % 256; + //u8 green = ((color - blue) / 256) % 256; + //u8 red = ((color - blue) / (int)pow((double)256, (double)2))-(green / 256); diff --git a/makefile b/makefile index e119d2d..3d6b13a 100644 --- a/makefile +++ b/makefile @@ -8,9 +8,7 @@ NAME = fire WARNING = -Wall -Wextra -pedantic -Wmissing-prototypes \ -Wold-style-definition -Werror -SUBDIR = sub - -INC = -I$(SUBDIR)/termbox_next/ +INC = -Isub/termbox_next/src CC = clang CFLAGS = -g -std=c99 $(WARNING) $(INC) @@ -18,6 +16,7 @@ LDFLAGS = -fuse-ld=lld SRC = main.c OBJ = $(SRC:.c=.o) +TRMBOX = sub/termbox_next/bin/termbox.a all: $(NAME) @@ -28,7 +27,11 @@ clean: @echo "\tCC\t\t$@" @$(CC) $(CFLAGS) -c $< -$(NAME): $(OBJ) +$(TRMBOX): + @echo "\tCC\t\ttermbox.c" + @cd sub/termbox_next/ && make + +$(NAME): $(OBJ) $(TRMBOX) @echo "\tLD\t\t$(NAME)" @$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) diff --git a/src/outp.h b/outp.h similarity index 100% rename from src/outp.h rename to outp.h diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 7bb8205..0000000 --- a/src/main.c +++ /dev/null @@ -1,109 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define BOOL unsigned char -#define TRUE 1 -#define FALSE 0 -#define PRINT(...) fprintf(stderr, __VA_ARGS__); - -// I miss the Rust type system :( -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; - -#if UINTPTR_MAX == 0xffff -typedef uint16_t usize; -#elif UINTPTR_MAX == 0xffffffff -typedef uint32_t usize; -#elif UINTPTR_MAX == 0xffffffffffffffff -typedef uint64_t usize; -#endif - - -char esc = (char) 27; -usize width; -usize height; -usize palette[] = { 0x00, 0xff0000, 0xff6611, 0xffff66, 0xffffff }; -usize *pixels; - -void dofire ( void ); -void draw ( void ); - -int -main ( void ) -{ - // populate width and height - struct winsize w; - ioctl(0, TIOCGWINSZ, &w); - - width = w.ws_col; - height = w.ws_row; - - pixels = (usize*) calloc((width * height), sizeof(usize)); - - // init pixels - for (usize c = 0; c < (width * height); c++) pixels[c] = (usize) NULL; - for (usize i = 0; i < width; i++) - { - pixels[((width * (height - 1)) - width) + i] = palette[4]; - } - - draw(); - dofire(); - return 0; -} - -void -dofire ( void ) -{ - for (usize x = 0; x < width; x++) - { - for (usize y = 0; y < height; y++) - { - usize src = y * width + x; - usize random = 0;//(rand() % 7) & 3; - usize dest = src - random + 1; - usize color; - - if (width > dest ) dest = 0; - else dest -= width; - - color = pixels[src]; - - if (color == 0 || color == (usize) NULL) - pixels[(src - width) % ((width * height) - 1)] = palette[0]; - else - pixels[(dest - width) % ((width * height) - 1)] = pixels[src] - (random & 1); - draw(); - } - } -} - -void -draw ( void ) -{ - // clear screen - PRINT("%c[2J", esc); - - // draw pixels onto screen - for (usize y = 0; y < height; y++) - { - for (usize x = 0; x < width; x++) - { - usize color = pixels[y * width + x]; - if (color == (usize) NULL) continue; - - // 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); - - PRINT("%c[48;2;%i;%i;%im %c[0m", esc, red, blue, green, esc); - } - PRINT("\n"); - } -} diff --git a/src/type.h b/type.h similarity index 97% rename from src/type.h rename to type.h index 345cda0..bf0af29 100644 --- a/src/type.h +++ b/type.h @@ -10,6 +10,7 @@ #ifndef TYPE_H #define TYPE_H +#include typedef uint8_t u8; typedef uint16_t u16;