mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-24 08:41:34 +11:00
Merge pull request #64 from corwinkuiper/map-as-refcell
let user decide whether to use slice or refcell
This commit is contained in:
commit
eb212e7f91
|
@ -1,4 +1,4 @@
|
||||||
use core::convert::TryInto;
|
use core::{borrow::Borrow, cell::RefCell, convert::TryInto};
|
||||||
|
|
||||||
use crate::memory_mapped::MemoryMapped1DArray;
|
use crate::memory_mapped::MemoryMapped1DArray;
|
||||||
|
|
||||||
|
@ -45,13 +45,18 @@ pub enum BackgroundSize {
|
||||||
pub struct Background<'a> {
|
pub struct Background<'a> {
|
||||||
background: u8,
|
background: u8,
|
||||||
block: u8,
|
block: u8,
|
||||||
map: Option<&'a [u16]>,
|
map: Option<MapStorage<'a>>,
|
||||||
map_dim_x: u32,
|
map_dim_x: u32,
|
||||||
map_dim_y: u32,
|
map_dim_y: u32,
|
||||||
pos_x: i32,
|
pos_x: i32,
|
||||||
pos_y: i32,
|
pos_y: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum MapStorage<'a> {
|
||||||
|
R(&'a RefCell<[u16]>),
|
||||||
|
S(&'a [u16]),
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Background<'a> {
|
impl<'a> Background<'a> {
|
||||||
unsafe fn new(layer: u8, block: u8) -> Background<'a> {
|
unsafe fn new(layer: u8, block: u8) -> Background<'a> {
|
||||||
let mut background = Background {
|
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
|
/// The portion of this map that is in view is copied to the map block
|
||||||
/// assigned to this background.
|
/// assigned to this background.
|
||||||
pub fn set_map(&mut self, map: &'a [u16], dim_x: u32, dim_y: u32) {
|
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_x = dim_x;
|
||||||
self.map_dim_y = dim_y;
|
self.map_dim_y = dim_y;
|
||||||
self.draw_full_map();
|
self.draw_full_map();
|
||||||
|
@ -132,14 +144,24 @@ impl Background<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn map_get(&self, x: i32, y: i32, default: u16) -> u16 {
|
fn map_get(&self, x: i32, y: i32, default: u16) -> u16 {
|
||||||
let map = self.map.unwrap();
|
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 {
|
if x >= self.map_dim_x as i32 || x < 0 || y >= self.map_dim_y as i32 || y < 0 {
|
||||||
default
|
default
|
||||||
} else {
|
} else {
|
||||||
map[(self.map_dim_x as i32 * y + x) as usize]
|
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]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn set_x(&mut self, x: u16) {
|
fn set_x(&mut self, x: u16) {
|
||||||
unsafe { *((0x0400_0010 + 4 * self.background as usize) as *mut u16) = x }
|
unsafe { *((0x0400_0010 + 4 * self.background as usize) as *mut u16) = x }
|
||||||
|
|
Loading…
Reference in a new issue