From 5701667f5312272c3fa75c95cdb9e4d4a8389a3a Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 7 Apr 2022 22:56:28 +0100 Subject: [PATCH 1/3] Avoid overwriting text on the line above --- agb/src/display/font.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/agb/src/display/font.rs b/agb/src/display/font.rs index 031978cf..ed41cdbb 100644 --- a/agb/src/display/font.rs +++ b/agb/src/display/font.rs @@ -3,7 +3,7 @@ use core::fmt::{Error, Write}; use crate::fixnum::Vector2D; use crate::hash_map::HashMap; -use super::tiled::{RegularMap, TileSetting, VRamManager}; +use super::tiled::{DynamicTile, RegularMap, TileSetting, VRamManager}; pub struct FontLetter { width: u8, @@ -72,6 +72,7 @@ impl Font { bg, background_colour, foreground_colour, + tiles: Default::default(), } } } @@ -85,13 +86,13 @@ pub struct TextRenderer<'a> { bg: &'a mut RegularMap, background_colour: u8, foreground_colour: u8, + tiles: HashMap<(usize, usize), DynamicTile<'a>>, } impl<'a> Write for TextRenderer<'a> { fn write_str(&mut self, text: &str) -> Result<(), Error> { - let mut tiles = HashMap::new(); - let vram_manager = &mut self.vram_manager; + let tiles = &mut self.tiles; let foreground_colour = self.foreground_colour; let background_colour = self.background_colour; From dc4ccee4510fa5c264429cbc8782927bd67d279c Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 7 Apr 2022 23:01:01 +0100 Subject: [PATCH 2/3] Add a commit method for text_render --- agb/examples/text_render.rs | 2 ++ agb/src/display/font.rs | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/agb/examples/text_render.rs b/agb/examples/text_render.rs index 39b9049b..f465a67a 100644 --- a/agb/examples/text_render.rs +++ b/agb/examples/text_render.rs @@ -42,6 +42,8 @@ fn main(mut gba: agb::Gba) -> ! { writeln!(&mut writer, "Hello, World!").unwrap(); writeln!(&mut writer, "This is a font rendering example").unwrap(); + writer.commit(); + bg.commit(); bg.show(); diff --git a/agb/src/display/font.rs b/agb/src/display/font.rs index ed41cdbb..70b8a136 100644 --- a/agb/src/display/font.rs +++ b/agb/src/display/font.rs @@ -142,6 +142,14 @@ impl<'a> Write for TextRenderer<'a> { 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() { self.bg.set_tile( self.vram_manager, @@ -151,7 +159,5 @@ impl<'a> Write for TextRenderer<'a> { ); self.vram_manager.remove_dynamic_tile(tile); } - - Ok(()) } } From d56e4d58886a04169b9ddd639536260fbd23d93d Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Thu, 7 Apr 2022 23:07:58 +0100 Subject: [PATCH 3/3] Correctly remove dynamic tiles if not committing a text renderer --- agb/src/display/font.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/agb/src/display/font.rs b/agb/src/display/font.rs index 70b8a136..badafc56 100644 --- a/agb/src/display/font.rs +++ b/agb/src/display/font.rs @@ -161,3 +161,13 @@ impl<'a> TextRenderer<'a> { } } } + +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); + } + } +}