From efa55917889861d37a59fb334efc7b367ef5da1f Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 22 Oct 2018 13:10:24 +0200 Subject: [PATCH] Added some comments to the previous commit --- src/graphics.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index d06e82f..1342dba 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -20,22 +20,22 @@ impl Default for DisplayRotation { } } -pub struct Display { +pub struct Display<'a> { width: u32, height: u32, rotation: DisplayRotation, - buffer: [u8; 15000], //buffer: Box//[u8; 15000] + buffer: &'a mut [u8], //buffer: Box//[u8; 15000] } -impl Display { - pub fn new(width: u32, height: u32, buffer: & mut [u8]) -> Display { +impl<'a> Display<'a> { + pub fn new(width: u32, height: u32, buffer: &'a mut [u8]) -> Display<'a> { let len = buffer.len() as u32; assert!(width / 8 * height >= len); Display { width, height, rotation: DisplayRotation::default(), - buffer: [0u8; 15000], + buffer, } } @@ -59,25 +59,28 @@ impl Display { } -impl Drawing for Display { +impl<'a> Drawing for Display<'a> { fn draw(&mut self, item_pixels: T) where T: Iterator> { for Pixel(UnsignedCoord(x,y), color) in item_pixels { + if outside_display(x, y, self.width, self.height, self.rotation) { - return; + continue; } - let (idx, bit) = rotation(x, y, self.width, self.height, self.rotation); + // Give us index inside the buffer and the bit-position in that u8 which needs to be changed + let (index, bit) = rotation(x, y, self.width, self.height, self.rotation); + let index = index as usize; - let idx = idx as usize; + // "Draw" the Pixel on that bit match color { Color::Black => { - self.buffer[idx] &= !bit; + self.buffer[index] &= !bit; } Color::White => { - self.buffer[idx] |= bit; + self.buffer[index] |= bit; } } } @@ -85,7 +88,7 @@ impl Drawing for Display { } -pub(crate) fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> bool { +fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> bool { match rotation { DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => { if x >= width || y >= height { @@ -101,7 +104,7 @@ pub(crate) fn outside_display(x: u32, y: u32, width: u32, height: u32, rotation: false } -pub(crate) fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, u8) { +fn rotation(x: u32, y: u32, width: u32, height: u32, rotation: DisplayRotation) -> (u32, u8) { match rotation { DisplayRotation::Rotate0 => ( x / 8 + (width / 8) * y,