mirror of
https://github.com/kiedtl/fire.git
synced 2024-12-22 14:16:43 +00:00
partial refactor with termbox_next
This commit is contained in:
parent
03cc3bc723
commit
4ed14729d8
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "sub/termbox_next"]
|
||||
path = sub/termbox_next
|
||||
url = https://github.com/cylgom/termbox_next
|
63
args.h
63
args.h
@ -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
|
86
main.c
86
main.c
@ -3,58 +3,58 @@
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#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));
|
||||
// initialize drawing
|
||||
|
||||
// init pixels
|
||||
for (usize i = 0; i < width; i++)
|
||||
{
|
||||
pixels[((width * height) - width) + i] = palette[4];
|
||||
}
|
||||
|
||||
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 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);
|
||||
|
15
makefile
15
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
|
||||
|
16
src/bool.h
Normal file
16
src/bool.h
Normal file
@ -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
|
109
src/main.c
Normal file
109
src/main.c
Normal file
@ -0,0 +1,109 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#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");
|
||||
}
|
||||
}
|
6
src/outp.h
Normal file
6
src/outp.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef OUTP_H
|
||||
#define OUTP_H
|
||||
|
||||
#define PRINT(...) fprintf(stderr, __VA_ARGS__);
|
||||
|
||||
#endif
|
40
src/type.h
Normal file
40
src/type.h
Normal file
@ -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
|
1
sub/termbox_next
Submodule
1
sub/termbox_next
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 2312da153e44face7bb45aa2798ec284289c17ca
|
Loading…
Reference in New Issue
Block a user