|
1 | | -[](https://crates.io/crates/gba_agb_font_eb_renderer "Crates.io version") |
2 | | -[](https://docs.rs/gba_agb_font_eb_renderer "Documentation") |
| 1 | +[](https://crates.io/crates/gba_agb_font_renderer "Crates.io version") |
| 2 | +[](https://docs.rs/gba_agb_font_renderer "Documentation") |
3 | 3 |
|
4 | 4 | # Bitmap Font for GBA/AGB |
5 | 5 |
|
6 | | -## AGB Font Converter |
| 6 | +Renderer for bitmap fonts on the GBA using the [`agb`](https://github.com/agbrs/agb) framework |
7 | 7 |
|
8 | | -[Converter](https://github.com/emmabritton/agb_font_converter) |
| 8 | +## Font converter |
9 | 9 |
|
10 | | -## Font binary format |
| 10 | +Use the [AGB Font Converter](https://github.com/emmabritton/agb_font_converter) to convert images to fonts |
11 | 11 |
|
12 | | -Two modes are supported, selected by the `mode` byte at offset 0. |
| 12 | +## Usage |
13 | 13 |
|
14 | | -### Common header (all modes) |
| 14 | +### Loading a font |
15 | 15 |
|
16 | | -| Offset | Size | Field | |
17 | | -|--------|------|-----------------------------------------------------| |
18 | | -| 0 | 1 | `mode` — `0` = alphanum, `1` = all-256 | |
19 | | -| 1 | 1 | `glyph_width` — width of the glyph cell in pixels | |
20 | | -| 2 | 1 | `glyph_height` — height of the glyph cell in pixels | |
| 16 | +Two font types are available: `FullFont` (all 256 code points) and `PrintableFont` (95 printable ASCII chars, 0x20–0x7E). Load them at compile time with the corresponding macro: |
21 | 17 |
|
22 | | -### Mode 0 — printable ASCII (95 glyphs) |
23 | | - |
24 | | -Supports all 95 printable ASCII characters: 0x20 (space) through 0x7E (tilde). |
| 18 | +```rust |
| 19 | +use gba_agb_font_renderer::prelude::*; |
25 | 20 |
|
26 | | -Glyphs are stored in ascending byte-value order (space, `!`, `"`, … `~`), so the compact glyph index is `char - 0x20`. |
| 21 | +// Full 256-char font from ROM (default) |
| 22 | +let font = full_font!(include_bytes!("my_font.bin")); |
27 | 23 |
|
28 | | -| Offset | Size | Field | |
29 | | -|--------|---------------------------------------|---------------------------------------------------------------------------------------| |
30 | | -| 3 | 95 | `char_widths` — advance width for each of the 95 characters, in ascending byte order | |
31 | | -| 98 | 2 | padding (aligns data to a 4-byte boundary) | |
32 | | -| 100 | `glyph_width × glyph_height × 95 / 2` | pixel data | |
| 24 | +// Printable ASCII font, placed in IWRAM for faster blitting |
| 25 | +// (~8× fewer waitstates per pixel row — only worthwhile for small fonts ~3 KB) |
| 26 | +let font = printable_font!(include_bytes!("my_font.bin"), iwram); |
33 | 27 |
|
34 | | -### Mode 1 — all-256 (256 glyphs) |
| 28 | +// Full 256-char font placed in EWRAM |
| 29 | +let font = full_font!(include_bytes!("my_font.bin"), ewram); |
| 30 | +``` |
35 | 31 |
|
36 | | -All 256 Latin-1 code points, indexed directly by byte value. |
| 32 | +### Rendering text |
37 | 33 |
|
38 | | -| Offset | Size | Field | |
39 | | -|--------|----------------------------------------|----------------------------------------------------------------------| |
40 | | -| 3 | 256 | `char_widths` — per-character advance width, one byte per code point | |
41 | | -| 259 | 1 | padding (aligns data to a 4-byte boundary) | |
42 | | -| 260 | `glyph_width × glyph_height × 256 / 2` | pixel data | |
| 34 | +`wrap_at` is a maximum line width in pixels (relative to `pos`). Pass `None` to disable wrapping. |
43 | 35 |
|
44 | | -### Pixel data |
| 36 | +```rust |
| 37 | +use gba_agb_font_renderer::prelude::*; |
| 38 | +use agb::fixnum::vec2; |
45 | 39 |
|
46 | | -Stored as `u32` values, each holding 8 pixels at 4 bits per pixel (4bpp). Each row of a glyph occupies `(glyph_width + 7) / 8` `u32`s, so the full glyph table is `(glyph_width + 7) / 8 × glyph_height × glyph_count` `u32`s. |
| 40 | +let mut renderer = TextRenderer::default(); |
| 41 | +renderer.draw_text(b"Hello, world!", &font, &mut bg, vec2(8, 16), Some(200), 0); |
| 42 | +``` |
47 | 43 |
|
48 | | -## Usage |
| 44 | +### Typewriter effect |
49 | 45 |
|
50 | | -Embed a font at compile time using the `agb_font!` macro: |
| 46 | +`TypewriterRenderer` reveals text one chunk at a time. Call `update()` once per frame and `draw()` to blit newly revealed characters. |
51 | 47 |
|
52 | 48 | ```rust |
53 | | -let font = agb_font!(include_bytes!("my_font.bin")); |
| 49 | +use gba_agb_font_renderer::prelude::*; |
| 50 | +use agb::fixnum::vec2; |
| 51 | + |
| 52 | +// 1 character revealed every 2 frames, wrap at 200px |
| 53 | +let mut tw = TypewriterRenderer::new(1, 2, Some(200)); |
| 54 | +tw.load_text(b"Hello, world!", vec2(8, 16)); |
| 55 | + |
| 56 | +loop { |
| 57 | + tw.update(); |
| 58 | + tw.draw(&font, &mut bg, 0); |
| 59 | + if tw.is_complete() { break; } |
| 60 | + vblank.wait_for_vblank(); |
| 61 | +} |
54 | 62 | ``` |
55 | 63 |
|
56 | | -Place the font data in IWRAM for faster blitting (~8× fewer waitstates per pixel row read). |
57 | | -Only worthwhile for small fonts such as alphanum mode (~3 KB): |
| 64 | +To clear the text area before loading new text: |
58 | 65 |
|
59 | 66 | ```rust |
60 | | -let font = agb_font!(include_bytes!("my_font.bin"), iwram); |
| 67 | +tw.clear(&font, &mut bg, 0); |
| 68 | +tw.load_text(b"New message", vec2(8, 16)); |
61 | 69 | ``` |
62 | 70 |
|
63 | | -Remap palette colour indices (e.g. change colour 15 to colour 1): |
| 71 | +## Font binary format |
| 72 | + |
| 73 | +Two modes are supported, selected by the `mode` byte at offset 0. |
64 | 74 |
|
65 | | -```rust |
66 | | -let font = agb_font_remapped!(include_bytes!("my_font.bin"), [15 => 1]); |
67 | | -// multiple remappings: |
68 | | -let font = agb_font_remapped!(include_bytes!("my_font.bin"), [15 => 1, 14 => 2]); |
69 | | -// with IWRAM source: |
70 | | -let font = agb_font_remapped!(include_bytes!("my_font.bin"), [15 => 1], iwram); |
71 | | -``` |
| 75 | +### Common header |
72 | 76 |
|
73 | | -Construct from typed data (all-256 mode only): |
| 77 | +| Offset | Size | Field | |
| 78 | +|--------|------|-----------------------------------------------| |
| 79 | +| 0 | 1 | `mode` — `0` = printable ASCII, `1` = all-256 | |
| 80 | +| 1 | 1 | `glyph_width` — glyph cell width in pixels | |
| 81 | +| 2 | 1 | `glyph_height` — glyph cell height in pixels | |
74 | 82 |
|
75 | | -```rust |
76 | | -let font = AgbFont::new(8, 12, CHAR_WIDTHS, &PIXEL_DATA); |
77 | | -``` |
| 83 | +### Mode 0 — printable ASCII (95 glyphs) |
| 84 | + |
| 85 | +| Offset | Size | Field | |
| 86 | +|--------|--------|-------------------------------------------------------------------| |
| 87 | +| 3 | 95 | `char_widths` — advance width per character, ascending byte order | |
| 88 | +| 98 | 2 | padding to 4-byte boundary | |
| 89 | +| 100 | varies | 4bpp pixel data | |
| 90 | + |
| 91 | +### Mode 1 — all-256 (256 glyphs) |
| 92 | + |
| 93 | +| Offset | Size | Field | |
| 94 | +|--------|--------|----------------------------------------------| |
| 95 | +| 3 | 256 | `char_widths` — advance width per code point | |
| 96 | +| 259 | 1 | padding to 4-byte boundary | |
| 97 | +| 260 | varies | 4bpp pixel data | |
| 98 | + |
| 99 | +### Pixel data |
78 | 100 |
|
79 | | -For runtime loading from a byte slice, use `AgbFont::from_bytes`. |
| 101 | +Stored as `u32` values, each holding 8 pixels at 4 bits per pixel (4bpp). Each glyph row occupies `(glyph_width + 7) / 8` `u32`s. Palette index 0 is transparent. |
0 commit comments