2024-10-27 11:19:26 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::cell::RefCell;
|
2024-10-28 22:22:03 +01:00
|
|
|
use std::io;
|
2024-10-27 11:19:26 +01:00
|
|
|
|
2024-10-30 19:59:19 +01:00
|
|
|
#[cfg(feature="threads")]
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
2024-10-27 11:19:26 +01:00
|
|
|
use crate::lib8::RGB8;
|
2024-10-27 15:14:28 +01:00
|
|
|
use crate::geometry::*;
|
|
|
|
|
2024-10-27 11:19:26 +01:00
|
|
|
pub trait Shader: Send {
|
2024-10-27 15:14:28 +01:00
|
|
|
fn draw(&self, surface_coords: VirtualCoordinates) -> RGB8;
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 23:30:26 +01:00
|
|
|
pub trait Surface: Default + Clone {
|
2024-10-28 22:22:03 +01:00
|
|
|
fn with_shader<F: FnMut(&dyn Shader)>(&self, f: F);
|
|
|
|
fn set_shader(&mut self, shader: Box<dyn Shader>);
|
|
|
|
fn clear_shader(&mut self);
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
pub trait Surfaces<T: Surface> {
|
|
|
|
fn new_surface(&mut self) -> Result<T, io::Error>;
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
pub trait Display<T: Surface>: Surfaces<T> {
|
|
|
|
fn start_frame(&mut self) {}
|
|
|
|
fn end_frame(&mut self) {}
|
|
|
|
fn render_frame(&mut self);
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
pub struct ShaderBinding {
|
2024-10-27 15:14:28 +01:00
|
|
|
shader: Option<Box<dyn Shader>>,
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-10-27 15:14:28 +01:00
|
|
|
#[derive(Clone)]
|
2024-10-28 22:22:03 +01:00
|
|
|
pub struct BoundSurface<T> {
|
|
|
|
pub binding: T
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
pub type SimpleSurface = BoundSurface<Rc<RefCell<ShaderBinding>>>;
|
|
|
|
|
|
|
|
impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
binding: Rc::new(RefCell::new(ShaderBinding {
|
|
|
|
shader: None,
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Surface for BoundSurface<Rc<RefCell<ShaderBinding>>> {
|
|
|
|
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
|
|
|
|
if let Some(ref shader) = self.binding.borrow().shader {
|
|
|
|
f(shader.as_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
|
|
|
self.binding.borrow_mut().shader = Some(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_shader(&mut self) {
|
|
|
|
self.binding.borrow_mut().shader = None;
|
|
|
|
}
|
2024-10-30 19:59:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="threads")]
|
|
|
|
pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
|
2024-10-30 19:59:19 +01:00
|
|
|
#[cfg(feature="threads")]
|
|
|
|
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
binding: Arc::new(Mutex::new(ShaderBinding {
|
|
|
|
shader: None,
|
|
|
|
})),
|
|
|
|
}
|
2024-10-28 22:22:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 19:59:19 +01:00
|
|
|
#[cfg(feature="threads")]
|
2024-10-28 22:22:03 +01:00
|
|
|
impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
|
|
|
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
|
2024-10-27 15:14:28 +01:00
|
|
|
if let Some(ref shader) = self.binding.lock().unwrap().shader {
|
|
|
|
f(shader.as_ref());
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
2024-10-27 15:14:28 +01:00
|
|
|
self.binding.lock().unwrap().shader = Some(shader);
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
fn clear_shader(&mut self) {
|
2024-10-27 15:14:28 +01:00
|
|
|
self.binding.lock().unwrap().shader = None;
|
2024-10-27 11:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
2024-10-28 22:22:03 +01:00
|
|
|
|
|
|
|
pub struct SurfacePool<S: Surface + Default> {
|
|
|
|
surfaces: Vec<S>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Surface + Default> SurfacePool<S> {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
surfaces: Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter(&self) -> std::slice::Iter<S> {
|
|
|
|
self.surfaces.iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Surface + Default + Clone> Surfaces<S> for SurfacePool<S> {
|
|
|
|
fn new_surface(&mut self) -> Result<S, io::Error> {
|
|
|
|
let surface = S::default();
|
|
|
|
self.surfaces.push(surface.clone());
|
|
|
|
return Ok(surface);
|
|
|
|
}
|
|
|
|
}
|