Support rendering multiple lines

This commit is contained in:
Gwilym Kuiper 2022-04-05 23:03:10 +01:00
parent 8a1a433f99
commit 1f65f94ee3
4 changed files with 18 additions and 3 deletions

View file

@ -6,7 +6,7 @@ use agb::{
include_font,
};
const FONT: Font = include_font!("examples/RobotoCondensed-Regular.ttf", 14);
const FONT: Font = include_font!("examples/unscii-8.ttf", 8);
#[agb::entry]
fn main(mut gba: agb::Gba) -> ! {
@ -35,7 +35,15 @@ fn main(mut gba: agb::Gba) -> ! {
vram.remove_dynamic_tile(background_tile);
FONT.render_text(3, 3, "Hello, World!", 1, 2, &mut bg, &mut vram);
FONT.render_text(
0,
3,
"Hello, World!\nThis is a font rendering example",
1,
2,
&mut bg,
&mut vram,
);
bg.commit();
bg.show();

BIN
agb/examples/unscii-8.ttf Normal file

Binary file not shown.

View file

@ -88,12 +88,19 @@ impl Font {
};
let mut current_x_pos = 0i32;
let mut current_y_pos = 0i32;
for c in text.chars() {
if c == '\n' {
current_y_pos += self.line_height;
current_x_pos = 0;
continue;
}
let letter = self.letter(c);
let xmin = (current_x_pos + letter.xmin as i32).max(0);
let y_start = self.ascent - letter.height as i32 - letter.ymin as i32;
let y_start = current_y_pos + self.ascent - letter.height as i32 - letter.ymin as i32;
for letter_y in 0..(letter.height as i32) {
for letter_x in 0..(letter.width as i32) {