Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add get_pixel to buffered_graphics #193

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 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 self.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,19 @@ where

(idx, bit)
}
};
}
}

pub fn get_pixel(&mut 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 (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