185 lines
4.6 KiB
Rust
185 lines
4.6 KiB
Rust
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<F: FnMut(&dyn Shader)>(&self, f: F);
|
|
fn set_shader(&mut self, shader: Box<dyn Shader>);
|
|
fn clear_shader(&mut self);
|
|
}
|
|
|
|
pub trait Surfaces<T: Surface>: Debug {
|
|
fn new_surface(&mut self) -> Result<T, io::Error>;
|
|
}
|
|
|
|
pub trait Display<T: Surface>: Surfaces<T> {
|
|
fn start_frame(&mut self) {}
|
|
fn end_frame(&mut self) {}
|
|
fn render_frame(&mut self);
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Renderer<T: Display<S>, S: Surface> {
|
|
display: T,
|
|
fps: RealTimeRunningAverage<u32>,
|
|
fps_display: Periodically,
|
|
_sfc: PhantomData<S>
|
|
}
|
|
|
|
impl<T: Display<S>, S: Surface> Renderer<T, S> {
|
|
pub fn new(display: T) -> Self {
|
|
Self {
|
|
display,
|
|
fps: RealTimeRunningAverage::default(),
|
|
fps_display: Periodically::new_every_n_seconds(5),
|
|
_sfc: PhantomData::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: Display<S>, S: Surface> Task for Renderer<T, S> {
|
|
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<Box<dyn Shader>>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct BoundSurface<T> {
|
|
pub binding: T
|
|
}
|
|
|
|
impl Debug for BoundSurface<Rc<RefCell<ShaderBinding>>> {
|
|
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<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;
|
|
}
|
|
}
|
|
|
|
#[cfg(feature="threads")]
|
|
pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
|
|
|
|
#[cfg(feature="threads")]
|
|
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
|
fn default() -> Self {
|
|
Self {
|
|
binding: Arc::new(Mutex::new(ShaderBinding {
|
|
shader: None,
|
|
})),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(feature="threads")]
|
|
impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
|
fn with_shader<F: FnMut(&dyn 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<dyn Shader>) {
|
|
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<Arc<Mutex<ShaderBinding>>> {
|
|
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<S: Surface + Default> {
|
|
surfaces: Vec<S>
|
|
}
|
|
|
|
impl<S: Surface + Debug> Debug for SurfacePool<S> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.surfaces.fmt(f)
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|