summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-06-28 18:06:54 +0200
committerHampusM <hampus@hampusmat.com>2026-06-30 18:24:47 +0200
commit921ca69cfdd0a03cbe3f5e301c51869eeee4bee5 (patch)
tree784812c7f03c5b8ac4c921f9677671587bb2f457 /engine
parent3dc6e1131d951f88220403fef75cbe13f5edda6b (diff)
refactor(engine): move some windowing::Context fields to SharedState struct
Diffstat (limited to 'engine')
-rw-r--r--engine/src/windowing.rs48
1 files changed, 15 insertions, 33 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs
index c79812a..8dba8f9 100644
--- a/engine/src/windowing.rs
+++ b/engine/src/windowing.rs
@@ -191,15 +191,13 @@ fn update_stuff(
keyboard.set_text_keys(iter_array_queue(&context.shared_state.text_keys));
let Context {
- ref message_from_app_queue,
ref mut display_handle,
ref mut windows,
- ref thread_panicked,
ref shared_state,
..
} = *context;
- for message in iter_array_queue(message_from_app_queue) {
+ for message in iter_array_queue(&shared_state.message_from_app_queue) {
tracing::trace!(message=?message, "Received message from app");
match message {
@@ -301,7 +299,7 @@ fn update_stuff(
}
}
- if thread_panicked.load(Ordering::Relaxed) {
+ if shared_state.thread_panicked.load(Ordering::Relaxed) {
cold_path();
return Err("Windowing app thread panicked".into());
}
@@ -375,10 +373,7 @@ fn handle_window_removed(
#[derive(Debug, Sole)]
pub struct Context
{
- message_from_app_queue: Arc<ArrayQueue<MessageFromApp>>,
- message_to_app_queue: Arc<ArrayQueue<MessageToApp>>,
shared_state: Arc<SharedState>,
- thread_panicked: Arc<AtomicBool>,
display_handle: Option<OwnedDisplayHandle>,
windows: MapVec<WindowId, (Arc<winit::window::Window>, Uid)>,
}
@@ -421,7 +416,7 @@ impl Context
fn try_send_message_to_app(&self, message: MessageToApp)
{
- if self.message_to_app_queue.push(message).is_err() {
+ if self.shared_state.message_to_app_queue.push(message).is_err() {
tracing::error!(
"Failed to send message. Queue for messages to windowing app is full"
);
@@ -441,30 +436,16 @@ impl Context
panic!("A windowing context have already been created");
}
- let message_from_app_queue = Arc::new(ArrayQueue::<MessageFromApp>::new(
- MESSAGE_FROM_APP_QUEUE_SIZE,
- ));
-
- let message_from_app_queue_b = message_from_app_queue.clone();
-
- let message_to_app_queue =
- Arc::new(ArrayQueue::<MessageToApp>::new(MESSAGE_TO_APP_QUEUE_SIZE));
-
- let message_to_app_queue_b = message_to_app_queue.clone();
-
let shared_state = Arc::new(SharedState::default());
let shared_state_b = shared_state.clone();
- let thread_panicked = Arc::new(AtomicBool::new(false));
- let thread_panicked_b = thread_panicked.clone();
-
ThreadBuilder::new()
.name("windowing app".to_string())
.spawn(move || {
+ let shared_state_c = shared_state_b.clone();
+
match catch_unwind(move || {
let mut app = App {
- message_from_app_queue: message_from_app_queue_b,
- message_to_app_queue: message_to_app_queue_b,
shared_state: shared_state_b,
windows: MapVec::default(),
};
@@ -488,17 +469,14 @@ impl Context
app.shared_state.has_thread_err.store(true, Ordering::Relaxed);
}
Err(_) => {
- thread_panicked_b.store(true, Ordering::Relaxed);
+ shared_state_c.thread_panicked.store(true, Ordering::Relaxed);
}
};
})
.expect("Failed to create windowing thread");
Self {
- message_from_app_queue,
- message_to_app_queue,
shared_state,
- thread_panicked,
display_handle: None,
windows: MapVec::default(),
}
@@ -568,10 +546,13 @@ enum MessageToApp
#[derive(Debug)]
struct SharedState
{
+ message_from_app_queue: ArrayQueue<MessageFromApp>,
+ message_to_app_queue: ArrayQueue<MessageToApp>,
relative_mouse_pos_delta: AtomicTwoF64,
absolute_mouse_pos: AtomicTwoF64,
text_keys: ArrayQueue<char>,
is_dropped: AtomicBool,
+ thread_panicked: AtomicBool,
thread_err: Mutex<Option<AppThreadError>>,
has_thread_err: AtomicBool,
}
@@ -581,10 +562,13 @@ impl Default for SharedState
fn default() -> Self
{
Self {
+ message_from_app_queue: ArrayQueue::new(MESSAGE_FROM_APP_QUEUE_SIZE),
+ message_to_app_queue: ArrayQueue::new(MESSAGE_TO_APP_QUEUE_SIZE),
relative_mouse_pos_delta: AtomicTwoF64::new((0.0, 0.0)),
absolute_mouse_pos: AtomicTwoF64::new((0.0, 0.0)),
text_keys: ArrayQueue::new(TEXT_KEY_QUEUE_SIZE),
is_dropped: AtomicBool::new(false),
+ thread_panicked: AtomicBool::new(false),
thread_err: Mutex::new(None),
has_thread_err: AtomicBool::new(false)
}
@@ -594,8 +578,6 @@ impl Default for SharedState
#[derive(Debug)]
struct App
{
- message_from_app_queue: Arc<ArrayQueue<MessageFromApp>>,
- message_to_app_queue: Arc<ArrayQueue<MessageToApp>>,
shared_state: Arc<SharedState>,
windows: MapVec<WindowId, (Weak<WinitWindow>, WindowSettings)>,
}
@@ -605,7 +587,7 @@ impl App
#[tracing::instrument(skip_all)]
fn handle_received_messages(&mut self, event_loop: &ActiveEventLoop)
{
- for message in iter_array_queue(&self.message_to_app_queue) {
+ for message in iter_array_queue(&self.shared_state.message_to_app_queue) {
match message {
MessageToApp::CreateWindow(window_ent_id, window_creation_attrs) => {
tracing::info!(
@@ -656,13 +638,13 @@ impl App
#[tracing::instrument(skip_all)]
fn send_message(&self, message: MessageFromApp)
{
- if self.message_from_app_queue.is_full() {
+ if self.shared_state.message_from_app_queue.is_full() {
tracing::warn!(
"Queue for messages to windowing app is full! Dropping oldest message"
);
}
- self.message_from_app_queue.force_push(message);
+ self.shared_state.message_from_app_queue.force_push(message);
}
}