summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-06-04 10:30:53 +0200
committerBad Diode <bd@badd10de.dev>2021-06-04 10:30:53 +0200
commit76343003abd8bc686e6bb5ca4bcb77cb33b76e10 (patch)
treee3fbcd37b5697396ffea25f2fe4eac3b1f5f5433
parent5ca4491aa46b7090189685fecf422ee7316de724 (diff)
downloadgba-sequencer-76343003abd8bc686e6bb5ca4bcb77cb33b76e10.tar.gz
gba-sequencer-76343003abd8bc686e6bb5ca4bcb77cb33b76e10.zip
Add text drawing support for framebuffer
-rw-r--r--src/main.c10
-rw-r--r--src/renderer.c26
-rw-r--r--src/renderer.h34
-rw-r--r--src/text/text.h27
4 files changed, 63 insertions, 34 deletions
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.
31#define PROF_SHOW_Y 0 31#define PROF_SHOW_Y 0
32#endif 32#endif
33#define PROF_SHOW() \ 33#define PROF_SHOW() \
34 do { \ 34 do {\
35 txt_position((PROF_SHOW_X), (PROF_SHOW_Y));\ 35 txt_position((PROF_SHOW_X), (PROF_SHOW_Y));\
36 txt_printf("EVAL: %lu ", eval_cycles);\ 36 txt_printf("EVAL: %lu ", eval_cycles);\
37 txt_position((PROF_SHOW_X), (PROF_SHOW_Y)+1);\ 37 txt_position((PROF_SHOW_X), (PROF_SHOW_Y)+1);\
@@ -64,15 +64,11 @@ int main(void) {
64 irq_init(); 64 irq_init();
65 irs_set(IRQ_VBLANK, irs_stub); 65 irs_set(IRQ_VBLANK, irs_stub);
66 66
67 Tile *tile = FONT_DATA; 67 txt_drawf("Hello world: %d", 4, 4, 6, 10);
68 tile += 'A';
69 draw_tile(0, 0, tile, true);
70 draw_tile(0, 4, tile, true);
71 draw_tile(4, 0, tile, true);
72 68
73 // Main loop. 69 // Main loop.
74 PROF_INIT(); 70 PROF_INIT();
75 while(true) { 71 while (true) {
76 bios_vblank_wait(); 72 bios_vblank_wait();
77 PROF_SHOW(); 73 PROF_SHOW();
78 PROF(flip_buffer(), flip_cycles); 74 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 @@
16// 16//
17 17
18#include "text.h" 18#include "text.h"
19 19#include "renderer.h"
20// The frontbuffer is located at the beginning of the VRAM, and requires 20KB of
21// video memory for 32 * 20 tiles at 4bpp.
22#define FRONTBUF ((u32*)(MEM_VRAM))
23
24// Adjust both of these if the location of the map changes. Each screnblock
25// requires less than 2KB.
26#define FRONTBUF_TILEMAP ((u16*)(MEM_VRAM + KB(20)))
27#define FRONTBUF_SB 10
28
29// The backbuffer is located at the end of the VRAM. This can allow us to use
30// more backgrounds but eats into the available memory for sprites. This should
31// be fine for non sprite intensive applications. If more sprite memory is
32// needed, the backbuffer can be located at the end of the background memory
33// instead (64KB - 20KB).
34#define BACKBUF ((u32*)(MEM_VRAM + KB(96) - KB(20)))
35
36// The font data is located at the end of the frontbuffer memory, after the tile
37// map and requires 8KB for 256 8x8 characters at 4bpp. This, along with the
38// tilemap information allow us to store the frontbuffer and font for a text
39// background in the first 2 charblocks (32KB).
40#define FONT_DATA ((u32*)(MEM_VRAM + KB(22)))
41#define FONT_TILEMAP ((u16*)(MEM_VRAM + KB(30)))
42#define FONT_SB 15
43#define FONT_OFFSET 192
44 20
45// Keep track of which tiles need to be copied to the frontbuffer. 21// Keep track of which tiles need to be copied to the frontbuffer.
46static u32 dirty_tiles[21] = {0}; 22static 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 @@
1#ifndef RENDERER_H
2#define RENDERER_H
3
4#include "gba/gba.h"
5
6// The frontbuffer is located at the beginning of the VRAM, and requires 20KB of
7// video memory for 32 * 20 tiles at 4bpp.
8#define FRONTBUF ((u32*)(MEM_VRAM))
9
10// Adjust both of these if the location of the map changes. Each screnblock
11// requires less than 2KB.
12#define FRONTBUF_TILEMAP ((u16*)(MEM_VRAM + KB(20)))
13#define FRONTBUF_SB 10
14
15// The backbuffer is located at the end of the VRAM. This can allow us to use
16// more backgrounds but eats into the available memory for sprites. This should
17// be fine for non sprite intensive applications. If more sprite memory is
18// needed, the backbuffer can be located at the end of the background memory
19// instead (64KB - 20KB).
20#define BACKBUF ((u32*)(MEM_VRAM + KB(96) - KB(20)))
21
22// The font data is located at the end of the frontbuffer memory, after the tile
23// map and requires 8KB for 256 8x8 characters at 4bpp. This, along with the
24// tilemap information allow us to store the frontbuffer and font for a text
25// background in the first 2 charblocks (32KB).
26#define FONT_DATA ((u32*)(MEM_VRAM + KB(22)))
27#define FONT_TILEMAP ((u16*)(MEM_VRAM + KB(30)))
28#define FONT_SB 15
29#define FONT_OFFSET 192
30
31void draw_pixel(u16 x, u16 y, u8 color);
32void draw_tile(u16 x, u16 y, Tile *tile, bool merge);
33
34#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 @@
1#ifndef TEXT_H 1#ifndef TEXT_H
2#define TEXT_H 2#define TEXT_H
3 3
4#include "bd-font.c"
5#include "shorthand.h"
6#include "posprintf.h" 4#include "posprintf.h"
5#include "renderer.h"
6
7#include "bd-font.c"
7 8
8typedef struct TextEngine { 9typedef struct TextEngine {
9 // Cursor for tiled text mode The X and Y positions correspond to the tile 10 // 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) {
103 text_engine.cursor_y = tile_y; 104 text_engine.cursor_y = tile_y;
104} 105}
105 106
107// 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
109// color merging issues.
110static inline
111void
112txt_draws(char *msg, size_t x, size_t y, size_t spacing) {
113 while (*msg) {
114 char c = *msg++;
115 Tile *tile = FONT_DATA;
116 tile += c;
117 draw_tile(x, y, tile, true);
118 x += spacing;
119 }
120}
121
106// Print text to the screen with formatting. 122// Print text to the screen with formatting.
107#define txt_printf(msg, ...) \ 123#define txt_printf(msg, ...) \
108 { \ 124 { \
@@ -111,4 +127,11 @@ txt_position(size_t tile_x, size_t tile_y) {
111 txt_puts(buf); \ 127 txt_puts(buf); \
112 } 128 }
113 129
130#define txt_drawf(msg, x, y, s, ...) \
131 { \
132 char buf[256] = {0}; \
133 posprintf(buf, msg, ##__VA_ARGS__); \
134 txt_draws(buf, x, y, s); \
135 }
136
114#endif // TEXT_H 137#endif // TEXT_H