diff --git a/gb-emu/src/window.rs b/gb-emu/src/window.rs index 434f7f0..738055f 100644 --- a/gb-emu/src/window.rs +++ b/gb-emu/src/window.rs @@ -23,17 +23,17 @@ use winit_input_helper::WinitInputHelper; pub struct WindowInfo { id: WindowId, - data: Arc>, + data: Arc, } pub struct WindowData { - scaled_buf: Vec<[u8; 4]>, - pixels: Pixels, + scaled_buf: Mutex>, + pixels: Mutex, } pub struct WindowManager { event_loop: EventLoop<()>, - windows: HashMap>>, + windows: HashMap>, input: Arc>, } @@ -76,14 +76,13 @@ impl WindowManager { } Event::RedrawRequested(window_id) => { if let Some(w) = self.windows.get(&window_id) { - if let Ok(mut w) = w.lock() { - let scaled = w.scaled_buf.clone(); + if let Ok(scaled_buf) = w.scaled_buf.lock() && let Ok(mut pixels) = w.pixels.lock() { for (pixel, source) in - w.pixels.frame_mut().chunks_exact_mut(4).zip(&scaled) + pixels.frame_mut().chunks_exact_mut(4).zip(scaled_buf.iter()) { pixel.copy_from_slice(source); } - w.pixels.render().unwrap(); + pixels.render().unwrap(); } } } @@ -96,7 +95,7 @@ impl WindowManager { pub struct WindowRenderer { window: Window, input: Arc>, - data: Arc>, + data: Arc, width: usize, height: usize, factor: usize, @@ -133,10 +132,10 @@ impl WindowRenderer { .unwrap() }; - let data = Arc::new(Mutex::new(WindowData { - scaled_buf: Vec::new(), - pixels, - })); + let data = Arc::new(WindowData { + scaled_buf: Mutex::new(Vec::new()), + pixels: Mutex::new(pixels), + }); let info = WindowInfo { id: window.id(), data: data.clone(), @@ -170,24 +169,24 @@ impl Renderer<[u8; 4]> for WindowRenderer { let h = (height * self.real_factor) as u32; self.window.set_inner_size(PhysicalSize::new(w, h)); - if let Ok(mut data) = self.data.lock() { - data.pixels = { + if let Ok(mut scaled_buf) = self.data.scaled_buf.lock() && let Ok(mut pixels) = self.data.pixels.lock() { + *pixels = { let window_size = self.window.inner_size(); let surface_texture = SurfaceTexture::new(window_size.width, window_size.height, &self.window); Pixels::new(window_size.width, window_size.height, surface_texture).unwrap() }; - data.scaled_buf.resize((w * h) as usize, [0; 4]); + scaled_buf.resize((w * h) as usize, [0; 4]); } self.window.request_redraw(); } fn display(&mut self, buffer: &[[u8; 4]]) { - if let Ok(mut data) = self.data.lock() { + if let Ok(mut scaled_buf) = self.data.scaled_buf.lock() { scale_buffer_in_place( buffer, - &mut data.scaled_buf, + &mut scaled_buf, self.width, self.height, self.real_factor, diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 456a861..dc1beeb 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(exclusive_range_pattern, let_chains, bigint_helper_methods, step_trait)] +#![feature(exclusive_range_pattern, let_chains, bigint_helper_methods)] use crate::{ processor::{memory::Memory, Flags},