//! The `render` module contains the structures for displaying //! the game, with each frame represented by a `Renderer` and //! frame specific information in `FrameInfo`. use dungeon::{Dungeon, Entity}; use raylib::{ color::Color, prelude::{RaylibDraw, RaylibDrawHandle, RaylibHandle}, }; /// The `FrameInfo` struct contains information about /// the current frame being rendered. pub struct FrameInfo { /// Time in seconds since last frame drawn pub delta: f32, /// FPS for last frame drawn pub fps: u32, } impl FrameInfo { /// Creates a new `FrameInfo` from the provided /// `RaylibHandle`. pub fn new(handle: &RaylibHandle) -> Self { Self { delta: handle.get_frame_time(), fps: handle.get_fps(), } } } /// A `Renderer` is a renderer for a single /// frame of the game. It is created per frame. pub struct Renderer<'a> { handle: RaylibDrawHandle<'a>, info: FrameInfo, } impl<'a> Renderer<'a> { /// Creates the renderer for the current frame pub(crate) fn new(handle: RaylibDrawHandle<'a>, info: FrameInfo) -> Self { Self { handle, info } } /// Returns the info struct for the current frame pub fn info(&self) -> &FrameInfo { &self.info } /// Returns the per frame delta time pub fn delta_time(&self) -> f32 { self.info.delta } /// Returns the current render width pub fn render_width(&mut self) -> i32 { self.handle.get_render_width() } /// Returns the current render height pub fn render_height(&mut self) -> i32 { self.handle.get_render_height() } /// Clear the screen pub fn clear(&mut self) { self.handle.clear_background(Color::BLACK); } /// Draws an entire frame /// /// # Examples /// ```no_run /// use dungeon::Dungeon; /// use graphics::Window; /// let mut window = Window::new(800, 600, "Dungeon Crawl"); /// let mut renderer = window.renderer(); /// let dungeon = Dungeon::new(); /// renderer.draw_frame(&dungeon); /// ``` pub fn draw_frame(&mut self, dungeon: &Dungeon) { // Clear the background to black self.clear(); // Draw the dungeon self.draw_tiles(dungeon); self.draw_player(dungeon); #[cfg(feature = "debug")] // Draw fps (debug only) self.draw_fps(); } /// Draw game over screen pub fn draw_game_over(&mut self) { // TODO: } /// Draw the player sprite pub fn draw_player(&mut self, dungeon: &Dungeon) { self.draw_entity(dungeon.player()); } /// Draws an entity pub fn draw_entity(&mut self, _entity: &Entity) { // TODO: } /// Draw dungeon tiles pub fn draw_tiles(&mut self, _dungeon: &Dungeon) { // TODO: } /// Draw FPS counter pub fn draw_fps(&mut self) { let fps_str = format!("{}", self.info.fps); self.handle.draw_text(&fps_str, 10, 10, 30, Color::YELLOW); } }