fix vwf in zfont

This commit is contained in:
lifning 2021-10-21 03:40:13 -07:00
parent b5cdf18eea
commit 9db8822df9
2 changed files with 19 additions and 18 deletions

View File

@ -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_PNG: &'static [u8] = include_bytes!("zmouse.png");
pub const MOUSE_SHADOW_PNG: &'static [u8] = include_bytes!("zmouse-shadow.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> { pub struct SurfaceFont<'a> {
char_rects: HashMap<String, Rect>, char_rects: HashMap<String, Rect>,
pub surf: Surface<'a>, pub surf: Surface<'a>,
@ -70,6 +65,11 @@ impl<'a> SurfaceFont<'a> {
}) })
} }
pub fn load_zfont() -> Result<SurfaceFont<'a>> { pub fn load_zfont() -> Result<SurfaceFont<'a>> {
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(); let mut zfont_txt = String::new();
// max 8x8 fonts, 16*16=256 characters // 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 x0 = ((index % FONT_COLS) * FONT_MAX_W) as i32;
let y0 = ((index / FONT_COLS) * FONT_MAX_H) as i32; let y0 = ((index / FONT_COLS) * FONT_MAX_H) as i32;
let mut y = y0; 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)] { for row in &lines[line_num + 1..=(line_num + FONT_MAX_H).min(lines.len() - 1)] {
if let Some(';') = row.chars().next() { if let Some(';') = row.chars().next() {
break; break;
@ -130,10 +130,9 @@ impl<'a> SurfaceFont<'a> {
row_bits <<= 16 - FONT_MAX_W; row_bits <<= 16 - FONT_MAX_W;
while row_bits != 0 { while row_bits != 0 {
if (row_bits & (1 << 15)) != 0 { if (row_bits & (1 << 15)) != 0 {
canvas.draw_point(Point::new(x, y))?; let point = Point::new(x, y);
if x > max_x { points.push(point);
max_x = x; canvas.draw_point(point)?;
}
} }
row_bits <<= 1; row_bits <<= 1;
x += 1; x += 1;
@ -143,16 +142,18 @@ impl<'a> SurfaceFont<'a> {
} }
y += 1; y += 1;
} }
let mut width = 1 + (max_x - x0) as u32;
let height = (y - y0) as u32; let height = (y - y0) as u32;
if height > max_height { if height > max_height {
max_height = height; max_height = height;
} }
if width == 1 { if points.len() == 0 {
width = height;
name = " ".to_string(); 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; line_num += 1 + height as usize;
} }
Ok(SurfaceFont { Ok(SurfaceFont {
@ -168,11 +169,11 @@ impl<'a> SurfaceFont<'a> {
let mut cursor_x = dest_rect.x(); let mut cursor_x = dest_rect.x();
let mut cursor_y = dest_rect.y(); let mut cursor_y = dest_rect.y();
for i in 0..text.len() { for i in 0..text.len() {
let c = &text[i..=i]; let c = (&text[i..=i]).to_uppercase();
if c == "\n" { if c == "\n" {
cursor_x = dest_rect.x(); cursor_x = dest_rect.x();
cursor_y += self.line_height as i32 + 1; 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(); let mut dst_char_rect = src_char_rect.clone();
dst_char_rect.set_x(cursor_x); dst_char_rect.set_x(cursor_x);
dst_char_rect.set_y(cursor_y); dst_char_rect.set_y(cursor_y);

View File

@ -104,7 +104,7 @@ impl Zretro {
emu.init().unwrap(); emu.init().unwrap();
emu.load_game(&opt.rom).unwrap(); // TODO: optional via CLI positional arg, menu-browsable 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_surf = surface_asset(MOUSE_PNG)?;
let mouse_shadow_surf = surface_asset(MOUSE_SHADOW_PNG)?; let mouse_shadow_surf = surface_asset(MOUSE_SHADOW_PNG)?;
@ -237,7 +237,7 @@ impl Zretro {
self.font.blit_text( self.font.blit_text(
ui_bg_mut, ui_bg_mut,
"Hello world\nfrom lifont", "Hello world\nfrom zfon't!! ^_^;",
Rect::new(144, 32, 100, 20) Rect::new(144, 32, 100, 20)
)?; )?;
let mut ui_tx = tc.create_texture_from_surface(ui_bg_mut)?; let mut ui_tx = tc.create_texture_from_surface(ui_bg_mut)?;