mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-11 09:31:34 +11:00
Merge pull request #211 from gwilymk/avoid-overwriting-text-on-line-above
Avoid overwriting text on line above
This commit is contained in:
commit
0bb364f333
|
@ -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();
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
@ -141,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,
|
||||
|
@ -150,7 +159,15 @@ impl<'a> Write for TextRenderer<'a> {
|
|||
);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue