Newer
Older
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "textbuffer.h"
#include "gfx.h"
#include <string.h>
void txt_init(struct txt_buffer *txtb, struct gfx_region *reg, sFONT *f)
{
txtb->reg = reg;
txtb->font = f;
txtb->cursor_column = 0;
txtb->cursor_row = 0;
txtb->fg_color = gfx_color_rgb_f(reg, 1.0f, 1.0f, 1.0f);
txtb->bg_color = gfx_color_rgb_f(reg, .0f, .0f, .0f);
txtb->draw_cursor = 1;
txtb->auto_update = 1;
}
static inline size_t width_(struct txt_buffer *tm)
{
return tm->reg->width / tm->font->Width;
}
static inline size_t height_(struct txt_buffer *tm)
{
return tm->reg->height / tm->font->Height;
}
size_t txt_width(struct txt_buffer *tm)
{
return width_(tm);
}
size_t txt_height(struct txt_buffer *tm)
{
return height_(tm);
}
static void scrollup(struct txt_buffer *tm)
{
const int last_row = height_(tm) - 1;
const size_t line_size = width_(tm) * sizeof(struct txt_glyph);
for (int row = 0; row < last_row; row++)
memcpy(&tm->text[row][0], &tm->text[row + 1][0], line_size);
for (int col = 0; col < width_(tm); col++) {
struct txt_glyph *g = &tm->text[last_row][col];
g->ch = ' ';
g->fg_color = tm->fg_color;
g->bg_color = tm->bg_color;
}
}
static void newline(struct txt_buffer *tm)
{
const int last_row = height_(tm) - 1;
scrollup(tm);
tm->cursor_row = last_row;
tm->cursor_column = 0;
}
static inline void advance_cursor(struct txt_buffer *tm)
{
const int last_row = height_(tm) - 1;
tm->cursor_column++;
if (tm->cursor_column >= width_(tm)) {
tm->cursor_column = 0;
tm->cursor_row++;
if (tm->cursor_row > last_row)
newline(tm);
}
}
static void tab(struct txt_buffer *tm)
{
while (tm->cursor_column % 5)
txt_putchar(tm, ' ');
}
static inline struct txt_glyph *getchar_(struct txt_buffer *tm, int x, int y)
{
if (x < 0 || x >= TEXTBUFFER_MAX_WIDTH)
return NULL;
if (y < 0 || y >= TEXTBUFFER_MAX_HEIGHT)
return NULL;
return &tm->text[y][x];
}
void txt_clear(struct txt_buffer *tm)
{
int w = width_(tm);
int h = height_(tm);
for (int r = 0; r < h; r++) {
for (int c = 0; c < w; c++) {
struct txt_glyph *g = getchar_(tm, c, r);
if (g == NULL)
continue;
g->ch = ' ';
g->fg_color = tm->fg_color;
g->bg_color = tm->bg_color;
}
}
tm->cursor_column = 0;
tm->cursor_row = 0;
if (tm->auto_update)
txt_update(tm);
}
void txt_putchar(struct txt_buffer *tm, char ch)
{
struct txt_glyph *g = getchar_(tm, tm->cursor_column, tm->cursor_row);
if (g == NULL)
return;
switch (ch) {
case '\n':
newline(tm);
break;
case '\t':
tab(tm);
break;
default:
g->ch = ch;
g->fg_color = tm->fg_color;
g->bg_color = tm->bg_color;
advance_cursor(tm);
}
if (tm->auto_update)
txt_update(tm);
}
void txt_puts(struct txt_buffer *tm, const char *str)
{
while (*str) {
txt_putchar(tm, *str);
str++;
}
}
static inline int cursor_px_column(struct txt_buffer *tm)
{
return tm->font->Width * tm->cursor_column;
}
static inline int cursor_px_row(struct txt_buffer *tm)
{
return tm->font->Height * tm->cursor_row;
}
static void draw_cursor_(struct txt_buffer *tm)
{
const int x = cursor_px_column(tm);
const int y = cursor_px_row(tm);
const int w = tm->font->Width;
const int h = tm->font->Height;
gfx_rectangle_fill(tm->reg, x, y, w, h, tm->fg_color);
}
void txt_draw(struct txt_buffer *tm)
{
const int w = width_(tm);
const int h = height_(tm);
int px_column = 0;
int px_row = 0;
for (int r = 0; r < h; r++) {
px_column = 0;
for (int c = 0; c < w; c++) {
struct txt_glyph *g = getchar_(tm, c, r);
if (g == NULL)
continue;
gfx_putchar(
tm->font,
tm->reg,
px_column,
px_row,
g->ch,
g->fg_color,
g->bg_color
);
px_column += tm->font->Width;
}
px_row += tm->font->Height;
}
if (tm->draw_cursor)
draw_cursor_(tm);
}
void txt_set_color_f(
struct txt_buffer *tm, enum txt_color sw, float r, float g, float b
) {
Color c = gfx_color_rgb_f(tm->reg, r, g, b);
switch (c) {
case TEXT_FOREGROUND:
tm->fg_color = c;
break;
case TEXT_BACKGROUND:
tm->bg_color = c;
break;
}
}
void txt_set_color(struct txt_buffer *tm, enum txt_color sw, int r, int g, int b)
{
Color c = gfx_color_rgb(tm->reg, r, g, b);
switch (c) {
case TEXT_FOREGROUND:
tm->fg_color = c;
break;
case TEXT_BACKGROUND:
tm->bg_color = c;
break;
}
}
void txt_set_cursor(struct txt_buffer *tm, int x, int y, int draw_cursor)
{
tm->draw_cursor = draw_cursor;
if (x < 0 || x >= width_(tm))
return;
if (y < 0 || y >= height_(tm))
return;
tm->cursor_column = x;
tm->cursor_row = y;
if (tm->auto_update)
txt_update(tm);
}
void txt_set_transparent(struct txt_buffer *tm)
{
tm->bg_color = tm->fg_color;
}
void txt_update(struct txt_buffer *tm)
{
gfx_update(tm->reg);
}