From 5e0795ae1424b278d8590d31ea22237ab8e69716 Mon Sep 17 00:00:00 2001 From: Corwin Date: Sun, 2 Oct 2022 20:05:13 +0100 Subject: [PATCH] add garbage collection using hashmap retain --- agb/examples/sprites.rs | 3 +-- agb/src/display/object.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/agb/examples/sprites.rs b/agb/examples/sprites.rs index 0ae5ffb5..c5ea5ba8 100644 --- a/agb/examples/sprites.rs +++ b/agb/examples/sprites.rs @@ -46,9 +46,8 @@ fn all_sprites(gfx: &ObjectController) { if count % 5 == 0 { image += 1; image %= SPRITES.len(); - let objs_len = objs.len(); for (i, obj) in objs.iter_mut().enumerate() { - let this_image = (image + i * SPRITES.len() / objs_len) % SPRITES.len(); + let this_image = (image + i) % SPRITES.len(); obj.set_sprite(gfx.sprite(&SPRITES[this_image])); } gfx.commit(); diff --git a/agb/src/display/object.rs b/agb/src/display/object.rs index 49006c55..2b6eaf23 100644 --- a/agb/src/display/object.rs +++ b/agb/src/display/object.rs @@ -741,6 +741,8 @@ impl ObjectController { } } } + + s.sprite_controller.gc(); } pub(crate) fn new() -> Self { @@ -1186,6 +1188,12 @@ impl SpriteControllerInner { Some(SpriteBorrow { sprite }) } + /// Cleans up weak references to sprites and palettes no longer in vram + fn gc(&mut self) { + self.static_palette_map.retain(|_, v| v.strong_count() != 0); + self.static_sprite_map.retain(|_, v| v.strong_count() != 0); + } + fn new() -> Self { Default::default() }