summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-07 23:50:57 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-07 23:50:57 -0500
commit83c106455c828da18b5065179cf0fa73060f6539 (patch)
treeb77916eeaf06bce7a9ad30900f152dee8ba0de7f
parentoptimize assets (diff)
downloadDungeonCrawl-83c106455c828da18b5065179cf0fa73060f6539.tar.gz
DungeonCrawl-83c106455c828da18b5065179cf0fa73060f6539.tar.bz2
DungeonCrawl-83c106455c828da18b5065179cf0fa73060f6539.zip
graphics: refactor text rendering code
-rw-r--r--graphics/src/render.rs85
1 files changed, 45 insertions, 40 deletions
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
index 085f9c9..95aecb8 100644
--- a/graphics/src/render.rs
+++ b/graphics/src/render.rs
@@ -1,4 +1,4 @@
-use std::ops::Div;
+use std::{io::Write, ops::Div};
use dungeon::{
Direction, Dungeon, Entity, EntityKind, Floor, Item, MAP_SIZE, PLAYER_INVENTORY_SIZE,
@@ -437,7 +437,7 @@ impl Renderer {
// Draw MAP vert text
let text_x = padding;
- self.draw_vertical_text(r, text_x, y, "MAP");
+ self.draw_text_vertical(r, b"MAP", text_x, y);
// Draw minimap in top left of UI bar
let minimap_x = text_x + self.get_ui_font_size() + padding;
@@ -469,25 +469,6 @@ impl Renderer {
draw_dot(dungeon.player.entity.pos, Color::LIME);
}
- /// Draws vertical text
- fn draw_vertical_text<R>(&self, r: &mut R, x: u16, y: u16, text: &str)
- where
- R: RaylibDraw,
- {
- let font_size = self.get_ui_font_size();
- let padding = self.get_ui_padding() / 2;
- let spacing = font_size + padding;
- let mut byte_off = 0;
- for (idx_usize, char) in text.chars().enumerate() {
- let idx = downcast!(idx_usize, u16);
- let byte_len = char.len_utf8();
- let str = &text[byte_off..byte_off + byte_len];
- let char_y = y + idx * spacing;
- self.draw_text(r, str, x, char_y);
- byte_off += byte_len;
- }
- }
-
/// Draws the player's inventory
/// NOTE: Nothing is drawn if the inventory is empty
fn draw_inventory<R>(&self, r: &mut R, player: &Player)
@@ -509,7 +490,7 @@ impl Renderer {
let slots_x = text_x + text_len;
// Draw text
- self.draw_vertical_text(r, text_x, padding, "INV");
+ self.draw_text_vertical(r, b"INV", text_x, padding);
// Draw slots
for idx in 0..PLAYER_INVENTORY_SIZE {
@@ -570,7 +551,11 @@ impl Renderer {
let icon_x = stats_x;
let text_x = icon_x + icon_width;
+ // text buffer
+ let mut buffer = [b'x', b'0', b'0'];
+
// draw health
+ let _ = write!(&mut buffer[1..], "{health:02}");
let heart_y = padding;
r.draw_inv_atlas(
&self.textures.atlas,
@@ -580,9 +565,10 @@ impl Renderer {
icon_width,
0.0,
);
- self.draw_text(r, &format!("x{health:02}"), text_x, heart_y + padding);
+ self.draw_text(r, &buffer, text_x, heart_y + padding);
// draw damage
+ let _ = write!(&mut buffer[1..], "{damage:02}");
let damage_y = heart_y + font_size + padding;
r.draw_inv_atlas(
&self.textures.atlas,
@@ -592,7 +578,7 @@ impl Renderer {
icon_width,
0.0,
);
- self.draw_text(r, &format!("x{damage:02}"), text_x, damage_y + padding);
+ self.draw_text(r, &buffer, text_x, damage_y + padding);
}
/// Draws debug information ontop of other UI elements
@@ -615,28 +601,47 @@ impl Renderer {
r.draw_text(&fps_str, 10, 10, 30, Color::YELLOW);
}
+ /// Draws vertical text
+ fn draw_text_vertical<R>(&self, r: &mut R, text: &[u8], x: u16, y_start: u16)
+ where
+ R: RaylibDraw,
+ {
+ let spacing = self.get_ui_font_size() + self.get_ui_padding() / 2;
+ for (idx, char) in text.iter().enumerate() {
+ let y = y_start + downcast!(idx, u16) * spacing;
+ self.draw_char(r, *char, x, y);
+ }
+ }
+
/// Draw text helper function
- fn draw_text<R>(&self, r: &mut R, text: &str, x_start: u16, y: u16)
+ fn draw_text<R>(&self, r: &mut R, text: &[u8], x_start: u16, y: u16)
where
R: RaylibDraw,
{
- for (idx, char) in text.bytes().enumerate() {
- if !(32..=127).contains(&char) {
- println!("{char}");
- continue;
- }
- let ax = char % 16;
- let ay = (char - 32) / 16;
+ for (idx, char) in text.iter().enumerate() {
let x = x_start + downcast!(idx, u16) * self.get_ui_font_size();
- r.draw_inv_atlas(
- &self.textures.font,
- (ax.into(), ay.into()),
- x,
- y,
- self.get_ui_font_size(),
- 0.0,
- );
+ self.draw_char(r, *char, x, y);
+ }
+ }
+
+ /// Draws a char to the screen
+ fn draw_char<R>(&self, r: &mut R, char: u8, x: u16, y: u16)
+ where
+ R: RaylibDraw,
+ {
+ if !(32..=127).contains(&char) {
+ return;
}
+ let ax = char % 16;
+ let ay = (char - 32) / 16;
+ r.draw_inv_atlas(
+ &self.textures.font,
+ (ax.into(), ay.into()),
+ x,
+ y,
+ self.get_ui_font_size(),
+ 0.0,
+ );
}
}