summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--game/Cargo.toml4
-rw-r--r--graphics/Cargo.toml4
-rw-r--r--graphics/src/lib.rs17
-rw-r--r--graphics/src/render.rs49
4 files changed, 39 insertions, 35 deletions
diff --git a/game/Cargo.toml b/game/Cargo.toml
index 01a8ec9..bbd9b77 100644
--- a/game/Cargo.toml
+++ b/game/Cargo.toml
@@ -9,3 +9,7 @@ graphics = { path = "../graphics" }
[lints]
workspace = true
+
+[features]
+default = ["debug"]
+debug = ["graphics/debug"]
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