mirror of
https://github.com/italicsjenga/agb.git
synced 2025-01-26 00:56:38 +11:00
Store the used backgrounds in a bit array
This commit is contained in:
parent
2d99d017fc
commit
2c8fce40d3
3 changed files with 70 additions and 9 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Bitarray<const N: usize> {
|
pub struct Bitarray<const N: usize> {
|
||||||
a: [u32; N],
|
a: [u32; N],
|
||||||
}
|
}
|
||||||
|
@ -21,7 +22,26 @@ impl<const N: usize> Bitarray<N> {
|
||||||
let value_mask = value << (index % 32);
|
let value_mask = value << (index % 32);
|
||||||
self.a[index / 32] = self.a[index / 32] & !mask | value_mask
|
self.a[index / 32] = self.a[index / 32] & !mask | value_mask
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn first_zero(&self) -> Option<usize> {
|
||||||
|
for index in 0..N * 32 {
|
||||||
|
if let Some(bit) = self.get(index) {
|
||||||
|
if !bit {
|
||||||
|
return Some(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Default for Bitarray<N> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
use core::cell::RefCell;
|
||||||
|
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use alloc::{boxed::Box, vec};
|
use alloc::{boxed::Box, vec};
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
|
|
||||||
|
use crate::bitarray::Bitarray;
|
||||||
use crate::{
|
use crate::{
|
||||||
display,
|
display,
|
||||||
fixnum::{Rect, Vector2D},
|
fixnum::{Rect, Vector2D},
|
||||||
|
@ -611,8 +614,7 @@ fn div_ceil(x: i32, y: i32) -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Tiled0<'a> {
|
pub struct Tiled0<'a> {
|
||||||
num_regular: u8,
|
regular: RefCell<Bitarray<1>>,
|
||||||
next_screenblock: u8,
|
|
||||||
|
|
||||||
pub vram: VRamManager<'a>,
|
pub vram: VRamManager<'a>,
|
||||||
}
|
}
|
||||||
|
@ -623,22 +625,22 @@ impl Tiled0<'_> {
|
||||||
set_graphics_mode(DisplayMode::Tiled0);
|
set_graphics_mode(DisplayMode::Tiled0);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
num_regular: 0,
|
regular: Default::default(),
|
||||||
next_screenblock: 16,
|
|
||||||
|
|
||||||
vram: VRamManager::new(),
|
vram: VRamManager::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn background(&mut self, priority: Priority) -> RegularMap {
|
pub fn background(&mut self, priority: Priority) -> RegularMap {
|
||||||
if self.num_regular == 4 {
|
let mut regular = self.regular.borrow_mut();
|
||||||
panic!("Can only create 4 backgrounds");
|
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;
|
regular.set(new_background, true);
|
||||||
self.next_screenblock += 1;
|
|
||||||
|
|
||||||
bg
|
bg
|
||||||
}
|
}
|
||||||
|
|
39
examples/the-hat-chooses-the-wizard/Cargo.lock
generated
39
examples/the-hat-chooses-the-wizard/Cargo.lock
generated
|
@ -24,6 +24,7 @@ dependencies = [
|
||||||
"agb_sound_converter",
|
"agb_sound_converter",
|
||||||
"bare-metal",
|
"bare-metal",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -65,6 +66,17 @@ dependencies = [
|
||||||
"syn",
|
"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]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
|
@ -77,6 +89,12 @@ 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"
|
||||||
|
@ -136,6 +154,15 @@ dependencies = [
|
||||||
"wasi",
|
"wasi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8c21d40587b92fa6a6c6e3c1bdbf87d75511db5672f9c93175574b3a00df1758"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hound"
|
name = "hound"
|
||||||
version = "3.4.0"
|
version = "3.4.0"
|
||||||
|
@ -219,6 +246,12 @@ dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "da32515d9f6e6e489d7bc9d84c71b060db7247dc035bbe44eac88cf87486d8d5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "png"
|
name = "png"
|
||||||
version = "0.17.4"
|
version = "0.17.4"
|
||||||
|
@ -357,6 +390,12 @@ version = "0.2.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "version_check"
|
||||||
|
version = "0.9.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.10.2+wasi-snapshot-preview1"
|
version = "0.10.2+wasi-snapshot-preview1"
|
||||||
|
|
Loading…
Add table
Reference in a new issue