From 2c8fce40d320e5b72a53ded49bcefaadb889ef4d Mon Sep 17 00:00:00 2001 From: Gwilym Kuiper Date: Mon, 31 Jan 2022 20:53:33 +0000 Subject: [PATCH] Store the used backgrounds in a bit array --- agb/src/bitarray.rs | 20 ++++++++++ agb/src/display/background.rs | 20 +++++----- .../the-hat-chooses-the-wizard/Cargo.lock | 39 +++++++++++++++++++ 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/agb/src/bitarray.rs b/agb/src/bitarray.rs index 25c99e16..f953ac15 100644 --- a/agb/src/bitarray.rs +++ b/agb/src/bitarray.rs @@ -1,3 +1,4 @@ +#[derive(Debug)] pub struct Bitarray { a: [u32; N], } @@ -21,7 +22,26 @@ impl Bitarray { let value_mask = value << (index % 32); self.a[index / 32] = self.a[index / 32] & !mask | value_mask } + + pub fn first_zero(&self) -> Option { + for index in 0..N * 32 { + if let Some(bit) = self.get(index) { + if !bit { + return Some(index); + } + } + } + + None + } } + +impl Default for Bitarray { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/agb/src/display/background.rs b/agb/src/display/background.rs index da86088e..275f8629 100644 --- a/agb/src/display/background.rs +++ b/agb/src/display/background.rs @@ -1,7 +1,10 @@ +use core::cell::RefCell; + use alloc::vec::Vec; use alloc::{boxed::Box, vec}; use hashbrown::HashMap; +use crate::bitarray::Bitarray; use crate::{ display, fixnum::{Rect, Vector2D}, @@ -611,8 +614,7 @@ fn div_ceil(x: i32, y: i32) -> i32 { } pub struct Tiled0<'a> { - num_regular: u8, - next_screenblock: u8, + regular: RefCell>, pub vram: VRamManager<'a>, } @@ -623,22 +625,22 @@ impl Tiled0<'_> { set_graphics_mode(DisplayMode::Tiled0); Self { - num_regular: 0, - next_screenblock: 16, + regular: Default::default(), vram: VRamManager::new(), } } pub fn background(&mut self, priority: Priority) -> RegularMap { - if self.num_regular == 4 { - panic!("Can only create 4 backgrounds"); + let mut regular = self.regular.borrow_mut(); + let new_background = regular.first_zero().unwrap(); + if new_background >= 4 { + panic!("can only have 4 active backgrounds"); } - let bg = RegularMap::new(self.num_regular, self.next_screenblock, priority); + let bg = RegularMap::new(new_background as u8, (new_background + 16) as u8, priority); - self.num_regular += 1; - self.next_screenblock += 1; + regular.set(new_background, true); bg } diff --git a/examples/the-hat-chooses-the-wizard/Cargo.lock b/examples/the-hat-chooses-the-wizard/Cargo.lock index 72ded3c8..aed684ac 100644 --- a/examples/the-hat-chooses-the-wizard/Cargo.lock +++ b/examples/the-hat-chooses-the-wizard/Cargo.lock @@ -24,6 +24,7 @@ dependencies = [ "agb_sound_converter", "bare-metal", "bitflags", + "hashbrown", ] [[package]] @@ -65,6 +66,17 @@ dependencies = [ "syn", ] +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -77,6 +89,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603" +[[package]] +name = "bare-metal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603" + [[package]] name = "bitflags" version = "1.3.2" @@ -136,6 +154,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "hashbrown" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c21d40587b92fa6a6c6e3c1bdbf87d75511db5672f9c93175574b3a00df1758" +dependencies = [ + "ahash", +] + [[package]] name = "hound" version = "3.4.0" @@ -219,6 +246,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "once_cell" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5" + [[package]] name = "png" version = "0.17.4" @@ -357,6 +390,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasi" version = "0.10.2+wasi-snapshot-preview1"