summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/src/util.rs117
-rw-r--r--engine/src/windowing.rs35
2 files changed, 131 insertions, 21 deletions
diff --git a/engine/src/util.rs b/engine/src/util.rs
index 9d1c887..84ce726 100644
--- a/engine/src/util.rs
+++ b/engine/src/util.rs
@@ -1,6 +1,6 @@
use std::fmt::{Debug, Display};
-use crate::ecs::util::VecExt;
+use crate::ecs::util::{StreamingIterator, VecExt};
#[derive(Debug, Clone)]
pub struct MapVec<Key: Ord, Value>
@@ -259,16 +259,18 @@ impl<const SIZE: usize, const BITS_PER_ITEM: usize> BitArray<SIZE, BITS_PER_ITEM
self.inner[byte_index] |= item_bits << bit_index_in_byte;
}
- pub fn clear(&mut self)
+ pub fn iter_occupied(&self) -> BitArrayOccupiedIter<'_, BITS_PER_ITEM>
{
- self.inner.fill(0);
+ BitArrayOccupiedIter {
+ inner: self.inner.iter().enumerate().peekable(),
+ mask: u8::MAX,
+ }
}
- pub fn iter_occupied(&self) -> BitArrayOccupiedIter<'_, BITS_PER_ITEM>
+ pub fn iter_occupied_mut(&mut self) -> BitArrayOccupiedMutIter<'_, BITS_PER_ITEM>
{
- BitArrayOccupiedIter {
- inner: self.inner.iter().copied().enumerate(),
- byte: None,
+ BitArrayOccupiedMutIter {
+ inner: self.inner.iter_mut().enumerate().peekable(),
mask: u8::MAX,
}
}
@@ -290,8 +292,7 @@ impl<const SIZE: usize, const BITS_PER_ITEM: usize> Default
pub struct BitArrayOccupiedIter<'a, const BITS_PER_ITEM: usize>
{
- inner: std::iter::Enumerate<std::iter::Copied<std::slice::Iter<'a, u8>>>,
- byte: Option<(u8, usize)>,
+ inner: std::iter::Peekable<std::iter::Enumerate<std::slice::Iter<'a, u8>>>,
mask: u8,
}
@@ -307,8 +308,8 @@ impl<const BITS_PER_ITEM: usize> Iterator for BitArrayOccupiedIter<'_, BITS_PER_
fn next(&mut self) -> Option<Self::Item>
{
let (byte_masked, byte_index, item_bit_index_in_byte) = loop {
- let current = self.byte.and_then(|(byte, byte_index)| {
- let byte_masked = byte & self.mask;
+ let current = self.inner.peek().and_then(|(byte_index, byte)| {
+ let byte_masked = **byte & self.mask;
let lowest_one = byte_masked.lowest_one()?;
@@ -321,9 +322,8 @@ impl<const BITS_PER_ITEM: usize> Iterator for BitArrayOccupiedIter<'_, BITS_PER_
});
let Some((byte_masked, byte_index, item_bit_index_in_byte)) = current else {
- let (next_byte_index, next_byte) = self.inner.next()?;
+ let _ = self.inner.next()?;
- self.byte = Some((next_byte, next_byte_index));
self.mask = u8::MAX;
continue;
@@ -346,6 +346,97 @@ impl<const BITS_PER_ITEM: usize> Iterator for BitArrayOccupiedIter<'_, BITS_PER_
}
}
+pub struct BitArrayOccupiedMutIter<'a, const BITS_PER_ITEM: usize>
+{
+ inner: std::iter::Peekable<std::iter::Enumerate<std::slice::IterMut<'a, u8>>>,
+ mask: u8,
+}
+
+impl<const BITS_PER_ITEM: usize> BitArrayOccupiedMutIter<'_, BITS_PER_ITEM>
+{
+ const ITEM_MASK: u8 = !(u8::MAX << BITS_PER_ITEM);
+}
+
+impl<const BITS_PER_ITEM: usize> StreamingIterator
+ for BitArrayOccupiedMutIter<'_, BITS_PER_ITEM>
+{
+ type Item<'a>
+ = BitArrayItemMut<'a, BITS_PER_ITEM>
+ where
+ Self: 'a;
+
+ fn streaming_next(&mut self) -> Option<Self::Item<'_>>
+ {
+ let (byte_masked, byte_index, item_bit_index_in_byte) = loop {
+ let current = self.inner.peek().and_then(|(byte_index, byte)| {
+ let byte_masked = **byte & self.mask;
+
+ let lowest_one = byte_masked.lowest_one()?;
+
+ let item_bit_index_in_byte = match lowest_one as usize % BITS_PER_ITEM {
+ 0 => lowest_one as usize,
+ remainder => lowest_one as usize - remainder,
+ };
+
+ Some((byte_masked, byte_index, item_bit_index_in_byte))
+ });
+
+ let Some((byte_masked, byte_index, item_bit_index_in_byte)) = current else {
+ let _ = self.inner.next()?;
+
+ self.mask = u8::MAX;
+
+ continue;
+ };
+
+ break (byte_masked, byte_index, item_bit_index_in_byte);
+ };
+
+ let item_bits = (byte_masked >> (item_bit_index_in_byte)) & Self::ITEM_MASK;
+
+ self.mask &= (!Self::ITEM_MASK) << item_bit_index_in_byte;
+
+ let item_index_in_byte = item_bit_index_in_byte as usize / BITS_PER_ITEM;
+
+ let prev_bytes_item_cnt = (byte_index * 8) / BITS_PER_ITEM;
+
+ let index = prev_bytes_item_cnt + item_index_in_byte;
+
+ Some(BitArrayItemMut {
+ index,
+ bits: item_bits,
+ byte: self.inner.peek_mut().unwrap().1,
+ bit_index_in_byte: item_bit_index_in_byte,
+ })
+ }
+}
+
+pub struct BitArrayItemMut<'a, const BITS_PER_ITEM: usize>
+{
+ pub index: usize,
+ pub bits: u8,
+ byte: &'a mut u8,
+ bit_index_in_byte: usize,
+}
+
+impl<'a, const BITS_PER_ITEM: usize> BitArrayItemMut<'a, BITS_PER_ITEM>
+{
+ const ITEM_MASK: u8 = !(u8::MAX << BITS_PER_ITEM);
+
+ pub fn clear_and_set(&mut self, new_bits: u8, clear_mask: u8)
+ {
+ let new_bits = new_bits & Self::ITEM_MASK;
+
+ let clear_mask = clear_mask & Self::ITEM_MASK;
+
+ *self.byte &= !(clear_mask << self.bit_index_in_byte);
+
+ *self.byte |= new_bits << self.bit_index_in_byte;
+
+ self.bits = new_bits;
+ }
+}
+
macro_rules! try_option {
($expr: expr) => {
match $expr {
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs
index 4279148..aa86f01 100644
--- a/engine/src/windowing.rs
+++ b/engine/src/windowing.rs
@@ -31,6 +31,7 @@ use crate::ecs::phase::{Phase, PRE_UPDATE as PRE_UPDATE_PHASE};
use crate::ecs::sole::Single;
use crate::ecs::system::observer::Observe;
use crate::ecs::uid::Uid;
+use crate::ecs::util::StreamingIterator;
use crate::ecs::{declare_entity, pair, Query, Sole};
use crate::util::{BitArray, MapVec};
use crate::vector::Vec2;
@@ -207,21 +208,41 @@ 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 mut key_updates = input.keys.iter_occupied_mut();
- let key_state = match key_state_bits {
+ while let Some(mut key_item) = key_updates.streaming_next() {
+ let key = Key::KEYS[key_item.index];
+
+ if key_item.bits == KEY_PRESSED_BITS | KEY_RELEASED_BITS {
+ match keyboard.get_key_state(key) {
+ KeyState::Pressed => {
+ keyboard.set_key_state(key, KeyState::Released);
+
+ key_item.clear_and_set(KEY_PRESSED_BITS, u8::MAX);
+ }
+ KeyState::Released => {
+ keyboard.set_key_state(key, KeyState::Pressed);
+
+ key_item.clear_and_set(KEY_RELEASED_BITS, u8::MAX);
+ }
+ }
+
+ continue;
+ }
+
+ let key_state = match key_item.bits {
KEY_PRESSED_BITS => KeyState::Pressed,
KEY_RELEASED_BITS => KeyState::Released,
_ => unreachable!(),
};
keyboard.set_key_state(key, key_state);
+
+ key_item.clear_and_set(0, u8::MAX);
}
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));
@@ -776,7 +797,7 @@ struct MouseButtonFlags: u8
const BITS_PER_KEY: usize = 2;
-const KEY_PRESSED_BITS: u8 = 0b11;
+const KEY_PRESSED_BITS: u8 = 0b10;
const KEY_RELEASED_BITS: u8 = 0b01;
#[derive(Debug)]
@@ -1005,9 +1026,7 @@ impl ApplicationHandler for App
KeyState::Released => KEY_RELEASED_BITS,
};
- input
- .keys
- .clear_and_set(key as usize, key_state_bits, u8::MAX);
+ input.keys.clear_and_set(key as usize, key_state_bits, 0);
}
WindowEvent::CursorMoved { device_id: _, position } => {
{