use std::rc::Rc; use std::cell::RefCell; use std::io; use rgb::RGB8; #[cfg(feature="threads")] use std::sync::{Arc, Mutex}; use crate::geometry::*; use crate::task::Task; use crate::time::Periodically; use running_average::RealTimeRunningAverage; use std::marker::PhantomData; use std::fmt::Debug; pub trait Shader: Send + Debug { fn draw(&self, surface_coords: &VirtualCoordinates) -> RGB8; } pub trait Surface: Default + Clone + Debug { fn with_shader(&self, f: F); fn set_shader(&mut self, shader: Box); fn clear_shader(&mut self); } pub trait Surfaces: Debug { fn new_surface(&mut self) -> Result; } pub trait Display: Surfaces { fn start_frame(&mut self) {} fn end_frame(&mut self) {} fn render_frame(&mut self); } #[derive(Debug)] pub struct Renderer, S: Surface> { display: T, fps: RealTimeRunningAverage, fps_display: Periodically, _sfc: PhantomData } impl, S: Surface> Renderer { pub fn new(display: T) -> Self { Self { display, fps: RealTimeRunningAverage::default(), fps_display: Periodically::new_every_n_seconds(5), _sfc: PhantomData::default() } } } impl, S: Surface> Task for Renderer { fn name(&self) -> &'static str { "Renderer" } fn tick(&mut self) { self.display.start_frame(); self.display.render_frame(); self.display.end_frame(); self.fps.insert(1); self.fps_display.run(|| { log::info!("FPS: {} {:?}", self.fps.measurement(), self.display); }); } } #[derive(Debug)] pub struct ShaderBinding { shader: Option>, } #[derive(Clone)] pub struct BoundSurface { pub binding: T } impl Debug for BoundSurface>> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("BoundSurface") .field("shader", &self.binding.borrow().shader) .finish() } } pub type SimpleSurface = BoundSurface>>; impl Default for BoundSurface>>{ fn default() -> Self { Self { binding: Rc::new(RefCell::new(ShaderBinding { shader: None, })), } } } impl Surface for BoundSurface>> { fn with_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) { self.binding.borrow_mut().shader = Some(shader); } fn clear_shader(&mut self) { self.binding.borrow_mut().shader = None; } } #[cfg(feature="threads")] pub type SharedSurface = BoundSurface>>; #[cfg(feature="threads")] impl Default for BoundSurface>> { fn default() -> Self { Self { binding: Arc::new(Mutex::new(ShaderBinding { shader: None, })), } } } #[cfg(feature="threads")] impl Surface for BoundSurface>> { fn with_shader(&self, mut f: F) { if let Some(ref shader) = self.binding.lock().unwrap().shader { f(shader.as_ref()); } } fn set_shader(&mut self, shader: Box) { self.binding.lock().unwrap().shader = Some(shader); } fn clear_shader(&mut self) { self.binding.lock().unwrap().shader = None; } } #[cfg(feature="threads")] impl Debug for BoundSurface>> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("BoundSurface") .field("shader", &self.binding.lock().unwrap().shader) .finish() } } #[derive(Clone)] pub struct SurfacePool { surfaces: Vec } impl Debug for SurfacePool { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.surfaces.fmt(f) } } impl SurfacePool { pub const fn new() -> Self { Self { surfaces: Vec::new() } } pub fn iter(&self) -> std::slice::Iter { self.surfaces.iter() } } impl Surfaces for SurfacePool { fn new_surface(&mut self) -> Result { let surface = S::default(); self.surfaces.push(surface.clone()); return Ok(surface); } }