aboutsummaryrefslogtreecommitdiffstats
path: root/src/text.h
blob: c27537fdb0fd21c732cb8fa4caf9de02433be9e9 (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#ifndef GBAEXP_TILES_H
#define GBAEXP_TILES_H

#include <stdarg.h>
#include <stdio.h>

#include "common.h"
#include "bitmap.h"
#include "posprintf.h"


typedef enum {
    TXT_MODE_TILED_BG,
    TXT_MODE_HYBRID,
    TXT_MODE_MODE3,
} TextMode;

typedef struct Font {
    // A pointer to an area of memory containing font data.
    // TODO: Should we unpack each char everytime or unpack everything into RAM?
    // Maybe this should be optional?
    u16 *data;
    // The char_map stores the index to the character position within the font
    // array depending on the ascii number we want to render. This allows
    // the usage reduced font sets, for example just uppercase letters and
    // numbers.
    u8 *char_map;
    // Width and height of each font character. Only monospaced fonts are
    // currently supported.
    u8 char_width;
    u8 char_height;
    // The color of this font.
    Color color;
} Font;

typedef struct TextEngine {
    // Currently working on tiled backgrounds only. The X and Y positions
    // correspond to the tile X and Y starting from the top left of the screen.
    // For a 240x160 screen, we have 30x20 tiles available.
    size_t cursor_x;
    size_t cursor_y;
    // Pointer to the memory being used for writing to the screen.
    u16 *memory;
    // TODO: Support other modes and monospaced fonts should be simple but we
    // need to keep track of a couple of other variables, which may not be
    // available for all modes. For example, tile modes can't have a font width
    // smaller than the tile size, although bigger fonts (For example fonts
    // composed of multiple tiles) should still be possible. This may not be
    // a good time investment, but can be done in the future if needed.
    // The mode controls how the text is rendered and how the memory is managed.
    TextMode mode;
    // The font used to render the text.
    Font font;
} TextEngine;

static TextEngine text_engine = {0};

static u8 default_char_map[256] = {0};

void
txt_putc_tile(char c) {
    if (c == '\0') {
        return;
    }
    if (c == '\n') {
        text_engine.cursor_x = 0;
        text_engine.cursor_y++;
    } else {
        text_engine.memory[text_engine.cursor_x + 32 * text_engine.cursor_y] = c;
        text_engine.cursor_x++;
        if (text_engine.cursor_x >= 30) {
            text_engine.cursor_x = 0;
            text_engine.cursor_y++;
        }
    }
    if (text_engine.cursor_y >= 20) {
        text_engine.cursor_y = 0;
    }
}

void
txt_putc_m3(char c) {
    if (c == '\0') {
        return;
    }
    if (c == '\n') {
        text_engine.cursor_x = 0;
        text_engine.cursor_y += text_engine.font.char_height;
    } else {
        u8 idx = text_engine.font.char_map[(int)c] * 2;
        u32 *packed_char = text_engine.font.data;
        packed_char += idx;
        Tile tile = {0};
        unpack_tiles(packed_char, &tile, 1);
        int x = text_engine.cursor_x;
        int y = text_engine.cursor_y;
        for (size_t i = 0; i < text_engine.font.char_height; ++i) {
            for (size_t j = 0; j < text_engine.font.char_width; ++j) {
                if ((tile.row[i] >> 4 * j) & 0x1) {
                    // put_pixel_m4(x + j, y + i, 1, backbuffer);
                    // TODO: Clean this up please.
                    put_pixel_m3(x + j,
                            y + i,
                            text_engine.font.color,
                            FRAMEBUFFER);
                }
            }
        }
        text_engine.cursor_x += text_engine.font.char_width;
        if (text_engine.cursor_x >= SCREEN_WIDTH) {
            text_engine.cursor_x = 0;
            text_engine.cursor_y += text_engine.font.char_height;
        }
    }
    if (text_engine.cursor_y >= SCREEN_HEIGHT) {
        text_engine.cursor_y = 0;
    }
}

void
txt_putc_hybrid(char c) {
    if (c == '\0') {
        return;
    }
    if (c == '\n') {
        text_engine.cursor_x = 0;
        text_engine.cursor_y++;
    } else {
        u8 idx = text_engine.font.char_map[(int)c] * 2;
        u32 *packed_char = text_engine.font.data;
        packed_char += idx;
        Tile tile = {0};
        unpack_tiles(packed_char, &tile, 1);
        int x = text_engine.cursor_x;
        int y = text_engine.cursor_y;
        Tile *buf = text_engine.memory;
        buf[x + y * 32] = tile;
        dirty_tiles[y] |= 1 << x;
        text_engine.cursor_x += 1;
        if (text_engine.cursor_x >= 30) {
            text_engine.cursor_x = 0;
            text_engine.cursor_y++;
        }
    }
    if (text_engine.cursor_y >= 20) {
        text_engine.cursor_y = 0;
    }
}

void
txt_putc(char c) {
    switch (text_engine.mode) {
        case TXT_MODE_TILED_BG: {
            txt_putc_tile(c);
        } break;
        case TXT_MODE_MODE3: {
            txt_putc_m3(c);
        } break;
        case TXT_MODE_HYBRID: {
            txt_putc_hybrid(c);
        } break;
    }
}

static inline void
txt_puts(char *msg) {
    while (*msg) {
        txt_putc(*msg++);
    }
}

void
txt_init_tile(size_t bg, Font font, size_t cb_idx) {
    // The screenblock for the tile map should start after the tile memory
    // (MEM_VRAM + 0x2000 for 256 characters). Since each screenblock is
    // composed of 1024 screenblock entries of u16 (2 bytes), we need an
    // screenblock index offset of 8192 / (1024 * 2) = 4 screen blocks.
    size_t sb_idx = cb_idx * 8 + 4;

    // Set the background parameters for the text layer.
    BG_CTRL(bg) = BG_CHARBLOCK(cb_idx) | BG_SCREENBLOCK(sb_idx) | BG_PRIORITY(3);

    // Load font data in video memory. Each character is unpacked into a tile of
    // 8 32bit values (4bpp), meaning that for the full ASCII set of 256
    // characters, we need 8192 bytes of VRAM (8 * 4 * 256).
    text_engine.font = font;
    unpack_tiles(font.data, &TILE_MEM[cb_idx], 256);

    // Initialize default values.
    if (font.color == 0) {
        font.color = COLOR_WHITE;
    }

    // Load palette color.
    PAL_BUFFER_BG[1] = font.color;

    // Update text_engine variables.
    text_engine.memory = SCREENBLOCK_MEM[sb_idx];
}

void
txt_init_bitmap(TextMode mode, Font font) {
    // If font_map is NULL, initialize the standard 0-255 character map.
    if (font.char_map == NULL) {
        for (size_t i = 0; i < 256; ++i) {
            default_char_map[i] = i;
        }
        font.char_map = &default_char_map;
    }

    // Initialize default values if set to zero.
    if (font.char_width == 0) {
        font.char_width = 8;
    }
    if (font.char_height == 0) {
        font.char_height = 8;
    }
    if (font.color == 0) {
        font.color = COLOR_WHITE;
    }

    // Prepare text engine.
    text_engine.font = font;
    text_engine.mode = mode;
}

void
txt_init_hybrid(TextMode mode, Font font, u32 *buf) {
    // If font_map is NULL, initialize the standard 0-255 character map.
    if (font.char_map == NULL) {
        for (size_t i = 0; i < 256; ++i) {
            default_char_map[i] = i;
        }
        font.char_map = &default_char_map;
    }

    // Initialize default values if set to zero.
    if (font.char_width == 0) {
        font.char_width = 8;
    }
    if (font.char_height == 0) {
        font.char_height = 8;
    }
    if (font.color == 0) {
        font.color = COLOR_WHITE;
    }

    // Prepare text engine.
    text_engine.font = font;
    text_engine.mode = mode;
    text_engine.memory = buf;
}

// Print text to the screen with formatting.

#define txt_printf(msg, ...) \
	{ \
		char buf[256] = {0}; \
		posprintf(buf, msg, ##__VA_ARGS__); \
		txt_puts(buf); \
	}

void
txt_clear_line_tiled(void) {
    for (size_t i = 0; i < 30; ++i) {
        text_engine.memory[i + 32 * text_engine.cursor_y] = ' ';
    }
    text_engine.cursor_x = 0;
}

void
txt_clear_line_bitmap(void) {
    // DEBUG: should be on the text struct.
    Tile *buf = &TILE_MEM[0];
    for (size_t i = 0; i < 30; ++i) {
        buf[i + text_engine.cursor_y * 30] = (Tile){0};
    }
}

void
txt_clear_line(void) {
    switch (text_engine.mode) {
        case TXT_MODE_TILED_BG: {
            txt_clear_line_tiled();
        } break;
        case TXT_MODE_MODE3:
        case TXT_MODE_HYBRID: {
            txt_clear_line_bitmap();
        } break;
    }
}

void
txt_clear_screen(void) {
    for (size_t j = 0; j < 20; ++j) {
        text_engine.cursor_y = j;
        txt_clear_line();
    }
    text_engine.cursor_x = 0;
    text_engine.cursor_y = 0;
}

void
txt_position(size_t tile_x, size_t tile_y) {
    text_engine.cursor_x = tile_x;
    text_engine.cursor_y = tile_y;
}

#endif // GBAEXP_TILES_H