aboutsummaryrefslogtreecommitdiffstats
path: root/src/text
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2023-04-22 21:12:14 +0200
committerBad Diode <bd@badd10de.dev>2023-04-22 21:12:14 +0200
commitdeb9c48fbd3dc5854de4ae3a04dc999029c10ae0 (patch)
tree53d34672d676c5f9b9b56b2b185c511470c068a0 /src/text
parentd4fe4d95f105d8b9b47d26264c4876cbf4095a5d (diff)
downloadstepper-deb9c48fbd3dc5854de4ae3a04dc999029c10ae0.tar.gz
stepper-deb9c48fbd3dc5854de4ae3a04dc999029c10ae0.zip
Add new renderer and prepare for render overhaul
Diffstat (limited to 'src/text')
-rw-r--r--src/text/font.h131
-rw-r--r--src/text/text.h115
2 files changed, 179 insertions, 67 deletions
diff --git a/src/text/font.h b/src/text/font.h
index daf0c24..e4b089c 100644
--- a/src/text/font.h
+++ b/src/text/font.h
@@ -128,3 +128,134 @@ static const u32 font[] = {
128 0x00000000, 0x00000000, 0x00000000, 0x00000000, 128 0x00000000, 0x00000000, 0x00000000, 0x00000000,
129 0x00000000, 0x00000000, 0x00000000, 0x00000000, 129 0x00000000, 0x00000000, 0x00000000, 0x00000000,
130}; 130};
131
132u32 font_icn[] = {
133 0x00000000, 0x00000000, 0x00240000, 0x00003c42,
134 0x00240000, 0x0000423c, 0x3e360000, 0x00081c3e,
135 0x3e1c0800, 0x00081c3e, 0x3e1c1c00, 0x1c083e36,
136 0x3e1c0800, 0x1c083e3e, 0x18000000, 0x00000018,
137 0x667e7e7e, 0x7e7e7e66, 0x24180000, 0x00001824,
138 0x5a667e7e, 0x7e7e665a, 0x3e1c0800, 0x1c221c08,
139 0x1c221c00, 0x083e0808, 0x28381800, 0x0c0c0808,
140 0x24243c00, 0x06363424, 0x08220800, 0x0822081c,
141 0x3c1c0c04, 0x00040c1c, 0x3c383020, 0x00203038,
142 0x083e1c08, 0x081c3e08, 0x14141400, 0x00140014,
143 0x2a2a3c00, 0x2828282c, 0x3c043800, 0x1e203c42,
144 0x00000000, 0x0000007e, 0x083e1c08, 0x3e081c3e,
145 0x3e1c0800, 0x00080808, 0x08080800, 0x00081c3e,
146 0x30100000, 0x0010307e, 0x0c080000, 0x00080c7e,
147 0x00000000, 0x3e020200, 0x36140000, 0x0014367f,
148 0x1c080800, 0x003e3e1c, 0x1c3e3e00, 0x0008081c,
149 0x00000000, 0x00000000, 0x08080800, 0x00080008,
150 0x14141400, 0x00000000, 0x3e140000, 0x00143e14,
151 0x021c0800, 0x081e201c, 0x16260000, 0x00323408,
152 0x08140800, 0x002c1234, 0x08080800, 0x00000000,
153 0x04080000, 0x00080404, 0x10080000, 0x00081010,
154 0x08140000, 0x0014083e, 0x08080000, 0x0008083e,
155 0x00000000, 0x04080c00, 0x00000000, 0x0000003e,
156 0x00000000, 0x000c0c00, 0x18302000, 0x0002060c,
157 0x221c0000, 0x001c222a, 0x0a0c0000, 0x003e0808,
158 0x201c0000, 0x003e021c, 0x221c0000, 0x001c2218,
159 0x12120000, 0x00103e12, 0x023e0000, 0x001e201e,
160 0x021c0000, 0x001c221e, 0x203e0000, 0x00040810,
161 0x221c0000, 0x001c221c, 0x221c0000, 0x001c203c,
162 0x0c0c0000, 0x000c0c00, 0x0c0c0000, 0x04080c00,
163 0x08100000, 0x00100804, 0x3e000000, 0x00003e00,
164 0x08040000, 0x00040810, 0x10221c00, 0x00080008,
165 0x2a1c0000, 0x001c023a, 0x221c0000, 0x00223e22,
166 0x221e0000, 0x001e221e, 0x221c0000, 0x001c2202,
167 0x221e0000, 0x001e2222, 0x023e0000, 0x003e021e,
168 0x023e0000, 0x0002021e, 0x021c0000, 0x001c2232,
169 0x22220000, 0x0022223e, 0x083e0000, 0x003e0808,
170 0x20200000, 0x001c2220, 0x12220000, 0x0022120e,
171 0x02020000, 0x003e0202, 0x36220000, 0x0022222a,
172 0x2a260000, 0x00222232, 0x221c0000, 0x001c2222,
173 0x221e0000, 0x00021e22, 0x221c0000, 0x002c1222,
174 0x221e0000, 0x00221e22, 0x021c0000, 0x001e201c,
175 0x083e0000, 0x00080808, 0x22220000, 0x001c2222,
176 0x22220000, 0x00081422, 0x2a220000, 0x00142a2a,
177 0x14220000, 0x00221408, 0x22220000, 0x00080814,
178 0x103e0000, 0x003e0408, 0x041c0000, 0x001c0404,
179 0x0c060200, 0x00203018, 0x101c0000, 0x001c1010,
180 0x22140800, 0x00000000, 0x00000000, 0x003e0000,
181 0x10080400, 0x00000000, 0x201c0000, 0x003c223c,
182 0x1e020200, 0x001e2222, 0x021c0000, 0x001c0202,
183 0x3c202000, 0x003c2222, 0x221c0000, 0x001c021e,
184 0x023c0000, 0x0002021e, 0x223c0000, 0x1c203c22,
185 0x1e020200, 0x00222222, 0x0c000800, 0x003e0808,
186 0x20002000, 0x1c222020, 0x0a120200, 0x00320a06,
187 0x08080c00, 0x003e0808, 0x2a160000, 0x002a2a2a,
188 0x320e0000, 0x00222222, 0x221c0000, 0x001c2222,
189 0x221e0000, 0x02021e22, 0x223c0000, 0x70203c22,
190 0x261a0000, 0x00020202, 0x021c0000, 0x001e201c,
191 0x043e0400, 0x00180404, 0x22220000, 0x001c2222,
192 0x22220000, 0x00081422, 0x22220000, 0x00142a2a,
193 0x14220000, 0x00221408, 0x22220000, 0x1c203c22,
194 0x103e0000, 0x003e0408, 0x041c0000, 0x001c0406,
195 0x08080000, 0x00080808, 0x101c0000, 0x001c1030,
196 0x1a2c0000, 0x00000000, 0x14080000, 0x003e2222,
197 0x00000000, 0x00000000, 0x08080000, 0x00080008,
198 0x14140000, 0x00000000, 0x3e140000, 0x00143e14,
199 0x041c0800, 0x081c101c, 0x10040000, 0x00100408,
200 0x14080000, 0x00381438, 0x08080000, 0x00000000,
201 0x04080000, 0x00080404, 0x10080000, 0x00081010,
202 0x08140000, 0x0014081c, 0x08000000, 0x0000081c,
203 0x00000000, 0x04080000, 0x00000000, 0x0000001c,
204 0x00000000, 0x00080000, 0x18100000, 0x00040c08,
205 0x141c0000, 0x001c1414, 0x0c080000, 0x001c0808,
206 0x101c0000, 0x001c041c, 0x101c0000, 0x001c1018,
207 0x14140000, 0x0010101c, 0x041c0000, 0x001c101c,
208 0x041c0000, 0x001c141c, 0x101c0000, 0x00040408,
209 0x141c0000, 0x001c141c, 0x141c0000, 0x0010101c,
210 0x08000000, 0x00080000, 0x08000000, 0x04080000,
211 0x08100000, 0x00100804, 0x1c000000, 0x00001c00,
212 0x08040000, 0x00040810, 0x101c0000, 0x00080008,
213 0x141c0000, 0x001c0414, 0x141c0000, 0x00141c14,
214 0x141c0000, 0x001c140c, 0x041c0000, 0x001c0404,
215 0x140c0000, 0x000c1414, 0x041c0000, 0x001c040c,
216 0x041c0000, 0x0004040c, 0x041c0000, 0x001c1414,
217 0x14140000, 0x0014141c, 0x081c0000, 0x001c0808,
218 0x10100000, 0x001c1410, 0x14140000, 0x0014140c,
219 0x04040000, 0x001c0404, 0x1c140000, 0x00141414,
220 0x140c0000, 0x00141414, 0x140c0000, 0x001c1414,
221 0x141c0000, 0x0004041c, 0x141c0000, 0x00101c14,
222 0x141c0000, 0x0014140c, 0x04180000, 0x000c101c,
223 0x081c0000, 0x00080808, 0x14140000, 0x001c1414,
224 0x14140000, 0x00181414, 0x14140000, 0x00141c14,
225 0x14140000, 0x00141408, 0x14140000, 0x0008081c,
226 0x101c0000, 0x001c0408, 0x041c0000, 0x001c0404,
227 0x0c040000, 0x00101808, 0x101c0000, 0x001c1010,
228 0x14080000, 0x00000000, 0x00000000, 0x001c0000,
229 0x00000000, 0x00000000, 0x00000000, 0x00000000,
230 0x00000000, 0x00000000, 0x00000000, 0x00000000,
231 0x00000000, 0x00000000, 0x00000000, 0x00000000,
232 0x00000000, 0x00000000, 0x00000000, 0x00000000,
233 0x00000000, 0x00000000, 0x00000000, 0x00000000,
234 0x00000000, 0x00000000, 0x00000000, 0x00000000,
235 0x00000000, 0x00000000, 0x00000000, 0x00000000,
236 0x00000000, 0x00000000, 0x00000000, 0x00000000,
237 0x3232323e, 0x003e3232, 0x18181818, 0x00181818,
238 0x3e30303e, 0x003e0606, 0x3c30303e, 0x003e3030,
239 0x32323232, 0x0030303e, 0x3e06063e, 0x003e3030,
240 0x3e06063e, 0x003e2626, 0x1830303e, 0x0006060c,
241 0x3e32323e, 0x003e3232, 0x3e32323e, 0x003e3030,
242 0x00000000, 0x00060600, 0x18362600, 0x0032360c,
243 0x32323e1c, 0x0032323e, 0x1e26261e, 0x001e2626,
244 0x0606063c, 0x003c0606, 0x3232321e, 0x001e3232,
245 0x1e06063e, 0x003e0606, 0x1e06063e, 0x00060606,
246 0x3606063c, 0x001c2626, 0x3e323232, 0x00323232,
247 0x0c0c0c1e, 0x001e0c0c, 0x30303030, 0x003e3232,
248 0x0e162626, 0x00262616, 0x06060606, 0x003e0606,
249 0x6a7e7662, 0x00626262, 0x323a3e36, 0x00323232,
250 0x3232321c, 0x001c3232, 0x3e32323e, 0x00020202,
251 0x3232321c, 0x002c1a3a, 0x1e26261e, 0x00262626,
252 0x1e06063c, 0x001e3030, 0x1818187e, 0x00181818,
253 0x32323232, 0x003e3232, 0x32323232, 0x00183432,
254 0x32323232, 0x00143a32, 0x0c323232, 0x00323232,
255 0x32323232, 0x001e203c, 0x1c30303e, 0x003e0606,
256 0x00000000, 0x00000000, 0x00000000, 0x00000000,
257 0x00000000, 0x00000000, 0x00000000, 0x00000000,
258 0x00000000, 0x00000000, 0x00000000, 0x00000000,
259 0x00000000, 0x00000000, 0x00000000, 0x00000000,
260 0x00000000, 0x00000000, 0x00000000, 0x00000000,
261};
diff --git a/src/text/text.h b/src/text/text.h
index 0bcf090..ab525d4 100644
--- a/src/text/text.h
+++ b/src/text/text.h
@@ -2,10 +2,8 @@
2#define TEXT_H 2#define TEXT_H
3 3
4#include "posprintf.h" 4#include "posprintf.h"
5#include "renderer.h"
6
7#include "font.h"
8 5
6typedef void (*TxtDrawc)(char c, size_t x, size_t y, u8 clr);
9typedef struct TextEngine { 7typedef struct TextEngine {
10 // Cursor for tiled text mode The X and Y positions correspond to the tile 8 // Cursor for tiled text mode The X and Y positions correspond to the tile
11 // X and Y starting from the top left of the screen. For a 240x160 screen, 9 // X and Y starting from the top left of the screen. For a 240x160 screen,
@@ -13,14 +11,10 @@ typedef struct TextEngine {
13 size_t cursor_x; 11 size_t cursor_x;
14 size_t cursor_y; 12 size_t cursor_y;
15 13
16 // Memory location of font tile data and tilemap. Likely located on the 14 u8 buffer[30 * 20];
17 // VRAM. 15 u8 spacing;
18 u32 *font_data; 16 u8 color;
19 u16 *font_tilemap; 17 TxtDrawc drawc;
20
21 // The font map for tiled text. Writing the character stored in this
22 // position on the tilemap will show a character on the screen.
23 u16 font_map[256];
24} TextEngine; 18} TextEngine;
25 19
26static TextEngine text_engine = {0}; 20static TextEngine text_engine = {0};
@@ -28,20 +22,11 @@ static TextEngine text_engine = {0};
28// Initializes the text engine. 22// Initializes the text engine.
29static inline 23static inline
30void 24void
31txt_init(u32 *font_data, u16 *font_tilemap, u16 font_offset) { 25txt_init(TxtDrawc drawc) {
32 // Load font data into VRAM.
33 unpack_tiles(&font, font_data, 256);
34
35 // Initialize the font map translation table. That way we can write
36 // a character on the text background layer with:
37 // FONT_TILEMAP[tile_x + 32 * tile_y] = font_map['A'];
38 for (size_t i = 0; i < 256; ++i) {
39 text_engine.font_map[i] = font_offset + i;
40 }
41
42 // Initialize remaining variables. 26 // Initialize remaining variables.
43 text_engine.font_data = font_data; 27 text_engine.spacing = 8;
44 text_engine.font_tilemap = font_tilemap; 28 text_engine.color = 1;
29 text_engine.drawc = drawc;
45} 30}
46 31
47// Writes a message to the tile text background. 32// Writes a message to the tile text background.
@@ -59,7 +44,7 @@ txt_puts(char *msg) {
59 } else { 44 } else {
60 int x = text_engine.cursor_x; 45 int x = text_engine.cursor_x;
61 int y = text_engine.cursor_y; 46 int y = text_engine.cursor_y;
62 text_engine.font_tilemap[x + 32 * y] = text_engine.font_map[(u16)c]; 47 text_engine.buffer[x + 30 * y] = c;
63 text_engine.cursor_x += 1; 48 text_engine.cursor_x += 1;
64 if (text_engine.cursor_x >= 30) { 49 if (text_engine.cursor_x >= 30) {
65 text_engine.cursor_x = 0; 50 text_engine.cursor_x = 0;
@@ -76,18 +61,15 @@ txt_puts(char *msg) {
76static inline 61static inline
77void 62void
78txt_clear_line(void) { 63txt_clear_line(void) {
79 for (size_t i = 0; i < 30; ++i) { 64 dma_fill(text_engine.buffer, 0, sizeof(text_engine.buffer), 3);
80 int x = text_engine.cursor_x;
81 int y = text_engine.cursor_y;
82 text_engine.font_tilemap[x + 32 * y] = text_engine.font_map[0];
83 }
84 text_engine.cursor_x = 0; 65 text_engine.cursor_x = 0;
66 text_engine.cursor_y = 0;
85} 67}
86 68
87// Clears the screen on the tile text mode. 69// Clears the screen on the tile text mode.
88static inline 70static inline
89void 71void
90txt_clear_screen(void) { 72txt_clear(void) {
91 for (size_t j = 0; j < 20; ++j) { 73 for (size_t j = 0; j < 20; ++j) {
92 text_engine.cursor_y = j; 74 text_engine.cursor_y = j;
93 txt_clear_line(); 75 txt_clear_line();
@@ -104,30 +86,49 @@ txt_position(size_t tile_x, size_t tile_y) {
104 text_engine.cursor_y = tile_y; 86 text_engine.cursor_y = tile_y;
105} 87}
106 88
89static inline
90void
91txt_color(u8 clr) {
92 text_engine.color = clr;
93}
94
95static inline
96void
97txt_spacing(u8 spacing) {
98 text_engine.spacing = spacing;
99}
100
101// Renders the contents of the scrollback buffer to the screen.
102void
103txt_render(void) {
104 for (size_t y = 0; y < 20; y++) {
105 for (size_t x = 0; x < 30; x++) {
106 size_t pos = x + y * 30;
107 if (text_engine.buffer[pos] == 0) {
108 continue;
109 }
110 text_engine.drawc(
111 text_engine.buffer[pos],
112 x * text_engine.spacing,
113 y * text_engine.spacing,
114 text_engine.color);
115 }
116 }
117}
118
107// Draws a message where the first character's top-left corner begins at the 119// Draws a message where the first character's top-left corner begins at the
108// given x and y position. The character spacing can be adjusted, but beware of 120// given x and y position. The character spacing can be adjusted, but beware of
109// color merging issues. 121// color merging issues.
110static inline 122static inline
111void 123void
112txt_draws(char *msg, size_t x, size_t y, size_t spacing, u8 clr) { 124txt_draws(char *msg, size_t x, size_t y, u8 clr) {
125 size_t i = 0;
113 while (*msg) { 126 while (*msg) {
114 char c = *msg++; 127 char c = *msg++;
115 Tile *tile = FONT_DATA; 128 text_engine.drawc(c, x + i++ * text_engine.spacing, y, clr);
116 tile += c;
117 draw_tile(x, y, tile, clr, true);
118 x += spacing;
119 } 129 }
120} 130}
121 131
122static inline
123void
124txt_drawc(char c, size_t x, size_t y, size_t spacing, u8 clr) {
125 Tile *tile = FONT_DATA;
126 tile += c;
127 draw_tile(x, y, tile, clr, true);
128 x += spacing;
129}
130
131// Print text to the screen with formatting. 132// Print text to the screen with formatting.
132#define txt_printf(msg, ...) \ 133#define txt_printf(msg, ...) \
133 { \ 134 { \
@@ -138,31 +139,11 @@ txt_drawc(char c, size_t x, size_t y, size_t spacing, u8 clr) {
138 139
139// Draws text to the screen with formatting starting on the x and y position and 140// Draws text to the screen with formatting starting on the x and y position and
140// with custom character spacing. 141// with custom character spacing.
141#define txt_drawf(msg, x, y, s, c, ...) \ 142#define txt_drawf(msg, x, y, c, ...) \
142 { \
143 char buf[256] = {0}; \
144 posprintf(buf, msg, ##__VA_ARGS__); \
145 txt_draws(buf, x, y, s, c); \
146 }
147
148// Small font is located after the initial ASCII characters, and only supports
149// lowercase characters.
150// NOTE: Slow, we should do this with a LUT.
151#define txt_drawf_small(msg, x, y, s, c, ...) \
152 { \ 143 { \
153 char buf[256] = {0}; \ 144 char buf[256] = {0}; \
154 posprintf(buf, msg, ##__VA_ARGS__); \ 145 posprintf(buf, msg, ##__VA_ARGS__); \
155 for (size_t i = 0; i < 256; i++) { \ 146 txt_draws(buf, x, y, c); \
156 if (buf[i] == 0) { \
157 break; \
158 } \
159 if (buf[i] < 'a') { \
160 buf[i] += 16 * 6; \
161 } else { \
162 buf[i] += 16 * 4; \
163 }\
164 } \
165 txt_draws(buf, x, y, s, c); \
166 } 147 }
167 148
168#endif // TEXT_H 149#endif // TEXT_H