Continue new platform/glium implementations
- Assets, asset manager system - Rendering system trait - Kick off glium platform implementation - And more
This commit is contained in:
parent
f7d5c14caf
commit
e817abf8ab
18 changed files with 2557 additions and 32 deletions
|
|
@ -1,30 +1,47 @@
|
|||
use raidillon_platform::AssetManager;
|
||||
use raidillon_platform::assets::AssetManager;
|
||||
use crate::model::Model;
|
||||
use std::path::Path;
|
||||
use raidillon_platform::assets::ModelHandle;
|
||||
use std::path::{Path, PathBuf};
|
||||
use crate::gltf_loader::load_gltf;
|
||||
use glium::backend::Facade;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry;
|
||||
|
||||
/// Glium platform asset manager implementation.
|
||||
pub struct GliumAssetManager {
|
||||
models: Vec<Option<Model>>,
|
||||
models: HashMap<PathBuf, Model>,
|
||||
facade: Box<dyn Facade>,
|
||||
next_model_id: usize,
|
||||
}
|
||||
|
||||
impl AssetManager for GliumAssetManager {
|
||||
fn load_gltf(&mut self, path: &Path) -> ModelHandle {
|
||||
let model_id = self.next_model_id;
|
||||
let model = load_gltf(path, self.facade.as_ref()).unwrap();
|
||||
self.models.push(Some(model));
|
||||
self.next_model_id += 1;
|
||||
model_id
|
||||
}
|
||||
|
||||
fn unload_model(&mut self, model_handle: ModelHandle) {
|
||||
if model_handle < self.models.len() {
|
||||
self.models[model_handle] = None;
|
||||
impl GliumAssetManager {
|
||||
pub fn new(facade: Box<dyn Facade>) -> Self {
|
||||
let models = HashMap::new();
|
||||
Self {
|
||||
models,
|
||||
facade,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AssetManager for GliumAssetManager {
|
||||
type Model = Model;
|
||||
|
||||
fn load_gltf(&mut self, path: &Path) {
|
||||
let model = load_gltf(path, self.facade.as_ref()).unwrap();
|
||||
self.models.insert(path.to_path_buf(), model);
|
||||
}
|
||||
|
||||
fn unload_model(&mut self, path: &Path) {
|
||||
self.models.remove(&path.to_path_buf());
|
||||
}
|
||||
|
||||
fn get_model(&mut self, path: &Path) -> &Self::Model {
|
||||
let path_buf = path.to_path_buf();
|
||||
match self.models.entry(path_buf) {
|
||||
Entry::Occupied(entry) => entry.into_mut(),
|
||||
Entry::Vacant(entry) => {
|
||||
let model = load_gltf(path, self.facade.as_ref()).unwrap();
|
||||
entry.insert(model)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@ pub mod platform;
|
|||
pub mod assets;
|
||||
pub mod model;
|
||||
pub mod gltf_loader;
|
||||
pub mod system;
|
||||
mod render;
|
||||
|
||||
pub use assets::GliumAssetManager;
|
||||
pub use platform::GliumPlatform;
|
||||
pub use system::RenderingSystem;
|
||||
|
|
|
|||
|
|
@ -1,17 +1,24 @@
|
|||
use std::sync::{Arc, RwLock};
|
||||
use glium::backend::Facade;
|
||||
use raidillon_platform::Platform;
|
||||
use glium::winit::event_loop::EventLoop;
|
||||
use glium::winit::window::Window;
|
||||
use glium::backend::glutin::Display;
|
||||
use glium::glutin::surface::WindowSurface;
|
||||
use glium::backend::glutin::SimpleWindowBuilder;
|
||||
use crate::system::{RenderingSystemManager, RenderingSystem};
|
||||
use winit::event::{Event, WindowEvent};
|
||||
use crate::GliumAssetManager;
|
||||
|
||||
pub struct GliumPlatform {
|
||||
pub struct GliumPlatform<RS: RenderingSystem> {
|
||||
event_loop: EventLoop<()>,
|
||||
window: Window,
|
||||
display: Display<WindowSurface>,
|
||||
rendering_system_manager: RenderingSystemManager<RS>,
|
||||
asset_manager: Arc<RwLock<GliumAssetManager>>,
|
||||
}
|
||||
|
||||
impl Platform for GliumPlatform {
|
||||
impl<RS: RenderingSystem> Platform for GliumPlatform<RS> {
|
||||
fn initialize(title: String, width: u32, height: u32) -> Self {
|
||||
let event_loop = glium::winit::event_loop::EventLoop::builder()
|
||||
.build()
|
||||
|
|
@ -22,10 +29,32 @@ impl Platform for GliumPlatform {
|
|||
.with_inner_size(width, height)
|
||||
.build(&event_loop);
|
||||
|
||||
let rendering_system_manager = RenderingSystemManager::new();
|
||||
let asset_manager = Arc::new(RwLock::new(GliumAssetManager::new(Box::new(display.clone()))));
|
||||
Self {
|
||||
event_loop,
|
||||
window,
|
||||
display
|
||||
display,
|
||||
rendering_system_manager,
|
||||
asset_manager,
|
||||
}
|
||||
}
|
||||
|
||||
fn run(self) {
|
||||
let _ = &self.event_loop.run(move |event, el| {
|
||||
match event {
|
||||
Event::WindowEvent { event, .. } => match event {
|
||||
WindowEvent::CloseRequested => {},
|
||||
WindowEvent::RedrawRequested => {
|
||||
|
||||
}
|
||||
_ => {},
|
||||
},
|
||||
Event::AboutToWait => {
|
||||
self.window.request_redraw();
|
||||
}
|
||||
_ => {},
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
raidillon_glium/src/render/basic.rs
Normal file
12
raidillon_glium/src/render/basic.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use glium::Surface;
|
||||
use crate::RenderingSystem;
|
||||
use crate::system::RenderingContext;
|
||||
|
||||
/// A basic renderer pipeline step.
|
||||
pub struct BasicRenderingSystem;
|
||||
|
||||
impl RenderingSystem for BasicRenderingSystem {
|
||||
fn render<S: Surface>(ctx: &mut RenderingContext<S>) {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
2
raidillon_glium/src/render/mod.rs
Normal file
2
raidillon_glium/src/render/mod.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
mod basic;
|
||||
pub use basic::BasicRenderingSystem;
|
||||
35
raidillon_glium/src/system.rs
Normal file
35
raidillon_glium/src/system.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use std::collections::HashMap;
|
||||
use raidillon_core::Scene;
|
||||
use glium::Surface;
|
||||
|
||||
pub struct RenderingContext<'a, S: Surface> {
|
||||
pub scene: &'a mut Scene,
|
||||
pub target: &'a mut S,
|
||||
}
|
||||
|
||||
/// The internal "rendering system" trait of raidillon_glium.
|
||||
/// This is unrelated to the main System trait in raidillon_core.
|
||||
pub trait RenderingSystem {
|
||||
fn render<S: Surface>(ctx: &mut RenderingContext<S>);
|
||||
}
|
||||
|
||||
type SystemID = String;
|
||||
|
||||
pub struct RenderingSystemManager<T: RenderingSystem> {
|
||||
pub systems: HashMap<SystemID, Box<T>>,
|
||||
}
|
||||
|
||||
impl<T: RenderingSystem> RenderingSystemManager<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
systems: HashMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn add_system(&mut self, id: SystemID, system: T) {
|
||||
self.systems.insert(id, Box::new(system));
|
||||
}
|
||||
|
||||
pub fn remove_system(&mut self, id: SystemID) {
|
||||
self.systems.remove(&id);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue