chore(ppu): implement IntoIterator and iter() for ObjectBuffer
This commit is contained in:
parent
067049e2dd
commit
2586314f9a
42
src/ppu.rs
42
src/ppu.rs
|
@ -196,8 +196,7 @@ impl Ppu {
|
|||
// Determine whether we need to enable sprite fetching
|
||||
let mut obj_attr = None;
|
||||
|
||||
for i in 0..self.obj_buffer.len() {
|
||||
if let Some(attr) = self.obj_buffer.get(i) {
|
||||
for attr in self.obj_buffer.iter().flatten() {
|
||||
if attr.x <= (self.x_pos + 8) {
|
||||
// self.fetcher.obj.resume(); TODO: Try running only when there's a sprite
|
||||
self.fetcher.bg.reset();
|
||||
|
@ -208,7 +207,6 @@ impl Ppu {
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(attr) = obj_attr {
|
||||
match self.fetcher.obj.state {
|
||||
|
@ -1012,6 +1010,36 @@ struct ObjectBuffer {
|
|||
len: usize,
|
||||
}
|
||||
|
||||
impl ObjectBuffer {
|
||||
pub fn iter(&self) -> std::slice::Iter<'_, Option<ObjectAttribute>> {
|
||||
self.into_iter()
|
||||
}
|
||||
|
||||
pub fn iter_mut(&mut self) -> &mut std::slice::IterMut<'_, Option<ObjectAttribute>> {
|
||||
todo!("Figure out the lifetimes for ObjectBuffer::iter_mut()");
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a ObjectBuffer {
|
||||
type Item = &'a Option<ObjectAttribute>;
|
||||
|
||||
type IntoIter = std::slice::Iter<'a, Option<ObjectAttribute>>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.buf.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a mut ObjectBuffer {
|
||||
type Item = &'a Option<ObjectAttribute>;
|
||||
|
||||
type IntoIter = std::slice::Iter<'a, Option<ObjectAttribute>>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.buf.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectBuffer {
|
||||
pub fn is_full(&self) -> bool {
|
||||
self.len == OBJECT_LIMIT
|
||||
|
@ -1027,14 +1055,6 @@ impl ObjectBuffer {
|
|||
self.len += 1;
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.len
|
||||
}
|
||||
|
||||
pub fn get(&self, index: usize) -> Option<&ObjectAttribute> {
|
||||
self.buf[index].as_ref()
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, attr: &ObjectAttribute) {
|
||||
let maybe_index = self.buf.iter().position(|maybe_attr| match maybe_attr {
|
||||
Some(other_attr) => attr == other_attr,
|
||||
|
|
Loading…
Reference in New Issue