diff --git a/src/gui.rs b/src/gui.rs index 221a9e4..6c2bb1e 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -16,11 +16,6 @@ pub const LIFONT_PNG: &'static [u8] = include_bytes!("lifont.png"); pub const MOUSE_PNG: &'static [u8] = include_bytes!("zmouse.png"); pub const MOUSE_SHADOW_PNG: &'static [u8] = include_bytes!("zmouse-shadow.png"); -const FONT_ROWS: usize = 16; -const FONT_COLS: usize = 16; -const FONT_MAX_W: usize = 8; -const FONT_MAX_H: usize = 8; - pub struct SurfaceFont<'a> { char_rects: HashMap, pub surf: Surface<'a>, @@ -70,6 +65,11 @@ impl<'a> SurfaceFont<'a> { }) } pub fn load_zfont() -> Result> { + const FONT_ROWS: usize = 16; + const FONT_COLS: usize = 16; + const FONT_MAX_W: usize = 8; + const FONT_MAX_H: usize = 8; + let mut zfont_txt = String::new(); // max 8x8 fonts, 16*16=256 characters @@ -117,7 +117,7 @@ impl<'a> SurfaceFont<'a> { let x0 = ((index % FONT_COLS) * FONT_MAX_W) as i32; let y0 = ((index / FONT_COLS) * FONT_MAX_H) as i32; let mut y = y0; - let mut max_x = 0; + let mut points = Vec::new(); for row in &lines[line_num + 1..=(line_num + FONT_MAX_H).min(lines.len() - 1)] { if let Some(';') = row.chars().next() { break; @@ -130,10 +130,9 @@ impl<'a> SurfaceFont<'a> { row_bits <<= 16 - FONT_MAX_W; while row_bits != 0 { if (row_bits & (1 << 15)) != 0 { - canvas.draw_point(Point::new(x, y))?; - if x > max_x { - max_x = x; - } + let point = Point::new(x, y); + points.push(point); + canvas.draw_point(point)?; } row_bits <<= 1; x += 1; @@ -143,16 +142,18 @@ impl<'a> SurfaceFont<'a> { } y += 1; } - let mut width = 1 + (max_x - x0) as u32; let height = (y - y0) as u32; if height > max_height { max_height = height; } - if width == 1 { - width = height; + if points.len() == 0 { name = " ".to_string(); } - char_rects.insert(name, Rect::new(x0, y0, width, height)); + let mut bounding = Rect::from_enclose_points(&points, None) + .unwrap_or(Rect::new(0, 0, height, height)); + bounding.set_height(height); + bounding.set_y(y0); + char_rects.insert(name, bounding); line_num += 1 + height as usize; } Ok(SurfaceFont { @@ -168,11 +169,11 @@ impl<'a> SurfaceFont<'a> { let mut cursor_x = dest_rect.x(); let mut cursor_y = dest_rect.y(); for i in 0..text.len() { - let c = &text[i..=i]; + let c = (&text[i..=i]).to_uppercase(); if c == "\n" { cursor_x = dest_rect.x(); cursor_y += self.line_height as i32 + 1; - } else if let Some(src_char_rect) = self.char_rects.get(c) { + } else if let Some(src_char_rect) = self.char_rects.get(&c) { let mut dst_char_rect = src_char_rect.clone(); dst_char_rect.set_x(cursor_x); dst_char_rect.set_y(cursor_y); diff --git a/src/main.rs b/src/main.rs index 18f3733..36030d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -104,7 +104,7 @@ impl Zretro { emu.init().unwrap(); emu.load_game(&opt.rom).unwrap(); // TODO: optional via CLI positional arg, menu-browsable - let font = SurfaceFont::load_lifont()?; + let font = SurfaceFont::load_zfont()?; let mouse_surf = surface_asset(MOUSE_PNG)?; let mouse_shadow_surf = surface_asset(MOUSE_SHADOW_PNG)?; @@ -237,7 +237,7 @@ impl Zretro { self.font.blit_text( ui_bg_mut, - "Hello world\nfrom lifont", + "Hello world\nfrom zfon't!! ^_^;", Rect::new(144, 32, 100, 20) )?; let mut ui_tx = tc.create_texture_from_surface(ui_bg_mut)?;