diff options
Diffstat (limited to 'graphics')
| -rw-r--r-- | graphics/src/render.rs | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/graphics/src/render.rs b/graphics/src/render.rs index a3fa528..aac41b7 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -10,6 +10,7 @@ use raylib::{ color::Color, math::{Rectangle, Vector2}, prelude::{RaylibDraw, RaylibMode2DExt, RaylibTextureModeExt}, + text::Font, texture::{RaylibTexture2D, RenderTexture2D, Texture2D}, }; @@ -80,13 +81,20 @@ struct Textures { atlas: Texture2D, // Misc error: Texture2D, + // Fonts + pixantiqua: Font, } impl Textures { fn new(handle: &mut RaylibHandle, thread: &RaylibThread) -> crate::Result<Self> { let atlas = handle.load_texture(thread, "assets/atlas.bmp")?; let error = handle.load_texture(thread, "assets/error.bmp")?; + let pixantiqua = handle.load_font(thread, "assets/pixantiqua.fnt")?; - Ok(Self { atlas, error }) + Ok(Self { + atlas, + error, + pixantiqua, + }) } fn item_texture(&self, _item: &Item) -> &Texture2D { @@ -442,7 +450,7 @@ impl Renderer { let byte_len = char.len_utf8(); let str = &text[byte_off..byte_off + byte_len]; let char_y = y + idx * spacing; - r.draw_text_ext(str, x, char_y, font_size, color); + self.draw_text(r, str, x, char_y, color); byte_off += byte_len; } } @@ -539,11 +547,11 @@ impl Renderer { icon_width, 0.0, ); - r.draw_text_ext( + self.draw_text( + r, &format!("x{health:02}"), text_x, heart_y + padding, - font_size, Color::WHITE, ); @@ -557,11 +565,11 @@ impl Renderer { icon_width, 0.0, ); - r.draw_text_ext( + self.draw_text( + r, &format!("x{damage:02}"), text_x, damage_y + padding, - font_size, Color::WHITE, ); } @@ -583,7 +591,17 @@ impl Renderer { R: RaylibDraw, { let fps_str = format!("{}", self.fps); - r.draw_text_ext(&fps_str, 10, 10, 30, Color::YELLOW); + r.draw_text(&fps_str, 10, 10, 30, Color::YELLOW); + } + + /// Draw text helper function + fn draw_text<R>(&self, r: &mut R, text: &str, x: u16, y: u16, color: Color) + where + R: RaylibDraw, + { + let font = &self.textures.pixantiqua; + let font_size = self.get_ui_font_size(); + r.draw_text_ex(font, text, vec2! {x, y}, font_size.into(), 0.0, color); } } @@ -690,17 +708,6 @@ where self.draw_texture_pro(tex, source_rec, dest_rec, origin, 0.0, Color::WHITE); } - fn draw_text_ext( - &mut self, - text: &str, - x: impl Into<i32>, - y: impl Into<i32>, - font_size: impl Into<i32>, - color: Color, - ) { - self.draw_text(text, x.into(), y.into(), font_size.into(), color); - } - fn draw_rectangle_ext( &mut self, x: impl Into<i32>, |