From e74b6f7108a75f1e721553e19b3dcd98e12ebafd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Vis=C3=A9e?= Date: Mon, 12 Feb 2018 15:40:58 +0100 Subject: [PATCH] Update image updates in all threads at the same time --- src/painter/painter.rs | 25 +++++++++++++++++-------- src/pix/canvas.rs | 8 +------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/painter/painter.rs b/src/painter/painter.rs index 79181f8..8450a9f 100644 --- a/src/painter/painter.rs +++ b/src/painter/painter.rs @@ -1,10 +1,8 @@ use std::io::Error; -use std::thread; -use std::time::Duration; +use std::sync::mpsc::Receiver; use image::{DynamicImage, Pixel}; -use app::PAINTER_IMAGE_WAIT_DELAY_MILLIS; use color::Color; use pix::client::Client; use rect::Rect; @@ -32,15 +30,21 @@ impl Painter { /// Perform work. /// Paint the whole defined area. - pub fn work(&mut self) -> Result<(), Error> { - // Make sure there is an image + pub fn work(&mut self, img_receiver: &Receiver) -> Result<(), Error> { + // Wait for an image, if no image has been set yet if self.image.is_none() { // Show a warning - println!("Painter thread has no image yet to paint, waiting..."); + println!("Painter thread is waiting for an image..."); // Sleep a little - thread::sleep(Duration::from_millis(PAINTER_IMAGE_WAIT_DELAY_MILLIS)); - return Ok(()); + // TODO: Do a proper error return here + match img_receiver.recv() { + Ok(image) => self.set_image(image), + Err(_) => return Ok(()), + } + + // We may now continue + println!("Painter thread received an image, painting..."); } // Get an RGB image @@ -49,6 +53,11 @@ impl Painter { // Loop through all the pixels, and set their color for x in 0..self.area.w { for y in 0..self.area.h { + // Update the image to paint + if let Ok(image) = img_receiver.try_recv() { + self.set_image(image); + } + // Get the pixel at this location let pixel = image.get_pixel(x, y); diff --git a/src/pix/canvas.rs b/src/pix/canvas.rs index 2268fb7..b1d1eba 100644 --- a/src/pix/canvas.rs +++ b/src/pix/canvas.rs @@ -95,14 +95,8 @@ impl Canvas { // Do some work loop { - // Work - painter.work() + painter.work(&rx) .expect("Painter failed to perform work"); - - // Update the image to paint - if let Ok(image) = rx.try_recv() { - painter.set_image(image); - } } });