Merge pull request #1 from gwilymk/textrender-rework-gwilym

Textrender rework gwilym
This commit is contained in:
Jem Bishop 2022-11-21 21:37:55 +00:00 committed by GitHub
commit 3b19088b5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 19 deletions

View file

@ -40,7 +40,8 @@ fn main(mut gba: agb::Gba) -> ! {
vram.remove_dynamic_tile(background_tile); vram.remove_dynamic_tile(background_tile);
let mut writer = FONT.render_text((0u16, 3u16).into(), 1, 2, &mut bg, &mut vram); let mut renderer = FONT.render_text((0u16, 3u16).into());
let mut writer = renderer.writer(1, 2, &mut bg, &mut vram);
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();
@ -53,7 +54,8 @@ fn main(mut gba: agb::Gba) -> ! {
let mut frame = 0; let mut frame = 0;
loop { loop {
let mut writer = FONT.render_text((4u16, 0u16).into(), 1, 2, &mut bg, &mut vram); let mut renderer = FONT.render_text((4u16, 0u16).into());
let mut writer = renderer.writer(1, 2, &mut bg, &mut vram);
writeln!(&mut writer, "Frame {}", frame).unwrap(); writeln!(&mut writer, "Frame {}", frame).unwrap();
writer.commit(); writer.commit();
@ -62,5 +64,7 @@ fn main(mut gba: agb::Gba) -> ! {
vblank.wait_for_vblank(); vblank.wait_for_vblank();
bg.commit(&mut vram); bg.commit(&mut vram);
renderer.clear(&mut vram);
} }
} }

View file

@ -9,7 +9,6 @@ use super::tiled::{DynamicTile, RegularMap, TileSetting, VRamManager};
/// bitmap font using dynamic tiles as a rendering surface. /// bitmap font using dynamic tiles as a rendering surface.
/// Does not support any unicode features. /// Does not support any unicode features.
/// For usage see the `text_render.rs` example /// For usage see the `text_render.rs` example
pub struct FontLetter { pub struct FontLetter {
width: u8, width: u8,
height: u8, height: u8,
@ -75,8 +74,8 @@ impl Font {
} }
} }
/// Keeps track of the cursor and manages rendered tiles.
pub struct TextRenderer<'a> { pub struct TextRenderer<'a> {
/// Keeps track of the cursor and manages rendered tiles.
current_x_pos: i32, current_x_pos: i32,
current_y_pos: i32, current_y_pos: i32,
font: &'a Font, font: &'a Font,
@ -84,17 +83,17 @@ pub struct TextRenderer<'a> {
tiles: HashMap<(i32, i32), DynamicTile<'a>>, tiles: HashMap<(i32, i32), DynamicTile<'a>>,
} }
pub struct TextWriter<'a, 'b: 'a, 'c> { /// Generated from the renderer for use
/// Generated from the renderer for use /// with `Write` trait methods.
/// with `Write` trait methods. pub struct TextWriter<'a, 'b> {
foreground_colour: u8, foreground_colour: u8,
background_colour: u8, background_colour: u8,
text_renderer: &'a mut TextRenderer<'c>, text_renderer: &'a mut TextRenderer<'b>,
vram_manager: &'b mut VRamManager, vram_manager: &'a mut VRamManager,
bg: &'b mut RegularMap, bg: &'a mut RegularMap,
} }
impl<'a, 'b, 'c> Write for TextWriter<'a, 'b, 'c> { impl<'a, 'b> Write for TextWriter<'a, 'b> {
fn write_str(&mut self, text: &str) -> Result<(), Error> { fn write_str(&mut self, text: &str) -> Result<(), Error> {
for c in text.chars() { for c in text.chars() {
self.text_renderer.write_char( self.text_renderer.write_char(
@ -108,8 +107,8 @@ impl<'a, 'b, 'c> Write for TextWriter<'a, 'b, 'c> {
Ok(()) Ok(())
} }
} }
impl<'a, 'b, 'c> TextWriter<'a, 'b, 'c> {
/// Calls commit on the parent renderer, consuming the writer. impl<'a, 'b> TextWriter<'a, 'b> {
pub fn commit(self) { pub fn commit(self) {
self.text_renderer.commit(self.bg, self.vram_manager); self.text_renderer.commit(self.bg, self.vram_manager);
} }
@ -119,14 +118,14 @@ fn div_ceil(quotient: i32, divisor: i32) -> i32 {
(quotient + divisor - 1) / divisor (quotient + divisor - 1) / divisor
} }
impl<'a, 'b, 'c> TextRenderer<'c> { impl<'a, 'b> TextRenderer<'b> {
pub fn writer( pub fn writer(
&'a mut self, &'a mut self,
foreground_colour: u8, foreground_colour: u8,
background_colour: u8, background_colour: u8,
bg: &'b mut RegularMap, bg: &'a mut RegularMap,
vram_manager: &'b mut VRamManager, vram_manager: &'a mut VRamManager,
) -> TextWriter<'a, 'b, 'c> { ) -> TextWriter<'a, 'b> {
TextWriter { TextWriter {
text_renderer: self, text_renderer: self,
foreground_colour, foreground_colour,
@ -205,8 +204,9 @@ impl<'a, 'b, 'c> TextRenderer<'c> {
} }
} }
} }
/// Commit the dynamic tiles that contain the text to the background. /// Commit the dynamic tiles that contain the text to the background.
pub fn commit(&self, bg: &'b mut RegularMap, vram_manager: &'b mut VRamManager) { pub fn commit(&self, bg: &'a mut RegularMap, vram_manager: &'a mut VRamManager) {
for ((x, y), tile) in self.tiles.iter() { for ((x, y), tile) in self.tiles.iter() {
bg.set_tile( bg.set_tile(
vram_manager, vram_manager,
@ -216,6 +216,7 @@ impl<'a, 'b, 'c> TextRenderer<'c> {
); );
} }
} }
/// Write another char into the text, moving the cursor as appropriate. /// Write another char into the text, moving the cursor as appropriate.
pub fn write_char( pub fn write_char(
&mut self, &mut self,