From bc03961a49f121adeb9dfd756006956729a76f0f Mon Sep 17 00:00:00 2001 From: gered Date: Fri, 27 May 2022 17:52:23 -0400 Subject: [PATCH] bitmap font/char rendering now requires a FontRenderOpts this is more flexible for future font types, which may not require only a simple u8 color value (e.g. bitmap fonts) --- examples/balls/src/main.rs | 2 +- examples/balls_v2/src/states.rs | 2 +- examples/slimed/src/states.rs | 13 +++++++------ examples/template_minimal/src/main.rs | 2 +- libretrogd/src/graphics/bitmap/primitives.rs | 8 ++++---- libretrogd/src/graphics/font.rs | 2 +- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/balls/src/main.rs b/examples/balls/src/main.rs index 670a83c..74fed48 100644 --- a/examples/balls/src/main.rs +++ b/examples/balls/src/main.rs @@ -109,7 +109,7 @@ fn main() -> Result<()> { system .video - .print_string("hello, world!", 10, 10, 15, &font); + .print_string("hello, world!", 10, 10, FontRenderOpts::Color(15), &font); for i in 0..NUM_BALLS { system.video.blit( diff --git a/examples/balls_v2/src/states.rs b/examples/balls_v2/src/states.rs index cd19348..34106bc 100644 --- a/examples/balls_v2/src/states.rs +++ b/examples/balls_v2/src/states.rs @@ -95,7 +95,7 @@ impl GameState for SimulationState { fn render(&mut self, _state: State, context: &mut Game) { context.context.system.video.clear(2); context.component_systems.render(&mut context.context); - context.context.system.video.print_string("hello, world!", 10, 10, 15, &context.context.font); + context.context.system.video.print_string("hello, world!", 10, 10, FontRenderOpts::Color(15), &context.context.font); } fn transition(&mut self, _state: State, _context: &mut Game) -> bool { diff --git a/examples/slimed/src/states.rs b/examples/slimed/src/states.rs index c0f9e84..1ea6cb1 100644 --- a/examples/slimed/src/states.rs +++ b/examples/slimed/src/states.rs @@ -3,6 +3,7 @@ use std::path::Path; use sdl2::keyboard::Scancode; use libretrogd::entities::*; +use libretrogd::graphics::*; use libretrogd::math::Vector2; use libretrogd::states::*; @@ -64,10 +65,10 @@ impl GameState for MainMenuState { draw_window(&mut context.core.system.video, &context.core.ui, x, y, x + width, y + height); let selection_y = y + SPACER + (self.selection as i32 * 16); - context.core.system.video.print_string(">", x + SPACER, selection_y, 15, &context.core.font); + context.core.system.video.print_string(">", x + SPACER, selection_y, FontRenderOpts::Color(15), &context.core.font); - context.core.system.video.print_string("Play", x + SPACER + SPACER, y + SPACER, 15, &context.core.font); - context.core.system.video.print_string("Quit", x + SPACER + SPACER, y + SPACER + 16, 15, &context.core.font); + context.core.system.video.print_string("Play", x + SPACER + SPACER, y + SPACER, FontRenderOpts::Color(15), &context.core.font); + context.core.system.video.print_string("Quit", x + SPACER + SPACER, y + SPACER + 16, FontRenderOpts::Color(15), &context.core.font); } fn transition(&mut self, state: State, context: &mut Game) -> bool { @@ -176,10 +177,10 @@ impl GameState for GamePlayState { draw_window(&mut context.core.system.video, &context.core.ui, x, y, x + width, y + height); let selection_y = y + SPACER + (self.selection as i32 * 16); - context.core.system.video.print_string(">", x + SPACER, selection_y, 15, &context.core.font); + context.core.system.video.print_string(">", x + SPACER, selection_y, FontRenderOpts::Color(15), &context.core.font); - context.core.system.video.print_string("Continue", x + SPACER + SPACER, y + SPACER, 15, &context.core.font); - context.core.system.video.print_string("Quit", x + SPACER + SPACER, y + SPACER + 16, 15, &context.core.font); + context.core.system.video.print_string("Continue", x + SPACER + SPACER, y + SPACER, FontRenderOpts::Color(15), &context.core.font); + context.core.system.video.print_string("Quit", x + SPACER + SPACER, y + SPACER + 16, FontRenderOpts::Color(15), &context.core.font); } } diff --git a/examples/template_minimal/src/main.rs b/examples/template_minimal/src/main.rs index 9a77462..fb7b8be 100644 --- a/examples/template_minimal/src/main.rs +++ b/examples/template_minimal/src/main.rs @@ -13,7 +13,7 @@ fn main() -> Result<()> { let font = BitmaskFont::new_vga_font()?; system.video.clear(0); - system.video.print_string("Hello, world!", 20, 20, 15, &font); + system.video.print_string("Hello, world!", 20, 20, FontRenderOpts::Color(15), &font); while is_running { system.do_events_with(|event| { diff --git a/libretrogd/src/graphics/bitmap/primitives.rs b/libretrogd/src/graphics/bitmap/primitives.rs index 18b3c66..9b6c25b 100644 --- a/libretrogd/src/graphics/bitmap/primitives.rs +++ b/libretrogd/src/graphics/bitmap/primitives.rs @@ -47,13 +47,13 @@ impl Bitmap { /// Renders a single character using the font given. #[inline] - pub fn print_char(&mut self, ch: char, x: i32, y: i32, color: u8, font: &T) { + pub fn print_char(&mut self, ch: char, x: i32, y: i32, opts: FontRenderOpts, font: &T) { font.character(ch) - .draw(self, x, y, FontRenderOpts::Color(color)); + .draw(self, x, y, opts); } /// Renders the string of text using the font given. - pub fn print_string(&mut self, text: &str, x: i32, y: i32, color: u8, font: &T) { + pub fn print_string(&mut self, text: &str, x: i32, y: i32, opts: FontRenderOpts, font: &T) { let mut current_x = x; let mut current_y = y; for ch in text.chars() { @@ -65,7 +65,7 @@ impl Bitmap { } '\r' => (), otherwise => { - self.print_char(otherwise, current_x, current_y, color, font); + self.print_char(otherwise, current_x, current_y, opts, font); current_x += font.character(otherwise).bounds().width as i32; } } diff --git a/libretrogd/src/graphics/font.rs b/libretrogd/src/graphics/font.rs index ab3786e..3e6fd41 100644 --- a/libretrogd/src/graphics/font.rs +++ b/libretrogd/src/graphics/font.rs @@ -23,7 +23,7 @@ pub enum FontError { IOError(#[from] std::io::Error), } -#[derive(Debug)] +#[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum FontRenderOpts { Color(u8), None,