diff --git a/engine/src/engine/engine.rs b/engine/src/engine/engine.rs index b3bafd1..5060d30 100644 --- a/engine/src/engine/engine.rs +++ b/engine/src/engine/engine.rs @@ -176,33 +176,18 @@ impl Engine { gui_handler: GuiHandler::new(create_info.gui_info, &context)?, }; - context - .render_core_mut() - .add_render_routine::(10_000_000); - - world.resources.insert(gui_handler); - - // default keyboard navigation - let mut direction_mapping = HashMap::new(); - // direction_mapping.insert(Keycode::A, GuiDirection::Left); - // direction_mapping.insert(Keycode::D, GuiDirection::Right); - // direction_mapping.insert(Keycode::W, GuiDirection::Up); - // direction_mapping.insert(Keycode::S, GuiDirection::Down); - direction_mapping.insert(Keycode::Left, GuiDirection::Left); - direction_mapping.insert(Keycode::Right, GuiDirection::Right); - direction_mapping.insert(Keycode::Up, GuiDirection::Up); - direction_mapping.insert(Keycode::Down, GuiDirection::Down); - let engine = Engine { asset_manager, resource_base_path: create_info.resource_base_path, }; - world.resources.insert(InputMap { direction_mapping }); + context + .render_core_mut() + .add_render_routine::(10_000_000); + context.render_core_mut().add_render_routine::(100); + world.resources.insert(context); - world.resources.insert(engine); - world.resources.insert(engine_settings); let scene = Scene::new( create_info.rasterizer_info, @@ -211,6 +196,18 @@ impl Engine { world, )?; + // default keyboard navigation + let mut direction_mapping = HashMap::new(); + direction_mapping.insert(Keycode::Left, GuiDirection::Left); + direction_mapping.insert(Keycode::Right, GuiDirection::Right); + direction_mapping.insert(Keycode::Up, GuiDirection::Up); + direction_mapping.insert(Keycode::Down, GuiDirection::Down); + + world.resources.insert(gui_handler); + world.resources.insert(InputMap { direction_mapping }); + + world.resources.insert(engine); + world.resources.insert(engine_settings); world.resources.insert(scene); world.add_system(Self::main_system::); diff --git a/presentation/src/renderbackend.rs b/presentation/src/renderbackend.rs index ccbb739..74f525a 100644 --- a/presentation/src/renderbackend.rs +++ b/presentation/src/renderbackend.rs @@ -49,6 +49,52 @@ impl Default for VRTransformations { } } +struct SceneHandle { + type_id: TypeId, + priority: u32, + + render: Box< + dyn FnMut( + &mut CommandBufferRecorder<'_>, + &TargetMode>>, + &TargetMode, + &mut World, + ) -> Result<()> + + Send + + Sync + + 'static, + >, + + resize: Box< + dyn FnMut(f32, f32, &TargetMode>>, &mut World) -> Result<()> + + Send + + Sync + + 'static, + >, +} + +impl SceneHandle { + fn new(priority: u32) -> Self { + Self { + type_id: TypeId::of::(), + priority, + render: Box::new(|recorder, images, indices, world| { + world + .resources + .get_mut_unchecked::() + .process(recorder, images, indices, world) + }), + + resize: Box::new(|width, height, images, world| { + world + .resources + .get_mut_unchecked::() + .resize(width, height, images) + }), + } + } +} + pub struct RenderBackend { device: Arc, queue: Arc>, @@ -61,7 +107,7 @@ pub struct RenderBackend { command_buffer: Arc, - render_routines: Vec<(u32, TypeId)>, + render_routines: Vec, } impl RenderBackend { @@ -169,17 +215,13 @@ impl RenderBackend { // make a call to the connected scenes self.render_routines .iter_mut() - .try_for_each(|(_, type_id)| -> Result<()> { - let scene: &mut dyn TScene = world.resources.get_mut_by_type_id_untyped(*type_id); - - scene.process( + .try_for_each(|scene_handle| { + (scene_handle.render)( &mut buffer_recorder, &*self.swapchain_images.lock().unwrap(), &image_indices, world, - )?; - - Ok(()) + ) })?; Ok(&self.command_buffer) @@ -205,10 +247,8 @@ impl RenderBackend { self.render_routines .iter_mut() - .try_for_each(|(_, type_id)| -> Result<()> { - let scene: &mut dyn TScene = world.resources.get_mut_by_type_id_untyped(*type_id); - - scene.resize(width as f32, height as f32, &images) + .try_for_each(|scene_handle| { + (scene_handle.resize)(width as f32, height as f32, &images, world) })?; Ok(()) @@ -216,15 +256,16 @@ impl RenderBackend { /// lower priority means it is more important pub fn add_render_routine(&mut self, priority: u32) { - self.render_routines.push((priority, TypeId::of::())); - self.render_routines.sort_by_key(|(p, _)| *p); + self.render_routines.push(SceneHandle::new::(priority)); + self.render_routines + .sort_by_key(|scene_handle| scene_handle.priority); } pub fn remove_render_routine(&mut self) { - if let Some(&(index, _)) = self + if let Some(index) = self .render_routines .iter() - .find(|(_, type_id)| *type_id == TypeId::of::()) + .position(|scene_handle| scene_handle.type_id == TypeId::of::()) { self.render_routines.remove(index as usize); } diff --git a/skybox/src/lib.rs b/skybox/src/lib.rs index 1c046fc..289c613 100644 --- a/skybox/src/lib.rs +++ b/skybox/src/lib.rs @@ -52,3 +52,24 @@ impl SkyBox { Ok(Self { cube_map }) } } + +impl TScene for SkyBox { + fn process( + &mut self, + buffer_recorder: &mut CommandBufferRecorder<'_>, + images: &TargetMode>>, + indices: &TargetMode, + world: &World, + ) -> Result<()> { + todo!() + } + + fn resize( + &mut self, + window_width: f32, + window_height: f32, + images: &TargetMode>>, + ) -> Result<()> { + todo!() + } +}