From 31b3456877e0ae155bc32efd0b90edf94ac0e364 Mon Sep 17 00:00:00 2001 From: Chance Date: Mon, 24 Mar 2025 23:26:33 -0400 Subject: [PATCH] convert the vec of windows to a hashmap --- engine/src/core/render/mod.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/engine/src/core/render/mod.rs b/engine/src/core/render/mod.rs index b2569eb..aeeb262 100644 --- a/engine/src/core/render/mod.rs +++ b/engine/src/core/render/mod.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::io::{Read, Write}; use std::ops::Deref; use std::sync::Arc; @@ -13,20 +14,21 @@ pub mod ctx; #[derive(Default)] pub struct App<'window> { - window: Vec>, + windows: HashMap>, ctx: Option>, } impl ApplicationHandler for App<'_> { fn resumed(&mut self, event_loop: &ActiveEventLoop) { - if self.window.is_empty() { + if self.windows.is_empty() { let win_attr = Window::default_attributes().with_title("Zenyx"); let window = Arc::new( event_loop .create_window(win_attr) .expect("create window err."), ); - self.window.push(window.clone()); + let window_id = window.id(); + self.windows.insert(window_id, window.clone()); let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap(); self.ctx = Some(wgpu_ctx) } @@ -40,16 +42,14 @@ impl ApplicationHandler for App<'_> { ) { match event { WindowEvent::CloseRequested => { - if let Some(index) = self.window.iter().position(|window| window.id() == window_id) { - let mut window = self.window.remove(index); + if let Some(window) = self.windows.remove(&window_id) { let mut window = Arc::into_inner(window); - window = None; - drop(window); - - println!("Window: {:?} closed, exiting",window_id); + drop(window); + + println!("Window: {:?} closed, exiting", window_id); } - if self.window.is_empty() { + if self.windows.is_empty() { event_loop.exit(); } } @@ -82,7 +82,8 @@ impl ApplicationHandler for App<'_> { .create_window(win_attr) .expect("create window err."), ); - self.window.push(new_window.clone()); + let window_id = new_window.id(); + self.windows.insert(window_id, new_window.clone()); let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap(); self.ctx = Some(wgpu_ctx); } @@ -92,7 +93,7 @@ impl ApplicationHandler for App<'_> { _ => {} }, WindowEvent::RedrawRequested => { - for window in &self.window { + for window in self.windows.values() { if let Some(ctx) = &mut self.ctx { ctx.draw(); } @@ -100,7 +101,7 @@ impl ApplicationHandler for App<'_> { } } WindowEvent::Resized(size) => { - if let Some(window) = self.window.iter().find(|w| w.id() == window_id) { + if let Some(window) = self.windows.get(&window_id) { if let Some(wgpu_ctx) = &mut self.ctx { wgpu_ctx.resize(size.into()); window.request_redraw();