aboutsummaryrefslogtreecommitdiff
path: root/src/utils.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-03-15 15:36:20 +0000
committerAkshay <[email protected]>2021-03-15 15:36:20 +0000
commit2d4f2b5a8c0ba1dbec8e295a1b5605dd9028d071 (patch)
tree56e1d9256471287d2b63500a4b097ddcd48335e1 /src/utils.rs
parent9b0dff82215da7dfcefcea12862d18faab55bb91 (diff)
factor out common utils into module
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs30
1 files changed, 30 insertions, 0 deletions
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 @@
1use crate::consts::FONT_PATH;
2use sdl2::{pixels::Color, render::Canvas, ttf::Sdl2TtfContext, video::Window};
3
4#[macro_export]
5macro_rules! rect(
6 ($x:expr, $y:expr, $w:expr, $h:expr) => (
7 ::sdl2::rect::Rect::new($x as i32, $y as i32, $w as u32, $h as u32)
8 )
9);
10
11pub fn draw_text<S: AsRef<str>>(
12 canvas: &mut Canvas<Window>,
13 ttf_context: &Sdl2TtfContext,
14 text: S,
15 color: Color,
16 (x, y): (u32, u32),
17) {
18 let text = text.as_ref();
19 let texture_creator = canvas.texture_creator();
20 let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap();
21 font.set_style(sdl2::ttf::FontStyle::NORMAL);
22 font.set_hinting(sdl2::ttf::Hinting::Mono);
23 let surface = font.render(text.as_ref()).blended(color).unwrap();
24 let texture = texture_creator
25 .create_texture_from_surface(&surface)
26 .unwrap();
27 let (width, height) = font.size_of_latin1(text.as_bytes()).unwrap();
28 let area = rect!(x, y, width, height);
29 canvas.copy(&texture, None, area).unwrap();
30}