forked from karx/catbox
Rustfmt
This commit is contained in:
parent
70cd3e2102
commit
0e2bc757bc
29
src/lib.rs
29
src/lib.rs
|
@ -56,8 +56,9 @@ use sdl2::{
|
|||
render::{Canvas, TextureCreator, TextureValueError},
|
||||
rwops::RWops,
|
||||
surface::Surface,
|
||||
ttf::{FontError, Sdl2TtfContext},
|
||||
video::{Window, WindowBuildError, WindowContext},
|
||||
EventPump, IntegerOrSdlError, ttf::{Sdl2TtfContext, FontError},
|
||||
EventPump, IntegerOrSdlError,
|
||||
};
|
||||
|
||||
#[doc(no_inline)]
|
||||
|
@ -245,7 +246,7 @@ impl Sprite {
|
|||
pub struct Context {
|
||||
canvas: Canvas<Window>,
|
||||
texture_creator: TextureCreator<WindowContext>,
|
||||
ttf_subsystem: Sdl2TtfContext
|
||||
ttf_subsystem: Sdl2TtfContext,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
|
@ -254,7 +255,7 @@ impl Context {
|
|||
Self {
|
||||
canvas,
|
||||
texture_creator: creator,
|
||||
ttf_subsystem
|
||||
ttf_subsystem,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -282,16 +283,14 @@ impl Context {
|
|||
/// Set the mode for drawing text.
|
||||
pub enum TextMode {
|
||||
/// Render the text transparently.
|
||||
Transparent {
|
||||
colour: (u8, u8, u8)
|
||||
},
|
||||
Transparent { colour: (u8, u8, u8) },
|
||||
/// Render the text with a foreground and a background colour.
|
||||
///
|
||||
/// This creates a box around the text.
|
||||
Shaded {
|
||||
foreground: (u8, u8, u8),
|
||||
background: (u8, u8, u8)
|
||||
}
|
||||
background: (u8, u8, u8),
|
||||
},
|
||||
}
|
||||
|
||||
/// Draw text to the screen.
|
||||
|
@ -312,13 +311,23 @@ pub enum TextMode {
|
|||
/// };
|
||||
/// draw_text(ctx, "text to draw", "arial.ttf", 72, (300, 300), mode);
|
||||
/// # });
|
||||
pub fn draw_text<S: AsRef<str>>(ctx: &mut Context, text: S, font: &str, size: u16, pos: (i32, i32), mode: TextMode) -> Result<()> {
|
||||
pub fn draw_text<S: AsRef<str>>(
|
||||
ctx: &mut Context,
|
||||
text: S,
|
||||
font: &str,
|
||||
size: u16,
|
||||
pos: (i32, i32),
|
||||
mode: TextMode,
|
||||
) -> Result<()> {
|
||||
let font = ctx.ttf_subsystem.load_font(font, size)?;
|
||||
let renderer = font.render(text.as_ref());
|
||||
|
||||
let surf = match mode {
|
||||
TextMode::Transparent { colour: (r, g, b) } => renderer.solid(Color::RGB(r, g, b)),
|
||||
TextMode::Shaded { foreground: (fr, fg, fb), background: (br, bg, bb) } => renderer.shaded(Color::RGB(fr, fg, fb), Color::RGB(br, bg, bb)),
|
||||
TextMode::Shaded {
|
||||
foreground: (fr, fg, fb),
|
||||
background: (br, bg, bb),
|
||||
} => renderer.shaded(Color::RGB(fr, fg, fb), Color::RGB(br, bg, bb)),
|
||||
}?;
|
||||
|
||||
drop(font);
|
||||
|
|
15
src/main.rs
15
src/main.rs
|
@ -1,4 +1,4 @@
|
|||
use cat_box::{Event, Game, Keycode, Sprite, draw_text};
|
||||
use cat_box::{draw_text, Event, Game, Keycode, Sprite};
|
||||
|
||||
fn main() {
|
||||
let game = Game::new("catbox demo", 1000, 800);
|
||||
|
@ -10,7 +10,18 @@ fn main() {
|
|||
i = (i + 1.0) % 360.0;
|
||||
ctx.set_background_colour(i as u8, 64, 255);
|
||||
|
||||
draw_text(ctx, format!("i is {}", i), "MesloLGS NF Regular.ttf", 72, (300, 300), cat_box::TextMode::Shaded {foreground: (255, 255, 255), background: (0, 0, 0)}).unwrap();
|
||||
draw_text(
|
||||
ctx,
|
||||
format!("i is {}", i),
|
||||
"MesloLGS NF Regular.ttf",
|
||||
72,
|
||||
(300, 300),
|
||||
cat_box::TextMode::Shaded {
|
||||
foreground: (255, 255, 255),
|
||||
background: (0, 0, 0),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let (start_x, start_y) = s.position();
|
||||
let m = sdl2::mouse::MouseState::new(event_pump.as_ref());
|
||||
|
|
Loading…
Reference in a new issue