move pipeline loader in world

This commit is contained in:
Florian RICHER 2025-06-12 14:24:11 +02:00
parent 9c651c5e0a
commit 6ba61e040e
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
5 changed files with 70 additions and 99 deletions

View file

@ -1,33 +1,42 @@
use std::cell::RefCell;
use std::error::Error;
use std::rc::Rc;
use crate::core::app::DEPTH_IMAGE_ID;
use crate::core::app::context::WindowContext;
use crate::core::render::primitives::vulkan_resource::{
VulkanCommandBufferAllocator, VulkanComputeQueue, VulkanDescriptorSetAllocator, VulkanDevice,
VulkanGraphicsQueue, VulkanInstance, VulkanMemoryAllocator, VulkanTransferQueue,
};
use crate::core::render::resources::pipeline::PipelineLoader;
use crate::core::render::resources::texture::TextureLoader;
use crate::core::scene::AsScene;
use bevy_ecs::world::World;
use super::{AsScene, Scene};
use super::Scene;
#[derive(Default)]
pub struct SceneManager {
scenes: Vec<Scene>,
current_scene_index: Option<usize>,
current_scene: Option<Scene>,
new_scene: Option<Box<dyn AsScene>>,
}
impl SceneManager {
pub fn new() -> Self {
Self {
scenes: Vec::new(),
current_scene_index: None,
current_scene: None,
new_scene: None,
}
}
fn create_world_with_resources(window_context: &WindowContext) -> World {
fn create_world_with_resources(window_context: &mut WindowContext) -> World {
let mut world = World::new();
let depth_image_view = window_context.with_renderer_mut(|renderer| {
renderer.get_additional_image_view(DEPTH_IMAGE_ID).clone()
});
let swapchain_image_view =
window_context.with_renderer(|renderer| renderer.swapchain_image_view().clone());
let vulkan_context = window_context.vulkan_context();
let vulkano_context = vulkan_context.vulkano_context();
@ -47,6 +56,12 @@ impl SceneManager {
TextureLoader::select_best_suitable_queue(vulkan_graphics_queue, vulkan_transfer_queue),
);
let pipeline_loader = PipelineLoader::new(
vulkan_device.clone(),
swapchain_image_view.format(),
depth_image_view.format(),
);
world.insert_resource(VulkanInstance(vulkan_instance.clone()));
world.insert_resource(VulkanDevice(vulkan_device.clone()));
world.insert_resource(VulkanGraphicsQueue(vulkan_graphics_queue.clone()));
@ -60,59 +75,37 @@ impl SceneManager {
vulkan_descriptor_set_allocator.clone(),
));
world.insert_resource(texture_loader);
world.insert_resource(pipeline_loader);
world
}
pub fn load_scene(&mut self, scene_impl: Box<dyn AsScene>, window_context: &WindowContext) {
let world = Self::create_world_with_resources(window_context);
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_impl: Box<dyn AsScene>,
window_context: &WindowContext,
) {
if let Some(index) = self.current_scene_index {
if index < self.scenes.len() {
self.scenes[index].unload();
let world = Self::create_world_with_resources(window_context);
self.scenes[index] = Scene::new(scene_impl, world);
}
} else {
self.load_scene(scene_impl, window_context);
}
pub fn set_new_scene(&mut self, scene_impl: Box<dyn AsScene>) {
self.new_scene = Some(scene_impl);
}
pub fn current_scene(&self) -> Option<&Scene> {
if let Some(index) = self.current_scene_index {
self.scenes.get(index)
} else {
None
}
self.current_scene.as_ref()
}
pub fn current_scene_mut(&mut self) -> Option<&mut Scene> {
if let Some(index) = self.current_scene_index {
self.scenes.get_mut(index)
} else {
None
}
self.current_scene.as_mut()
}
pub fn load_scene_if_not_loaded(
&mut self,
window_context: &mut WindowContext,
) -> Result<(), Box<dyn Error>> {
if let Some(scene) = self.current_scene_mut() {
if !scene.loaded() {
scene.load(window_context)?;
if let Some(new_scene) = self.new_scene.take() {
let world = Self::create_world_with_resources(window_context);
let mut scene = Scene::new(new_scene, world);
scene.load(window_context)?;
if let Some(mut current_scene) = self.current_scene.take() {
current_scene.unload();
}
} else {
tracing::warn!("No scene found in SceneManager!");
self.current_scene = Some(scene);
}
Ok(())
}