From 4ed14729d87b5eb16e9562e395727a625f3f8f57 Mon Sep 17 00:00:00 2001 From: Kied Llaentenn <32681240+Kiedtl@users.noreply.github.com> Date: Thu, 12 Dec 2019 18:46:24 -0500 Subject: [PATCH] partial refactor with termbox_next --- .gitmodules | 3 ++ args.h | 63 --------------------------- cols.h | 7 --- main.c | 88 +++++++++++++++++++++----------------- makefile | 15 ++++--- src/bool.h | 16 +++++++ src/main.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++ src/outp.h | 6 +++ src/type.h | 40 +++++++++++++++++ sub/termbox_next | 1 + 10 files changed, 233 insertions(+), 115 deletions(-) create mode 100644 .gitmodules delete mode 100644 args.h delete mode 100644 cols.h create mode 100644 src/bool.h create mode 100644 src/main.c create mode 100644 src/outp.h create mode 100644 src/type.h create mode 160000 sub/termbox_next diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..78cd170 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "sub/termbox_next"] + path = sub/termbox_next + url = https://github.com/cylgom/termbox_next diff --git a/args.h b/args.h deleted file mode 100644 index 025780b..0000000 --- a/args.h +++ /dev/null @@ -1,63 +0,0 @@ -// -// 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 { -//} 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/cols.h b/cols.h deleted file mode 100644 index 8956f00..0000000 --- a/cols.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef COLS_H -#define COLS_H - -static int colors[] = { - - -#endif diff --git a/main.c b/main.c index 4922168..2f39815 100644 --- a/main.c +++ b/main.c @@ -3,58 +3,58 @@ #include #include #include -#include -#define BOOL unsigned char -#define TRUE 1 -#define FALSE 0 -#define PRINT(...) fprintf(stderr, __VA_ARGS__); +#include "termbox.h" +#include "bool.h" +#include "outp.h" +#include "type.h" -// 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; +#define STEPS 13 +void init ( struct term_buf buf ); void dofire ( void ); void draw ( void ); int main ( void ) { - // populate width and height - struct winsize w; - ioctl(0, TIOCGWINSZ, &w); + // initialize termbox + tb_init(); + tb_select_output_mode(TB_OUTPUT_NORMAL); + tb_clear(); - width = w.ws_row; - height = w.ws_col; + struct term_buf buf; + draw_init(&buf); - pixels = (usize*) calloc((width * height), sizeof(usize)); - - // init pixels - for (usize i = 0; i < width; i++) - { - pixels[((width * height) - width) + i] = palette[4]; - } + // initialize drawing + + draw(); dofire(); + + // cleanup termbox + tb_shutdown(); + 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 = malloc(len); + len -= buf->width; + + if (buf->tmp_buf == NULL) + PRINT("fire: error: unable to malloc() memory for buffer\n"); + + memset(buf->tmp_buf, 0, len); + memset(buf->tmp_buf + len, STEPS - 1, buf->width); +} + void dofire ( void ) { @@ -63,14 +63,19 @@ dofire ( void ) for (usize y = 0; y < height; y++) { usize src = y * width + x; - usize random = (rand() % 7) & 3; + usize random = 0;//(rand() % 7) & 3; usize dest = src - random + 1; usize color; if (width > dest ) dest = 0; else dest -= width; - pixels[dest] = pixels[src] - (random & 1); + 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(); } } @@ -83,11 +88,14 @@ draw ( void ) PRINT("%c[2J", esc); // draw pixels onto screen - for (usize x = 0; x < width; x++) + for (usize y = 0; y < height; y++) { - 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); diff --git a/makefile b/makefile index b1fe981..a7914fd 100644 --- a/makefile +++ b/makefile @@ -8,8 +8,13 @@ NAME = fire WARNING = -Wall -Wextra -pedantic -Wmissing-prototypes \ -Wold-style-definition -Werror +SRCDIR = src +SUBDIR = sub + +INC = -I$(SUBDIR)/termbox_next/ + CC = clang -CFLAGS = -g -std=c99 $(WARNING) +CFLAGS = -g -std=c99 $(WARNING) $(INC) LDFLAGS = -fuse-ld=lld SRC = main.c @@ -18,14 +23,14 @@ OBJ = $(SRC:.c=.o) all: $(NAME) clean: - rm -f $(NAME) $(OBJ) + rm -f $(SRCDIR)/$(NAME) $(SRCDIR)/$(OBJ) .c.o: @echo "\tCC\t\t$@" - @$(CC) $(CFLAGS) -c $< + @$(CC) $(CFLAGS) -c $< -o $@ -$(NAME): $(OBJ) +$(NAME): $(SRCDIR)/$(OBJ) @echo "\tLD\t\t$(NAME)" - @$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) + @$(CC) -o $(SRCDIR)/$@ $^ $(CFLAGS) $(LDFLAGS) .PHONY: all clean diff --git a/src/bool.h b/src/bool.h new file mode 100644 index 0000000..9a93430 --- /dev/null +++ b/src/bool.h @@ -0,0 +1,16 @@ +// +// bool.h: defined the boolean type. +// this file is part of the LPTSTR common C header collection, and +// is licensed under the MIT license. +// (c) Kied Llaentenn and LPTSTR contributors. +// + +#ifndef BOOL_INCLUDED +#define BOOL_INCLUDED + +#define TRUE 1 +#define FALSE 0 + +typedef char bool + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..7bb8205 --- /dev/null +++ b/src/main.c @@ -0,0 +1,109 @@ +#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/outp.h b/src/outp.h new file mode 100644 index 0000000..c93af7a --- /dev/null +++ b/src/outp.h @@ -0,0 +1,6 @@ +#ifndef OUTP_H +#define OUTP_H + +#define PRINT(...) fprintf(stderr, __VA_ARGS__); + +#endif diff --git a/src/type.h b/src/type.h new file mode 100644 index 0000000..345cda0 --- /dev/null +++ b/src/type.h @@ -0,0 +1,40 @@ +// +// 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 + +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 diff --git a/sub/termbox_next b/sub/termbox_next new file mode 160000 index 0000000..2312da1 --- /dev/null +++ b/sub/termbox_next @@ -0,0 +1 @@ +Subproject commit 2312da153e44face7bb45aa2798ec284289c17ca