make to generics

This commit is contained in:
Corwin Kuiper 2021-08-01 17:48:48 +01:00
parent e176e986ad
commit 6652bdbd70
4 changed files with 67 additions and 17 deletions

View file

@ -48,18 +48,23 @@ pub fn main() -> ! {
let vblank = agb::interrupt::VBlank::get(); let vblank = agb::interrupt::VBlank::get();
let mut input = agb::input::ButtonController::new(); let mut input = agb::input::ButtonController::new();
gfx.set_sprite_palette_raw(&CHICKEN_PALETTE);
gfx.set_sprite_tilemap(&CHICKEN_TILES);
gfx.set_background_palette_raw(&MAP_PALETTE); gfx.set_background_palette_raw(&MAP_PALETTE);
gfx.set_background_tilemap(0, &MAP_TILES); gfx.set_background_tilemap(0, &MAP_TILES);
let mut background = gfx.get_background().unwrap(); let mut background = gfx.get_background().unwrap();
background.draw_full_map(&MAP_MAP, (32_u32, 32_u32).into(), 0); background.set_map(agb::display::tiled0::Map {
store: &MAP_MAP,
dimensions: (32_u32, 32_u32).into(),
default: 0,
});
background.show(); background.show();
background.commit();
let mut object = gba.display.object.get(); let mut object = gba.display.object.get();
object.set_sprite_palette_raw(&CHICKEN_PALETTE);
object.set_sprite_tilemap(&CHICKEN_TILES);
object.enable(); object.enable();
let mut chicken = Character { let mut chicken = Character {
object: object.get_object_standard(), object: object.get_object_standard(),

View file

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

View file

@ -1,12 +1,16 @@
use core::cell::RefCell; use core::cell::RefCell;
use super::{Priority, DISPLAY_CONTROL}; use super::{palette16, Priority, DISPLAY_CONTROL};
use crate::bitarray::Bitarray; use crate::bitarray::Bitarray;
use crate::memory_mapped::MemoryMapped1DArray; use crate::memory_mapped::MemoryMapped1DArray;
use crate::number::Vector2D; use crate::number::Vector2D;
const OBJECT_ATTRIBUTE_MEMORY: MemoryMapped1DArray<u16, 512> = const OBJECT_ATTRIBUTE_MEMORY: MemoryMapped1DArray<u16, 512> =
unsafe { MemoryMapped1DArray::new(0x0700_0000) }; unsafe { MemoryMapped1DArray::new(0x0700_0000) };
const PALETTE_SPRITE: MemoryMapped1DArray<u16, 256> =
unsafe { MemoryMapped1DArray::new(0x0500_0200) };
const TILE_SPRITE: MemoryMapped1DArray<u32, { 512 * 8 }> =
unsafe { MemoryMapped1DArray::new(0x06010000) };
/// Handles distributing objects and matricies along with operations that effect all objects. /// Handles distributing objects and matricies along with operations that effect all objects.
pub struct ObjectControl { pub struct ObjectControl {
@ -310,6 +314,39 @@ impl ObjectControl {
} }
} }
fn set_sprite_tilemap_entry(&self, index: usize, data: u32) {
TILE_SPRITE.set(index, data);
}
/// Copies raw palettes to the background palette without any checks.
pub fn set_sprite_palette_raw(&self, colour: &[u16]) {
for (index, &entry) in colour.iter().enumerate() {
self.set_sprite_palette_entry(index, entry)
}
}
fn set_sprite_palette_entry(&self, index: usize, colour: u16) {
PALETTE_SPRITE.set(index, colour)
}
fn set_sprite_palette(&self, pal_index: u8, palette: &palette16::Palette16) {
for (colour_index, &colour) in palette.colours.iter().enumerate() {
PALETTE_SPRITE.set(pal_index as usize * 16 + colour_index, colour);
}
}
pub fn set_sprite_palettes(&self, palettes: &[palette16::Palette16]) {
for (palette_index, entry) in palettes.iter().enumerate() {
self.set_sprite_palette(palette_index as u8, entry)
}
}
/// Copies tiles to the sprite tilemap without any checks.
pub fn set_sprite_tilemap(&self, tiles: &[u32]) {
for (index, &tile) in tiles.iter().enumerate() {
self.set_sprite_tilemap_entry(index, tile)
}
}
/// Enable objects on the GBA. /// Enable objects on the GBA.
pub fn enable(&mut self) { pub fn enable(&mut self) {
let disp = DISPLAY_CONTROL.get(); let disp = DISPLAY_CONTROL.get();

View file

@ -1,3 +1,5 @@
use core::ops::{Deref, Index};
use crate::{ use crate::{
memory_mapped::{MemoryMapped, MemoryMapped1DArray}, memory_mapped::{MemoryMapped, MemoryMapped1DArray},
number::{Rect, Vector2D}, number::{Rect, Vector2D},
@ -31,23 +33,26 @@ pub enum BackgroundSize {
/// 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<'a> { pub struct Background<S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> {
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<'a>>, map: Option<Map<S, D>>,
} }
pub struct Map<'a> { pub struct Map<S, D: Deref<Target = S>>
pub store: &'a mut [u16], where
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> Map<'a> { impl<'a, S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> Map<S, D> {
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
@ -59,8 +64,8 @@ impl<'a> Map<'a> {
} }
} }
impl<'a> Background<'a> { impl<'a, S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> Background<S, D> {
unsafe fn new(background: u8, block: u8) -> Background<'a> { unsafe fn new(background: u8, block: u8) -> Background<S, D> {
let mut b = Background { let mut b = Background {
background, background,
block, block,
@ -129,12 +134,13 @@ impl<'a> Background<'a> {
self.shadowed_position = position; self.shadowed_position = position;
} }
pub fn get_map(&mut self) -> &mut Option<Map<'a>> { pub fn get_map(&mut self) -> Option<&mut Map<S, D>> {
self.poisoned = true; self.poisoned = true;
&mut self.map self.map.as_mut()
} }
pub fn set_map(&mut self, map: Map<'a>) { pub fn set_map(&mut self, map: Map<S, D>) {
self.poisoned = true;
self.map = Some(map); self.map = Some(map);
} }
@ -259,7 +265,9 @@ 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(&mut self) -> Result<Background, &'static str> { pub fn get_background<S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>>(
&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");
} }