From 0eda19bd2bb551bf9186b0fbe1a806a28d5a3597 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 3 Jun 2021 11:24:31 +0200 Subject: Add initial text background drawing functions --- src/text/text.h | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/text/text.h (limited to 'src/text/text.h') 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 @@ +#ifndef TEXT_H +#define TEXT_H + +#include "bd-font.c" +#include "shorthand.h" +#include "posprintf.h" + +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, + // we have 30x20 tiles available. + 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]; +} TextEngine; + +static TextEngine text_engine = {0}; + +static inline +void +txt_init(u32 *font_data, u16 *font_tilemap, u16 font_offset) { + // Load font data into VRAM. + unpack_tiles(&bd_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; + } + + // Initialize remaining variables. + text_engine.font_data = font_data; + text_engine.font_tilemap = font_tilemap; +} + +static inline +void +txt_putc(char c) { + if (c == '\0') { + return; + } + if (c == '\n') { + text_engine.cursor_x = 0; + text_engine.cursor_y++; + } 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.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; + } +} + +static inline +void +txt_puts(char *msg) { + while (*msg) { + txt_putc(*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]; + } + text_engine.cursor_x = 0; +} + +static inline +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; +} + +static inline +void +txt_position(size_t tile_x, size_t tile_y) { + text_engine.cursor_x = tile_x; + text_engine.cursor_y = tile_y; +} + +// Print text to the screen with formatting. +#define txt_printf(msg, ...) \ + { \ + char buf[256] = {0}; \ + posprintf(buf, msg, ##__VA_ARGS__); \ + txt_puts(buf); \ + } + +#endif // TEXT_H -- cgit v1.2.1