diff --git a/src/ppu.rs b/src/ppu.rs index 3d28f0c..fcbba47 100644 --- a/src/ppu.rs +++ b/src/ppu.rs @@ -196,17 +196,15 @@ 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) { - if attr.x <= (self.x_pos + 8) { - // self.fetcher.obj.resume(); TODO: Try running only when there's a sprite - self.fetcher.bg.reset(); - self.fetcher.bg.pause(); - self.fifo.pause(); + 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(); + self.fetcher.bg.pause(); + self.fifo.pause(); - obj_attr = Some(*attr); - break; - } + obj_attr = Some(*attr); + break; } } @@ -1012,6 +1010,36 @@ struct ObjectBuffer { len: usize, } +impl ObjectBuffer { + pub fn iter(&self) -> std::slice::Iter<'_, Option> { + self.into_iter() + } + + pub fn iter_mut(&mut self) -> &mut std::slice::IterMut<'_, Option> { + todo!("Figure out the lifetimes for ObjectBuffer::iter_mut()"); + } +} + +impl<'a> IntoIterator for &'a ObjectBuffer { + type Item = &'a Option; + + type IntoIter = std::slice::Iter<'a, Option>; + + fn into_iter(self) -> Self::IntoIter { + self.buf.iter() + } +} + +impl<'a> IntoIterator for &'a mut ObjectBuffer { + type Item = &'a Option; + + type IntoIter = std::slice::Iter<'a, Option>; + + 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,