From a5c3fe19926b88dda036344ac4da77f482ffb64f Mon Sep 17 00:00:00 2001 From: Kied Llaentenn <32681240+Kiedtl@users.noreply.github.com> Date: Thu, 12 Dec 2019 22:27:13 -0500 Subject: [PATCH] refactor :P --- .gitmodules | 3 ++ args.h | 67 +++++++++++++++++++++++ colors.h | 27 ++++++++++ draw.c | 69 ++++++++++++++++++++++++ draw.h | 16 ++++++ main.c | 129 ++++++--------------------------------------- makefile | 8 +-- outp.h => output.h | 0 sub/ccommon | 1 + type.h | 41 -------------- 10 files changed, 203 insertions(+), 158 deletions(-) create mode 100644 args.h create mode 100644 colors.h create mode 100644 draw.c create mode 100644 draw.h rename outp.h => output.h (100%) create mode 160000 sub/ccommon delete mode 100644 type.h diff --git a/.gitmodules b/.gitmodules index 78cd170..11c8a6c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "sub/termbox_next"] path = sub/termbox_next url = https://github.com/cylgom/termbox_next +[submodule "sub/ccommon"] + path = sub/ccommon + url = https://github.com/lptstr/ccommon diff --git a/args.h b/args.h new file mode 100644 index 0000000..e7091d2 --- /dev/null +++ b/args.h @@ -0,0 +1,67 @@ +// +// Copy me if you can. +// by 20h +// +// Copied by kiedtl +// on 2019-12-11 1516 +// + +// This file is was proudly stol^Hborrowed +// from the st project and is copyright all +// st contributors. View the LICENSE at: +// https://git.suckless.org/st/file/LICENSE.html + + +#ifndef ARGS_INCLUDED +#define ARGS_INCLUDED +#include "bool.h" + +extern char *argv0; + +typedef struct Options { + bool perf_opt1; + bool perf_opt2; + bool perf_opt3; + bool verbose; +} Options; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + int i_;\ + for (i_ = 1, brk_ = 0, argv_ = argv;\ + argv[0][i_] && !brk_;\ + i_++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][i_];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/colors.h b/colors.h new file mode 100644 index 0000000..5c2a5dd --- /dev/null +++ b/colors.h @@ -0,0 +1,27 @@ +// colors.h: defines the colors for the fire. +// feel free to modify to customize the look of the animation. :) +// structure: , , + +#ifndef COLORS_INCLUDED +#define COLORS_INCLUDED + +#include "termbox.h" + +struct tb_cell colors[13] = +{ + { ' ', 9, 0 }, + { 0x2591, 2, 0 }, + { 0x2592, 2, 0 }, + { 0x2593, 2, 0 }, + { 0x2588, 2, 0 }, + { 0x2591, 4, 2 }, + { 0x2592, 4, 2 }, + { 0x2593, 4, 2 }, + { 0x2588, 4, 2 }, + { 0x2591, 8, 4 }, + { 0x2592, 8, 4 }, + { 0x2593, 8, 4 }, + { 0x2588, 8, 4 }, +}; + +#endif diff --git a/draw.c b/draw.c new file mode 100644 index 0000000..352c03b --- /dev/null +++ b/draw.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#include "termbox.h" +#include "types.h" +#include "draw.h" +#include "output.h" +#include "colors.h" + +// initialize the framebuffer +void +init ( struct buffer *buf ) +{ + // initialize width/height of terminal + buf->width = tb_width(); + buf->height = tb_height(); + + usize len = buf->width * buf->height; + buf->buf = (u8*) malloc(len); + 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 +dofire ( struct buffer *buf ) +{ + usize src; + usize random; + usize dest; + + struct tb_cell *realbuf = tb_cell_buffer(); + + for (usize x = 0; x < buf->width; ++x) + { + for (usize y = 1; y < buf->height; ++y) + { + 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; + + realbuf[dest] = colors[buf->buf[dest]]; + realbuf[src] = colors[buf->buf[src]]; + } + } +} diff --git a/draw.h b/draw.h new file mode 100644 index 0000000..968b8d7 --- /dev/null +++ b/draw.h @@ -0,0 +1,16 @@ +#ifndef DRAW_INCLUDED +#define DRAW_INCLUDED +#include "types.h" + +typedef struct buffer +{ + usize width; + usize height; + + u8* buf; +} buffer; + +void init ( struct buffer *buf ); +void dofire ( struct buffer *buf ); + +#endif diff --git a/main.c b/main.c index dfe7bff..0836aa3 100644 --- a/main.c +++ b/main.c @@ -1,27 +1,15 @@ -#include -#include -#include #include -#include "termbox.h" #include "bool.h" -#include "outp.h" -#include "type.h" +#include "output.h" +#include "draw.h" +#include "types.h" +#include "termbox.h" +#include "args.h" -#define STEPS 13 - -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 ); +// argument parsing (args.h) +char *argv0; +struct Options *opts; int main ( void ) @@ -30,114 +18,29 @@ main ( void ) tb_init(); tb_select_output_mode(TB_OUTPUT_NORMAL); tb_clear(); - struct term_buf buf; + struct buffer buf; // initialize drawing - buf.width = tb_width(); - buf.height = tb_height(); init(&buf); // animate while (TRUE) { + // clear the screen tb_clear(); + + // update framebuffer dofire(&buf); + + // draw framebuffer to terminal tb_present(); } // cleanup termbox tb_shutdown(); + + // cleanup framebuffer free(buf.tmp_buf); return 0; } - -void -init ( struct term_buf *buf ) -{ - buf->init_width = buf->width; - buf->init_height = buf->height; - - usize len = buf->width * buf->height; - buf->tmp_buf = (u8*) malloc(len); - len -= buf->width; - - 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 ( struct term_buf *term_buf ) -{ - static struct tb_cell fire[STEPS] = - { - {' ', 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) - { - src = y * w + x; - random = ((rand() % 7) & 3); - dst = src - random + 1; - - if (w > dst) - { - dst = 0; - } - else - { - 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]]; - } - } - -} - - // 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); diff --git a/makefile b/makefile index 3d6b13a..e2de510 100644 --- a/makefile +++ b/makefile @@ -8,15 +8,15 @@ NAME = fire WARNING = -Wall -Wextra -pedantic -Wmissing-prototypes \ -Wold-style-definition -Werror -INC = -Isub/termbox_next/src +INC = -Isub/termbox_next/src -Isub/ccommon/ CC = clang CFLAGS = -g -std=c99 $(WARNING) $(INC) LDFLAGS = -fuse-ld=lld -SRC = main.c -OBJ = $(SRC:.c=.o) TRMBOX = sub/termbox_next/bin/termbox.a +SRC = main.c draw.c +OBJ = $(SRC:.c=.o) all: $(NAME) @@ -29,7 +29,7 @@ clean: $(TRMBOX): @echo "\tCC\t\ttermbox.c" - @cd sub/termbox_next/ && make + @cd sub/termbox_next/ && (make >/dev/null) $(NAME): $(OBJ) $(TRMBOX) @echo "\tLD\t\t$(NAME)" diff --git a/outp.h b/output.h similarity index 100% rename from outp.h rename to output.h diff --git a/sub/ccommon b/sub/ccommon new file mode 160000 index 0000000..4a52d51 --- /dev/null +++ b/sub/ccommon @@ -0,0 +1 @@ +Subproject commit 4a52d5153f63b20691410debdaba619f7923e493 diff --git a/type.h b/type.h deleted file mode 100644 index bf0af29..0000000 --- a/type.h +++ /dev/null @@ -1,41 +0,0 @@ -// -// type.h: defines integer types. -// this file is part of the LPTSTR common C header collection, and -// is licensed under the MIT license. -// (c) Kied Llaentenn and LPTSTR contributors. -// - -// why? -// I miss the Rust type system :P - -#ifndef TYPE_H -#define TYPE_H -#include - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; - -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; - -#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 - -#if INTPTR_MAX == 0xffff -typedef int16_t isize; -#elif INTPTR_MAX == 0xffffffff -typedef int32_t isize; -#elif INTPTR_MAX == 0xffffffffffffffff -typedef int64_t isize; -#endif - -#endif