Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. impl Display {
  2. pub fn clear(&mut self) {
  3. self.vram = [0x00; SCREEN_HEIGHT * SCREEN_WIDTH * 4];
  4. }
  5. // fixme: support alpha channel
  6. pub unsafe fn set_pixel_greyscale(&mut self, pos: usize, val: u8) {
  7. // set to unsafe as it does not do any bounds checking
  8. self.vram
  9. .iter_mut()
  10. .skip(pos * 4)
  11. .take(3)
  12. .for_each(|pixel| *pixel = val);
  13. self.vram
  14. .iter_mut()
  15. .skip(pos * 4 + 3)
  16. .take(1)
  17. .for_each(|pixel| *pixel = 0xFF);
  18. }
  19. pub fn set_pixel(&mut self, x: usize, y: usize) -> Result<(), Error> {
  20. match (x, y) {
  21. (_, _) if x < 64 && y < 64 => unsafe {
  22. self.set_pixel_greyscale(x + (SCREEN_WIDTH * y), 0xFF)
  23. },
  24. (_, _) if x >= 64 => {
  25. return Err(Error::InvalidValue(
  26. "Value x is too large (was > 63)".to_string(),
  27. ))
  28. }
  29. (_, _) if y > 62 => {
  30. return Err(Error::InvalidValue(
  31. "Value y is too large (was > 31)".to_string(),
  32. ))
  33. }
  34. (_, _) => (),
  35. }
  36. Ok(())
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement