From 0410f8a14cb5c1571ea524dde81c5ac850b914c4 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Fri, 17 Oct 2025 23:38:28 -0400 Subject: graphicsL add input functionality to --- graphics/src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++--------- graphics/src/render.rs | 4 ++-- 2 files changed, 39 insertions(+), 11 deletions(-) (limited to 'graphics') 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, 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 { + 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; -- cgit v1.2.3-freya