Minor changes

This commit is contained in:
transistor 2023-03-05 13:32:45 -08:00
parent 2cc7db8bf5
commit 0ac3f48b64
4 changed files with 53 additions and 34 deletions

View File

@ -1,11 +1,9 @@
use std::mem; use std::mem;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::host::traits::{BlitableSurface, ClockedQueue, WindowUpdater};
use crate::Clock; use crate::Clock;
use crate::Error; use crate::Error;
use crate::host::traits::{WindowUpdater, BlitableSurface, ClockedQueue};
pub const MASK_COLOUR: u32 = 0xFFFFFFFF; pub const MASK_COLOUR: u32 = 0xFFFFFFFF;
@ -18,7 +16,11 @@ pub struct Frame {
impl Frame { impl Frame {
pub fn new(width: u32, height: u32) -> Self { 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<Mutex<Frame>> { pub fn new_shared(width: u32, height: u32) -> Arc<Mutex<Frame>> {
@ -26,7 +28,6 @@ impl Frame {
} }
} }
impl BlitableSurface for Frame { impl BlitableSurface for Frame {
fn set_size(&mut self, width: u32, height: u32) { fn set_size(&mut self, width: u32, height: u32) {
self.width = width; self.width = width;
@ -36,21 +37,37 @@ impl BlitableSurface for Frame {
fn set_pixel(&mut self, pos_x: u32, pos_y: u32, pixel: u32) { fn set_pixel(&mut self, pos_x: u32, pos_y: u32, pixel: u32) {
match pixel { match pixel {
MASK_COLOUR => { }, MASK_COLOUR => {}
value if pos_x < self.width && pos_y < self.height => { value if pos_x < self.width && pos_y < self.height => {
self.bitmap[(pos_x + (pos_y * self.width)) as usize] = value; self.bitmap[(pos_x + (pos_y * self.width)) as usize] = value;
}, }
_ => { }, _ => {}
} }
} }
fn blit<B: Iterator<Item=u32>>(&mut self, pos_x: u32, pos_y: u32, mut bitmap: B, width: u32, height: u32) { fn blit<B: Iterator<Item = u32>>(&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 y in pos_y..(pos_y + height) {
for x in pos_x..(pos_x + width) { for x in pos_x..(pos_x + width) {
match bitmap.next().unwrap() { match bitmap.next().unwrap() {
MASK_COLOUR => { }, MASK_COLOUR => {}
value if x < self.width && y < self.height => { self.bitmap[(x + (y * self.width)) as usize] = value; }, 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) { fn clear(&mut self, value: u32) {
let value = if value == MASK_COLOUR { 0 } else { value }; let value = if value == MASK_COLOUR { 0 } else { value };
for i in 0..((self.width as usize) * (self.height as usize)) { self.bitmap.iter_mut().for_each(|pixel| *pixel = value);
self.bitmap[i] = value;
}
} }
} }
@ -106,7 +121,6 @@ impl WindowUpdater for FrameSwapper {
} }
} }
#[derive(Clone)] #[derive(Clone)]
pub struct FrameQueue { pub struct FrameQueue {
max_size: (u32, u32), max_size: (u32, u32),
@ -136,7 +150,8 @@ impl WindowUpdater for FrameQueue {
} }
fn take_frame(&mut self) -> Result<Frame, Error> { fn take_frame(&mut self) -> Result<Frame, Error> {
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"))
} }
} }

View File

@ -1,9 +1,10 @@
use std::rc::Rc;
use std::cell::{Cell, RefCell, RefMut}; use std::cell::{Cell, RefCell, RefMut};
use std::rc::Rc;
pub trait Observable<T> { pub trait Observable<T> {
fn set_observer<F>(&self, f: F) where F: Fn(&T) + 'static; fn set_observer<F>(&self, f: F)
where
F: Fn(&T) + 'static;
fn notify(&self); fn notify(&self);
} }
@ -16,7 +17,6 @@ type Input<T> = Signal<T>;
#[allow(dead_code)] #[allow(dead_code)]
type TriState<T> = Signal<T>; type TriState<T> = Signal<T>;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Signal<T: Copy>(Rc<Cell<T>>); pub struct Signal<T: Copy>(Rc<Cell<T>>);
@ -34,7 +34,6 @@ impl<T: Copy> Signal<T> {
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct EdgeSignal(Signal<bool>); pub struct EdgeSignal(Signal<bool>);
@ -54,7 +53,6 @@ impl EdgeSignal {
} }
} }
#[derive(Clone)] #[derive(Clone)]
pub struct ObservableSignal<T>(Rc<RefCell<(T, Option<Box<dyn Fn(&T)>>)>>); pub struct ObservableSignal<T>(Rc<RefCell<(T, Option<Box<dyn Fn(&T)>>)>>);
@ -69,7 +67,10 @@ impl<T> ObservableSignal<T> {
} }
impl<T> Observable<T> for ObservableSignal<T> { impl<T> Observable<T> for ObservableSignal<T> {
fn set_observer<F>(&self, f: F) where F: Fn(&T) + 'static { fn set_observer<F>(&self, f: F)
where
F: Fn(&T) + 'static,
{
self.0.borrow_mut().1 = Some(Box::new(f)); self.0.borrow_mut().1 = Some(Box::new(f));
} }
@ -81,7 +82,6 @@ impl<T> Observable<T> for ObservableSignal<T> {
} }
} }
pub struct ObservableEdgeSignal(ObservableSignal<bool>); pub struct ObservableEdgeSignal(ObservableSignal<bool>);
impl ObservableEdgeSignal { impl ObservableEdgeSignal {
@ -103,7 +103,10 @@ impl ObservableEdgeSignal {
} }
impl Observable<bool> for ObservableEdgeSignal { impl Observable<bool> for ObservableEdgeSignal {
fn set_observer<F>(&self, f: F) where F: Fn(&bool) + 'static { fn set_observer<F>(&self, f: F)
where
F: Fn(&bool) + 'static,
{
self.0.set_observer(f) self.0.set_observer(f)
} }
@ -111,5 +114,3 @@ impl Observable<bool> for ObservableEdgeSignal {
self.0.notify() self.0.notify()
} }
} }

View File

@ -545,17 +545,17 @@ impl Ym7101State {
_ => [ pixel_sprite, pixel_a, pixel_b, bg_colour ], _ => [ pixel_sprite, pixel_a, pixel_b, bg_colour ],
}; };
for i in 0..pixels.len() { for (i, pixel) in pixels.iter().enumerate() {
if pixels[i].1 != 0 || i == pixels.len() - 1 { if pixel.1 != 0 || i == pixels.len() - 1 {
let mode = if pixels[i] == (3, 14) { let mode = if *pixel == (3, 14) {
ColourMode::Highlight ColourMode::Highlight
} else if (!priority_a && !priority_b) || pixels[i] == (3, 15) { } else if (!priority_a && !priority_b) || *pixel == (3, 15) {
ColourMode::Shadow ColourMode::Shadow
} else { } else {
ColourMode::Normal 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; break;
} }
} }

View File

@ -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) * 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) * re-enable sound on webassembly, see if it works (it does not. Very lagged and jittery with what sounds like repeated frames)