From 13a85df430f32750a1d31372f648e58b3f0c7c9f Mon Sep 17 00:00:00 2001 From: memdmp Date: Sun, 12 Jan 2025 02:57:36 +0100 Subject: feat: specify amount to advance x position --- src/font.rs | 8 ++++++-- src/main.rs | 51 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/font.rs b/src/font.rs index fc5217e..a547928 100644 --- a/src/font.rs +++ b/src/font.rs @@ -17,12 +17,15 @@ pub struct RenderableCharacter { pub data: &'static [u8], /** The offset to draw the character at */ pub offset: Point, + /** The amount to advance the x position of the cursor when drawing */ + pub advance_width: f32, } /** A trait describing a generated font. We use traits implemented by each font because it's somehow optimized better in preliminary testing(?) */ pub trait BakedFont { + fn font_scale_y() -> f32; fn has_char(&self, character: char) -> bool; fn get_char_bytes(&self, character: char) -> &'static [u8]; fn get_char(&self, character: char) -> RenderableCharacter { @@ -30,7 +33,8 @@ pub trait BakedFont { let width = u16::from_le_bytes(bytes[0..2].try_into().unwrap()); let offset_x = i32::from_le_bytes(bytes[2..6].try_into().unwrap()); let offset_y = i32::from_le_bytes(bytes[6..10].try_into().unwrap()); - let data = &bytes[10..]; + let advance_width = f32::from_le_bytes(bytes[10..14].try_into().unwrap()); + let data = &bytes[14..]; let height = if data.len() == 0 { 0 } else { @@ -41,6 +45,7 @@ pub trait BakedFont { height, offset: Point::new(offset_x, offset_y), data, + advance_width, } } } @@ -93,7 +98,6 @@ impl RenderableCharacter { surface.as_texture(texture_creator) } pub fn to_rect(&self, x: i32, y: i32) -> Rect { - println!("{:#?}",self.offset); Rect::new( x + self.offset.x, y + self.offset.y, diff --git a/src/main.rs b/src/main.rs index ebe73d7..c212386 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,11 +4,10 @@ mod font; pub mod generated; use font::BakedFont; -use generated::fonts::FONT_CHERRY_BOMB_ONE; +use generated::fonts::{FONT_CHERRY_BOMB_ONE, FONT_GALMURI}; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; -use sdl2::rect::Rect; use std::time::Duration; pub fn main() { @@ -26,27 +25,41 @@ pub fn main() { let texture_creator = canvas.texture_creator(); // let uwu_font = font::UwUFont::new(&texture_creator); canvas.clear(); - let mut offset: u16 = 0; - for c in "UwU Space".chars() { - let char = FONT_CHERRY_BOMB_ONE.get_char(c); - canvas - .copy( - &char - .to_texture(&texture_creator, Color::RGB(255, 10, 100)) - .unwrap(), - None, - char.to_rect(offset as i32, 0), - ) - .unwrap(); - offset += char.width; - } - canvas.draw_rect(Rect::new(100, 100, 200, 200)).unwrap(); canvas.present(); let mut event_pump = sdl_context.event_pump().unwrap(); let mut i = 0; 'running: loop { i = (i + 1) % 255; - // canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); + + let mut offset: f32 = 0.0; + for c in "UwU Space".chars() { + let char = FONT_CHERRY_BOMB_ONE.get_char(c); + canvas + .copy( + &char + .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i)) + .unwrap(), + None, + char.to_rect(offset as i32, 0), + ) + .unwrap(); + offset += char.advance_width; + } + offset=0.0; + for c in "All hail Blahaj".chars() { + let char = FONT_GALMURI.get_char(c); + canvas + .copy( + &char + .to_texture(&texture_creator, Color::RGB(i, 64, 255 - i)) + .unwrap(), + None, + char.to_rect(offset as i32, 40), + ) + .unwrap(); + offset += char.advance_width; + } + // canvas.set_draw_color(); // canvas.clear(); for event in event_pump.poll_iter() { match event { @@ -59,7 +72,7 @@ pub fn main() { } } // The rest of the game loop goes here... - // canvas.present(); + canvas.present(); ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); } } -- cgit v1.2.3