render: implement surface mapping api
This commit is contained in:
@@ -21,10 +21,12 @@ pub trait Surface: Default + Clone + Debug {
|
||||
fn with_shader<F: FnMut(&dyn Shader)>(&self, f: F);
|
||||
fn set_shader(&mut self, shader: Box<dyn Shader>);
|
||||
fn clear_shader(&mut self);
|
||||
|
||||
fn rect(&self) -> Rectangle<u8, Virtual>;
|
||||
}
|
||||
|
||||
pub trait Surfaces<T: Surface>: Debug {
|
||||
fn new_surface(&mut self) -> Result<T, io::Error>;
|
||||
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<T, io::Error>;
|
||||
}
|
||||
|
||||
pub trait Display<T: Surface>: Surfaces<T> {
|
||||
@@ -69,6 +71,7 @@ impl<T: Display<S>, S: Surface> Task for Renderer<T, S> {
|
||||
#[derive(Debug)]
|
||||
pub struct ShaderBinding {
|
||||
shader: Option<Box<dyn Shader>>,
|
||||
rect: Rectangle<u8, Virtual>
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -91,12 +94,17 @@ impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
|
||||
Self {
|
||||
binding: Rc::new(RefCell::new(ShaderBinding {
|
||||
shader: None,
|
||||
rect: Rectangle::everything()
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Surface for BoundSurface<Rc<RefCell<ShaderBinding>>> {
|
||||
fn rect(&self) -> Rectangle<u8, Virtual> {
|
||||
self.binding.borrow().rect.clone()
|
||||
}
|
||||
|
||||
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
|
||||
if let Some(ref shader) = self.binding.borrow().shader {
|
||||
f(shader.as_ref());
|
||||
@@ -175,8 +183,8 @@ impl<S: Surface + Default> SurfacePool<S> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Surface + Default + Clone> Surfaces<S> for SurfacePool<S> {
|
||||
fn new_surface(&mut self) -> Result<S, io::Error> {
|
||||
impl<S: Surface + Default> Surfaces<S> for SurfacePool<S> {
|
||||
fn new_surface(&mut self, _area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
|
||||
let surface = S::default();
|
||||
self.surfaces.push(surface.clone());
|
||||
return Ok(surface);
|
||||
|
Reference in New Issue
Block a user