From 666867e933f87fbaeb400a2221479f88f03d3f62 Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Mon, 7 Feb 2022 20:39:05 +0000 Subject: [PATCH] Track the generation along with the tile data --- agb/src/display/background.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/agb/src/display/background.rs b/agb/src/display/background.rs index 2c1b2070..b9b33e52 100644 --- a/agb/src/display/background.rs +++ b/agb/src/display/background.rs @@ -95,7 +95,7 @@ pub struct VRamManager<'a> { generation: u16, free_pointer: Option, - tile_set_to_vram: HashMap, + tile_set_to_vram: HashMap, references: Vec, vram_free_pointer: Option, } @@ -167,8 +167,10 @@ impl<'a> VRamManager<'a> { fn add_tile(&mut self, tile_set_ref: TileSetReference, tile: u16) -> TileIndex { let tile_ref = TileReference(tile_set_ref.id, tile); if let Some(&reference) = self.tile_set_to_vram.get(&tile_ref) { - self.references[reference as usize].increase_reference(); - return TileIndex(reference as u16); + if reference.1 == tile_set_ref.generation { + self.references[reference.0 as usize].increase_reference(); + return TileIndex(reference.0 as u16); + } } let index_to_copy_into = if let Some(ptr) = self.vram_free_pointer.take() { @@ -211,7 +213,7 @@ impl<'a> VRamManager<'a> { self.tile_set_to_vram.insert( TileReference(tile_set_ref.id, tile), - index_to_copy_into as u16, + (index_to_copy_into as u16, tile_set_ref.generation), ); TileIndex(index_to_copy_into as u16)