Simplify type

This commit is contained in:
Gwilym Kuiper 2022-03-28 21:27:40 +01:00
parent 003c0d7e1a
commit d15f582aba

View file

@ -69,14 +69,29 @@ impl TileIndex {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct TileReference(NonNull<u32>); struct TileReference(NonNull<u32>);
#[derive(Clone, PartialEq, Eq, Hash)]
struct TileInTileSetReference {
tileset: NonNull<[u8]>,
tile: u16,
}
impl TileInTileSetReference {
fn new(tileset: &'_ TileSet<'_>, tile: u16) -> Self {
Self {
tileset: tileset.reference(),
tile,
}
}
}
pub struct VRamManager { pub struct VRamManager {
tile_set_to_vram: HashMap<(NonNull<[u8]>, u16), TileReference>, tile_set_to_vram: HashMap<TileInTileSetReference, TileReference>,
reference_counts: Vec<(u16, Option<(NonNull<[u8]>, u16)>)>, reference_counts: Vec<(u16, Option<TileInTileSetReference>)>,
} }
impl VRamManager { impl VRamManager {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
let tile_set_to_vram: HashMap<(NonNull<[u8]>, u16), TileReference> = let tile_set_to_vram: HashMap<TileInTileSetReference, TileReference> =
HashMap::with_capacity(256); HashMap::with_capacity(256);
Self { Self {
@ -96,7 +111,9 @@ impl VRamManager {
} }
pub(crate) fn add_tile(&mut self, tile_set: &TileSet<'_>, tile: u16) -> TileIndex { pub(crate) fn add_tile(&mut self, tile_set: &TileSet<'_>, tile: u16) -> TileIndex {
let reference = self.tile_set_to_vram.get(&(tile_set.reference(), tile)); let reference = self
.tile_set_to_vram
.get(&TileInTileSetReference::new(tile_set, tile));
if let Some(reference) = reference { if let Some(reference) = reference {
let index = Self::index_from_reference(*reference); let index = Self::index_from_reference(*reference);
@ -113,12 +130,12 @@ impl VRamManager {
let index = Self::index_from_reference(tile_reference); let index = Self::index_from_reference(tile_reference);
self.tile_set_to_vram self.tile_set_to_vram
.insert((tile_set.reference(), tile), tile_reference); .insert(TileInTileSetReference::new(tile_set, tile), tile_reference);
self.reference_counts self.reference_counts
.resize(self.reference_counts.len().max(index + 1), (0, None)); .resize(self.reference_counts.len().max(index + 1), (0, None));
self.reference_counts[index] = (1, Some((tile_set.reference(), tile))); self.reference_counts[index] = (1, Some(TileInTileSetReference::new(tile_set, tile)));
TileIndex::new(index) TileIndex::new(index)
} }
@ -142,8 +159,8 @@ impl VRamManager {
TILE_ALLOCATOR.dealloc_no_normalise(tile_reference.0.cast().as_ptr(), TILE_LAYOUT); TILE_ALLOCATOR.dealloc_no_normalise(tile_reference.0.cast().as_ptr(), TILE_LAYOUT);
} }
let tile_ref = self.reference_counts[index].1.unwrap(); let tile_ref = self.reference_counts[index].1.as_ref().unwrap();
self.tile_set_to_vram.remove(&tile_ref); self.tile_set_to_vram.remove(tile_ref);
self.reference_counts[index].1 = None; self.reference_counts[index].1 = None;
} }
@ -156,7 +173,7 @@ impl VRamManager {
) { ) {
if let Some(&reference) = self if let Some(&reference) = self
.tile_set_to_vram .tile_set_to_vram
.get(&(source_tile_set.reference(), source_tile)) .get(&TileInTileSetReference::new(source_tile_set, source_tile))
{ {
self.copy_tile_to_location(target_tile_set, target_tile, reference); self.copy_tile_to_location(target_tile_set, target_tile, reference);
} }