summaryrefslogtreecommitdiff
path: root/graphics/src
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-10-17 23:38:28 -0400
committerFreya Murphy <freya@freyacat.org>2025-10-18 00:04:48 -0400
commit0410f8a14cb5c1571ea524dde81c5ac850b914c4 (patch)
tree813ee48d11dd769ff42930b514409233037df0e1 /graphics/src
parentAdded enemy concepts and updated some info (diff)
downloadDungeonCrawl-0410f8a14cb5c1571ea524dde81c5ac850b914c4.tar.gz
DungeonCrawl-0410f8a14cb5c1571ea524dde81c5ac850b914c4.tar.bz2
DungeonCrawl-0410f8a14cb5c1571ea524dde81c5ac850b914c4.zip
graphicsL add input functionality to
Diffstat (limited to 'graphics/src')
-rw-r--r--graphics/src/lib.rs46
-rw-r--r--graphics/src/render.rs4
2 files changed, 39 insertions, 11 deletions
diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs
index 4f55388..6e5eb91 100644
--- a/graphics/src/lib.rs
+++ b/graphics/src/lib.rs
@@ -1,23 +1,28 @@
//! The `graphics` crate contains the core functionality for
//! rendering using the `raylib` library.
+use std::cell::RefCell;
+
use raylib::prelude::*;
use crate::render::{FrameInfo, Renderer};
pub mod render;
+/// The `KeyCode` type represents different keys being pressed on the users keyboard
+pub use raylib::consts::KeyboardKey as KeyCode;
+
/// The `Window` type represents the game window
#[derive(Debug)]
pub struct Window {
- handle: RaylibHandle,
+ handle: RefCell<RaylibHandle>,
thread: RaylibThread,
}
impl Window {
/// Instantiates a new window provided with the default
/// window `width`, `height`, and `title`.
- ///
+ ///
/// # Examples
/// ```no_run
/// use graphics::Window;
@@ -32,32 +37,55 @@ impl Window {
.log_level(TraceLogLevel::LOG_WARNING)
.vsync()
.build();
- Self { handle, thread }
+ Self {
+ handle: RefCell::new(handle),
+ thread,
+ }
}
/// Returns if the window should be closed.
/// This usually means the 'x' button has been pressed.
pub fn is_open(&self) -> bool {
- !self.handle.window_should_close()
+ !self.handle.borrow().window_should_close()
}
/// Returns the renderer for the game
- ///
+ ///
/// # Examples
/// ```no_run
/// use graphics::Window;
- ///
+ ///
/// let mut window = Window::new(800, 600, "Dungeon Crawl");
/// let mut renderer = window.renderer();
/// ```
pub fn renderer(&mut self) -> Renderer<'_> {
- let info = FrameInfo::new(&self.handle);
- let handle = self.handle.begin_drawing(&self.thread);
+ let info = FrameInfo::new(self.handle.get_mut());
+ let handle = self.handle.get_mut().begin_drawing(&self.thread);
Renderer::new(handle, info)
}
/// Returns the per frame delta time
pub fn delta_time(&self) -> f32 {
- self.handle.get_frame_time()
+ self.handle.borrow().get_frame_time()
+ }
+
+ /// Returns if the provided `KeyCode` has been pressed once
+ pub fn is_key_pressed(&self, key: KeyCode) -> bool {
+ self.handle.borrow().is_key_pressed(key)
+ }
+
+ /// 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 `KeyCode` is currently pressed
+ pub fn is_key_down(&self, key: KeyCode) -> bool {
+ self.handle.borrow().is_key_down(key)
+ }
+
+ /// Get the last key pressed
+ pub fn get_key_pressed(&self) -> Option<KeyCode> {
+ self.handle.borrow_mut().get_key_pressed()
}
}
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
index 68a7b68..1bb32ee 100644
--- a/graphics/src/render.rs
+++ b/graphics/src/render.rs
@@ -1,5 +1,5 @@
//! The `render` module contains the structures for displaying
-//! the game, with each frame represented by a `Renderer` and
+//! the game, with each frame represented by a `Renderer` and
//! frame specific information in `FrameInfo`.
use dungeon::{Dungeon, Entity};
@@ -65,7 +65,7 @@ impl<'a> Renderer<'a> {
}
/// Draws an entire frame
- ///
+ ///
/// # Examples
/// ```no_run
/// use dungeon::Dungeon;