summaryrefslogtreecommitdiff
path: root/engine/src/windowing.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-08-14 20:20:41 +0200
committerHampusM <hampus@hampusmat.com>2026-08-15 00:43:37 +0200
commit1ac2ff603b1c843541234648ab801e83b2947444 (patch)
treef8080920cd6c02de07b9f0314e18f473ad620adc /engine/src/windowing.rs
parenta61463b205f3c3aaa9174cdae3704a8a53542aea (diff)
perf(engine): store keyboard key states & state updates as bitsHEADmaster
Diffstat (limited to 'engine/src/windowing.rs')
-rw-r--r--engine/src/windowing.rs55
1 files changed, 41 insertions, 14 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs
index 808e85e..805004a 100644
--- a/engine/src/windowing.rs
+++ b/engine/src/windowing.rs
@@ -32,7 +32,7 @@ use crate::ecs::sole::Single;
use crate::ecs::system::observer::Observe;
use crate::ecs::uid::Uid;
use crate::ecs::{declare_entity, pair, Query, Sole};
-use crate::util::MapVec;
+use crate::util::{BitArray, MapVec};
use crate::vector::Vec2;
use crate::windowing::dpi::{PhysicalPosition, PhysicalSize, Position};
use crate::windowing::keyboard::{Key, KeyState, Keyboard, UnknownKeyCodeError};
@@ -207,8 +207,21 @@ fn update_stuff(
mouse_button_input.flags.clear();
}
+ for (key_index, key_state_bits) in input.keys.iter_occupied() {
+ let key = Key::KEYS[key_index];
+
+ let key_state = match key_state_bits {
+ KEY_PRESSED_BITS => KeyState::Pressed,
+ KEY_RELEASED_BITS => KeyState::Released,
+ _ => unreachable!(),
+ };
+
+ keyboard.set_key_state(key, key_state);
+ }
+
input.relative_mouse_pos_delta = Vec2 { x: 0.0, y: 0.0 };
input.mouse_scroll_delta = MouseScrollDelta { vert_lines: 0.0, hor_lines: 0.0 };
+ input.keys.clear();
};
keyboard.set_text_keys(iter_array_queue(&context.shared_state.text_keys));
@@ -307,9 +320,6 @@ fn update_stuff(
window.set_changed();
}
- MessageFromApp::KeyboardKeyStateChanged(key, key_state) => {
- keyboard.set_key_state(key, key_state);
- }
}
}
@@ -650,7 +660,9 @@ fn create_event_loop() -> Result<EventLoop<()>, EventLoopError>
true,
);
}
- _ => { compile_error!("Unsupported platform") }
+ _ => {
+ compile_error!("Unsupported platform")
+ }
}
event_loop_builder.build()
@@ -673,7 +685,6 @@ enum MessageFromApp
WindowResized(WindowId, PhysicalSize<u32>),
WindowCloseRequested(WindowId),
WindowScaleFactorChanged(WindowId, f64),
- KeyboardKeyStateChanged(Key, KeyState),
}
#[derive(Debug)]
@@ -724,6 +735,7 @@ struct Input
absolute_mouse_pos: PhysicalPosition<f64>,
mouse_scroll_delta: MouseScrollDelta,
mouse_buttons: MapVec<MouseButton, MouseButtonInput>,
+ keys: BitArray<{ (Key::KEYS.len() * BITS_PER_KEY).div_ceil(8) }, BITS_PER_KEY>,
}
impl Default for Input
@@ -741,6 +753,7 @@ impl Default for Input
(MouseButton::Back, MouseButtonInput::default()),
(MouseButton::Forward, MouseButtonInput::default()),
]),
+ keys: BitArray::new(),
}
}
}
@@ -761,6 +774,11 @@ struct MouseButtonFlags: u8
}
}
+const BITS_PER_KEY: usize = 2;
+
+const KEY_PRESSED_BITS: u8 = 0b11;
+const KEY_RELEASED_BITS: u8 = 0b01;
+
#[derive(Debug)]
struct InitData
{
@@ -977,10 +995,17 @@ impl ApplicationHandler for App
}
};
- self.send_message(MessageFromApp::KeyboardKeyStateChanged(
- key,
- keyboard_event.state.into(),
- ));
+ let Some(mut input) = self.lock_input() else {
+ tracing::error!("Locking input mutex timed out after 100ms");
+ return;
+ };
+
+ let key_state_bits = match keyboard_event.state.into() {
+ KeyState::Pressed => KEY_PRESSED_BITS,
+ KeyState::Released => KEY_RELEASED_BITS,
+ };
+
+ input.keys.set(key as usize, key_state_bits);
}
WindowEvent::CursorMoved { device_id: _, position } => {
{
@@ -1148,21 +1173,23 @@ fn get_monitor_native_id(
target_os = "linux" => {
use winit::platform::wayland::{
ActiveEventLoopExtWayland,
- MonitorHandleExtWayland
+ MonitorHandleExtWayland,
};
use winit::platform::x11::MonitorHandleExtX11;
if event_loop.is_wayland() {
NativeMonitorId::Wayland(
- <WinitMonitorHandle as MonitorHandleExtWayland>::native_id(monitor)
+ <WinitMonitorHandle as MonitorHandleExtWayland>::native_id(monitor),
)
} else {
NativeMonitorId::X11(
- <WinitMonitorHandle as MonitorHandleExtX11>::native_id(monitor)
+ <WinitMonitorHandle as MonitorHandleExtX11>::native_id(monitor),
)
}
}
- _ => { compile_error!("Unsupported target platform") }
+ _ => {
+ compile_error!("Unsupported target platform")
+ }
}
}