From 76343003abd8bc686e6bb5ca4bcb77cb33b76e10 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Fri, 4 Jun 2021 10:30:53 +0200 Subject: Add text drawing support for framebuffer --- src/main.c | 10 +++------- src/renderer.c | 26 +------------------------- src/renderer.h | 34 ++++++++++++++++++++++++++++++++++ src/text/text.h | 27 +++++++++++++++++++++++++-- 4 files changed, 63 insertions(+), 34 deletions(-) create mode 100644 src/renderer.h (limited to 'src') diff --git a/src/main.c b/src/main.c index 913043a..61547c8 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,7 @@ WITH REGARD TO THIS SOFTWARE. #define PROF_SHOW_Y 0 #endif #define PROF_SHOW() \ - do { \ + do {\ txt_position((PROF_SHOW_X), (PROF_SHOW_Y));\ txt_printf("EVAL: %lu ", eval_cycles);\ txt_position((PROF_SHOW_X), (PROF_SHOW_Y)+1);\ @@ -64,15 +64,11 @@ int main(void) { irq_init(); irs_set(IRQ_VBLANK, irs_stub); - Tile *tile = FONT_DATA; - tile += 'A'; - draw_tile(0, 0, tile, true); - draw_tile(0, 4, tile, true); - draw_tile(4, 0, tile, true); + txt_drawf("Hello world: %d", 4, 4, 6, 10); // Main loop. PROF_INIT(); - while(true) { + while (true) { bios_vblank_wait(); PROF_SHOW(); PROF(flip_buffer(), flip_cycles); diff --git a/src/renderer.c b/src/renderer.c index 500cc78..f950a4d 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -16,31 +16,7 @@ // #include "text.h" - -// The frontbuffer is located at the beginning of the VRAM, and requires 20KB of -// video memory for 32 * 20 tiles at 4bpp. -#define FRONTBUF ((u32*)(MEM_VRAM)) - -// Adjust both of these if the location of the map changes. Each screnblock -// requires less than 2KB. -#define FRONTBUF_TILEMAP ((u16*)(MEM_VRAM + KB(20))) -#define FRONTBUF_SB 10 - -// The backbuffer is located at the end of the VRAM. This can allow us to use -// more backgrounds but eats into the available memory for sprites. This should -// be fine for non sprite intensive applications. If more sprite memory is -// needed, the backbuffer can be located at the end of the background memory -// instead (64KB - 20KB). -#define BACKBUF ((u32*)(MEM_VRAM + KB(96) - KB(20))) - -// The font data is located at the end of the frontbuffer memory, after the tile -// map and requires 8KB for 256 8x8 characters at 4bpp. This, along with the -// tilemap information allow us to store the frontbuffer and font for a text -// background in the first 2 charblocks (32KB). -#define FONT_DATA ((u32*)(MEM_VRAM + KB(22))) -#define FONT_TILEMAP ((u16*)(MEM_VRAM + KB(30))) -#define FONT_SB 15 -#define FONT_OFFSET 192 +#include "renderer.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 new file mode 100644 index 0000000..8bc2069 --- /dev/null +++ b/src/renderer.h @@ -0,0 +1,34 @@ +#ifndef RENDERER_H +#define RENDERER_H + +#include "gba/gba.h" + +// The frontbuffer is located at the beginning of the VRAM, and requires 20KB of +// video memory for 32 * 20 tiles at 4bpp. +#define FRONTBUF ((u32*)(MEM_VRAM)) + +// Adjust both of these if the location of the map changes. Each screnblock +// requires less than 2KB. +#define FRONTBUF_TILEMAP ((u16*)(MEM_VRAM + KB(20))) +#define FRONTBUF_SB 10 + +// The backbuffer is located at the end of the VRAM. This can allow us to use +// more backgrounds but eats into the available memory for sprites. This should +// be fine for non sprite intensive applications. If more sprite memory is +// needed, the backbuffer can be located at the end of the background memory +// instead (64KB - 20KB). +#define BACKBUF ((u32*)(MEM_VRAM + KB(96) - KB(20))) + +// The font data is located at the end of the frontbuffer memory, after the tile +// map and requires 8KB for 256 8x8 characters at 4bpp. This, along with the +// tilemap information allow us to store the frontbuffer and font for a text +// background in the first 2 charblocks (32KB). +#define FONT_DATA ((u32*)(MEM_VRAM + KB(22))) +#define FONT_TILEMAP ((u16*)(MEM_VRAM + KB(30))) +#define FONT_SB 15 +#define FONT_OFFSET 192 + +void draw_pixel(u16 x, u16 y, u8 color); +void draw_tile(u16 x, u16 y, Tile *tile, bool merge); + +#endif // RENDERER__H diff --git a/src/text/text.h b/src/text/text.h index 65003c5..630f04f 100644 --- a/src/text/text.h +++ b/src/text/text.h @@ -1,9 +1,10 @@ #ifndef TEXT_H #define TEXT_H -#include "bd-font.c" -#include "shorthand.h" #include "posprintf.h" +#include "renderer.h" + +#include "bd-font.c" typedef struct TextEngine { // Cursor for tiled text mode The X and Y positions correspond to the tile @@ -103,6 +104,21 @@ txt_position(size_t tile_x, size_t tile_y) { text_engine.cursor_y = tile_y; } +// 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) { + while (*msg) { + char c = *msg++; + Tile *tile = FONT_DATA; + tile += c; + draw_tile(x, y, tile, true); + x += spacing; + } +} + // Print text to the screen with formatting. #define txt_printf(msg, ...) \ { \ @@ -111,4 +127,11 @@ txt_position(size_t tile_x, size_t tile_y) { txt_puts(buf); \ } +#define txt_drawf(msg, x, y, s, ...) \ + { \ + char buf[256] = {0}; \ + posprintf(buf, msg, ##__VA_ARGS__); \ + txt_draws(buf, x, y, s); \ + } + #endif // TEXT_H -- cgit v1.2.1