//! The `graphics` crate contains the core functionality for //! rendering using the `raylib` library. use std::cell::RefCell; use dungeon::Dungeon; use raylib::prelude::*; use crate::audio::Audio; 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; /// The `Result` type used witin this crate pub type Result = std::result::Result; /// The `Window` type represents the game window #[derive(Debug)] pub struct Window { // persistant renderer renderer: Renderer, // core raylib handles handle: RefCell, thread: RaylibThread, // audio data/subsystem audio: Audio, } impl Window { /// Instantiates a new window provided with the default /// window `width`, `height`, and `title`. /// /// # Examples /// ```no_run /// use graphics::Window; /// /// let window = Window::new(800, 600, "Dungeon Crawl").unwrap(); /// ``` pub fn new(width: u16, height: u16, title: &str) -> crate::Result { let (mut handle, thread) = raylib::init() .size(width.into(), height.into()) .title(title) .resizable() .log_level(TraceLogLevel::LOG_WARNING) .vsync() .build(); // update window min size handle.set_window_min_size(width.into(), height.into()); // load audio let audio = Audio::load()?; // load renderer let renderer = Renderer::new(&mut handle, &thread)?; Ok(Self { handle: RefCell::new(handle), thread, renderer, audio, }) } /// Instantiates a new window with a default tile and size /// /// # Examples /// ```no_run /// use graphics::Window; /// /// let window = Window::new_default().unwrap(); /// ``` pub fn new_default() -> crate::Result { Self::new(render::RENDER_WIDTH, render::RENDER_HEIGHT, "Dungeon Crawl") } /// 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() } /// Draws the next ingame frame /// /// # Examples /// ```no_run /// use graphics::Window; /// use dungeon::Dungeon; /// /// let dungeon = Dungeon::new(); /// let mut window = Window::new(800, 600, "Dungeon Crawl").unwrap(); /// window.draw_frame(&dungeon); /// ``` pub fn draw_frame(&mut self, dungeon: &Dungeon) { self.renderer .draw_frame(self.handle.get_mut(), &self.thread, dungeon); } /// Toggles the debug UI pub fn toggle_debug(&mut self) { self.renderer.toggle_debug(); } /// Returns the per frame delta time pub fn delta_time(&self) -> f32 { 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 { // 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()) } /// Get audio data for the window pub fn audio(&self) -> &Audio { &self.audio } }