diff options
author | Akshay <[email protected]> | 2021-03-15 15:36:20 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-03-15 15:36:20 +0000 |
commit | 2d4f2b5a8c0ba1dbec8e295a1b5605dd9028d071 (patch) | |
tree | 56e1d9256471287d2b63500a4b097ddcd48335e1 | |
parent | 9b0dff82215da7dfcefcea12862d18faab55bb91 (diff) |
factor out common utils into module
-rw-r--r-- | src/app.rs | 35 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/utils.rs | 30 |
3 files changed, 37 insertions, 31 deletions
@@ -1,11 +1,13 @@ | |||
1 | use crate::{ | 1 | use crate::{ |
2 | bitmap::{MapPoint, Pixmap}, | 2 | bitmap::{MapPoint, Pixmap}, |
3 | consts::{colors::*, FONT_PATH}, | 3 | consts::colors::*, |
4 | rect, | ||
4 | symmetry::Symmetry, | 5 | symmetry::Symmetry, |
5 | undo::{ModifyRecord, OpKind, Operation, UndoStack}, | 6 | undo::{ModifyRecord, OpKind, Operation, UndoStack}, |
7 | utils::draw_text, | ||
6 | }; | 8 | }; |
7 | 9 | ||
8 | use std::convert::{From, TryFrom}; | 10 | use std::convert::From; |
9 | 11 | ||
10 | use sdl2::{ | 12 | use sdl2::{ |
11 | event::Event, | 13 | event::Event, |
@@ -19,12 +21,6 @@ use sdl2::{ | |||
19 | Sdl, | 21 | Sdl, |
20 | }; | 22 | }; |
21 | 23 | ||
22 | macro_rules! quick_rect( | ||
23 | ($x:expr, $y:expr, $w:expr, $h:expr) => ( | ||
24 | Rect::new($x as i32, $y as i32, $w as u32, $h as u32) | ||
25 | ) | ||
26 | ); | ||
27 | |||
28 | pub struct AppState<'ctx> { | 24 | pub struct AppState<'ctx> { |
29 | active_color: bool, | 25 | active_color: bool, |
30 | brush_size: u8, | 26 | brush_size: u8, |
@@ -260,7 +256,7 @@ impl<'ctx> AppState<'ctx> { | |||
260 | let status_width = winsize_x; | 256 | let status_width = winsize_x; |
261 | self.canvas.set_draw_color(WHITE); | 257 | self.canvas.set_draw_color(WHITE); |
262 | self.canvas | 258 | self.canvas |
263 | .fill_rect(quick_rect!( | 259 | .fill_rect(rect!( |
264 | 0, | 260 | 0, |
265 | winsize_y - status_height, | 261 | winsize_y - status_height, |
266 | status_width, | 262 | status_width, |
@@ -520,24 +516,3 @@ impl<'ctx> AppState<'ctx> { | |||
520 | } | 516 | } |
521 | } | 517 | } |
522 | } | 518 | } |
523 | |||
524 | fn draw_text<S: AsRef<str>>( | ||
525 | canvas: &mut Canvas<Window>, | ||
526 | ttf_context: &Sdl2TtfContext, | ||
527 | text: S, | ||
528 | color: Color, | ||
529 | (x, y): (u32, u32), | ||
530 | ) { | ||
531 | let text = text.as_ref(); | ||
532 | let texture_creator = canvas.texture_creator(); | ||
533 | let mut font = ttf_context.load_font(FONT_PATH, 17).unwrap(); | ||
534 | font.set_style(sdl2::ttf::FontStyle::NORMAL); | ||
535 | font.set_hinting(sdl2::ttf::Hinting::Mono); | ||
536 | let surface = font.render(text.as_ref()).blended(color).unwrap(); | ||
537 | let texture = texture_creator | ||
538 | .create_texture_from_surface(&surface) | ||
539 | .unwrap(); | ||
540 | let (width, height) = font.size_of_latin1(text.as_bytes()).unwrap(); | ||
541 | let area = quick_rect!(x, y, width, height); | ||
542 | canvas.copy(&texture, None, area).unwrap(); | ||
543 | } | ||
diff --git a/src/main.rs b/src/main.rs index 1aaa954..c9c1bdd 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -3,11 +3,12 @@ mod bitmap; | |||
3 | mod consts; | 3 | mod consts; |
4 | mod symmetry; | 4 | mod symmetry; |
5 | mod undo; | 5 | mod undo; |
6 | mod utils; | ||
6 | 7 | ||
7 | use app::AppState; | 8 | use app::AppState; |
8 | 9 | ||
9 | pub fn main() { | 10 | pub fn main() { |
10 | let sdl_context = sdl2::init().unwrap(); | 11 | let sdl_context = sdl2::init().unwrap(); |
11 | let ttf_context = sdl2::ttf::init().unwrap(); | 12 | let ttf_context = sdl2::ttf::init().unwrap(); |
12 | AppState::init(100, 100, &sdl_context, &ttf_context).run(); | 13 | AppState::init(160, 160, &sdl_context, &ttf_context).run(); |
13 | } | 14 | } |
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 @@ | |||
1 | use crate::consts::FONT_PATH; | ||
2 | use sdl2::{pixels::Color, render::Canvas, ttf::Sdl2TtfContext, video::Window}; | ||
3 | |||
4 | #[macro_export] | ||
5 | macro_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 | |||
11 | pub 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 | } | ||