allocate palettes too

This commit is contained in:
Corwin 2022-02-12 14:40:31 +00:00
parent 6a2c9d8826
commit d764ff9900
2 changed files with 85 additions and 13 deletions

View file

@ -21,6 +21,13 @@ static SPRITE_ALLOCATOR: BlockAllocator = unsafe {
})
};
static PALETTE_ALLOCATOR: BlockAllocator = unsafe {
BlockAllocator::new(StartEnd {
start: || 0x0500_0200,
end: || 0x0500_0400,
})
};
const OBJECT_ATTRIBUTE_MEMORY: MemoryMapped1DArray<u16, 512> =
unsafe { MemoryMapped1DArray::new(0x0700_0000) };
const PALETTE_SPRITE: MemoryMapped1DArray<u16, 256> =
@ -74,6 +81,8 @@ impl Size {
struct SpriteBorrow<'a> {
id: SpriteId,
sprite_location: u16,
palette_location: u16,
controller: &'a RefCell<SpriteControllerInner>,
}
@ -83,12 +92,21 @@ struct Storage {
}
impl Storage {
fn from_ptr(d: NonNull<u8>) -> Self {
fn from_sprite_ptr(d: NonNull<u8>) -> Self {
Self {
location: (((d.as_ptr() as usize) - 0x06010000) / BYTES_PER_TILE_4BPP) as u16,
count: 1,
}
}
fn from_palette_ptr(d: NonNull<u8>) -> Self {
Self {
location: ((d.as_ptr() as usize - 0x0500_0200) / Palette16::layout().size()) as u16,
count: 1,
}
}
fn to_palette_ptr(&self) -> *mut u8 {
(self.location as usize * Palette16::layout().size() + 0x0500_0200) as *mut u8
}
}
pub struct Object<'a> {
@ -125,6 +143,21 @@ impl SpriteId {
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
struct PaletteId(usize);
impl PaletteId {
fn get_palette(self) -> &'static Palette16 {
unsafe { (self.0 as *const Palette16).as_ref().unwrap_unchecked() }
}
}
impl Palette16 {
fn get_id(&'static self) -> PaletteId {
PaletteId(self as *const _ as usize)
}
const fn layout() -> Layout {
Layout::new::<Self>()
}
}
impl Sprite {
fn get_id(&'static self) -> SpriteId {
SpriteId(self as *const _ as usize)
@ -140,49 +173,87 @@ impl SpriteController {
let id = sprite.get_id();
if let Some(storage) = inner.sprite.get_mut(&id) {
storage.count += 1;
let palette_location = inner.get_palette(sprite.palette).unwrap();
Some(SpriteBorrow {
id,
palette_location,
sprite_location: storage.location,
controller: &self.inner,
})
} else {
// layout is non zero sized, so this is safe to call
let dest = unsafe { SPRITE_ALLOCATOR.alloc(sprite.layout())? };
inner.sprite.insert(id, Storage::from_ptr(dest));
let palette_location = inner.get_palette(sprite.palette);
let palette_location = match palette_location {
Some(a) => a,
None => {
unsafe { SPRITE_ALLOCATOR.dealloc(dest.as_ptr(), sprite.layout()) }
return None;
}
};
// need to consider palette
todo!();
let storage = Storage::from_sprite_ptr(dest);
inner.sprite.insert(id, storage);
Some(SpriteBorrow {
id,
controller: &self.inner,
palette_location,
sprite_location: storage.location,
})
}
}
}
impl SpriteControllerInner {
fn get_palette(&mut self, palette: &'static Palette16) -> Option<u16> {
let id = palette.get_id();
if let Some(storage) = self.palette.get_mut(&id) {
storage.count += 1;
Some(storage.location)
} else {
let dest = unsafe { PALETTE_ALLOCATOR.alloc(Palette16::layout())? };
let storage = Storage::from_palette_ptr(dest);
self.palette.insert(id, storage);
Some(storage.location)
}
}
fn return_palette(&mut self, palette: &'static Palette16) {
let id = palette.get_id();
self.palette
.entry(id)
.and_replace_entry_with(|_, mut storage| {
storage.count -= 1;
if storage.count == 0 {
unsafe {
PALETTE_ALLOCATOR.dealloc(storage.to_palette_ptr(), Palette16::layout());
}
None
} else {
Some(storage)
}
});
}
}
impl<'a> Drop for SpriteBorrow<'a> {
fn drop(&mut self) {
let inner = self.controller.borrow_mut();
let entry = inner
inner
.sprite
.entry(self.id)
.and_replace_entry_with(|_, mut storage| {
storage.count -= 1;
if storage.count == 0 {
inner.return_palette(self.id.get_sprite().palette);
None
} else {
Some(storage)
}
});
match entry {
Entry::Vacant(_) => {
// free the underlying resource.
// palette might be unused too.
}
_ => {}
}
}
}

View file

@ -1,3 +1,4 @@
#[repr(C)]
#[derive(Clone)]
pub struct Palette16 {
pub(crate) colours: [u16; 16],