diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-11-11 13:41:10 -0500 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-11-11 13:41:10 -0500 |
| commit | fc1ea55d97a0751e8007d496bbfe77c695982bef (patch) | |
| tree | dc378421f6e06022114f9eaa76c040855df99b4c | |
| parent | fix render camera positioning, other minor changes (diff) | |
| download | DungeonCrawl-fc1ea55d97a0751e8007d496bbfe77c695982bef.tar.gz DungeonCrawl-fc1ea55d97a0751e8007d496bbfe77c695982bef.tar.bz2 DungeonCrawl-fc1ea55d97a0751e8007d496bbfe77c695982bef.zip | |
graphics: refactor input key type
| -rw-r--r-- | game/src/main.rs | 14 | ||||
| -rw-r--r-- | graphics/src/lib.rs | 122 |
2 files changed, 90 insertions, 46 deletions
diff --git a/game/src/main.rs b/game/src/main.rs index 6905fe3..9449710 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -6,7 +6,7 @@ struct Game { window: Window, dungeon: Dungeon, // to ensure the most recently-pressed direction key is used: - current_dir: Option<(Direction, KeyCode)>, + current_dir: Option<(Direction, Key)>, } impl Game { @@ -21,11 +21,11 @@ impl Game { } fn player_dir(&mut self) -> Option<Direction> { - const MOVE_KEYS: [(Direction, [KeyCode; 2]); 4] = [ - (Direction::North, [KeyCode::KEY_UP, KeyCode::KEY_W]), - (Direction::West, [KeyCode::KEY_LEFT, KeyCode::KEY_A]), - (Direction::South, [KeyCode::KEY_DOWN, KeyCode::KEY_S]), - (Direction::East, [KeyCode::KEY_RIGHT, KeyCode::KEY_D]), + const MOVE_KEYS: [(Direction, [Key; 2]); 4] = [ + (Direction::North, [Key::Up, Key::W]), + (Direction::West, [Key::Left, Key::A]), + (Direction::South, [Key::Down, Key::S]), + (Direction::East, [Key::Right, Key::D]), ]; // if a key was just pressed, use it for the new direction to move in @@ -59,7 +59,7 @@ impl Game { // Main game loop while self.window.is_open() { // Handle keyboard input - if self.window.is_key_pressed(KeyCode::KEY_F3) { + if self.window.is_key_pressed(Key::F3) { self.window.toggle_debug(); } diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index b021cc6..8dccd63 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -1,9 +1,6 @@ //! The `graphics` crate contains the core functionality for //! rendering using the `raylib` library. -use std::borrow::Cow; -use std::cell::RefCell; - use dungeon::Dungeon; use raylib::prelude::*; @@ -13,19 +10,76 @@ use crate::render::Renderer; mod audio; mod render; -/// The `KeyCode` type represents different keys being pressed on the users keyboard -pub use raylib::consts::KeyboardKey as KeyCode; - /// The `Error` type used within this crate pub type Error = Box<dyn std::error::Error>; /// The `Result` type used witin this crate pub type Result<T> = std::result::Result<T, crate::Error>; +#[repr(u8)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Key { + // Movement + W, + A, + S, + D, + // Movement (Arrow) + Up, + Down, + Left, + Right, + // Debug keys + F3, + // Unknown/Unused key + Unknown(KeyboardKey), +} +impl From<KeyboardKey> for Key { + fn from(key: KeyboardKey) -> Self { + use KeyboardKey as K; + match key { + // Movement + K::KEY_W => Self::W, + K::KEY_A => Self::A, + K::KEY_S => Self::S, + K::KEY_D => Self::D, + // Movement (Arrow) + K::KEY_UP => Self::Up, + K::KEY_DOWN => Self::Down, + K::KEY_LEFT => Self::Left, + K::KEY_RIGHT => Self::Right, + // Debug keys + K::KEY_F3 => Self::F3, + // Unknown/Unused key + _ => Self::Unknown(key), + } + } +} +impl From<Key> for KeyboardKey { + fn from(key: Key) -> Self { + match key { + // Movement + Key::W => Self::KEY_W, + Key::A => Self::KEY_A, + Key::S => Self::KEY_S, + Key::D => Self::KEY_D, + // Movement (Arrow) + Key::Up => Self::KEY_UP, + Key::Down => Self::KEY_DOWN, + Key::Left => Self::KEY_LEFT, + Key::Right => Self::KEY_RIGHT, + // Debug keys + Key::F3 => Self::KEY_F3, + // Unknown/Unused key + Key::Unknown(k) => k, + } + } +} + /// The `WindowBuilder` type allows setting partial options for the window #[derive(Debug)] pub struct WindowBuilder<'a> { - title: Cow<'a, str>, + title: &'a str, width: u16, height: u16, vsync: bool, @@ -38,8 +92,8 @@ impl<'a> WindowBuilder<'a> { } /// Set the window title - pub fn title(&mut self, title: impl Into<Cow<'a, str>>) -> &mut Self { - self.title = title.into(); + pub fn title(&mut self, title: &'a str) -> &mut Self { + self.title = title; self } @@ -73,7 +127,7 @@ impl<'a> WindowBuilder<'a> { // Set raylib args from builder builder.size(self.width.into(), self.height.into()); - builder.title(&self.title); + builder.title(self.title); builder.resizable(); if self.vsync { builder.vsync(); @@ -108,9 +162,9 @@ impl<'a> WindowBuilder<'a> { let renderer = Renderer::new(&mut handle, &thread)?; Ok(Window { - handle: RefCell::new(handle), - thread, renderer, + handle, + thread, audio, }) } @@ -118,7 +172,7 @@ impl<'a> WindowBuilder<'a> { impl Default for WindowBuilder<'_> { fn default() -> Self { Self { - title: Cow::Borrowed("Dungeon Crawl"), + title: "Dungeon Crawl", width: render::RENDER_WIDTH, height: render::RENDER_HEIGHT, vsync: false, @@ -133,7 +187,7 @@ pub struct Window { // persistant renderer renderer: Renderer, // core raylib handles - handle: RefCell<RaylibHandle>, + handle: RaylibHandle, thread: RaylibThread, // audio data/subsystem audio: Audio, @@ -142,13 +196,13 @@ impl Window { /// Returns if the window should be closed. /// This usually means the 'x' button has been pressed. pub fn is_open(&self) -> bool { - !self.handle.borrow().window_should_close() + !self.handle.window_should_close() } /// Draws the next ingame frame pub fn draw_frame(&mut self, dungeon: &Dungeon) { self.renderer - .draw_frame(self.handle.get_mut(), &self.thread, dungeon); + .draw_frame(&mut self.handle, &self.thread, dungeon); } /// Toggles the debug UI @@ -158,37 +212,27 @@ impl Window { /// Returns the per frame delta time pub fn delta_time(&self) -> f32 { - self.handle.borrow().get_frame_time() - } - - /// Returns if the provided `KeyCode` has just been pressed - pub fn is_key_pressed(&self, key: KeyCode) -> bool { - self.handle.borrow().is_key_pressed(key) + self.handle.get_frame_time() } - /// Returns if the provided `KeyCode` has just been released - pub fn is_key_released(&self, key: KeyCode) -> bool { - self.handle.borrow().is_key_released(key) + /// Returns if the provided `Key` has just been pressed + pub fn is_key_pressed(&self, key: Key) -> bool { + self.handle.is_key_pressed(key.into()) } - /// Returns if the provided `KeyCode` is NOT currently pressed - pub fn is_key_up(&self, key: KeyCode) -> bool { - self.handle.borrow().is_key_up(key) + /// Returns if the provided `Key` has just been released + pub fn is_key_released(&self, key: Key) -> bool { + self.handle.is_key_released(key.into()) } - /// Returns if the provided `KeyCode` is currently pressed - pub fn is_key_down(&self, key: KeyCode) -> bool { - self.handle.borrow().is_key_down(key) + /// Returns if the provided `Key` is NOT currently pressed + pub fn is_key_up(&self, key: Key) -> bool { + self.handle.is_key_up(key.into()) } - /// Get the last key pressed - pub fn get_key_pressed(&self) -> Option<KeyCode> { - // We dont want to require a mutable reference - // to read the last key pressed - self.handle - .try_borrow_mut() - .ok() - .and_then(|mut h| h.get_key_pressed()) + /// Returns if the provided `Key` is currently pressed + pub fn is_key_down(&self, key: Key) -> bool { + self.handle.is_key_down(key.into()) } /// Get audio data for the window |