diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-07-21 18:45:11 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-07-21 22:17:54 +0200 | 
| commit | 64e00aad013da92d29851d3e8109a006b4dff8c5 (patch) | |
| tree | 081e9a2829f3c247d48bf6468030f35c85b2db4f /pixel-client/src/render/font.rs | |
| parent | b1eba76afaf7a506ff912634da6220db15d0023e (diff) | |
| download | hurrycurry-64e00aad013da92d29851d3e8109a006b4dff8c5.tar hurrycurry-64e00aad013da92d29851d3e8109a006b4dff8c5.tar.bz2 hurrycurry-64e00aad013da92d29851d3e8109a006b4dff8c5.tar.zst | |
pc: draw text
Diffstat (limited to 'pixel-client/src/render/font.rs')
| -rw-r--r-- | pixel-client/src/render/font.rs | 43 | 
1 files changed, 43 insertions, 0 deletions
| diff --git a/pixel-client/src/render/font.rs b/pixel-client/src/render/font.rs new file mode 100644 index 00000000..ff4abade --- /dev/null +++ b/pixel-client/src/render/font.rs @@ -0,0 +1,43 @@ +use super::{sprite::SpriteDraw, AtlasLayout, Renderer}; +use hurrycurry_protocol::glam::Vec2; +use sdl2::rect::Rect; + +pub struct FontTextures { +    pub glyphs: [Rect; 128], +} + +impl FontTextures { +    pub fn init(layout: &AtlasLayout) -> Self { +        FontTextures { +            glyphs: (0..128) +                .into_iter() +                .map(|n| { +                    layout +                        .get(&format!("letter_{n}+a")) +                        .copied() +                        .unwrap_or(Rect::new(0, 0, 0, 0)) +                }) +                .collect::<Vec<_>>() +                .try_into() +                .unwrap(), +        } +    } +} + +impl<'a> Renderer<'a> { +    pub fn draw_text(&mut self, position: Vec2, text: &str) { +        let mut cursor = position; +        for c in text.chars() { +            if (c as u32) < 128 { +                let r = self.font_textures.glyphs[c as usize]; +                self.draw_ui(SpriteDraw::overlay( +                    r, +                    cursor, +                    Vec2::new(r.width() as f32, r.height() as f32), +                    None, +                )); +                cursor.x += r.width() as f32; +            } +        } +    } +} | 
