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 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_tilemap(0, &MAP_TILES);
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.commit();
let mut object = gba.display.object.get();
object.set_sprite_palette_raw(&CHICKEN_PALETTE);
object.set_sprite_tilemap(&CHICKEN_TILES);
object.enable();
let mut chicken = Character {
object: object.get_object_standard(),

View file

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

View file

@ -1,12 +1,16 @@
use core::cell::RefCell;
use super::{Priority, DISPLAY_CONTROL};
use super::{palette16, Priority, DISPLAY_CONTROL};
use crate::bitarray::Bitarray;
use crate::memory_mapped::MemoryMapped1DArray;
use crate::number::Vector2D;
const OBJECT_ATTRIBUTE_MEMORY: MemoryMapped1DArray<u16, 512> =
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.
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.
pub fn enable(&mut self) {
let disp = DISPLAY_CONTROL.get();

View file

@ -1,3 +1,5 @@
use core::ops::{Deref, Index};
use crate::{
memory_mapped::{MemoryMapped, MemoryMapped1DArray},
number::{Rect, Vector2D},
@ -31,23 +33,26 @@ pub enum BackgroundSize {
/// 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<'a> {
pub struct Background<S: Index<usize, Output = u16> + ?Sized, D: Deref<Target = S>> {
background: u8,
block: u8,
commited_position: Vector2D<i32>,
shadowed_position: Vector2D<i32>,
poisoned: bool,
shadowed_register: u16,
map: Option<Map<'a>>,
map: Option<Map<S, D>>,
}
pub struct Map<'a> {
pub store: &'a mut [u16],
pub struct Map<S, D: Deref<Target = S>>
where
S: Index<usize, Output = u16> + ?Sized,
{
pub store: D,
pub dimensions: Vector2D<u32>,
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 {
if x < 0 || x as u32 >= self.dimensions.x {
self.default
@ -59,8 +64,8 @@ impl<'a> Map<'a> {
}
}
impl<'a> Background<'a> {
unsafe fn new(background: u8, block: u8) -> 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<S, D> {
let mut b = Background {
background,
block,
@ -129,12 +134,13 @@ impl<'a> Background<'a> {
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;
&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);
}
@ -259,7 +265,9 @@ impl Tiled0 {
}
/// 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 {
return Err("too many backgrounds created, maximum is 4");
}