From 45ccf030f61988d728b2ed8cf14b9ca94a56dff3 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sat, 31 May 2025 12:56:00 +0200 Subject: [PATCH] app: refactor WindowContext name and creation --- src/core/app/context.rs | 10 ++---- src/core/app/mod.rs | 53 +++++++++++++++---------------- src/core/scene/manager.rs | 4 +-- src/core/scene/mod.rs | 8 ++--- src/game/scenes/main_scene.rs | 11 +++---- src/game/scenes/settings_scene.rs | 8 ++--- 6 files changed, 42 insertions(+), 52 deletions(-) diff --git a/src/core/app/context.rs b/src/core/app/context.rs index d6d1647..80b602e 100644 --- a/src/core/app/context.rs +++ b/src/core/app/context.rs @@ -13,11 +13,7 @@ use vulkano::{ memory::allocator::StandardMemoryAllocator, }; use vulkano_util::{renderer::VulkanoWindowRenderer, window::VulkanoWindows}; -use winit::{ - event_loop::EventLoopProxy, - monitor::MonitorHandle, - window::{Window, WindowId}, -}; +use winit::{event_loop::EventLoopProxy, monitor::MonitorHandle, window::WindowId}; use crate::core::{input::InputManager, render::vulkan_context::VulkanContext, timer::Timer}; @@ -25,7 +21,7 @@ use super::user_event::UserEvent; /// Contexte d'application unifié avec Arc> pour la mutabilité partagée #[derive(Clone)] -pub struct ApplicationContext { +pub struct WindowContext { // Données Vulkan (immutables) pub vulkan_context: Arc, pub device: Arc, @@ -46,7 +42,7 @@ pub struct ApplicationContext { pub gui: Rc>, } -impl ApplicationContext { +impl WindowContext { pub fn new( vulkan_context: Arc, vulkano_windows: Rc>, diff --git a/src/core/app/mod.rs b/src/core/app/mod.rs index d936f57..81fb0a3 100644 --- a/src/core/app/mod.rs +++ b/src/core/app/mod.rs @@ -20,7 +20,7 @@ use winit::event::WindowEvent; use winit::event_loop::{ActiveEventLoop, EventLoopProxy}; use winit::window::WindowId; -use self::context::ApplicationContext; +use self::context::WindowContext; pub mod context; pub mod user_event; @@ -37,7 +37,7 @@ pub struct App { event_loop_proxy: EventLoopProxy, // Context d'application partagé par fenêtre - architecture unifiée - app_contexts: HashMap>>, + app_contexts: HashMap>>, } impl App { @@ -103,6 +103,17 @@ impl ApplicationHandler for App { scene_manager.load_scene(Box::new(MainScene::default())); self.scene_manager.insert(window_id, scene_manager); + + let app_context = Rc::new(RefCell::new(WindowContext::new( + self.vulkan_context.clone(), + self.vulkano_windows.clone(), + self.input_manager.clone(), + self.timer.clone(), + self.gui.get(&window_id).unwrap().clone(), + self.event_loop_proxy.clone(), + window_id, + ))); + self.app_contexts.insert(window_id, app_context); } fn device_event( @@ -152,28 +163,12 @@ impl ApplicationHandler for App { } // Créer ou mettre à jour le contexte d'application - let app_context = self - .app_contexts - .entry(id) - .or_insert_with(|| { - Rc::new(RefCell::new(ApplicationContext::new( - self.vulkan_context.clone(), - self.vulkano_windows.clone(), - self.input_manager.clone(), - self.timer.clone(), - self.gui.get(&id).unwrap().clone(), - self.event_loop_proxy.clone(), - id, - ))) - }) - .clone(); - + let window_context = self.app_contexts.get(&id).unwrap().clone(); let scene_manager = self.scene_manager.get_mut(&id).unwrap(); // Utiliser le contexte partagé pour les scènes { - let _scene_span = tracing::info_span!("scene_processing").entered(); - let mut context = app_context.borrow_mut(); + let mut context = window_context.borrow_mut(); { let _scene_span = @@ -211,17 +206,19 @@ impl ApplicationHandler for App { tracing::warn!("No current scene found for update!"); } } + + { + let _gui_span = tracing::debug_span!("request_redraw").entered(); + let mut window_context = window_context.borrow_mut(); + window_context.with_renderer_mut(|renderer| { + renderer.window().request_redraw(); + }); + } } _ => {} } } - fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { - let vulkano_windows = self.vulkano_windows.borrow(); - let window = vulkano_windows.get_primary_window().unwrap(); - window.request_redraw(); - } - fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) { match event { UserEvent::CursorGrabMode(window_id, grab) => { @@ -247,7 +244,7 @@ impl ApplicationHandler for App { let _ = window.request_inner_size(winit::dpi::LogicalSize::new(width, height)); let renderer = vulkano_windows.get_renderer_mut(window_id).unwrap(); renderer.resize(); - tracing::info!( + tracing::trace!( "Resolution changed to {}x{} for window {:?}", width, height, @@ -255,7 +252,7 @@ impl ApplicationHandler for App { ); } UserEvent::Exit(window_id) => { - tracing::info!("Exit requested for window {:?}", window_id); + tracing::trace!("Exit requested for window {:?}", window_id); event_loop.exit(); } } diff --git a/src/core/scene/manager.rs b/src/core/scene/manager.rs index f8e5b0f..cf07ebc 100644 --- a/src/core/scene/manager.rs +++ b/src/core/scene/manager.rs @@ -1,6 +1,6 @@ use std::error::Error; -use crate::core::app::context::ApplicationContext; +use crate::core::app::context::WindowContext; use super::Scene; @@ -51,7 +51,7 @@ impl SceneManager { pub fn load_scene_if_not_loaded( &mut self, - app_context: &mut ApplicationContext, + app_context: &mut WindowContext, ) -> Result<(), Box> { if let Some(scene) = self.current_scene_mut() { if !scene.loaded() { diff --git a/src/core/scene/mod.rs b/src/core/scene/mod.rs index 58a75df..5dd8a57 100644 --- a/src/core/scene/mod.rs +++ b/src/core/scene/mod.rs @@ -2,18 +2,18 @@ use std::error::Error; use vulkano::sync::GpuFuture; -use crate::core::app::context::ApplicationContext; +use crate::core::app::context::WindowContext; pub mod manager; pub trait Scene { fn loaded(&self) -> bool; - fn load(&mut self, app_context: &mut ApplicationContext) -> Result<(), Box>; - fn update(&mut self, app_context: &mut ApplicationContext) -> Result<(), Box>; + fn load(&mut self, app_context: &mut WindowContext) -> Result<(), Box>; + fn update(&mut self, app_context: &mut WindowContext) -> Result<(), Box>; fn render( &mut self, acquire_future: Box, - app_context: &mut ApplicationContext, + app_context: &mut WindowContext, ) -> Result, Box>; fn unload(&mut self); } diff --git a/src/game/scenes/main_scene.rs b/src/game/scenes/main_scene.rs index 00232f7..37fd4ed 100644 --- a/src/game/scenes/main_scene.rs +++ b/src/game/scenes/main_scene.rs @@ -2,7 +2,7 @@ use std::error::Error; use super::settings_scene::SettingsScene; use crate::core::app::DEPTH_IMAGE_ID; -use crate::core::app::context::ApplicationContext; +use crate::core::app::context::WindowContext; use crate::core::app::user_event::UserEvent; use crate::core::render::primitives::camera::Camera3D; use crate::core::render::primitives::transform::Transform; @@ -38,10 +38,7 @@ impl Scene for MainScene { self.state.is_some() } - fn load( - &mut self, - app_context: &mut ApplicationContext, - ) -> Result<(), Box> { + fn load(&mut self, app_context: &mut WindowContext) -> Result<(), Box> { let depth_image_view = app_context.with_renderer_mut(|renderer| { renderer.get_additional_image_view(DEPTH_IMAGE_ID).clone() }); @@ -118,7 +115,7 @@ impl Scene for MainScene { Ok(()) } - fn update(&mut self, app_context: &mut ApplicationContext) -> Result<(), Box> { + fn update(&mut self, app_context: &mut WindowContext) -> Result<(), Box> { let state = self.state.as_mut().unwrap(); app_context.with_input_manager(|input_manager| { app_context.with_timer(|timer| { @@ -168,7 +165,7 @@ impl Scene for MainScene { fn render( &mut self, before_future: Box, - app_context: &mut ApplicationContext, + app_context: &mut WindowContext, ) -> Result, Box> { let state = self.state.as_ref().ok_or("State not loaded")?; diff --git a/src/game/scenes/settings_scene.rs b/src/game/scenes/settings_scene.rs index 4d6fc53..466652a 100644 --- a/src/game/scenes/settings_scene.rs +++ b/src/game/scenes/settings_scene.rs @@ -1,7 +1,7 @@ use std::error::Error; use crate::core::app::DEPTH_IMAGE_ID; -use crate::core::app::context::ApplicationContext; +use crate::core::app::context::WindowContext; use crate::core::app::user_event::UserEvent; use crate::core::render::render_pass_manager::{RenderPassConfig, RenderPassManager}; use crate::core::scene::Scene; @@ -27,7 +27,7 @@ impl Scene for SettingsScene { self.state.is_some() } - fn load(&mut self, app_context: &mut ApplicationContext) -> Result<(), Box> { + fn load(&mut self, app_context: &mut WindowContext) -> Result<(), Box> { let current_resolution = app_context.get_window_size(); let available_resolutions = app_context.get_available_resolutions(); @@ -39,14 +39,14 @@ impl Scene for SettingsScene { Ok(()) } - fn update(&mut self, _app_context: &mut ApplicationContext) -> Result<(), Box> { + fn update(&mut self, _app_context: &mut WindowContext) -> Result<(), Box> { Ok(()) } fn render( &mut self, before_future: Box, - app_context: &mut ApplicationContext, + app_context: &mut WindowContext, ) -> Result, Box> { let state = self.state.as_ref().ok_or("State not found")?;