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();
background.set_map(agb::display::tiled0::Map {
store: &MAP_MAP,
store: MAP_MAP.as_ref(),
dimensions: (32_u32, 32_u32).into(),
default: 0,
});

View file

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

View file

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

View file

@ -30,29 +30,30 @@ pub enum BackgroundSize {
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
/// automatically handles copying the correct portion of a provided map to the
/// 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,
block: u8,
commited_position: Vector2D<i32>,
shadowed_position: Vector2D<i32>,
poisoned: bool,
shadowed_register: u16,
map: Option<Map<S, D>>,
map: Option<Map<S>>,
}
pub struct Map<S, D: Deref<Target = S>>
where
S: Index<usize, Output = u16> + ?Sized,
{
pub store: D,
pub struct Map<S: MapStorage> {
pub store: S,
pub dimensions: Vector2D<u32>,
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 {
if x < 0 || x as u32 >= self.dimensions.x {
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> {
unsafe fn new(background: u8, block: u8) -> Background<S, D> {
impl<'a, S: MapStorage> Background<S> {
unsafe fn new(background: u8, block: u8) -> Background<S> {
let mut b = Background {
background,
block,
@ -134,12 +135,12 @@ impl<'a, S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> Backgroun
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.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.map = Some(map);
}
@ -265,9 +266,7 @@ impl Tiled0 {
}
/// 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>>(
&mut self,
) -> Result<Background<S, D>, &'static str> {
pub fn get_background<S: MapStorage>(&mut self) -> Result<Background<S>, &'static str> {
if self.num_backgrounds >= 4 {
return Err("too many backgrounds created, maximum is 4");
}