add garbage collection using hashmap retain

This commit is contained in:
Corwin 2022-10-02 20:05:13 +01:00
parent 7143621407
commit 5e0795ae14
2 changed files with 9 additions and 2 deletions

View file

@ -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();

View file

@ -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()
}