aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/app.rs35
-rw-r--r--src/main.rs3
-rw-r--r--src/utils.rs30
3 files changed, 37 insertions, 31 deletions
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 @@
1use crate::{ 1use 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
8use std::convert::{From, TryFrom}; 10use std::convert::From;
9 11
10use sdl2::{ 12use sdl2::{
11 event::Event, 13 event::Event,
@@ -19,12 +21,6 @@ use sdl2::{
19 Sdl, 21 Sdl,
20}; 22};
21 23
22macro_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
28pub struct AppState<'ctx> { 24pub 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
524fn 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;
3mod consts; 3mod consts;
4mod symmetry; 4mod symmetry;
5mod undo; 5mod undo;
6mod utils;
6 7
7use app::AppState; 8use app::AppState;
8 9
9pub fn main() { 10pub 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 @@
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}