diff options
author | Bad Diode <bd@badd10de.dev> | 2021-06-03 11:24:31 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-06-03 11:24:31 +0200 |
commit | 0eda19bd2bb551bf9186b0fbe1a806a28d5a3597 (patch) | |
tree | 859aad06f65f2502a13d26c372c4978147fa1864 /src/text/text.h | |
parent | 6b68cbd8a25dc2e554cf6741ff61ef87c030d841 (diff) | |
download | stepper-0eda19bd2bb551bf9186b0fbe1a806a28d5a3597.tar.gz stepper-0eda19bd2bb551bf9186b0fbe1a806a28d5a3597.zip |
Add initial text background drawing functions
Diffstat (limited to 'src/text/text.h')
-rw-r--r-- | src/text/text.h | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/text/text.h b/src/text/text.h new file mode 100644 index 0000000..65003c5 --- /dev/null +++ b/src/text/text.h | |||
@@ -0,0 +1,114 @@ | |||
1 | #ifndef TEXT_H | ||
2 | #define TEXT_H | ||
3 | |||
4 | #include "bd-font.c" | ||
5 | #include "shorthand.h" | ||
6 | #include "posprintf.h" | ||
7 | |||
8 | typedef struct TextEngine { | ||
9 | // Cursor for tiled text mode The X and Y positions correspond to the tile | ||
10 | // X and Y starting from the top left of the screen. For a 240x160 screen, | ||
11 | // we have 30x20 tiles available. | ||
12 | size_t cursor_x; | ||
13 | size_t cursor_y; | ||
14 | |||
15 | // Memory location of font tile data and tilemap. Likely located on the | ||
16 | // VRAM. | ||
17 | u32 *font_data; | ||
18 | u16 *font_tilemap; | ||
19 | |||
20 | // The font map for tiled text. Writing the character stored in this | ||
21 | // position on the tilemap will show a character on the screen. | ||
22 | u16 font_map[256]; | ||
23 | } TextEngine; | ||
24 | |||
25 | static TextEngine text_engine = {0}; | ||
26 | |||
27 | static inline | ||
28 | void | ||
29 | txt_init(u32 *font_data, u16 *font_tilemap, u16 font_offset) { | ||
30 | // Load font data into VRAM. | ||
31 | unpack_tiles(&bd_font, font_data, 256); | ||
32 | |||
33 | // Initialize the font map translation table. That way we can write | ||
34 | // a character on the text background layer with: | ||
35 | // FONT_TILEMAP[tile_x + 32 * tile_y] = font_map['A']; | ||
36 | for (size_t i = 0; i < 256; ++i) { | ||
37 | text_engine.font_map[i] = font_offset + i; | ||
38 | } | ||
39 | |||
40 | // Initialize remaining variables. | ||
41 | text_engine.font_data = font_data; | ||
42 | text_engine.font_tilemap = font_tilemap; | ||
43 | } | ||
44 | |||
45 | static inline | ||
46 | void | ||
47 | txt_putc(char c) { | ||
48 | if (c == '\0') { | ||
49 | return; | ||
50 | } | ||
51 | if (c == '\n') { | ||
52 | text_engine.cursor_x = 0; | ||
53 | text_engine.cursor_y++; | ||
54 | } else { | ||
55 | int x = text_engine.cursor_x; | ||
56 | int y = text_engine.cursor_y; | ||
57 | text_engine.font_tilemap[x + 32 * y] = text_engine.font_map[(u16)c]; | ||
58 | text_engine.cursor_x += 1; | ||
59 | if (text_engine.cursor_x >= 30) { | ||
60 | text_engine.cursor_x = 0; | ||
61 | text_engine.cursor_y++; | ||
62 | } | ||
63 | } | ||
64 | if (text_engine.cursor_y >= 20) { | ||
65 | text_engine.cursor_y = 0; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | static inline | ||
70 | void | ||
71 | txt_puts(char *msg) { | ||
72 | while (*msg) { | ||
73 | txt_putc(*msg++); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | static inline | ||
78 | void | ||
79 | txt_clear_line(void) { | ||
80 | for (size_t i = 0; i < 30; ++i) { | ||
81 | int x = text_engine.cursor_x; | ||
82 | int y = text_engine.cursor_y; | ||
83 | text_engine.font_tilemap[x + 32 * y] = text_engine.font_map[0]; | ||
84 | } | ||
85 | text_engine.cursor_x = 0; | ||
86 | } | ||
87 | |||
88 | static inline | ||
89 | void | ||
90 | txt_clear_screen(void) { | ||
91 | for (size_t j = 0; j < 20; ++j) { | ||
92 | text_engine.cursor_y = j; | ||
93 | txt_clear_line(); | ||
94 | } | ||
95 | text_engine.cursor_x = 0; | ||
96 | text_engine.cursor_y = 0; | ||
97 | } | ||
98 | |||
99 | static inline | ||
100 | void | ||
101 | txt_position(size_t tile_x, size_t tile_y) { | ||
102 | text_engine.cursor_x = tile_x; | ||
103 | text_engine.cursor_y = tile_y; | ||
104 | } | ||
105 | |||
106 | // Print text to the screen with formatting. | ||
107 | #define txt_printf(msg, ...) \ | ||
108 | { \ | ||
109 | char buf[256] = {0}; \ | ||
110 | posprintf(buf, msg, ##__VA_ARGS__); \ | ||
111 | txt_puts(buf); \ | ||
112 | } | ||
113 | |||
114 | #endif // TEXT_H | ||