summaryrefslogtreecommitdiff
path: root/engine/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-26 00:56:51 +0200
committerHampusM <hampus@hampusmat.com>2023-10-26 00:56:51 +0200
commitf94c1c531a1f9874e4303fb702f22b7e953c2a29 (patch)
tree5f8a737848e623f7d4bdb9dade17ae620575c584 /engine/src/lib.rs
parenta1ab78e904b52b025ed81bbc899188094308ff24 (diff)
feat(engine): add get keyboard input
Diffstat (limited to 'engine/src/lib.rs')
-rw-r--r--engine/src/lib.rs17
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),
}