diff --git a/src/core/app/mod.rs b/src/core/app/mod.rs index 8f0ace8..c62e4d5 100644 --- a/src/core/app/mod.rs +++ b/src/core/app/mod.rs @@ -186,7 +186,7 @@ impl ApplicationHandler for App { if let Some(scene) = scene_manager.current_scene_mut() { { let _update_span = tracing::debug_span!("scene_update").entered(); - scene.0.update(&mut scene.1, &context).unwrap(); + scene.update(&context).unwrap(); } let acquire_future = { @@ -198,10 +198,7 @@ impl ApplicationHandler for App { let acquire_future = { let _render_span = tracing::debug_span!("scene_render").entered(); - scene - .0 - .render(acquire_future, &mut scene.1, &mut context) - .unwrap() + scene.render(acquire_future, &mut context).unwrap() }; { @@ -245,7 +242,7 @@ impl ApplicationHandler for App { if let Some(scene_manager) = self.scene_manager.get_mut(&window_id) { if let Some(app_context) = self.app_contexts.get(&window_id) { let context = app_context.borrow(); - scene_manager.load_scene(scene, &context); + scene_manager.replace_current_scene(scene, &context); } } } diff --git a/src/core/app/user_event.rs b/src/core/app/user_event.rs index 21b70f1..498aac0 100644 --- a/src/core/app/user_event.rs +++ b/src/core/app/user_event.rs @@ -1,11 +1,11 @@ use winit::window::{CursorGrabMode, WindowId}; -use crate::core::scene::Scene; +use crate::core::scene::AsScene; pub enum UserEvent { CursorGrabMode(WindowId, CursorGrabMode), CursorVisible(WindowId, bool), - ChangeScene(WindowId, Box), + ChangeScene(WindowId, Box), ChangeResolution(WindowId, f32, f32), Exit(WindowId), } diff --git a/src/core/scene/manager.rs b/src/core/scene/manager.rs index e8660e8..652e83b 100644 --- a/src/core/scene/manager.rs +++ b/src/core/scene/manager.rs @@ -9,10 +9,10 @@ use crate::core::render::primitives::vulkan_resource::{ }; use bevy_ecs::world::World; -use super::Scene; +use super::{AsScene, Scene}; pub struct SceneManager { - scenes: Vec<(Box, World)>, + scenes: Vec, current_scene_index: Option, window_context: Option>>, } @@ -92,25 +92,30 @@ impl SceneManager { world } - pub fn load_scene(&mut self, scene: Box, window_context: &WindowContext) { + pub fn load_scene(&mut self, scene_impl: Box, window_context: &WindowContext) { let world = Self::create_world_with_resources(window_context); - self.scenes.push((scene, world)); + let scene = Scene::new(scene_impl, world); + self.scenes.push(scene); self.current_scene_index = Some(self.scenes.len() - 1); } - pub fn replace_current_scene(&mut self, scene: Box, window_context: &WindowContext) { + pub fn replace_current_scene( + &mut self, + scene_impl: Box, + window_context: &WindowContext, + ) { if let Some(index) = self.current_scene_index { if index < self.scenes.len() { - self.scenes[index].0.unload(); + self.scenes[index].unload(); let world = Self::create_world_with_resources(window_context); - self.scenes[index] = (scene, world); + self.scenes[index] = Scene::new(scene_impl, world); } } else { - self.load_scene(scene, window_context); + self.load_scene(scene_impl, window_context); } } - pub fn current_scene(&self) -> Option<&(Box, World)> { + pub fn current_scene(&self) -> Option<&Scene> { if let Some(index) = self.current_scene_index { self.scenes.get(index) } else { @@ -118,7 +123,7 @@ impl SceneManager { } } - pub fn current_scene_mut(&mut self) -> Option<&mut (Box, World)> { + pub fn current_scene_mut(&mut self) -> Option<&mut Scene> { if let Some(index) = self.current_scene_index { self.scenes.get_mut(index) } else { @@ -130,9 +135,9 @@ impl SceneManager { &mut self, window_context: &mut WindowContext, ) -> Result<(), Box> { - if let Some((scene, world)) = self.current_scene_mut() { + if let Some(scene) = self.current_scene_mut() { if !scene.loaded() { - scene.load(world, window_context)?; + scene.load(window_context)?; } } else { tracing::warn!("No scene found in SceneManager!"); diff --git a/src/core/scene/mod.rs b/src/core/scene/mod.rs index c12f00e..7abb795 100644 --- a/src/core/scene/mod.rs +++ b/src/core/scene/mod.rs @@ -7,7 +7,48 @@ use crate::core::app::context::WindowContext; pub mod manager; -pub trait Scene { +/// Structure Scene qui contient le world et l'implémentation AsScene +pub struct Scene { + pub world: World, + pub implementation: Box, +} + +impl Scene { + pub fn new(implementation: Box, world: World) -> Self { + Self { + world, + implementation, + } + } + + pub fn loaded(&self) -> bool { + self.implementation.loaded() + } + + pub fn load(&mut self, window_context: &mut WindowContext) -> Result<(), Box> { + self.implementation.load(&mut self.world, window_context) + } + + pub fn update(&mut self, window_context: &WindowContext) -> Result<(), Box> { + self.implementation.update(&mut self.world, window_context) + } + + pub fn render( + &mut self, + acquire_future: Box, + window_context: &mut WindowContext, + ) -> Result, Box> { + self.implementation + .render(acquire_future, &mut self.world, window_context) + } + + pub fn unload(&mut self) { + self.implementation.unload() + } +} + +/// Trait pour les implémentations de scènes +pub trait AsScene { fn loaded(&self) -> bool; fn load( &mut self, diff --git a/src/game/scenes/main_scene.rs b/src/game/scenes/main_scene.rs index 33532f8..0079a5a 100644 --- a/src/game/scenes/main_scene.rs +++ b/src/game/scenes/main_scene.rs @@ -16,7 +16,7 @@ use crate::core::render::render_pass_manager::{RenderPassConfig, RenderPassManag use crate::core::render::resources::meshes::{ObjMesh, SquareMesh}; use crate::core::render::resources::pipeline::PipelineLoader; use crate::core::render::resources::texture::{TextureLoadInfo, TextureLoader, TextureSourceKind}; -use crate::core::scene::Scene; +use crate::core::scene::AsScene; use crate::game::assets::pipelines::simple::SimplePipeline; use bevy_ecs::world::World; use egui_winit_vulkano::egui; @@ -47,7 +47,7 @@ pub struct MainScene { state: Option, } -impl Scene for MainScene { +impl AsScene for MainScene { fn loaded(&self) -> bool { self.state.is_some() } @@ -393,7 +393,7 @@ impl Scene for MainScene { gui.draw_on_image(render_future, swapchain_image_view.clone()) }); - Ok(render_future) + Ok(Box::new(render_future)) } fn unload(&mut self) { diff --git a/src/game/scenes/settings_scene.rs b/src/game/scenes/settings_scene.rs index f2e1eff..38e44b2 100644 --- a/src/game/scenes/settings_scene.rs +++ b/src/game/scenes/settings_scene.rs @@ -7,7 +7,7 @@ use crate::core::render::primitives::vulkan_resource::{ VulkanCommandBufferAllocator, VulkanGraphicsQueue, }; use crate::core::render::render_pass_manager::{RenderPassConfig, RenderPassManager}; -use crate::core::scene::Scene; +use crate::core::scene::AsScene; use bevy_ecs::world::World; use egui_winit_vulkano::egui; use vulkano::{ @@ -26,7 +26,7 @@ pub struct SettingsScene { state: Option, } -impl Scene for SettingsScene { +impl AsScene for SettingsScene { fn loaded(&self) -> bool { self.state.is_some() } @@ -144,8 +144,10 @@ impl Scene for SettingsScene { gui.draw_on_image(render_future, swapchain_image_view.clone()) }); - Ok(render_future) + Ok(Box::new(render_future)) } - fn unload(&mut self) {} + fn unload(&mut self) { + self.state = None; + } }