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/app.rs | 35 +++++------------------------------ src/main.rs | 3 ++- src/utils.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 31 deletions(-) create mode 100644 src/utils.rs diff --git a/src/app.rs b/src/app.rs index 03389fb..059635a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,11 +1,13 @@ use crate::{ bitmap::{MapPoint, Pixmap}, - consts::{colors::*, FONT_PATH}, + consts::colors::*, + rect, symmetry::Symmetry, undo::{ModifyRecord, OpKind, Operation, UndoStack}, + utils::draw_text, }; -use std::convert::{From, TryFrom}; +use std::convert::From; use sdl2::{ event::Event, @@ -19,12 +21,6 @@ use sdl2::{ Sdl, }; -macro_rules! quick_rect( - ($x:expr, $y:expr, $w:expr, $h:expr) => ( - Rect::new($x as i32, $y as i32, $w as u32, $h as u32) - ) -); - pub struct AppState<'ctx> { active_color: bool, brush_size: u8, @@ -260,7 +256,7 @@ impl<'ctx> AppState<'ctx> { let status_width = winsize_x; self.canvas.set_draw_color(WHITE); self.canvas - .fill_rect(quick_rect!( + .fill_rect(rect!( 0, winsize_y - status_height, status_width, @@ -520,24 +516,3 @@ impl<'ctx> AppState<'ctx> { } } } - -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 = quick_rect!(x, y, width, height); - canvas.copy(&texture, None, area).unwrap(); -} 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; mod consts; mod symmetry; mod undo; +mod utils; use app::AppState; pub fn main() { let sdl_context = sdl2::init().unwrap(); let ttf_context = sdl2::ttf::init().unwrap(); - AppState::init(100, 100, &sdl_context, &ttf_context).run(); + AppState::init(160, 160, &sdl_context, &ttf_context).run(); } 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