diff --git a/emulator/core/src/host/gfx.rs b/emulator/core/src/host/gfx.rs index f5dfa72..45a4024 100644 --- a/emulator/core/src/host/gfx.rs +++ b/emulator/core/src/host/gfx.rs @@ -1,11 +1,9 @@ - use std::mem; use std::sync::{Arc, Mutex}; +use crate::host::traits::{BlitableSurface, ClockedQueue, WindowUpdater}; use crate::Clock; use crate::Error; -use crate::host::traits::{WindowUpdater, BlitableSurface, ClockedQueue}; - pub const MASK_COLOUR: u32 = 0xFFFFFFFF; @@ -18,7 +16,11 @@ pub struct Frame { impl Frame { pub fn new(width: u32, height: u32) -> Self { - Self { width, height, bitmap: vec![0; (width * height) as usize] } + Self { + width, + height, + bitmap: vec![0; (width * height) as usize], + } } pub fn new_shared(width: u32, height: u32) -> Arc> { @@ -26,7 +28,6 @@ impl Frame { } } - impl BlitableSurface for Frame { fn set_size(&mut self, width: u32, height: u32) { self.width = width; @@ -36,21 +37,37 @@ impl BlitableSurface for Frame { fn set_pixel(&mut self, pos_x: u32, pos_y: u32, pixel: u32) { match pixel { - MASK_COLOUR => { }, + MASK_COLOUR => {} value if pos_x < self.width && pos_y < self.height => { self.bitmap[(pos_x + (pos_y * self.width)) as usize] = value; - }, - _ => { }, + } + _ => {} } } - fn blit>(&mut self, pos_x: u32, pos_y: u32, mut bitmap: B, width: u32, height: u32) { + fn blit>(&mut self, pos_x: u32, pos_y: u32, mut bitmap: B, width: u32, height: u32) { + /* + (pos_y..(pos_y + height)) + .for_each(|y| { + self.bitmap[(y * self.width) as usize .. (y * self.width + self.width) as usize] + .iter_mut() + .for_each(|pixel| + match bitmap.next().unwrap() { + MASK_COLOUR => {}, + value => *pixel = value, + } + ) + }); + */ + for y in pos_y..(pos_y + height) { for x in pos_x..(pos_x + width) { match bitmap.next().unwrap() { - MASK_COLOUR => { }, - value if x < self.width && y < self.height => { self.bitmap[(x + (y * self.width)) as usize] = value; }, - _ => { }, + MASK_COLOUR => {} + value if x < self.width && y < self.height => { + self.bitmap[(x + (y * self.width)) as usize] = value; + } + _ => {} } } } @@ -58,9 +75,7 @@ impl BlitableSurface for Frame { fn clear(&mut self, value: u32) { let value = if value == MASK_COLOUR { 0 } else { value }; - for i in 0..((self.width as usize) * (self.height as usize)) { - self.bitmap[i] = value; - } + self.bitmap.iter_mut().for_each(|pixel| *pixel = value); } } @@ -106,7 +121,6 @@ impl WindowUpdater for FrameSwapper { } } - #[derive(Clone)] pub struct FrameQueue { max_size: (u32, u32), @@ -136,7 +150,8 @@ impl WindowUpdater for FrameQueue { } fn take_frame(&mut self) -> Result { - self.latest().map(|(_, f)| f).ok_or_else(|| Error::new("No frame available")) + self.latest() + .map(|(_, f)| f) + .ok_or_else(|| Error::new("No frame available")) } } - diff --git a/emulator/core/src/signals.rs b/emulator/core/src/signals.rs index ffaf04f..36aa222 100644 --- a/emulator/core/src/signals.rs +++ b/emulator/core/src/signals.rs @@ -1,9 +1,10 @@ - -use std::rc::Rc; use std::cell::{Cell, RefCell, RefMut}; +use std::rc::Rc; pub trait Observable { - fn set_observer(&self, f: F) where F: Fn(&T) + 'static; + fn set_observer(&self, f: F) + where + F: Fn(&T) + 'static; fn notify(&self); } @@ -16,7 +17,6 @@ type Input = Signal; #[allow(dead_code)] type TriState = Signal; - #[derive(Clone, Debug)] pub struct Signal(Rc>); @@ -34,7 +34,6 @@ impl Signal { } } - #[derive(Clone, Debug)] pub struct EdgeSignal(Signal); @@ -54,7 +53,6 @@ impl EdgeSignal { } } - #[derive(Clone)] pub struct ObservableSignal(Rc>)>>); @@ -69,7 +67,10 @@ impl ObservableSignal { } impl Observable for ObservableSignal { - fn set_observer(&self, f: F) where F: Fn(&T) + 'static { + fn set_observer(&self, f: F) + where + F: Fn(&T) + 'static, + { self.0.borrow_mut().1 = Some(Box::new(f)); } @@ -81,7 +82,6 @@ impl Observable for ObservableSignal { } } - pub struct ObservableEdgeSignal(ObservableSignal); impl ObservableEdgeSignal { @@ -103,7 +103,10 @@ impl ObservableEdgeSignal { } impl Observable for ObservableEdgeSignal { - fn set_observer(&self, f: F) where F: Fn(&bool) + 'static { + fn set_observer(&self, f: F) + where + F: Fn(&bool) + 'static, + { self.0.set_observer(f) } @@ -111,5 +114,3 @@ impl Observable for ObservableEdgeSignal { self.0.notify() } } - - diff --git a/emulator/systems/genesis/src/peripherals/ym7101.rs b/emulator/systems/genesis/src/peripherals/ym7101.rs index 96f837f..609eb01 100644 --- a/emulator/systems/genesis/src/peripherals/ym7101.rs +++ b/emulator/systems/genesis/src/peripherals/ym7101.rs @@ -545,17 +545,17 @@ impl Ym7101State { _ => [ pixel_sprite, pixel_a, pixel_b, bg_colour ], }; - for i in 0..pixels.len() { - if pixels[i].1 != 0 || i == pixels.len() - 1 { - let mode = if pixels[i] == (3, 14) { + for (i, pixel) in pixels.iter().enumerate() { + if pixel.1 != 0 || i == pixels.len() - 1 { + let mode = if *pixel == (3, 14) { ColourMode::Highlight - } else if (!priority_a && !priority_b) || pixels[i] == (3, 15) { + } else if (!priority_a && !priority_b) || *pixel == (3, 15) { ColourMode::Shadow } else { ColourMode::Normal }; - frame.set_pixel(x as u32, y as u32, self.get_palette_colour(pixels[i].0, pixels[i].1, mode)); + frame.set_pixel(x as u32, y as u32, self.get_palette_colour(pixel.0, pixel.1, mode)); break; } } diff --git a/todo.txt b/todo.txt index 7805564..2d4ce9d 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,7 @@ +* if index access always does a bounds check, you should try to convert some of the copy loops to use iterators + instead, such as frame updating, audio copying, etc + * can you refactor the update timeout to put it in rust? Would that make it faster? (the tricky part is the closure) * re-enable sound on webassembly, see if it works (it does not. Very lagged and jittery with what sounds like repeated frames)