simplify the generics

This commit is contained in:
Corwin Kuiper 2021-08-07 14:56:38 +01:00
parent 6652bdbd70
commit d6b388e1d6
4 changed files with 16 additions and 18 deletions

View file

@ -53,7 +53,7 @@ pub fn main() -> ! {
let mut background = gfx.get_background().unwrap(); let mut background = gfx.get_background().unwrap();
background.set_map(agb::display::tiled0::Map { background.set_map(agb::display::tiled0::Map {
store: &MAP_MAP, store: MAP_MAP.as_ref(),
dimensions: (32_u32, 32_u32).into(), dimensions: (32_u32, 32_u32).into(),
default: 0, default: 0,
}); });

View file

@ -4,7 +4,6 @@
extern crate agb; extern crate agb;
use agb::display::example_logo; use agb::display::example_logo;
use agb::display::tiled0::Map;
#[no_mangle] #[no_mangle]
pub fn main() -> ! { pub fn main() -> ! {

View file

@ -17,7 +17,7 @@ pub fn display_logo(gfx: &mut Tiled0) {
} }
back.set_map(Map { back.set_map(Map {
store: &entries, store: entries.as_ref(),
dimensions: (30_u32, 20_u32).into(), dimensions: (30_u32, 20_u32).into(),
default: 0, default: 0,
}); });

View file

@ -30,29 +30,30 @@ pub enum BackgroundSize {
S64x64 = 3, S64x64 = 3,
} }
pub trait MapStorage: Deref<Target = [u16]> {}
impl MapStorage for &[u16] {}
impl MapStorage for &mut [u16] {}
/// The map background is the method of drawing game maps to the screen. It /// The map background is the method of drawing game maps to the screen. It
/// automatically handles copying the correct portion of a provided map to the /// automatically handles copying the correct portion of a provided map to the
/// assigned block depending on given coordinates. /// assigned block depending on given coordinates.
pub struct Background<S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> { pub struct Background<S: MapStorage> {
background: u8, background: u8,
block: u8, block: u8,
commited_position: Vector2D<i32>, commited_position: Vector2D<i32>,
shadowed_position: Vector2D<i32>, shadowed_position: Vector2D<i32>,
poisoned: bool, poisoned: bool,
shadowed_register: u16, shadowed_register: u16,
map: Option<Map<S, D>>, map: Option<Map<S>>,
} }
pub struct Map<S, D: Deref<Target = S>> pub struct Map<S: MapStorage> {
where pub store: S,
S: Index<usize, Output = u16> + ?Sized,
{
pub store: D,
pub dimensions: Vector2D<u32>, pub dimensions: Vector2D<u32>,
pub default: u16, pub default: u16,
} }
impl<'a, S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> Map<S, D> { impl<'a, S: MapStorage> Map<S> {
fn get_position(&self, x: i32, y: i32) -> u16 { fn get_position(&self, x: i32, y: i32) -> u16 {
if x < 0 || x as u32 >= self.dimensions.x { if x < 0 || x as u32 >= self.dimensions.x {
self.default self.default
@ -64,8 +65,8 @@ impl<'a, S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> Map<S, D>
} }
} }
impl<'a, S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> Background<S, D> { impl<'a, S: MapStorage> Background<S> {
unsafe fn new(background: u8, block: u8) -> Background<S, D> { unsafe fn new(background: u8, block: u8) -> Background<S> {
let mut b = Background { let mut b = Background {
background, background,
block, block,
@ -134,12 +135,12 @@ impl<'a, S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> Backgroun
self.shadowed_position = position; self.shadowed_position = position;
} }
pub fn get_map(&mut self) -> Option<&mut Map<S, D>> { pub fn get_map(&mut self) -> Option<&mut Map<S>> {
self.poisoned = true; self.poisoned = true;
self.map.as_mut() self.map.as_mut()
} }
pub fn set_map(&mut self, map: Map<S, D>) { pub fn set_map(&mut self, map: Map<S>) {
self.poisoned = true; self.poisoned = true;
self.map = Some(map); self.map = Some(map);
} }
@ -265,9 +266,7 @@ impl Tiled0 {
} }
/// Gets a map background if possible and assigns an unused block to it. /// Gets a map background if possible and assigns an unused block to it.
pub fn get_background<S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>>( pub fn get_background<S: MapStorage>(&mut self) -> Result<Background<S>, &'static str> {
&mut self,
) -> Result<Background<S, D>, &'static str> {
if self.num_backgrounds >= 4 { if self.num_backgrounds >= 4 {
return Err("too many backgrounds created, maximum is 4"); return Err("too many backgrounds created, maximum is 4");
} }