Update background and foreground independentently

This commit is contained in:
Gwilym Kuiper 2022-02-07 23:24:59 +00:00
parent 3941117aa7
commit 08aeb56f1e
3 changed files with 44 additions and 22 deletions

View file

@ -25,6 +25,18 @@ const TILE_BACKGROUND: MemoryMapped1DArray<u32, { 2048 * 8 }> =
const PALETTE_BACKGROUND: MemoryMapped1DArray<u16, 256> = const PALETTE_BACKGROUND: MemoryMapped1DArray<u16, 256> =
unsafe { MemoryMapped1DArray::new(0x0500_0000) }; unsafe { MemoryMapped1DArray::new(0x0500_0000) };
#[cfg(debug_assertions)]
const unsafe fn debug_unreachable_unchecked() -> ! {
unreachable!();
}
#[cfg(not(debug_assertions))]
const unsafe fn debug_unreachable_unchecked() -> ! {
use core::hint::unreachable_unchecked;
unreachable_unchecked();
}
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum TileFormat { pub enum TileFormat {
FourBpp, FourBpp,
@ -72,7 +84,7 @@ impl VRamState {
if let VRamState::ReferenceCounted(count, _) = self { if let VRamState::ReferenceCounted(count, _) = self {
*count += 1; *count += 1;
} else { } else {
panic!("Corrupted vram state"); unsafe { debug_unreachable_unchecked() };
} }
} }
@ -81,7 +93,7 @@ impl VRamState {
*count -= 1; *count -= 1;
(*count, *tile_ref) (*count, *tile_ref)
} else { } else {
panic!("Corrupted vram state"); unsafe { debug_unreachable_unchecked() };
} }
} }
} }
@ -134,7 +146,7 @@ impl<'a> VRamManager<'a> {
self.tilesets[ptr] = tileset; self.tilesets[ptr] = tileset;
ptr ptr
} }
_ => panic!("Free pointer shouldn't point to valid data"), _ => unsafe { debug_unreachable_unchecked() },
} }
} else { } else {
self.tilesets.push(tileset); self.tilesets.push(tileset);
@ -149,7 +161,7 @@ impl<'a> VRamManager<'a> {
match tileset { match tileset {
ArenaStorageItem::Data(_, generation) => { ArenaStorageItem::Data(_, generation) => {
assert_eq!( debug_assert_eq!(
*generation, tile_set_ref.generation, *generation, tile_set_ref.generation,
"Tileset generation must be the same when removing" "Tileset generation must be the same when removing"
); );
@ -162,7 +174,7 @@ impl<'a> VRamManager<'a> {
self.free_pointer = Some(tile_set_ref.id as usize); self.free_pointer = Some(tile_set_ref.id as usize);
} }
_ => panic!("Already freed, probably a double free?"), _ => unsafe { debug_unreachable_unchecked() },
} }
} }
@ -182,7 +194,7 @@ impl<'a> VRamManager<'a> {
self.vram_free_pointer = Some(next_free as usize); self.vram_free_pointer = Some(next_free as usize);
} }
} }
VRamState::ReferenceCounted(_, _) => panic!("Corrupted tile reference state"), VRamState::ReferenceCounted(_, _) => unsafe { debug_unreachable_unchecked() },
} }
self.references[ptr] = VRamState::ReferenceCounted(1, tile_ref); self.references[ptr] = VRamState::ReferenceCounted(1, tile_ref);
@ -196,7 +208,7 @@ impl<'a> VRamManager<'a> {
let tile_slice = if let ArenaStorageItem::Data(data, generation) = let tile_slice = if let ArenaStorageItem::Data(data, generation) =
&self.tilesets[tile_set_ref.id as usize] &self.tilesets[tile_set_ref.id as usize]
{ {
assert_eq!( debug_assert_eq!(
*generation, tile_set_ref.generation, *generation, tile_set_ref.generation,
"Stale tile data requested" "Stale tile data requested"
); );
@ -204,7 +216,7 @@ impl<'a> VRamManager<'a> {
let tile_offset = (tile as usize) * data.format.tile_size() / 4; let tile_offset = (tile as usize) * data.format.tile_size() / 4;
&data.tiles[tile_offset..(tile_offset + data.format.tile_size() / 4)] &data.tiles[tile_offset..(tile_offset + data.format.tile_size() / 4)]
} else { } else {
panic!("Cannot find tile data at given reference"); unsafe { debug_unreachable_unchecked() };
}; };
let tile_size_in_words = TileFormat::FourBpp.tile_size() / 4; let tile_size_in_words = TileFormat::FourBpp.tile_size() / 4;

View file

@ -25,6 +25,7 @@ dependencies = [
"bare-metal", "bare-metal",
"bitflags", "bitflags",
"hashbrown", "hashbrown",
"rustc-hash",
] ]
[[package]] [[package]]
@ -89,12 +90,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603" checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603"
[[package]]
name = "bare-metal"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -103,9 +98,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.7.3" version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439989e6b8c38d1b6570a384ef1e49c8848128f5a97f3914baef02920842712f" checksum = "0e851ca7c24871e7336801608a4797d7376545b6928a10d32d75685687141ead"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
@ -248,15 +243,15 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.9.0" version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]] [[package]]
name = "png" name = "png"
version = "0.17.4" version = "0.17.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02cd7d51cea7e2fa6bbcb8af5fbcad15b871451bfc2d20ed72dff2f4ae072a84" checksum = "dc38c0ad57efb786dd57b9864e5b18bae478c00c824dc55a38bbc9da95dde3ba"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"crc32fast", "crc32fast",
@ -318,6 +313,12 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.9" version = "1.0.9"

View file

@ -285,8 +285,11 @@ impl<'a, 'b> Map<'a, 'b> {
self.foreground.commit(); self.foreground.commit();
} }
pub fn init(&mut self, vram: &mut VRamManager) { pub fn init_background(&mut self, vram: &mut VRamManager) {
self.background.init(vram, self.position.floor()); self.background.init(vram, self.position.floor());
}
pub fn init_foreground(&mut self, vram: &mut VRamManager) {
self.foreground.init(vram, self.position.floor()); self.foreground.init(vram, self.position.floor());
} }
} }
@ -874,7 +877,13 @@ fn main(mut agb: agb::Gba) -> ! {
agb::input::ButtonController::new(), agb::input::ButtonController::new(),
); );
level.background.init(&mut vram); level.background.init_background(&mut vram);
music_box.before_frame(&mut mixer);
mixer.frame();
vblank.wait_for_vblank();
mixer.after_vblank();
level.background.init_foreground(&mut vram);
for _ in 0..60 { for _ in 0..60 {
music_box.before_frame(&mut mixer); music_box.before_frame(&mut mixer);