basic i/o
This commit is contained in:
78
src/screen.rs
Normal file
78
src/screen.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use embedded_graphics::{pixelcolor::Rgb888, prelude::*, primitives::Rectangle};
|
||||
use linfb::{Framebuffer, shape::Color};
|
||||
|
||||
use crate::rgba::Rgba;
|
||||
|
||||
|
||||
pub struct Screen {
|
||||
framebuffer: Framebuffer,
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
impl Screen {
|
||||
pub fn new() -> Self {
|
||||
let framebuffer = Framebuffer::open()
|
||||
.expect("Failed to open framebuffer");
|
||||
|
||||
let width = framebuffer.screen_info.xres;
|
||||
let height = framebuffer.screen_info.yres;
|
||||
|
||||
Self {
|
||||
framebuffer,
|
||||
width,
|
||||
height
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(&self) -> u32 {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn height(&self) -> u32 {
|
||||
self.height
|
||||
}
|
||||
}
|
||||
|
||||
impl Dimensions for Screen {
|
||||
fn bounding_box(&self) -> Rectangle {
|
||||
Rectangle::new(
|
||||
Point::new(0, 0),
|
||||
Size::new(self.width, self.height),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl DrawTarget for Screen {
|
||||
type Color = Rgba<Rgb888>;
|
||||
type Error = Box<dyn std::error::Error>;
|
||||
|
||||
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
|
||||
where
|
||||
I: IntoIterator<Item = embedded_graphics::Pixel<Self::Color>>,
|
||||
{
|
||||
let width = self.bounding_box().size.width;
|
||||
let height = self.bounding_box().size.height;
|
||||
|
||||
for p in pixels {
|
||||
let x = p.0.x as u32;
|
||||
let y = p.0.y as u32;
|
||||
|
||||
let color = Color {
|
||||
red: p.1.r(),
|
||||
green: p.1.g(),
|
||||
blue: p.1.b(),
|
||||
alpha: p.1.a(),
|
||||
};
|
||||
|
||||
if !(0..width).contains(&x) || !(0..height).contains(&y) {
|
||||
continue;
|
||||
}
|
||||
|
||||
self.framebuffer.set_pixel(x, y, color);
|
||||
}
|
||||
self.framebuffer.flush();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user