summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-10-08 20:37:19 -0400
committerFreya Murphy <freya@freyacat.org>2025-10-08 20:39:00 -0400
commitf7d7a32fe0efa34cb0c3be7feac747752de428c9 (patch)
tree5a75df9228b2f0038fb54750e0ff1df4ab7f90d6
parentRebase changes into docs and README (diff)
downloadDungeonCrawl-f7d7a32fe0efa34cb0c3be7feac747752de428c9.tar.gz
DungeonCrawl-f7d7a32fe0efa34cb0c3be7feac747752de428c9.tar.bz2
DungeonCrawl-f7d7a32fe0efa34cb0c3be7feac747752de428c9.zip
graphics: create Renderer struct
-rw-r--r--game/src/main.rs2
-rw-r--r--graphics/src/lib.rs49
-rw-r--r--graphics/src/render.rs93
3 files changed, 113 insertions, 31 deletions
diff --git a/game/src/main.rs b/game/src/main.rs
index 405b19d..05a25bd 100644
--- a/game/src/main.rs
+++ b/game/src/main.rs
@@ -10,6 +10,6 @@ fn main() {
while window.is_open() {
// TODO update game state
// Draw a single frame
- window.draw(&dungeon);
+ window.renderer().draw_frame(&dungeon);
}
}
diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs
index 1991c18..42f0a39 100644
--- a/graphics/src/lib.rs
+++ b/graphics/src/lib.rs
@@ -1,6 +1,11 @@
-use dungeon::Dungeon;
use raylib::prelude::*;
+#[cfg(debug_assertions)]
+use crate::render::DebugInfo;
+use crate::render::Renderer;
+
+pub mod render;
+
pub struct Window {
handle: RaylibHandle,
thread: RaylibThread,
@@ -10,14 +15,13 @@ impl Window {
/// Instantiates a new window provided with the default
/// window `width`, `height`, and `title`.
pub fn new(width: i32, height: i32, title: &str) -> Self {
- let (mut handle, thread) = raylib::init()
+ let (handle, thread) = raylib::init()
.size(width, height)
.title(title)
.resizable()
.log_level(TraceLogLevel::LOG_WARNING)
+ .vsync()
.build();
- // Set the target FPS (TODO: modify based on target system)
- handle.set_target_fps(60);
Self { handle, thread }
}
@@ -27,33 +31,18 @@ impl Window {
!self.handle.window_should_close()
}
- /// Draws a frame provided with the game state `Dungeon`
- pub fn draw(&mut self, _dungeon: &Dungeon) {
- let fps = self.handle.get_fps();
- let mut draw = self.handle.begin_drawing(&self.thread);
- draw.clear_background(Color::BLACK);
-
- let fps_str = format!("{fps}");
- draw.draw_text(&fps_str, 100, 100, 30, Color::YELLOW);
-
- // Clear the background to black
- draw.clear_background(Color::BLACK);
-
- draw.draw_text("test", 10, 10, 20, Color::GREEN);
- }
-
- /// Draw game over screen
- pub fn game_over(&mut self) {
- unimplemented!()
- }
-
- /// Draw player sprites
- pub fn draw_player(&mut self, _dungeon: &Dungeon) {
- unimplemented!()
+ /// Returns the renderer for the game
+ #[cfg(debug_assertions)]
+ pub fn renderer(&mut self) -> Renderer<'_> {
+ let debug = DebugInfo::new(&self.handle);
+ let handle = self.handle.begin_drawing(&self.thread);
+ Renderer::new(handle, debug)
}
- /// Draw dungeon tiles
- pub fn draw_tiles(&mut self, _dungeon: &Dungeon) {
- unimplemented!()
+ /// 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)
}
}
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
new file mode 100644
index 0000000..6a566ce
--- /dev/null
+++ b/graphics/src/render.rs
@@ -0,0 +1,93 @@
+use dungeon::Dungeon;
+use raylib::{
+ color::Color,
+ prelude::{RaylibDraw, RaylibDrawHandle},
+};
+
+#[cfg(debug_assertions)]
+use raylib::RaylibHandle;
+
+/// Debug information used each frame
+#[cfg(debug_assertions)]
+pub struct DebugInfo {
+ pub fps: u32,
+}
+
+#[cfg(debug_assertions)]
+impl DebugInfo {
+ pub fn new(handle: &RaylibHandle) -> Self {
+ let fps = handle.get_fps();
+ Self { 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>,
+ #[cfg(debug_assertions)]
+ debug: DebugInfo,
+}
+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 }
+ }
+
+ /// Creates the renderer for the current frame
+ #[cfg(not(debug_assertions))]
+ pub(crate) fn new(handle: RaylibDrawHandle<'a>) -> Self {
+ Self { handle }
+ }
+
+ /// 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
+ 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);
+
+ // Draw fps (debug mode only)
+ #[cfg(debug_assertions)]
+ self.draw_fps(self.debug.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) {
+ // TODO:
+ }
+
+ /// Draw dungeon tiles
+ pub fn draw_tiles(&mut self, _dungeon: &Dungeon) {
+ // TODO:
+ }
+
+ /// Draw FPS counter
+ pub fn draw_fps(&mut self, fps: u32) {
+ let fps_str = format!("{fps}");
+ self.handle.draw_text(&fps_str, 10, 10, 30, Color::YELLOW);
+ }
+}