diff options
author | Akshay <[email protected]> | 2021-04-28 07:15:04 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-04-28 07:15:04 +0100 |
commit | 7bc365c2d1cf9252f52fdcf50ded3890cfeb7ddd (patch) | |
tree | f358e7d1568d394598dd8bae1a7a7c10c72143a4 /src/app.rs | |
parent | 092e187304c596f35b8ef9d7ca71850418d8a05d (diff) |
initial attempt at moving into render threadrender-thread
Diffstat (limited to 'src/app.rs')
-rw-r--r-- | src/app.rs | 39 |
1 files changed, 10 insertions, 29 deletions
@@ -11,6 +11,7 @@ use crate::{ | |||
11 | lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, | 11 | lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, |
12 | message::Message, | 12 | message::Message, |
13 | rect, | 13 | rect, |
14 | render::Signal, | ||
14 | symmetry::Symmetry, | 15 | symmetry::Symmetry, |
15 | undo::{ModifyRecord, OpKind, PaintRecord, UndoStack}, | 16 | undo::{ModifyRecord, OpKind, PaintRecord, UndoStack}, |
16 | utils::{self, draw_text, handle_error, is_copy_event, is_paste_event, load_script}, | 17 | utils::{self, draw_text, handle_error, is_copy_event, is_paste_event, load_script}, |
@@ -24,6 +25,7 @@ use std::{ | |||
24 | fs::File, | 25 | fs::File, |
25 | io::prelude::*, | 26 | io::prelude::*, |
26 | path::{Path, PathBuf}, | 27 | path::{Path, PathBuf}, |
28 | sync::mpsc::{Receiver, Sender}, | ||
27 | }; | 29 | }; |
28 | 30 | ||
29 | use obi::{CompressionType, Image}; | 31 | use obi::{CompressionType, Image}; |
@@ -45,13 +47,11 @@ pub enum Mode { | |||
45 | Command, | 47 | Command, |
46 | } | 48 | } |
47 | 49 | ||
48 | pub struct AppState<'ctx> { | 50 | pub struct AppState { |
49 | pub active_color: bool, | 51 | pub active_color: bool, |
50 | pub brush: Brush, | 52 | pub brush: Brush, |
51 | pub canvas: Canvas<Window>, | ||
52 | pub command_box: CommandBox, | 53 | pub command_box: CommandBox, |
53 | pub cache: RefCell<Option<Cache>>, | 54 | pub cache: RefCell<Option<Cache>>, |
54 | pub context: &'ctx Sdl, | ||
55 | pub current_operation: Vec<PaintRecord>, | 55 | pub current_operation: Vec<PaintRecord>, |
56 | pub dither_level: u8, | 56 | pub dither_level: u8, |
57 | pub file_name: Option<PathBuf>, | 57 | pub file_name: Option<PathBuf>, |
@@ -60,17 +60,17 @@ pub struct AppState<'ctx> { | |||
60 | pub lisp_env: EnvList, | 60 | pub lisp_env: EnvList, |
61 | pub message: Message, | 61 | pub message: Message, |
62 | pub mode: Mode, | 62 | pub mode: Mode, |
63 | pub mouse: (i32, i32), | ||
64 | pub pixmap: Pixmap<bool>, | 63 | pub pixmap: Pixmap<bool>, |
65 | pub start: Point, | 64 | pub start: Point, |
66 | pub symmetry: Symmetry, | 65 | pub symmetry: Symmetry, |
67 | pub ttf_context: &'ctx Sdl2TtfContext, | ||
68 | pub undo_stack: UndoStack<ModifyRecord>, | 66 | pub undo_stack: UndoStack<ModifyRecord>, |
69 | pub zoom: u8, | 67 | pub zoom: u8, |
68 | pub sender: Sender<Signal>, | ||
69 | pub receiver: Receiver<Signal>, | ||
70 | } | 70 | } |
71 | 71 | ||
72 | // private actions on appstate | 72 | // private actions on appstate |
73 | impl<'ctx> AppState<'ctx> { | 73 | impl AppState { |
74 | fn pan<P: Into<Point>>(&mut self, direction: P) { | 74 | fn pan<P: Into<Point>>(&mut self, direction: P) { |
75 | self.start += direction.into(); | 75 | self.start += direction.into(); |
76 | } | 76 | } |
@@ -602,34 +602,17 @@ impl<'ctx> AppState<'ctx> { | |||
602 | pub fn init( | 602 | pub fn init( |
603 | width: u32, | 603 | width: u32, |
604 | height: u32, | 604 | height: u32, |
605 | context: &'ctx Sdl, | 605 | sender: Sender<Signal>, |
606 | ttf_context: &'ctx Sdl2TtfContext, | 606 | receiver: Receiver<Signal>, |
607 | start_data: Option<Vec<bool>>, | 607 | start_data: Option<Vec<bool>>, |
608 | file_name: Option<PathBuf>, | 608 | file_name: Option<PathBuf>, |
609 | ) -> Result<Self, AppError> { | 609 | ) -> Result<Self, AppError> { |
610 | let video_subsystem = context.video().map_err(AppError::Sdl)?; | ||
611 | |||
612 | let window = video_subsystem | ||
613 | .window("Pixel editor", 500, 500) | ||
614 | .position_centered() | ||
615 | .resizable() | ||
616 | .opengl() | ||
617 | .build() | ||
618 | .map_err(|e| AppError::Sdl(e.to_string()))?; | ||
619 | |||
620 | let canvas = window | ||
621 | .into_canvas() | ||
622 | .build() | ||
623 | .map_err(|e| AppError::Sdl(e.to_string()))?; | ||
624 | |||
625 | let data = start_data.unwrap_or_else(|| vec![false; (width * height) as usize]); | 610 | let data = start_data.unwrap_or_else(|| vec![false; (width * height) as usize]); |
626 | let pixmap = Pixmap::new_with(width, height, data); | 611 | let pixmap = Pixmap::new_with(width, height, data); |
627 | let mut app = Self { | 612 | let mut app = Self { |
628 | active_color: true, | 613 | active_color: true, |
629 | brush: Brush::new(0), | 614 | brush: Brush::new(0), |
630 | canvas, | ||
631 | command_box: CommandBox::new(), | 615 | command_box: CommandBox::new(), |
632 | context, | ||
633 | cache: RefCell::new(None), | 616 | cache: RefCell::new(None), |
634 | guides: HashMap::new(), | 617 | guides: HashMap::new(), |
635 | current_operation: Vec::new(), | 618 | current_operation: Vec::new(), |
@@ -639,13 +622,13 @@ impl<'ctx> AppState<'ctx> { | |||
639 | lisp_env: vec![prelude::new_env().map_err(AppError::Lisp)?], | 622 | lisp_env: vec![prelude::new_env().map_err(AppError::Lisp)?], |
640 | message: Message::new().text(" "), | 623 | message: Message::new().text(" "), |
641 | mode: Mode::Draw, | 624 | mode: Mode::Draw, |
642 | mouse: (0, 0), | ||
643 | pixmap, | 625 | pixmap, |
644 | start: Point::new(60, 60), | 626 | start: Point::new(60, 60), |
645 | symmetry: Default::default(), | 627 | symmetry: Default::default(), |
646 | ttf_context, | ||
647 | undo_stack: UndoStack::new(), | 628 | undo_stack: UndoStack::new(), |
648 | zoom: 5, | 629 | zoom: 5, |
630 | sender, | ||
631 | receiver, | ||
649 | }; | 632 | }; |
650 | load_script(STDLIB_PATH, &mut app).map_err(AppError::Lisp)?; | 633 | load_script(STDLIB_PATH, &mut app).map_err(AppError::Lisp)?; |
651 | std::fs::create_dir_all(RC_PATH).map_err(AppError::File)?; | 634 | std::fs::create_dir_all(RC_PATH).map_err(AppError::File)?; |
@@ -676,8 +659,6 @@ impl<'ctx> AppState<'ctx> { | |||
676 | 659 | ||
677 | let mut event_pump = self.context.event_pump().unwrap(); | 660 | let mut event_pump = self.context.event_pump().unwrap(); |
678 | 'running: loop { | 661 | 'running: loop { |
679 | let mouse = event_pump.mouse_state(); | ||
680 | self.mouse = (mouse.x(), mouse.y()); | ||
681 | for event in event_pump.poll_iter() { | 662 | for event in event_pump.poll_iter() { |
682 | if let Event::KeyDown { | 663 | if let Event::KeyDown { |
683 | keycode: Some(Keycode::Num9), | 664 | keycode: Some(Keycode::Num9), |