From 2d4f2b5a8c0ba1dbec8e295a1b5605dd9028d071 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 15 Mar 2021 21:06:20 +0530 Subject: factor out common utils into module --- src/utils.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/utils.rs (limited to 'src/utils.rs') diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..e36a8cc --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,30 @@ +use crate::consts::FONT_PATH; +use sdl2::{pixels::Color, render::Canvas, ttf::Sdl2TtfContext, video::Window}; + +#[macro_export] +macro_rules! rect( + ($x:expr, $y:expr, $w:expr, $h:expr) => ( + ::sdl2::rect::Rect::new($x as i32, $y as i32, $w as u32, $h as u32) + ) +); + +pub fn draw_text>( + canvas: &mut Canvas, + ttf_context: &Sdl2TtfContext, + text: S, + color: Color, + (x, y): (u32, u32), +) { + let text = text.as_ref(); + let texture_creator = canvas.texture_creator(); + let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); + font.set_style(sdl2::ttf::FontStyle::NORMAL); + font.set_hinting(sdl2::ttf::Hinting::Mono); + let surface = font.render(text.as_ref()).blended(color).unwrap(); + let texture = texture_creator + .create_texture_from_surface(&surface) + .unwrap(); + let (width, height) = font.size_of_latin1(text.as_bytes()).unwrap(); + let area = rect!(x, y, width, height); + canvas.copy(&texture, None, area).unwrap(); +} -- cgit v1.2.3