From 0578a6db65f81f868c527aadfb57a89875d09d94 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 4 Jun 2021 10:38:40 +0200 Subject: Add explanatory comments on text drawing functions --- src/renderer.c | 2 +- src/renderer.h | 2 ++ src/text/text.h | 50 ++++++++++++++++++++++++++------------------------ 3 files changed, 29 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/renderer.c b/src/renderer.c index f950a4d..076d698 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -15,8 +15,8 @@ // charblocks instead as described below. // -#include "text.h" #include "renderer.h" +#include "text.h" // Keep track of which tiles need to be copied to the frontbuffer. static u32 dirty_tiles[21] = {0}; diff --git a/src/renderer.h b/src/renderer.h index 8bc2069..f056f6a 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -30,5 +30,7 @@ void draw_pixel(u16 x, u16 y, u8 color); void draw_tile(u16 x, u16 y, Tile *tile, bool merge); +void flip_buffer(void); +void renderer_init(void); #endif // RENDERER__H diff --git a/src/text/text.h b/src/text/text.h index 630f04f..3b7921d 100644 --- a/src/text/text.h +++ b/src/text/text.h @@ -25,6 +25,7 @@ typedef struct TextEngine { static TextEngine text_engine = {0}; +// Initializes the text engine. static inline void txt_init(u32 *font_data, u16 *font_tilemap, u16 font_offset) { @@ -43,38 +44,35 @@ txt_init(u32 *font_data, u16 *font_tilemap, u16 font_offset) { text_engine.font_tilemap = font_tilemap; } +// Writes a message to the tile text background. 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) { +txt_puts(char *msg) { + while (*msg) { + char c = *msg++; + if (c == '\0') { + continue; + } + 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; } - } - if (text_engine.cursor_y >= 20) { - text_engine.cursor_y = 0; - } -} - -static inline -void -txt_puts(char *msg) { - while (*msg) { - txt_putc(*msg++); } } +// Clears the current line on the tile text mode. static inline void txt_clear_line(void) { @@ -86,6 +84,7 @@ txt_clear_line(void) { text_engine.cursor_x = 0; } +// Clears the screen on the tile text mode. static inline void txt_clear_screen(void) { @@ -97,6 +96,7 @@ txt_clear_screen(void) { text_engine.cursor_y = 0; } +// Moves the tile mode cursor to the specified position. static inline void txt_position(size_t tile_x, size_t tile_y) { @@ -127,6 +127,8 @@ txt_draws(char *msg, size_t x, size_t y, size_t spacing) { txt_puts(buf); \ } +// 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, ...) \ { \ char buf[256] = {0}; \ -- cgit v1.2.1