diff options
| author | HampusM <hampus@hampusmat.com> | 2026-05-31 23:53:04 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-05-31 23:53:04 +0200 |
| commit | f3f18a6d0593073a222d50a67584ee643d0d299d (patch) | |
| tree | c7b9724b92beeedd512dfda573ae9b3c2df0d0e8 /engine/src/windowing.rs | |
| parent | 81eb01170da4334dce88bd0d9e815d6d28e3c796 (diff) | |
fix(engine): prevent creation of multiple windowing contexts
Diffstat (limited to 'engine/src/windowing.rs')
| -rw-r--r-- | engine/src/windowing.rs | 19 |
1 files changed, 16 insertions, 3 deletions
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::<MessageFromApp>::new( MESSAGE_FROM_APP_QUEUE_SIZE, )); |
