From c1f270be9392bfac00061da5a70e16a71f49aa18 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Thu, 16 Oct 2025 11:28:19 -0400 Subject: graphics: refactor DebugInfo => FrameInfo, with other minor changes --- graphics/Cargo.toml | 4 ++++ graphics/src/lib.rs | 17 ++++++----------- graphics/src/render.rs | 49 +++++++++++++++++++++++++------------------------ 3 files changed, 35 insertions(+), 35 deletions(-) (limited to 'graphics') diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 0842f90..37c651a 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -13,3 +13,7 @@ raylib = { version = "5.5", features = ["wayland"] } [lints] workspace = true + +[features] +default = [] +debug = [] diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 42f0a39..5f44e41 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -1,8 +1,6 @@ use raylib::prelude::*; -#[cfg(debug_assertions)] -use crate::render::DebugInfo; -use crate::render::Renderer; +use crate::render::{FrameInfo, Renderer}; pub mod render; @@ -32,17 +30,14 @@ impl Window { } /// Returns the renderer for the game - #[cfg(debug_assertions)] pub fn renderer(&mut self) -> Renderer<'_> { - let debug = DebugInfo::new(&self.handle); + let info = FrameInfo::new(&self.handle); let handle = self.handle.begin_drawing(&self.thread); - Renderer::new(handle, debug) + Renderer::new(handle, info) } - /// Returns the renderer for the game - #[cfg(not(debug_assertions))] - pub fn renderer(&mut self) -> Renderer<'_> { - let handle = self.handle.begin_drawing(&self.thread); - Renderer::new(handle) + /// Returns the per frame delta time + pub fn delta_time(&self) -> f32 { + self.handle.get_frame_time() } } diff --git a/graphics/src/render.rs b/graphics/src/render.rs index 1164953..40b3e1f 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -1,23 +1,22 @@ use dungeon::{Dungeon, Entity}; use raylib::{ color::Color, - prelude::{RaylibDraw, RaylibDrawHandle}, + prelude::{RaylibDraw, RaylibDrawHandle, RaylibHandle}, }; -#[cfg(debug_assertions)] -use raylib::RaylibHandle; - -/// Debug information used each frame -#[cfg(debug_assertions)] -pub struct DebugInfo { +/// Information used each frame +pub struct FrameInfo { + /// Time in seconds since last frame drawn + pub delta: f32, + /// FPS for last frame drawn pub fps: u32, } - -#[cfg(debug_assertions)] -impl DebugInfo { +impl FrameInfo { pub fn new(handle: &RaylibHandle) -> Self { - let fps = handle.get_fps(); - Self { fps } + Self { + delta: handle.get_frame_time(), + fps: handle.get_fps(), + } } } @@ -25,20 +24,22 @@ impl DebugInfo { /// frame of the game. It is created per frame. pub struct Renderer<'a> { handle: RaylibDrawHandle<'a>, - #[cfg(debug_assertions)] - debug: DebugInfo, + info: FrameInfo, } impl<'a> Renderer<'a> { /// Creates the renderer for the current frame - #[cfg(debug_assertions)] - pub(crate) fn new(handle: RaylibDrawHandle<'a>, debug: DebugInfo) -> Self { - Self { handle, debug } + pub(crate) fn new(handle: RaylibDrawHandle<'a>, info: FrameInfo) -> Self { + Self { handle, info } } - /// Creates the renderer for the current frame - #[cfg(not(debug_assertions))] - pub(crate) fn new(handle: RaylibDrawHandle<'a>) -> Self { - Self { handle } + /// 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 @@ -65,9 +66,9 @@ impl<'a> Renderer<'a> { self.draw_tiles(dungeon); self.draw_player(dungeon); - // Draw fps (debug mode only) - #[cfg(debug_assertions)] - self.draw_fps(self.debug.fps); + #[cfg(feature = "debug")] + // Draw fps (debug only) + self.draw_fps(self.info.fps); } /// Draw game over screen -- cgit v1.2.3-freya