diff --git a/ecs/src/type_map.rs b/ecs/src/type_map.rs index d0aa76d..3dfb1a1 100644 --- a/ecs/src/type_map.rs +++ b/ecs/src/type_map.rs @@ -229,7 +229,7 @@ pub enum ComponentRequestType { #[derive(Debug)] pub struct ComponentNotFoundError { - request_type: ComponentRequestType, + pub request_type: ComponentRequestType, } impl ComponentNotFoundError { diff --git a/ecs/src/updates.rs b/ecs/src/updates.rs index 7b80fc4..638b324 100644 --- a/ecs/src/updates.rs +++ b/ecs/src/updates.rs @@ -341,7 +341,7 @@ pub struct ArchetypeInfo { } impl ArchetypeInfo { - pub(crate) fn new(entities: Vec<(Entity, Option)>) -> Self { + pub fn new(entities: Vec<(Entity, Option)>) -> Self { Self { entities } } @@ -388,7 +388,7 @@ impl Archetype { Ok(()) } - pub(crate) fn entities( + pub fn entities( &self, ) -> &IndexMap Result<()> + Send + Sync>> { &self.entities @@ -572,12 +572,12 @@ impl Updates { } } - pub(crate) fn clear(&mut self) { - self.updates.clear(); + // pub(crate) fn clear(&mut self) { + // self.updates.clear(); - #[cfg(feature = "timings")] - self.timings.clear(); - } + // #[cfg(feature = "timings")] + // self.timings.clear(); + // } pub(crate) fn add(&mut self, name: &str, priority: u32, update: Update) -> Result<()> { #[cfg(feature = "timings")] diff --git a/engine/src/engine/engine.rs b/engine/src/engine/engine.rs index 5060d30..040a702 100644 --- a/engine/src/engine/engine.rs +++ b/engine/src/engine/engine.rs @@ -13,9 +13,7 @@ use crate::prelude::*; use std::collections::HashMap; use std::sync::Arc; -pub struct GuiHandlerRenderer { - pub gui_handler: Arc, -} +pub struct GuiHandlerRenderer; impl TScene for GuiHandlerRenderer { fn process( @@ -23,9 +21,12 @@ impl TScene for GuiHandlerRenderer { buffer_recorder: &mut CommandBufferRecorder<'_>, _images: &TargetMode>>, indices: &TargetMode, - _world: &World, + world: &World, ) -> Result<()> { - self.gui_handler.process(buffer_recorder, indices) + world + .resources + .get::>() + .process(buffer_recorder, indices) } fn resize( @@ -33,16 +34,17 @@ impl TScene for GuiHandlerRenderer { window_width: f32, window_height: f32, images: &TargetMode>>, + world: &World, ) -> Result<()> { - self.gui_handler - .resize(window_width as u32, window_height as u32, images) + world.resources.get::>().resize( + window_width as u32, + window_height as u32, + images, + ) } } pub struct Engine { - // loads and keeps track of raw data - asset_manager: AssetManager, - pub(crate) resource_base_path: String, } @@ -171,14 +173,9 @@ impl Engine { )?; let asset_manager = AssetManager::new(&engine_settings)?; - - let gui_handler = GuiHandlerRenderer { - gui_handler: GuiHandler::new(create_info.gui_info, &context)?, - }; + let gui_handler = GuiHandler::new(create_info.gui_info, &context)?; let engine = Engine { - asset_manager, - resource_base_path: create_info.resource_base_path, }; @@ -205,7 +202,7 @@ impl Engine { world.resources.insert(gui_handler); world.resources.insert(InputMap { direction_mapping }); - + world.resources.insert(asset_manager); world.resources.insert(engine); world.resources.insert(engine_settings); world.resources.insert(scene); @@ -254,25 +251,18 @@ impl Engine { impl Engine { fn main_system(world: &mut World) -> Result { - let gui_handler = world.resources.get_unchecked::(); + let gui_handler = world.resources.get_unchecked::>(); let input_map = world.resources.get_unchecked::(); let context = world.resources.get_unchecked::(); let res = world.resources.get_mut_unchecked::().next_frame( world, |world, consumer: &mut T, event| { - Self::event( - world, - context, - &gui_handler.gui_handler, - input_map, - consumer, - event, - ) + Self::event(world, context, gui_handler, input_map, consumer, event) }, )?; - gui_handler.gui_handler.process_callbacks()?; + gui_handler.process_callbacks()?; Ok(res) } diff --git a/engine/src/scene/general/light.rs b/engine/src/scene/general/light.rs index 2f9ac57..bb89d1c 100644 --- a/engine/src/scene/general/light.rs +++ b/engine/src/scene/general/light.rs @@ -5,7 +5,7 @@ use crate::prelude::*; use anyhow::Result; use utilities::{ impl_reprc, - prelude::cgmath::{vec3, InnerSpace, Rad, Vector3, Zero}, + prelude::cgmath::{InnerSpace, Rad, Vector3, Zero, vec3}, }; use utilities::prelude::cgmath::{Matrix4, SquareMatrix}; @@ -127,15 +127,15 @@ pub struct Light { } impl Light { - pub(crate) fn point_light(device: &Arc) -> Result { + pub fn point_light(device: &Arc) -> Result { Self::new(LightType::Point, device) } - pub(crate) fn directional_light(device: &Arc) -> Result { + pub fn directional_light(device: &Arc) -> Result { Self::new(LightType::Direction, device) } - pub(crate) fn spot_light(device: &Arc) -> Result { + pub fn spot_light(device: &Arc) -> Result { Self::new(LightType::Spot, device) } diff --git a/engine/src/scene/scene/scene_base.rs b/engine/src/scene/scene/scene_base.rs index 82c7b29..960f798 100644 --- a/engine/src/scene/scene/scene_base.rs +++ b/engine/src/scene/scene/scene_base.rs @@ -14,7 +14,6 @@ use super::super::timings::Timings; use anyhow::Result; use ecs::*; -use rayon::prelude::*; use utilities::prelude::cgmath::{Matrix4, Vector3, vec3}; use std::sync::Mutex; @@ -28,9 +27,6 @@ pub struct Scene { screen_width: f32, screen_height: f32, - device: Arc, - queue: Arc>, - render_type: SceneType, last_frame: Duration, @@ -92,9 +88,6 @@ impl Scene { render_type, - device: device.clone(), - queue: queue.clone(), - frustum_check: None, renderer, @@ -486,6 +479,7 @@ impl TScene for Scene { window_width: f32, window_height: f32, images: &TargetMode>>, + _world: &World, ) -> anyhow::Result<()> { self.screen_width = window_width; self.screen_height = window_height; diff --git a/loading-screen/src/loadingscreen.rs b/loading-screen/src/loadingscreen.rs index 25e3f4d..3bb3e46 100644 --- a/loading-screen/src/loadingscreen.rs +++ b/loading-screen/src/loadingscreen.rs @@ -1,40 +1,60 @@ use context::prelude::*; -use std::{sync::Arc, thread}; +use std::{ + sync::{ + Arc, + mpsc::{Receiver, channel}, + }, + thread, +}; use anyhow::Result; -pub struct LoadingScreen; +pub struct LoadingScreen { + result: Option, + receiver: Receiver, +} -impl LoadingScreen { - pub fn load( - context: &Arc, - gui: Option>, - loader: L, - on_ready: R, - ) -> Result<()> +impl LoadingScreen { + pub fn load(gui: Arc, loader: L) -> Result where R: Fn(T) -> Result<()> + Send + Sync + 'static, T: Send + Sync + 'static, L: FnOnce() -> T + Send + Sync + 'static, G: TopLevelGui + TopGui + Send + Sync + 'static, { - if let Some(gui) = &gui { - gui.enable()?; - } + gui.enable()?; - let context = context.clone(); + let (sender, receiver) = channel(); thread::spawn(move || { - let result = loader(); - if let Some(gui) = &gui { - gui.disable().unwrap(); - } - - todo!(); - // context.push_event(move || (on_ready)(result)); + let _ = sender.send(loader()); + let _ = gui.disable(); }); - Ok(()) + Ok(Self { + result: None, + receiver, + }) + } + + pub fn check_ready(&mut self) -> bool { + if self.result.is_some() { + return true; + } + + match self.receiver.try_recv() { + Ok(result) => { + self.result = Some(result); + true + } + Err(_) => false, + } + } + + /// Will panic if there is no result. + /// Use `check_ready` before calling this. + pub fn take_result(mut self) -> T { + self.result.take().unwrap() } } diff --git a/presentation/src/renderbackend.rs b/presentation/src/renderbackend.rs index 74f525a..669335f 100644 --- a/presentation/src/renderbackend.rs +++ b/presentation/src/renderbackend.rs @@ -89,7 +89,7 @@ impl SceneHandle { world .resources .get_mut_unchecked::() - .resize(width, height, images) + .resize(width, height, images, world) }), } } diff --git a/presentation/src/traits.rs b/presentation/src/traits.rs index ab01f9d..9c66da6 100644 --- a/presentation/src/traits.rs +++ b/presentation/src/traits.rs @@ -25,6 +25,7 @@ pub trait TScene: Send + Sync + 'static { window_width: f32, window_height: f32, images: &TargetMode>>, + world: &World, ) -> Result<()>; } diff --git a/skybox/src/lib.rs b/skybox/src/lib.rs index 79bc60a..ba022e7 100644 --- a/skybox/src/lib.rs +++ b/skybox/src/lib.rs @@ -37,6 +37,8 @@ impl> From for SkyBoxImages { } pub struct SkyBox { + enabled: bool, + _cube_map: Arc, cube_buffer: Arc>, @@ -160,6 +162,8 @@ impl SkyBox { .submit()?; let me = Self { + enabled: true, + _cube_map: cube_map, cube_buffer, @@ -175,6 +179,14 @@ impl SkyBox { Ok(()) } + pub fn enable(&mut self) { + self.enabled = true; + } + + pub fn disable(&mut self) { + self.enabled = false; + } + fn create_render_target( context: &Context, sample_count: VkSampleCountFlags, @@ -256,6 +268,10 @@ impl TScene for SkyBox { indices: &TargetMode, _world: &World, ) -> Result<()> { + if !self.enabled { + return Ok(()); + } + self.render_target .chain(indices) .chain(&self.pipeline) @@ -279,10 +295,34 @@ impl TScene for SkyBox { fn resize( &mut self, - window_width: f32, - window_height: f32, - images: &TargetMode>>, + _window_width: f32, + _window_height: f32, + _images: &TargetMode>>, + world: &World, ) -> Result<()> { + let sample_count = world + .resources + .get::() + .graphics_info()? + .sample_count; + + let context = world.resources.get::(); + + let pipeline_layout = match &self.pipeline { + TargetMode::Mono(p) => p.pipeline_layout().clone(), + TargetMode::Stereo(p, _) => p.pipeline_layout().clone(), + }; + + self.render_target = Self::create_render_target(context, sample_count)?; + self.pipeline = Self::create_pipeline( + context, + sample_count, + &self.render_target, + &pipeline_layout, + &self.vertex_shader, + &self.fragment_shader, + )?; + Ok(()) } }