refactor :P

This commit is contained in:
Kied Llaentenn 2019-12-12 22:27:13 -05:00
parent 93a8575f08
commit a5c3fe1992
10 changed files with 203 additions and 158 deletions

3
.gitmodules vendored
View File

@ -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

67
args.h Normal file
View File

@ -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

27
colors.h Normal file
View File

@ -0,0 +1,27 @@
// colors.h: defines the colors for the fire.
// feel free to modify to customize the look of the animation. :)
// structure: <unicode hex value>, <foreground>, <background>
#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

69
draw.c Normal file
View File

@ -0,0 +1,69 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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]];
}
}
}

16
draw.h Normal file
View File

@ -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

129
main.c
View File

@ -1,27 +1,15 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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);

View File

@ -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)"

View File

1
sub/ccommon Submodule

@ -0,0 +1 @@
Subproject commit 4a52d5153f63b20691410debdaba619f7923e493

41
type.h
View File

@ -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 <stdint.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