chore(ppu): implement IntoIterator and iter() for ObjectBuffer

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-04-24 02:13:35 -05:00
parent 067049e2dd
commit 2586314f9a
1 changed files with 38 additions and 18 deletions

View File

@ -196,17 +196,15 @@ impl Ppu {
// Determine whether we need to enable sprite fetching // Determine whether we need to enable sprite fetching
let mut obj_attr = None; let mut obj_attr = None;
for i in 0..self.obj_buffer.len() { for attr in self.obj_buffer.iter().flatten() {
if let Some(attr) = self.obj_buffer.get(i) { if attr.x <= (self.x_pos + 8) {
if attr.x <= (self.x_pos + 8) { // self.fetcher.obj.resume(); TODO: Try running only when there's a sprite
// self.fetcher.obj.resume(); TODO: Try running only when there's a sprite self.fetcher.bg.reset();
self.fetcher.bg.reset(); self.fetcher.bg.pause();
self.fetcher.bg.pause(); self.fifo.pause();
self.fifo.pause();
obj_attr = Some(*attr); obj_attr = Some(*attr);
break; break;
}
} }
} }
@ -1012,6 +1010,36 @@ struct ObjectBuffer {
len: usize, 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 { impl ObjectBuffer {
pub fn is_full(&self) -> bool { pub fn is_full(&self) -> bool {
self.len == OBJECT_LIMIT self.len == OBJECT_LIMIT
@ -1027,14 +1055,6 @@ impl ObjectBuffer {
self.len += 1; 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) { pub fn remove(&mut self, attr: &ObjectAttribute) {
let maybe_index = self.buf.iter().position(|maybe_attr| match maybe_attr { let maybe_index = self.buf.iter().position(|maybe_attr| match maybe_attr {
Some(other_attr) => attr == other_attr, Some(other_attr) => attr == other_attr,