diff options
Diffstat (limited to 'graphics/src/render.rs')
| -rw-r--r-- | graphics/src/render.rs | 40 |
1 files changed, 15 insertions, 25 deletions
diff --git a/graphics/src/render.rs b/graphics/src/render.rs index 78ff94d..6fd2ea4 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -1,4 +1,4 @@ -use std::{f32, io::Write}; +use std::{f32, io::Write, time::Duration}; use dungeon::{ Direction, Dungeon, Entity, EntityKind, Floor, Item, MAP_SIZE, PLAYER_INVENTORY_SIZE, @@ -13,6 +13,8 @@ use raylib::{ texture::{RaylibTexture2D, RenderTexture2D, Texture2D}, }; +use crate::timer::Timer; + macro_rules! downcast { ($usize:expr, $type:ty) => { <$type>::try_from($usize).unwrap_or(<$type>::MAX) @@ -124,7 +126,6 @@ const UI_COL2: u16 = UI_COL1 + FONT_SIZE * 10; /// The maxmimum length any text string can be const MAX_TEXT_LEN: usize = 16; -#[derive(Debug)] struct Textures { // Tilemap atlas: Texture2D, @@ -156,7 +157,6 @@ impl Textures { } } -#[derive(Debug)] pub struct Renderer { /* Persistant Render Data */ /// All loaded image resources/textures @@ -175,11 +175,8 @@ pub struct Renderer { debug: bool, /// Scratch buffer to format text into text_buf: [u8; MAX_TEXT_LEN], - /* Per Frame Caculated Data */ - /// Last known FPS - fps: u32, - /// The current frame - frame: u64, + /// Frame timer + timer: Timer, } impl Renderer { pub fn new(handle: &mut RaylibHandle, thread: &RaylibThread) -> crate::Result<Self> { @@ -206,8 +203,7 @@ impl Renderer { framebuffer: Some(framebuffer), debug: false, text_buf: [0u8; MAX_TEXT_LEN], - fps: 0, - frame: 0, + timer: Timer::new(), }) } @@ -215,6 +211,10 @@ impl Renderer { self.debug = !self.debug; } + pub fn delta_time(&self) -> Duration { + self.timer.delta_time() + } + pub fn draw_frame( &mut self, handle: &mut RaylibHandle, @@ -224,12 +224,12 @@ impl Renderer { let render_width = handle.get_render_width() as f32; let render_height = handle.get_render_height() as f32; - // Update render info before drawing - self.update_per_frame_data(handle); - // Start the frame let mut r = handle.begin_drawing(t); + // Update render info before drawing + self.timer.update(); + // Update cached tilemaps self.update_tilemaps(&mut r, t, &dungeon.floor); @@ -263,16 +263,6 @@ impl Renderer { }; } - /// Update frame metadata while we still have access to the - /// `RaylibHandle`. These fields are inaccessable once it's - /// turned into a `RaylibDrawHandle`. - fn update_per_frame_data(&mut self, handle: &RaylibHandle) { - // Get last known fps - self.fps = handle.get_fps(); - // Update frame counter - self.frame += 1; - } - /// Draws the game dungeon fn draw_dungeon<R>(&mut self, r: &mut R, dungeon: &Dungeon) where @@ -668,7 +658,7 @@ impl Renderer { let player = &dungeon.player; // Draw FPS - draw_text!(self, r, UI_COL1, UI_ROW1, "FPS {}", self.fps); + draw_text!(self, r, UI_COL1, UI_ROW1, "FPS {}", self.timer.get_fps()); // Draw Player position let (x, y) = &player.entity.pos.xy(); @@ -687,7 +677,7 @@ impl Renderer { draw_text!(self, r, UI_COL2, UI_ROW2, "{hash:016X}"); // Draw current frame number - let frame = &self.frame; + let frame = self.timer.get_frame(); draw_text!(self, r, UI_COL2, UI_ROW3, "FRAME {frame}"); } |