From 87bace358454dbf3ff3ae8f03a8ba3783db4a1a1 Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sun, 6 Jun 2021 16:35:38 +0100 Subject: [PATCH] let user decide whether to use slice or refcell --- agb/src/display/tiled0.rs | 40 ++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/agb/src/display/tiled0.rs b/agb/src/display/tiled0.rs index d82725fa..cda2bccc 100644 --- a/agb/src/display/tiled0.rs +++ b/agb/src/display/tiled0.rs @@ -1,4 +1,4 @@ -use core::convert::TryInto; +use core::{borrow::Borrow, cell::RefCell, convert::TryInto}; use crate::memory_mapped::MemoryMapped1DArray; @@ -45,13 +45,18 @@ pub enum BackgroundSize { pub struct Background<'a> { background: u8, block: u8, - map: Option<&'a [u16]>, + map: Option>, map_dim_x: u32, map_dim_y: u32, pos_x: i32, pos_y: i32, } +enum MapStorage<'a> { + R(&'a RefCell<[u16]>), + S(&'a [u16]), +} + impl<'a> Background<'a> { unsafe fn new(layer: u8, block: u8) -> Background<'a> { let mut background = Background { @@ -77,7 +82,14 @@ impl<'a> Background<'a> { /// The portion of this map that is in view is copied to the map block /// assigned to this background. pub fn set_map(&mut self, map: &'a [u16], dim_x: u32, dim_y: u32) { - self.map = Some(map); + self.map = Some(MapStorage::S(map)); + self.map_dim_x = dim_x; + self.map_dim_y = dim_y; + self.draw_full_map(); + } + + pub fn set_map_refcell(&mut self, map: &'a RefCell<[u16]>, dim_x: u32, dim_y: u32) { + self.map = Some(MapStorage::R(map)); self.map_dim_x = dim_x; self.map_dim_y = dim_y; self.draw_full_map(); @@ -132,12 +144,22 @@ impl Background<'_> { } fn map_get(&self, x: i32, y: i32, default: u16) -> u16 { - let map = self.map.unwrap(); - - if x >= self.map_dim_x as i32 || x < 0 || y >= self.map_dim_y as i32 || y < 0 { - default - } else { - map[(self.map_dim_x as i32 * y + x) as usize] + match self.map.as_ref().unwrap() { + MapStorage::R(map) => { + let map = (*map).borrow(); + if x >= self.map_dim_x as i32 || x < 0 || y >= self.map_dim_y as i32 || y < 0 { + default + } else { + map[(self.map_dim_x as i32 * y + x) as usize] + } + } + MapStorage::S(map) => { + if x >= self.map_dim_x as i32 || x < 0 || y >= self.map_dim_y as i32 || y < 0 { + default + } else { + map[(self.map_dim_x as i32 * y + x) as usize] + } + } } }