Merge pull request #211 from gwilymk/avoid-overwriting-text-on-line-above

Avoid overwriting text on line above
This commit is contained in:
Gwilym Kuiper 2022-04-09 12:25:50 +01:00 committed by GitHub
commit 0bb364f333
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View file

@ -42,6 +42,8 @@ fn main(mut gba: agb::Gba) -> ! {
writeln!(&mut writer, "Hello, World!").unwrap(); writeln!(&mut writer, "Hello, World!").unwrap();
writeln!(&mut writer, "This is a font rendering example").unwrap(); writeln!(&mut writer, "This is a font rendering example").unwrap();
writer.commit();
bg.commit(); bg.commit();
bg.show(); bg.show();

View file

@ -3,7 +3,7 @@ use core::fmt::{Error, Write};
use crate::fixnum::Vector2D; use crate::fixnum::Vector2D;
use crate::hash_map::HashMap; use crate::hash_map::HashMap;
use super::tiled::{RegularMap, TileSetting, VRamManager}; use super::tiled::{DynamicTile, RegularMap, TileSetting, VRamManager};
pub struct FontLetter { pub struct FontLetter {
width: u8, width: u8,
@ -72,6 +72,7 @@ impl Font {
bg, bg,
background_colour, background_colour,
foreground_colour, foreground_colour,
tiles: Default::default(),
} }
} }
} }
@ -85,13 +86,13 @@ pub struct TextRenderer<'a> {
bg: &'a mut RegularMap, bg: &'a mut RegularMap,
background_colour: u8, background_colour: u8,
foreground_colour: u8, foreground_colour: u8,
tiles: HashMap<(usize, usize), DynamicTile<'a>>,
} }
impl<'a> Write for TextRenderer<'a> { impl<'a> Write for TextRenderer<'a> {
fn write_str(&mut self, text: &str) -> Result<(), Error> { fn write_str(&mut self, text: &str) -> Result<(), Error> {
let mut tiles = HashMap::new();
let vram_manager = &mut self.vram_manager; let vram_manager = &mut self.vram_manager;
let tiles = &mut self.tiles;
let foreground_colour = self.foreground_colour; let foreground_colour = self.foreground_colour;
let background_colour = self.background_colour; let background_colour = self.background_colour;
@ -141,6 +142,14 @@ impl<'a> Write for TextRenderer<'a> {
self.current_x_pos += letter.advance_width as i32; self.current_x_pos += letter.advance_width as i32;
} }
Ok(())
}
}
impl<'a> TextRenderer<'a> {
pub fn commit(mut self) {
let tiles = core::mem::take(&mut self.tiles);
for ((x, y), tile) in tiles.into_iter() { for ((x, y), tile) in tiles.into_iter() {
self.bg.set_tile( self.bg.set_tile(
self.vram_manager, self.vram_manager,
@ -150,7 +159,15 @@ impl<'a> Write for TextRenderer<'a> {
); );
self.vram_manager.remove_dynamic_tile(tile); self.vram_manager.remove_dynamic_tile(tile);
} }
}
Ok(()) }
impl<'a> Drop for TextRenderer<'a> {
fn drop(&mut self) {
let tiles = core::mem::take(&mut self.tiles);
for (_, tile) in tiles.into_iter() {
self.vram_manager.remove_dynamic_tile(tile);
}
} }
} }