-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtb_example.c
More file actions
135 lines (120 loc) · 2.89 KB
/
tb_example.c
File metadata and controls
135 lines (120 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Ensure feature macros are set before pulling in system headers inside
* termbox2.h */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
/* Include termbox2 header. To compile this example you can either
* - link with a system termbox2 library and do: gcc tb_example.c -I.
* -ltermbox2 -o tb_example
* - or build with the header-only implementation by defining TB_IMPL in one
* TU. (If you use header-only, ensure the environment provides the required
* feature macros.)
*/
#include "termbox2.h"
static const char *default_path =
"../b025fe84-7fa2-4ad3-95e6-f3348fc00a1b/frame_images/frame_0001.txt";
int main(int argc, char **argv) {
const char *path = argc > 1 ? argv[1] : default_path;
FILE *f = fopen(path, "r");
if (!f) {
fprintf(stderr, "failed to open '%s'\n", path);
return 1;
}
if (fseek(f, 0, SEEK_END) != 0) {
fclose(f);
return 1;
}
long sz = ftell(f);
if (sz < 0) {
fclose(f);
return 1;
}
rewind(f);
char *buf = malloc((size_t)sz + 1);
if (!buf) {
fclose(f);
return 1;
}
size_t got = fread(buf, 1, (size_t)sz, f);
buf[got] = '\0';
fclose(f);
/* Split into lines in-place */
int capacity = 128;
char **lines = malloc(sizeof(char *) * capacity);
int nlines = 0;
/* Use strtok for simplicity */
char *tok = strtok(buf, "\n");
while (tok) {
if (nlines >= capacity) {
capacity *= 2;
char **tmp = realloc(lines, sizeof(char *) * capacity);
if (!tmp)
break;
lines = tmp;
}
lines[nlines++] = tok;
tok = strtok(NULL, "\n");
}
if (tb_init() != TB_OK) {
fprintf(stderr, "termbox init failed\n");
free(buf);
free(lines);
return 1;
}
tb_clear();
int w = tb_width();
int h = tb_height();
int frame_w = 0;
for (int i = 0; i < nlines; i++) {
int l = (int)strlen(lines[i]);
if (l > frame_w)
frame_w = l;
}
int frame_h = nlines;
int offset_x = (w - frame_w) / 2;
if (offset_x < 0)
offset_x = 0;
int offset_y = (h - frame_h) / 2;
if (offset_y < 0)
offset_y = 0;
for (int y = 0; y < nlines && (offset_y + y) < h; y++) {
tb_print(offset_x, offset_y + y, TB_WHITE, TB_DEFAULT, lines[y]);
}
tb_present();
/* Wait for a key press or resize; exit on any key. */
struct tb_event ev;
while (tb_poll_event(&ev) == 0) {
if (ev.type == TB_EVENT_KEY)
break;
if (ev.type == TB_EVENT_RESIZE) {
/* on resize, simple re-render: recompute size and reprint */
tb_clear();
w = tb_width();
h = tb_height();
offset_x = (w - frame_w) / 2;
if (offset_x < 0)
offset_x = 0;
offset_y = (h - frame_h) / 2;
if (offset_y < 0)
offset_y = 0;
for (int y = 0; y < nlines && (offset_y + y) < h; y++) {
tb_print(offset_x, offset_y + y, TB_WHITE, TB_DEFAULT,
lines[y]);
}
tb_present();
}
}
tb_shutdown();
free(buf);
free(lines);
return 0;
}