From deb9c48fbd3dc5854de4ae3a04dc999029c10ae0 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sat, 22 Apr 2023 21:12:14 +0200 Subject: Add new renderer and prepare for render overhaul --- src/text/font.h | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/text/text.h | 115 +++++++++++++++++++++---------------------------- 2 files changed, 179 insertions(+), 67 deletions(-) (limited to 'src/text') 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[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, }; + +u32 font_icn[] = { + 0x00000000, 0x00000000, 0x00240000, 0x00003c42, + 0x00240000, 0x0000423c, 0x3e360000, 0x00081c3e, + 0x3e1c0800, 0x00081c3e, 0x3e1c1c00, 0x1c083e36, + 0x3e1c0800, 0x1c083e3e, 0x18000000, 0x00000018, + 0x667e7e7e, 0x7e7e7e66, 0x24180000, 0x00001824, + 0x5a667e7e, 0x7e7e665a, 0x3e1c0800, 0x1c221c08, + 0x1c221c00, 0x083e0808, 0x28381800, 0x0c0c0808, + 0x24243c00, 0x06363424, 0x08220800, 0x0822081c, + 0x3c1c0c04, 0x00040c1c, 0x3c383020, 0x00203038, + 0x083e1c08, 0x081c3e08, 0x14141400, 0x00140014, + 0x2a2a3c00, 0x2828282c, 0x3c043800, 0x1e203c42, + 0x00000000, 0x0000007e, 0x083e1c08, 0x3e081c3e, + 0x3e1c0800, 0x00080808, 0x08080800, 0x00081c3e, + 0x30100000, 0x0010307e, 0x0c080000, 0x00080c7e, + 0x00000000, 0x3e020200, 0x36140000, 0x0014367f, + 0x1c080800, 0x003e3e1c, 0x1c3e3e00, 0x0008081c, + 0x00000000, 0x00000000, 0x08080800, 0x00080008, + 0x14141400, 0x00000000, 0x3e140000, 0x00143e14, + 0x021c0800, 0x081e201c, 0x16260000, 0x00323408, + 0x08140800, 0x002c1234, 0x08080800, 0x00000000, + 0x04080000, 0x00080404, 0x10080000, 0x00081010, + 0x08140000, 0x0014083e, 0x08080000, 0x0008083e, + 0x00000000, 0x04080c00, 0x00000000, 0x0000003e, + 0x00000000, 0x000c0c00, 0x18302000, 0x0002060c, + 0x221c0000, 0x001c222a, 0x0a0c0000, 0x003e0808, + 0x201c0000, 0x003e021c, 0x221c0000, 0x001c2218, + 0x12120000, 0x00103e12, 0x023e0000, 0x001e201e, + 0x021c0000, 0x001c221e, 0x203e0000, 0x00040810, + 0x221c0000, 0x001c221c, 0x221c0000, 0x001c203c, + 0x0c0c0000, 0x000c0c00, 0x0c0c0000, 0x04080c00, + 0x08100000, 0x00100804, 0x3e000000, 0x00003e00, + 0x08040000, 0x00040810, 0x10221c00, 0x00080008, + 0x2a1c0000, 0x001c023a, 0x221c0000, 0x00223e22, + 0x221e0000, 0x001e221e, 0x221c0000, 0x001c2202, + 0x221e0000, 0x001e2222, 0x023e0000, 0x003e021e, + 0x023e0000, 0x0002021e, 0x021c0000, 0x001c2232, + 0x22220000, 0x0022223e, 0x083e0000, 0x003e0808, + 0x20200000, 0x001c2220, 0x12220000, 0x0022120e, + 0x02020000, 0x003e0202, 0x36220000, 0x0022222a, + 0x2a260000, 0x00222232, 0x221c0000, 0x001c2222, + 0x221e0000, 0x00021e22, 0x221c0000, 0x002c1222, + 0x221e0000, 0x00221e22, 0x021c0000, 0x001e201c, + 0x083e0000, 0x00080808, 0x22220000, 0x001c2222, + 0x22220000, 0x00081422, 0x2a220000, 0x00142a2a, + 0x14220000, 0x00221408, 0x22220000, 0x00080814, + 0x103e0000, 0x003e0408, 0x041c0000, 0x001c0404, + 0x0c060200, 0x00203018, 0x101c0000, 0x001c1010, + 0x22140800, 0x00000000, 0x00000000, 0x003e0000, + 0x10080400, 0x00000000, 0x201c0000, 0x003c223c, + 0x1e020200, 0x001e2222, 0x021c0000, 0x001c0202, + 0x3c202000, 0x003c2222, 0x221c0000, 0x001c021e, + 0x023c0000, 0x0002021e, 0x223c0000, 0x1c203c22, + 0x1e020200, 0x00222222, 0x0c000800, 0x003e0808, + 0x20002000, 0x1c222020, 0x0a120200, 0x00320a06, + 0x08080c00, 0x003e0808, 0x2a160000, 0x002a2a2a, + 0x320e0000, 0x00222222, 0x221c0000, 0x001c2222, + 0x221e0000, 0x02021e22, 0x223c0000, 0x70203c22, + 0x261a0000, 0x00020202, 0x021c0000, 0x001e201c, + 0x043e0400, 0x00180404, 0x22220000, 0x001c2222, + 0x22220000, 0x00081422, 0x22220000, 0x00142a2a, + 0x14220000, 0x00221408, 0x22220000, 0x1c203c22, + 0x103e0000, 0x003e0408, 0x041c0000, 0x001c0406, + 0x08080000, 0x00080808, 0x101c0000, 0x001c1030, + 0x1a2c0000, 0x00000000, 0x14080000, 0x003e2222, + 0x00000000, 0x00000000, 0x08080000, 0x00080008, + 0x14140000, 0x00000000, 0x3e140000, 0x00143e14, + 0x041c0800, 0x081c101c, 0x10040000, 0x00100408, + 0x14080000, 0x00381438, 0x08080000, 0x00000000, + 0x04080000, 0x00080404, 0x10080000, 0x00081010, + 0x08140000, 0x0014081c, 0x08000000, 0x0000081c, + 0x00000000, 0x04080000, 0x00000000, 0x0000001c, + 0x00000000, 0x00080000, 0x18100000, 0x00040c08, + 0x141c0000, 0x001c1414, 0x0c080000, 0x001c0808, + 0x101c0000, 0x001c041c, 0x101c0000, 0x001c1018, + 0x14140000, 0x0010101c, 0x041c0000, 0x001c101c, + 0x041c0000, 0x001c141c, 0x101c0000, 0x00040408, + 0x141c0000, 0x001c141c, 0x141c0000, 0x0010101c, + 0x08000000, 0x00080000, 0x08000000, 0x04080000, + 0x08100000, 0x00100804, 0x1c000000, 0x00001c00, + 0x08040000, 0x00040810, 0x101c0000, 0x00080008, + 0x141c0000, 0x001c0414, 0x141c0000, 0x00141c14, + 0x141c0000, 0x001c140c, 0x041c0000, 0x001c0404, + 0x140c0000, 0x000c1414, 0x041c0000, 0x001c040c, + 0x041c0000, 0x0004040c, 0x041c0000, 0x001c1414, + 0x14140000, 0x0014141c, 0x081c0000, 0x001c0808, + 0x10100000, 0x001c1410, 0x14140000, 0x0014140c, + 0x04040000, 0x001c0404, 0x1c140000, 0x00141414, + 0x140c0000, 0x00141414, 0x140c0000, 0x001c1414, + 0x141c0000, 0x0004041c, 0x141c0000, 0x00101c14, + 0x141c0000, 0x0014140c, 0x04180000, 0x000c101c, + 0x081c0000, 0x00080808, 0x14140000, 0x001c1414, + 0x14140000, 0x00181414, 0x14140000, 0x00141c14, + 0x14140000, 0x00141408, 0x14140000, 0x0008081c, + 0x101c0000, 0x001c0408, 0x041c0000, 0x001c0404, + 0x0c040000, 0x00101808, 0x101c0000, 0x001c1010, + 0x14080000, 0x00000000, 0x00000000, 0x001c0000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x3232323e, 0x003e3232, 0x18181818, 0x00181818, + 0x3e30303e, 0x003e0606, 0x3c30303e, 0x003e3030, + 0x32323232, 0x0030303e, 0x3e06063e, 0x003e3030, + 0x3e06063e, 0x003e2626, 0x1830303e, 0x0006060c, + 0x3e32323e, 0x003e3232, 0x3e32323e, 0x003e3030, + 0x00000000, 0x00060600, 0x18362600, 0x0032360c, + 0x32323e1c, 0x0032323e, 0x1e26261e, 0x001e2626, + 0x0606063c, 0x003c0606, 0x3232321e, 0x001e3232, + 0x1e06063e, 0x003e0606, 0x1e06063e, 0x00060606, + 0x3606063c, 0x001c2626, 0x3e323232, 0x00323232, + 0x0c0c0c1e, 0x001e0c0c, 0x30303030, 0x003e3232, + 0x0e162626, 0x00262616, 0x06060606, 0x003e0606, + 0x6a7e7662, 0x00626262, 0x323a3e36, 0x00323232, + 0x3232321c, 0x001c3232, 0x3e32323e, 0x00020202, + 0x3232321c, 0x002c1a3a, 0x1e26261e, 0x00262626, + 0x1e06063c, 0x001e3030, 0x1818187e, 0x00181818, + 0x32323232, 0x003e3232, 0x32323232, 0x00183432, + 0x32323232, 0x00143a32, 0x0c323232, 0x00323232, + 0x32323232, 0x001e203c, 0x1c30303e, 0x003e0606, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, +}; 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 @@ #define TEXT_H #include "posprintf.h" -#include "renderer.h" - -#include "font.h" +typedef void (*TxtDrawc)(char c, size_t x, size_t y, u8 clr); typedef struct TextEngine { // Cursor for tiled text mode The X and Y positions correspond to the tile // X and Y starting from the top left of the screen. For a 240x160 screen, @@ -13,14 +11,10 @@ typedef struct TextEngine { size_t cursor_x; size_t cursor_y; - // Memory location of font tile data and tilemap. Likely located on the - // VRAM. - u32 *font_data; - u16 *font_tilemap; - - // The font map for tiled text. Writing the character stored in this - // position on the tilemap will show a character on the screen. - u16 font_map[256]; + u8 buffer[30 * 20]; + u8 spacing; + u8 color; + TxtDrawc drawc; } TextEngine; static TextEngine text_engine = {0}; @@ -28,20 +22,11 @@ static TextEngine text_engine = {0}; // Initializes the text engine. static inline void -txt_init(u32 *font_data, u16 *font_tilemap, u16 font_offset) { - // Load font data into VRAM. - unpack_tiles(&font, font_data, 256); - - // Initialize the font map translation table. That way we can write - // a character on the text background layer with: - // FONT_TILEMAP[tile_x + 32 * tile_y] = font_map['A']; - for (size_t i = 0; i < 256; ++i) { - text_engine.font_map[i] = font_offset + i; - } - +txt_init(TxtDrawc drawc) { // Initialize remaining variables. - text_engine.font_data = font_data; - text_engine.font_tilemap = font_tilemap; + text_engine.spacing = 8; + text_engine.color = 1; + text_engine.drawc = drawc; } // Writes a message to the tile text background. @@ -59,7 +44,7 @@ txt_puts(char *msg) { } else { int x = text_engine.cursor_x; int y = text_engine.cursor_y; - text_engine.font_tilemap[x + 32 * y] = text_engine.font_map[(u16)c]; + text_engine.buffer[x + 30 * y] = c; text_engine.cursor_x += 1; if (text_engine.cursor_x >= 30) { text_engine.cursor_x = 0; @@ -76,18 +61,15 @@ txt_puts(char *msg) { static inline void txt_clear_line(void) { - for (size_t i = 0; i < 30; ++i) { - int x = text_engine.cursor_x; - int y = text_engine.cursor_y; - text_engine.font_tilemap[x + 32 * y] = text_engine.font_map[0]; - } + dma_fill(text_engine.buffer, 0, sizeof(text_engine.buffer), 3); text_engine.cursor_x = 0; + text_engine.cursor_y = 0; } // Clears the screen on the tile text mode. static inline void -txt_clear_screen(void) { +txt_clear(void) { for (size_t j = 0; j < 20; ++j) { text_engine.cursor_y = j; txt_clear_line(); @@ -104,30 +86,49 @@ txt_position(size_t tile_x, size_t tile_y) { text_engine.cursor_y = tile_y; } +static inline +void +txt_color(u8 clr) { + text_engine.color = clr; +} + +static inline +void +txt_spacing(u8 spacing) { + text_engine.spacing = spacing; +} + +// Renders the contents of the scrollback buffer to the screen. +void +txt_render(void) { + for (size_t y = 0; y < 20; y++) { + for (size_t x = 0; x < 30; x++) { + size_t pos = x + y * 30; + if (text_engine.buffer[pos] == 0) { + continue; + } + text_engine.drawc( + text_engine.buffer[pos], + x * text_engine.spacing, + y * text_engine.spacing, + text_engine.color); + } + } +} + // Draws a message where the first character's top-left corner begins at the // given x and y position. The character spacing can be adjusted, but beware of // color merging issues. static inline void -txt_draws(char *msg, size_t x, size_t y, size_t spacing, u8 clr) { +txt_draws(char *msg, size_t x, size_t y, u8 clr) { + size_t i = 0; while (*msg) { char c = *msg++; - Tile *tile = FONT_DATA; - tile += c; - draw_tile(x, y, tile, clr, true); - x += spacing; + text_engine.drawc(c, x + i++ * text_engine.spacing, y, clr); } } -static inline -void -txt_drawc(char c, size_t x, size_t y, size_t spacing, u8 clr) { - Tile *tile = FONT_DATA; - tile += c; - draw_tile(x, y, tile, clr, true); - x += spacing; -} - // Print text to the screen with formatting. #define txt_printf(msg, ...) \ { \ @@ -138,31 +139,11 @@ txt_drawc(char c, size_t x, size_t y, size_t spacing, u8 clr) { // Draws text to the screen with formatting starting on the x and y position and // with custom character spacing. -#define txt_drawf(msg, x, y, s, c, ...) \ - { \ - char buf[256] = {0}; \ - posprintf(buf, msg, ##__VA_ARGS__); \ - txt_draws(buf, x, y, s, c); \ - } - -// Small font is located after the initial ASCII characters, and only supports -// lowercase characters. -// NOTE: Slow, we should do this with a LUT. -#define txt_drawf_small(msg, x, y, s, c, ...) \ +#define txt_drawf(msg, x, y, c, ...) \ { \ char buf[256] = {0}; \ posprintf(buf, msg, ##__VA_ARGS__); \ - for (size_t i = 0; i < 256; i++) { \ - if (buf[i] == 0) { \ - break; \ - } \ - if (buf[i] < 'a') { \ - buf[i] += 16 * 6; \ - } else { \ - buf[i] += 16 * 4; \ - }\ - } \ - txt_draws(buf, x, y, s, c); \ + txt_draws(buf, x, y, c); \ } #endif // TEXT_H -- cgit v1.2.1