render: implement surface mapping api
This commit is contained in:
@ -4,6 +4,7 @@ use crate::lib8::Rgb8Blend;
|
||||
use crate::render::{Surface, SurfacePool, Display, Surfaces};
|
||||
use crate::power::{brightness_for_mw, AsMilliwatts};
|
||||
use crate::geometry::*;
|
||||
use crate::mappings::*;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
use smart_leds::brightness;
|
||||
@ -13,7 +14,8 @@ use std::io;
|
||||
use rgb::Rgb;
|
||||
|
||||
pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> {
|
||||
surfaces : SurfacePool<S>,
|
||||
surfaces : Option<SurfacePool<S>>,
|
||||
pixmap: StrideMapping,
|
||||
target: T,
|
||||
pixbuf: [T::Color; PIXEL_NUM],
|
||||
max_mw: u32
|
||||
@ -31,10 +33,11 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Deb
|
||||
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> SmartLedDisplay<T, S, PIXEL_NUM> {
|
||||
pub fn new(target: T, max_mw: u32) -> Self {
|
||||
SmartLedDisplay {
|
||||
surfaces: SurfacePool::new(),
|
||||
pixbuf: [Rgb::new(0, 0, 0); PIXEL_NUM],
|
||||
surfaces: Some(SurfacePool::new()),
|
||||
target: target,
|
||||
max_mw: max_mw,
|
||||
pixmap: StrideMapping::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -49,8 +52,12 @@ impl<T, S, const PIXEL_NUM: usize> Surfaces<S> for SmartLedDisplay<T, S, PIXEL_N
|
||||
where
|
||||
T: SmartLedsWrite<Color = Rgb<u8>>,
|
||||
S: Surface {
|
||||
fn new_surface(&mut self) -> Result<S, io::Error> {
|
||||
self.surfaces.new_surface()
|
||||
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
|
||||
if let Some(ref mut s) = self.surfaces {
|
||||
s.new_surface(area)
|
||||
} else {
|
||||
panic!("Could not grab surface list")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,16 +73,21 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Dis
|
||||
}
|
||||
|
||||
fn render_frame(&mut self) {
|
||||
for x in 0..self.pixbuf.len() {
|
||||
let virt_coords = VirtualCoordinates::new(x as u8, 0);
|
||||
let mut pixel = Rgb::new(0, 0, 0);
|
||||
for surface in self.surfaces.iter() {
|
||||
surface.with_shader(|shader| {
|
||||
pixel = pixel.saturating_add(shader.draw(&virt_coords));
|
||||
})
|
||||
}
|
||||
self.pixbuf[x] = Rgb::new(pixel.r, pixel.g, pixel.b);
|
||||
};
|
||||
let surfaces = self.surfaces.take().unwrap();
|
||||
for surface in surfaces.iter() {
|
||||
let rect = surface.rect().clone();
|
||||
let mut sel = self.pixmap.select(&rect);
|
||||
surface.with_shader(|shader| {
|
||||
while let Some((virt_coords, phys_coords)) = sel.next() {
|
||||
let idx = phys_coords.x as usize;
|
||||
if idx >= PIXEL_NUM {
|
||||
continue;
|
||||
}
|
||||
self.pixbuf[idx] = self.pixbuf[idx].saturating_add(shader.draw(&virt_coords));
|
||||
}
|
||||
})
|
||||
}
|
||||
self.surfaces = Some(surfaces);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user