Skip to content

Commit

Permalink
add get_pixel to buffered_graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas725 authored Aug 31, 2023
1 parent 5c50f11 commit 9ff1f37
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/mode/buffered_graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,8 @@ where
}
}

/// Turn a pixel on or off. A non-zero `value` is treated as on, `0` as off. If the X and Y
/// coordinates are out of the bounds of the display, this method call is a noop.
pub fn set_pixel(&mut self, x: u32, y: u32, value: bool) {
let value = value as u8;
let rotation = self.rotation;

let (idx, bit) = match rotation {
fn pixel_location(&self, x: u32, y: u32) -> (usize, u32) {
match rotation {
DisplayRotation::Rotate0 | DisplayRotation::Rotate180 => {
let idx = ((y as usize) / 8 * SIZE::WIDTH as usize) + (x as usize);
let bit = y % 8;
Expand All @@ -175,7 +170,21 @@ where

(idx, bit)
}
};
}
}

pub fn get_pixel(&self, x: u32, y: u32) -> Option<bool> {
let (idx, bit) = self.pixel_location(x,y);
self.mode.buffer.as_mut().get(idx).map(|byte| byte & !(1 << bit) != 0)
}

/// Turn a pixel on or off. A non-zero `value` is treated as on, `0` as off. If the X and Y
/// coordinates are out of the bounds of the display, this method call is a noop.
pub fn set_pixel(&mut self, x: u32, y: u32, value: bool) {
let value = value as u8;
let rotation = self.rotation;

let (idx, bit) = self.pixel_location(x,y);

if let Some(byte) = self.mode.buffer.as_mut().get_mut(idx) {
// Keep track of max and min values
Expand Down

0 comments on commit 9ff1f37

Please sign in to comment.