app: refactor WindowContext name and creation

This commit is contained in:
Florian RICHER 2025-05-31 12:56:00 +02:00
parent a293b962f7
commit 45ccf030f6
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
6 changed files with 42 additions and 52 deletions

View file

@ -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<Mutex<>> pour la mutabilité partagée
#[derive(Clone)]
pub struct ApplicationContext {
pub struct WindowContext {
// Données Vulkan (immutables)
pub vulkan_context: Arc<VulkanContext>,
pub device: Arc<Device>,
@ -46,7 +42,7 @@ pub struct ApplicationContext {
pub gui: Rc<RefCell<Gui>>,
}
impl ApplicationContext {
impl WindowContext {
pub fn new(
vulkan_context: Arc<VulkanContext>,
vulkano_windows: Rc<RefCell<VulkanoWindows>>,

View file

@ -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<UserEvent>,
// Context d'application partagé par fenêtre - architecture unifiée
app_contexts: HashMap<WindowId, Rc<RefCell<ApplicationContext>>>,
app_contexts: HashMap<WindowId, Rc<RefCell<WindowContext>>>,
}
impl App {
@ -103,6 +103,17 @@ impl ApplicationHandler<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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<UserEvent> 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();
}
}

View file

@ -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<dyn Error>> {
if let Some(scene) = self.current_scene_mut() {
if !scene.loaded() {

View file

@ -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<dyn Error>>;
fn update(&mut self, app_context: &mut ApplicationContext) -> Result<(), Box<dyn Error>>;
fn load(&mut self, app_context: &mut WindowContext) -> Result<(), Box<dyn Error>>;
fn update(&mut self, app_context: &mut WindowContext) -> Result<(), Box<dyn Error>>;
fn render(
&mut self,
acquire_future: Box<dyn GpuFuture>,
app_context: &mut ApplicationContext,
app_context: &mut WindowContext,
) -> Result<Box<dyn GpuFuture>, Box<dyn Error>>;
fn unload(&mut self);
}

View file

@ -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<dyn std::error::Error>> {
fn load(&mut self, app_context: &mut WindowContext) -> Result<(), Box<dyn std::error::Error>> {
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<dyn Error>> {
fn update(&mut self, app_context: &mut WindowContext) -> Result<(), Box<dyn Error>> {
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<dyn GpuFuture>,
app_context: &mut ApplicationContext,
app_context: &mut WindowContext,
) -> Result<Box<dyn GpuFuture>, Box<dyn Error>> {
let state = self.state.as_ref().ok_or("State not loaded")?;

View file

@ -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<dyn Error>> {
fn load(&mut self, app_context: &mut WindowContext) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
fn update(&mut self, _app_context: &mut WindowContext) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn render(
&mut self,
before_future: Box<dyn GpuFuture>,
app_context: &mut ApplicationContext,
app_context: &mut WindowContext,
) -> Result<Box<dyn GpuFuture>, Box<dyn Error>> {
let state = self.state.as_ref().ok_or("State not found")?;