From f3f18a6d0593073a222d50a67584ee643d0d299d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 31 May 2026 23:53:04 +0200 Subject: fix(engine): prevent creation of multiple windowing contexts --- engine/src/windowing.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'engine/src/windowing.rs') diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index fac1c62..5cb34f7 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -54,6 +54,8 @@ const MESSAGE_FROM_APP_QUEUE_SIZE: usize = 512; const MESSAGE_TO_APP_QUEUE_SIZE: usize = 16; // Increase if more messages are added +static CONTEXT_CREATED: AtomicBool = AtomicBool::new(false); + declare_entity!( pub PHASE, ( @@ -73,7 +75,10 @@ impl crate::ecs::extension::Extension for Extension { fn collect(self, mut collector: crate::ecs::extension::Collector<'_>) { - collector.add_sole(Context::default()).ok(); + if !CONTEXT_CREATED.load(Ordering::Relaxed) { + collector.add_sole(Context::new()).ok(); + } + collector.add_sole(Keyboard::default()).ok(); collector.add_sole(Mouse::default()).ok(); collector.add_sole(MouseButtons::default()).ok(); @@ -352,10 +357,18 @@ impl Context } } -impl Default for Context +impl Context { - fn default() -> Self + /// Returns a new windowing context. + /// + /// # Panics + /// Will panic if a windowing context have already been created. + pub fn new() -> Self { + if CONTEXT_CREATED.swap(true, Ordering::Relaxed) { + panic!("A windowing context have already been created"); + } + let message_from_app_queue = Arc::new(ArrayQueue::::new( MESSAGE_FROM_APP_QUEUE_SIZE, )); -- cgit v1.2.3-18-g5258