diff options
-rw-r--r-- | build.rs | 13 | ||||
-rw-r--r-- | src/font.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 51 |
3 files changed, 47 insertions, 25 deletions
@@ -60,7 +60,7 @@ impl FontMetadata { } define_item( &mut image, - 10 + ((width as usize) * (height as usize)), + 14 + ((width as usize) * (height as usize)), 0x00, ); @@ -72,11 +72,12 @@ impl FontMetadata { // TODO: do we *really* need i32 values here? wouldn't i16 be sufficient? image[2..6].copy_from_slice(&pixel_bounding_box.min.x.to_le_bytes()); image[6..10].copy_from_slice(&(pixel_bounding_box.min.y + (scale.y / 2.0) as i32).to_le_bytes()); + image[10..14].copy_from_slice(&(glyph.h_metrics().advance_width).to_le_bytes()); positioned_glyph.draw(|gx: u32, gy: u32, v| { let bit = (v * 255.0) as u8; if gx < width { - define_item(&mut image, (10 + (gy * width) + gx) as usize, bit); + define_item(&mut image, (14 + (gy * width) + gx) as usize, bit); } }); image @@ -145,8 +146,12 @@ fn generate_struct(font: &FontMetadata) -> io::Result<String> { let mut contents = format!( "pub struct {name}Struct {{}} impl BakedFont for {name}Struct {{ + fn font_scale_y() -> f32 {{ + {} + }} fn has_char(&self, c: char) -> bool {{ - match c as u8 {{" + match c as u8 {{", + format!("{}{}",font.scale.y.to_string(),if font.scale.y % 1.0 == 0.0 {".0"} else {""}) ); for char in font.unique_chars() { contents = format!( @@ -197,7 +202,7 @@ use crate::font::BakedFont; name: "Galmuri", font: { Font::try_from_vec(fs::read("assets/fonts/Galmuri11.ttf")?).unwrap() }, charset: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz2053: ", - scale: Scale::uniform(20.0), + scale: Scale::uniform(32.0), }, FontMetadata { name: "CherryBombOne", 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)); } } |