From 1f1ba3522ba10e9d114f82004331d111ca3071fa Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Sun, 9 Nov 2025 15:30:54 -0500 Subject: graphics: refactor string formatting to use single common scratch buffer --- graphics/src/render.rs | 71 +++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/graphics/src/render.rs b/graphics/src/render.rs index 1888ef4..0fee7b5 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -40,10 +40,9 @@ macro_rules! vec2 { } macro_rules! draw_text { - ($self:ident, $r:expr, $x:expr, $y:expr, $buffer:expr, $offset:expr, $($arg:tt)*) => {{ - let mut buffer = *$buffer; - let _ = write!(&mut buffer[$offset..], $($arg)*); - $self.draw_text($r, &buffer, $x, $y); + ($self:ident, $r:expr, $x:expr, $y:expr, $($arg:tt)*) => {{ + let _ = writeln!(&mut $self.text_buf[0..], $($arg)*); + $self.draw_text($r, &$self.text_buf, $x, $y); }}; } @@ -106,6 +105,9 @@ const UI_ROW3: u16 = UI_ROW2 + FONT_SIZE + UI_PADDING; const UI_COL1: u16 = UI_PADDING; 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 @@ -155,6 +157,8 @@ pub struct Renderer { framebuffer: Option, /// Show debug UI debug: bool, + /// Scratch buffer to format text into + text_buf: [u8; MAX_TEXT_LEN], /* Per Frame Caculated Data */ /// Last known FPS fps: u32, @@ -185,6 +189,7 @@ impl Renderer { tiles_hash: None, framebuffer: Some(framebuffer), debug: false, + text_buf: [0u8; MAX_TEXT_LEN], fps: 0, frame: 0, }) @@ -411,7 +416,7 @@ impl Renderer { } /// Draw dungeon tiles (foreground layer) - fn draw_fg_tilemap(&mut self, r: &mut R) + fn draw_fg_tilemap(&self, r: &mut R) where R: RaylibDraw, { @@ -425,7 +430,7 @@ impl Renderer { } /// Draw dungeon tiles (background layer) - fn draw_bg_tilemap(&mut self, r: &mut R) + fn draw_bg_tilemap(&self, r: &mut R) where R: RaylibDraw, { @@ -433,7 +438,7 @@ impl Renderer { } /// Draws the entities on the map - fn draw_entities(&mut self, r: &mut R, dungeon: &Dungeon) + fn draw_entities(&self, r: &mut R, dungeon: &Dungeon) where R: RaylibDraw, { @@ -474,7 +479,7 @@ impl Renderer { } /// Draws player HP, inventory, and floor number - fn draw_ui(&self, r: &mut R, dungeon: &Dungeon) + fn draw_ui(&mut self, r: &mut R, dungeon: &Dungeon) where R: RaylibDraw, { @@ -581,7 +586,7 @@ impl Renderer { } /// Draw player health & equpped weapon damage - fn draw_stats(&self, r: &mut R, player: &Player) + fn draw_stats(&mut self, r: &mut R, player: &Player) where R: RaylibDraw, { @@ -603,7 +608,7 @@ impl Renderer { FONT_SIZE, 0.0, ); - draw_text!(self, r, TEXT_X, HEART_Y, b"x00", 1, "{health:02}"); + draw_text!(self, r, TEXT_X, HEART_Y, "x{health:02}"); // draw damage const DAMAGE_Y: u16 = HEART_Y + FONT_SIZE + UI_PADDING; @@ -615,62 +620,38 @@ impl Renderer { FONT_SIZE, 0.0, ); - draw_text!(self, r, TEXT_X, DAMAGE_Y, b"x00", 1, "{damage:02}"); + draw_text!(self, r, TEXT_X, DAMAGE_Y, "x{damage:02}"); } /// Draws debug information ontop of other UI elements - fn draw_debug_ui(&self, r: &mut R, dungeon: &Dungeon) + fn draw_debug_ui(&mut self, r: &mut R, dungeon: &Dungeon) where R: RaylibDraw, { let player = &dungeon.player; // Draw FPS - draw_text!(self, r, UI_COL1, UI_ROW1, b"FPS ", 4, "{}", self.fps); + draw_text!(self, r, UI_COL1, UI_ROW1, "FPS {}", self.fps); // Draw Player position let (x, y) = &player.entity.pos.xy(); - draw_text!(self, r, UI_COL1, UI_ROW2, b"POS 00,00", 4, "{x:02},{y:02}"); + draw_text!(self, r, UI_COL1, UI_ROW2, "POS {x:02},{y:02}"); // Draw Player direction let dir = &player.entity.dir; - draw_text!(self, r, UI_COL1, UI_ROW3, b"DIR ", 4, "{dir}"); + draw_text!(self, r, UI_COL1, UI_ROW3, "DIR {dir}"); // Draw Player Seed let seed = &dungeon.floor.seed(); - draw_text!( - self, - r, - UI_COL2, - UI_ROW1, - b" 0000000000000000", - 0, - "{seed:X}" - ); + draw_text!(self, r, UI_COL2, UI_ROW1, "{seed:016X}"); // Draw Dungeon Hash let hash = dungeon.floor.hash(); - draw_text!( - self, - r, - UI_COL2, - UI_ROW2, - b" 0000000000000000", - 0, - "{hash:X}" - ); + draw_text!(self, r, UI_COL2, UI_ROW2, "{hash:016X}"); // Draw current frame number let frame = &self.frame; - draw_text!( - self, - r, - UI_COL2, - UI_ROW3, - b"FRAME ", - 6, - "{frame}" - ); + draw_text!(self, r, UI_COL2, UI_ROW3, "FRAME {frame}"); } /// Draws vertical text @@ -680,6 +661,9 @@ impl Renderer { { const SPACING: u16 = FONT_SIZE + UI_PADDING / 2; for (idx, char) in text.iter().enumerate() { + if *char == b'\n' { + break; + } let y = y_start + downcast!(idx, u16) * SPACING; self.draw_char(r, *char, x, y); } @@ -691,6 +675,9 @@ impl Renderer { R: RaylibDraw, { for (idx, char) in text.iter().enumerate() { + if *char == b'\n' { + break; + } let x = x_start + downcast!(idx, u16) * FONT_SIZE; self.draw_char(r, *char, x, y); } -- cgit v1.2.3-freya