diff options
Diffstat (limited to 'engine/src/lib.rs')
-rw-r--r-- | engine/src/lib.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs index bec1e30..cc629b2 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use std::time::{Duration, Instant}; +use glfw::window::KeyState; use glfw::{Window, WindowBuilder}; use crate::camera::Camera; @@ -21,6 +22,7 @@ pub mod object; pub mod vector; pub mod vertex; +pub use glfw::window::Key; pub use glfw::WindowSize; #[derive(Debug)] @@ -134,6 +136,18 @@ impl Engine &self.delta_time } + /// Returns whether or not a keyboard key is currently pressed. + /// + /// # Errors + /// Will return `Err` if getting the key state fails. + pub fn is_key_pressed(&self, key: Key) -> Result<bool, Error> + { + self.window + .get_key(key) + .map(|key_state| matches!(key_state, KeyState::Pressed)) + .map_err(Error::GetKeyStateFailed) + } + fn update_delta_time(&mut self, prev_frame_start: &mut Option<Instant>) { let frame_start_time = Instant::now(); @@ -164,4 +178,7 @@ pub enum Error #[error("Failed to get window size")] GetWindowSizeFailed(#[source] glfw::Error), + + #[error("Failed to get key state")] + GetKeyStateFailed(#[source] glfw::Error), } |