raidillon/glium_platform/src/system.rs

66 lines
2 KiB
Rust

use std::any::TypeId;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use indexmap::IndexMap;
use glium::{Display, Frame};
use glium::glutin::surface::WindowSurface;
use raidillon_assets::ModelManagerRef;
use raidillon_core::{define_typemap, EguiQueue};
use raidillon_core::scene::Scene;
use glam::Vec3;
use winit::event_loop::EventLoop;
use std::cell::Cell;
pub struct RenderingContext<'a> {
pub scene: &'a Scene,
pub target: &'a mut Frame,
pub window: Arc<Mutex<glium::winit::window::Window>>,
pub display: &'a Display<WindowSurface>,
pub asset_manager: ModelManagerRef,
pub egui_queue: Rc<RefCell<EguiQueue>>,
pub env_light_dir: Vec3,
pub should_egui_receive_input_events: Rc<Cell<bool>>
}
/// The internal "rendering system" trait of glium_platform.
/// This is unrelated to the main System trait in core.
pub trait RenderingSystem {
fn handle_event(
&mut self,
_window: Arc<Mutex<glium::winit::window::Window>>,
_event: winit::event::Event<()>,
) {
}
fn prepare_frame(&mut self, _window: Arc<Mutex<glium::winit::window::Window>>) {}
fn render(&mut self, ctx: &mut RenderingContext);
fn initialize(display: &Display<WindowSurface>, window: Arc<Mutex<glium::winit::window::Window>>, event_loop: &EventLoop<()>) -> Self
where
Self: Sized;
}
// define_typemap!(RenderingSystemManager, RenderingSystem);
pub struct RenderingSystemManager {
pub systems: IndexMap<TypeId, Box<dyn RenderingSystem>>,
}
impl RenderingSystemManager {
pub fn new() -> Self {
Self {
systems: IndexMap::default(),
}
}
pub fn add<R>(&mut self, display: &Display<WindowSurface>, window: Arc<Mutex<glium::winit::window::Window>>, event_loop: &EventLoop<()>)
where
R: RenderingSystem + 'static,
{
let system = R::initialize(display, window, event_loop);
self.systems.insert(TypeId::of::<R>(), Box::new(system));
}
pub fn remove<R: 'static>(&mut self) {
self.systems.shift_remove(&TypeId::of::<R>());
}
}