Skip to content

Commit 92a77c7

Browse files
author
Yuichiro Nakada
committed
separete examples
1 parent 7612167 commit 92a77c7

11 files changed

+102
-37
lines changed

aimage.c

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <sys/ioctl.h>
99
#include <time.h>
1010

11+
//#define USE_HALF_BLOCKS
1112
//#include "termbox.h"
1213

1314
#define STB_IMAGE_IMPLEMENTATION
@@ -90,6 +91,13 @@ int main(int argc, char *argv[])
9091
}
9192

9293
#ifdef H_TERMBOX
94+
while (1) {
95+
struct tb_event ev;
96+
int t = tb_peek_event(&ev, 10);
97+
98+
if (t == -1) break;
99+
if (t == TB_EVENT_KEY) break;
100+
}
93101
tb_shutdown();
94102
#endif
95103
return 0;

aimage.h

+10-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ STBIDEF uint8_t *stbi_xload(char const *filename, int *x, int *y, int *frames)
3030
stbi__gif g;
3131
stbi_uc *data = 0;
3232

33+
memset(&g, 0, sizeof(g)); // important!!
3334
*frames = 0;
3435

3536
while ((data = stbi__gif_load_next(&s, &g, &c, 4, 0))) {
@@ -44,7 +45,7 @@ STBIDEF uint8_t *stbi_xload(char const *filename, int *x, int *y, int *frames)
4445
unsigned int size = 4 * g.w * g.h;
4546
uint8_t *pp = realloc(result, (*frames) * (size + 2));
4647
if (!pp) {
47-
printf("err!!\n");
48+
printf("err at realloc!!\n");
4849
return 0;
4950
}
5051
result = pp;
@@ -247,7 +248,7 @@ void simage(uint8_t *str, uint8_t *pix, int width, int height, int rx, int ry, i
247248
}
248249
}
249250

250-
void pimage(uint8_t *pix, int width, int height)
251+
void pimage(uint8_t *pix, int cx, int cy, int width, int height)
251252
{
252253
#ifdef H_TERMBOX
253254
for (int y=0; y<height; y++) {
@@ -256,11 +257,15 @@ void pimage(uint8_t *pix, int width, int height)
256257
/* int col = *pix++;
257258
int c = pal2rgb[col][0]*256*256 +pal2rgb[col][1]*256 +pal2rgb[col][2];
258259
tb_change_cell(x, y, ' ', 0, c);*/
259-
260-
tb_change_cell(x, y, ' ', 0, *pix++); // 256
260+
#ifdef USE_HALF_BLOCKS
261+
tb_pixel(cx+x, cy+y, *pix++); // half-blocks
262+
#else
263+
tb_change_cell(cx+x, cy+y, ' ', 0, *pix++); // 256
264+
#endif
261265
}
262266
}
263267
#else
268+
printf("\033[%d;%dH", cx, cy);
264269
printf("\033[1;1H");
265270
for (int y=0; y<height; y++) {
266271
for (int x=0; x<width; x++) {
@@ -326,7 +331,7 @@ void aviewer(char *name, int sx, int sy)
326331
tb_clear();
327332
#endif
328333
// pimage(screen[i], width/rx, height/ry);
329-
pimage(screen+i*w*h, w, h);
334+
pimage(screen+i*w*h, 0, 0, w, h);
330335
#ifdef H_TERMBOX
331336
tb_present();
332337
#endif

termbox/Makefile

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# ©2017-2022 YUICHIRO NAKADA
2+
3+
PROGRAM = $(patsubst %.c,%,$(wildcard *.c))
4+
5+
ifneq (, $(shell which clang))
6+
CC = clang
7+
endif
8+
ifneq (, $(shell which icc))
9+
CC = icc
10+
endif
11+
CFLAGS = -I../ -Os -ffunction-sections -fdata-sections -funroll-loops -finline-functions -ftree-vectorize
12+
#CFLAGS = -Os -ffunction-sections -fdata-sections -funroll-loops -finline-functions -ftree-vectorize -march=native
13+
LDFLAGS = -Wl,-s -Wl,--gc-sections
14+
#LDFLAGS = -lasound -lm -Wl,-s -Wl,-dead_strip
15+
16+
.PHONY: all
17+
all: depend $(PROGRAM)
18+
19+
%.o : %.c $(HEAD)
20+
$(CC) $(LDFLAGS) $(CFLAGS) -c $(@F:.o=.c) -o $@
21+
22+
.PHONY: clean
23+
clean:
24+
$(RM) $(PROGRAM) $(OBJS) _depend.inc
25+
26+
.PHONY: depend
27+
depend: $(OBJS:.o=.c)
28+
-@ $(RM) _depend.inc
29+
-@ for i in $^; do cpp -MM $$i | sed "s/\ [_a-zA-Z0-9][_a-zA-Z0-9]*\.c//g" >> _depend.inc; done
30+
31+
-include _depend.inc

termbox_fire.c termbox/termbox_fire.c

+43-30
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// gcc termbox_fire.c -o termbox_fire
22
#include "termbox.h"
33

4-
struct term_buf
5-
{
4+
struct term_buf {
65
uint16_t width;
76
uint16_t height;
87
uint16_t init_width;
@@ -18,8 +17,8 @@ struct term_buf
1817
uint8_t* buf;
1918
};
2019

21-
#define DOOM_STEPS 13
22-
static void doom_init(struct term_buf* buf)
20+
#define FIRE_STEPS 13
21+
static void fire_init(struct term_buf* buf)
2322
{
2423
buf->init_width = buf->width;
2524
buf->init_height = buf->height;
@@ -28,21 +27,22 @@ static void doom_init(struct term_buf* buf)
2827
buf->buf = malloc(tmp_len);
2928
tmp_len -= buf->width;
3029

31-
if (buf->buf == NULL) return; //dgn_throw(DGN_ALLOC);
30+
if (buf->buf == NULL) {
31+
return; //dgn_throw(DGN_ALLOC);
32+
}
3233

3334
memset(buf->buf, 0, tmp_len);
34-
memset(buf->buf + tmp_len, DOOM_STEPS - 1, buf->width);
35+
memset(buf->buf + tmp_len, FIRE_STEPS - 1, buf->width);
3536
}
3637

37-
static void doom_free(struct term_buf* buf)
38+
static void fire_free(struct term_buf* buf)
3839
{
3940
free(buf->buf);
4041
}
4142

42-
static void doom(struct term_buf* term_buf)
43+
static void fire(struct term_buf* term_buf)
4344
{
44-
static struct tb_cell fire[DOOM_STEPS] =
45-
{
45+
/* static struct tb_cell fire[FIRE_STEPS] = {
4646
{' ', 9, 0}, // default
4747
{0x2591, 2, 0}, // red
4848
{0x2592, 2, 0}, // red
@@ -56,6 +56,21 @@ static void doom(struct term_buf* term_buf)
5656
{0x2592, 8, 4}, // white
5757
{0x2593, 8, 4}, // white
5858
{0x2588, 8, 4}, // white
59+
};*/
60+
static struct tb_cell fire[FIRE_STEPS] = {
61+
{' ', 16, 0}, // default
62+
{0x2591, 5*6*6, 0}, // red
63+
{0x2592, 5*6*6, 0}, // red
64+
{0x2593, 5*6*6, 0}, // red
65+
{0x2588, 5*6*6, 0}, // red
66+
{0x2591, 5*6*6 +5*6, 5*6*6}, // yellow
67+
{0x2592, 5*6*6 +5*6, 5*6*6}, // yellow
68+
{0x2593, 5*6*6 +5*6, 5*6*6}, // yellow
69+
{0x2588, 5*6*6 +5*6, 5*6*6}, // yellow
70+
{0x2591, 5*6*6 +5*6 +5, 5*6*6 +5*6}, // white
71+
{0x2592, 5*6*6 +5*6 +5, 5*6*6 +5*6}, // white
72+
{0x2593, 5*6*6 +5*6 +5, 5*6*6 +5*6}, // white
73+
{0x2588, 5*6*6 +5*6 +5, 5*6*6 +5*6}, // white
5974
};
6075

6176
uint16_t src;
@@ -65,34 +80,27 @@ static void doom(struct term_buf* term_buf)
6580
uint16_t w = term_buf->init_width;
6681
uint8_t* tmp = term_buf->buf;
6782

68-
if ((term_buf->width != term_buf->init_width) || (term_buf->height != term_buf->init_height))
69-
{
83+
if ((term_buf->width != term_buf->init_width) || (term_buf->height != term_buf->init_height)) {
7084
return;
7185
}
7286

7387
struct tb_cell* buf = tb_cell_buffer();
7488

75-
for (uint16_t x = 0; x < w; ++x)
76-
{
77-
for (uint16_t y = 1; y < term_buf->init_height; ++y)
78-
{
89+
for (uint16_t x = 0; x < w; ++x) {
90+
for (uint16_t y = 1; y < term_buf->init_height; ++y) {
7991
src = y * w + x;
8092
random = ((rand() % 7) & 3);
8193
dst = src - random + 1;
8294

83-
if (w > dst)
84-
{
95+
if (w > dst) {
8596
dst = 0;
86-
}
87-
else
88-
{
97+
} else {
8998
dst -= w;
9099
}
91100

92101
tmp[dst] = tmp[src] - (random & 1);
93102

94-
if (tmp[dst] > 12)
95-
{
103+
if (tmp[dst] > 12) {
96104
tmp[dst] = 0;
97105
}
98106

@@ -105,27 +113,32 @@ static void doom(struct term_buf* term_buf)
105113
int main()
106114
{
107115
tb_init();
108-
tb_select_output_mode(TB_OUTPUT_NORMAL);
116+
// tb_select_output_mode(TB_OUTPUT_NORMAL);
117+
tb_select_output_mode(TB_OUTPUT_256);
109118
tb_clear();
110119

111120
struct term_buf buf;
112121
buf.width = tb_width();
113122
buf.height = tb_height();
114123

115-
doom_init(&buf);
124+
fire_init(&buf);
116125
while (1) {
117126
struct tb_event ev;
118127
int t = tb_peek_event(&ev, 10);
119128

120-
if (t == -1) break;
121-
if (t == TB_EVENT_KEY) break;
129+
if (t == -1) {
130+
break;
131+
}
132+
if (t == TB_EVENT_KEY) {
133+
break;
134+
}
122135

123136
tb_clear();
124-
doom(&buf);
125-
tb_print("Fire demo!", 33, 1, TB_MAGENTA | TB_BOLD, TB_DEFAULT);
137+
fire(&buf);
138+
// tb_print(33, 1, TB_MAGENTA | TB_BOLD, TB_DEFAULT, "Fire demo!");
126139
tb_present();
127140
}
128-
doom_free(&buf);
141+
fire_free(&buf);
129142

130143
tb_shutdown();
131144
return 0;
File renamed without changes.

termbox_matrix.c termbox/termbox_matrix.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ int main()
246246

247247
tb_clear();
248248
matrix(&buf);
249-
tb_print("Matrix demo!", 33, 1, TB_MAGENTA | TB_BOLD, TB_DEFAULT);
249+
tb_print(33, 1, TB_MAGENTA | TB_BOLD, TB_DEFAULT, "Matrix demo!");
250250
tb_present();
251251
}
252252
matrix_free(&buf);
File renamed without changes.
File renamed without changes.

termbox_pixel.c termbox/termbox_pixel.c

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ int main()
2323
tb_pixel(x, y, (x*256*256 + (rand() % 64)*256 + (rand() % 96 - y * 8)));
2424
}
2525
}
26+
27+
struct tb_cell* buf = tb_cell_buffer();
28+
buf += width * height/2;
29+
for (int n=30; n<512; n++) {
30+
buf->ch = n;//0x2580; // unicode
31+
buf->fg = (n*256*256 + (rand() % 64)*256 + (rand() % 96 - n * 8));//256*256*256;
32+
buf++;
33+
}
2634
tb_present();
2735
}
2836

termbox_rain.c termbox/termbox_rain.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ void rain()
166166
}
167167
}
168168

169-
tb_print("Rain demo!", 33, 1, TB_MAGENTA | TB_BOLD, TB_DEFAULT);
169+
tb_print(33, 1, TB_MAGENTA | TB_BOLD, TB_DEFAULT, "Rain demo!");
170170
tb_present();
171171
}
172172
}
File renamed without changes.

0 commit comments

Comments
 (0)