From 7bc365c2d1cf9252f52fdcf50ded3890cfeb7ddd Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 28 Apr 2021 11:45:04 +0530 Subject: initial attempt at moving into render thread --- src/app.rs | 39 ++++++++++----------------------------- src/lisp/eval.rs | 9 +++------ src/main.rs | 32 ++++++++++++++++++++++++-------- src/render.rs | 19 +++++++++++++++++++ 4 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 src/render.rs (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 23458f4..99c0bb5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,6 +11,7 @@ use crate::{ lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, message::Message, rect, + render::Signal, symmetry::Symmetry, undo::{ModifyRecord, OpKind, PaintRecord, UndoStack}, utils::{self, draw_text, handle_error, is_copy_event, is_paste_event, load_script}, @@ -24,6 +25,7 @@ use std::{ fs::File, io::prelude::*, path::{Path, PathBuf}, + sync::mpsc::{Receiver, Sender}, }; use obi::{CompressionType, Image}; @@ -45,13 +47,11 @@ pub enum Mode { Command, } -pub struct AppState<'ctx> { +pub struct AppState { pub active_color: bool, pub brush: Brush, - pub canvas: Canvas, pub command_box: CommandBox, pub cache: RefCell>, - pub context: &'ctx Sdl, pub current_operation: Vec, pub dither_level: u8, pub file_name: Option, @@ -60,17 +60,17 @@ pub struct AppState<'ctx> { pub lisp_env: EnvList, pub message: Message, pub mode: Mode, - pub mouse: (i32, i32), pub pixmap: Pixmap, pub start: Point, pub symmetry: Symmetry, - pub ttf_context: &'ctx Sdl2TtfContext, pub undo_stack: UndoStack, pub zoom: u8, + pub sender: Sender, + pub receiver: Receiver, } // private actions on appstate -impl<'ctx> AppState<'ctx> { +impl AppState { fn pan>(&mut self, direction: P) { self.start += direction.into(); } @@ -602,34 +602,17 @@ impl<'ctx> AppState<'ctx> { pub fn init( width: u32, height: u32, - context: &'ctx Sdl, - ttf_context: &'ctx Sdl2TtfContext, + sender: Sender, + receiver: Receiver, start_data: Option>, file_name: Option, ) -> Result { - let video_subsystem = context.video().map_err(AppError::Sdl)?; - - let window = video_subsystem - .window("Pixel editor", 500, 500) - .position_centered() - .resizable() - .opengl() - .build() - .map_err(|e| AppError::Sdl(e.to_string()))?; - - let canvas = window - .into_canvas() - .build() - .map_err(|e| AppError::Sdl(e.to_string()))?; - let data = start_data.unwrap_or_else(|| vec![false; (width * height) as usize]); let pixmap = Pixmap::new_with(width, height, data); let mut app = Self { active_color: true, brush: Brush::new(0), - canvas, command_box: CommandBox::new(), - context, cache: RefCell::new(None), guides: HashMap::new(), current_operation: Vec::new(), @@ -639,13 +622,13 @@ impl<'ctx> AppState<'ctx> { lisp_env: vec![prelude::new_env().map_err(AppError::Lisp)?], message: Message::new().text(" "), mode: Mode::Draw, - mouse: (0, 0), pixmap, start: Point::new(60, 60), symmetry: Default::default(), - ttf_context, undo_stack: UndoStack::new(), zoom: 5, + sender, + receiver, }; load_script(STDLIB_PATH, &mut app).map_err(AppError::Lisp)?; std::fs::create_dir_all(RC_PATH).map_err(AppError::File)?; @@ -676,8 +659,6 @@ impl<'ctx> AppState<'ctx> { let mut event_pump = self.context.event_pump().unwrap(); 'running: loop { - let mouse = event_pump.mouse_state(); - self.mouse = (mouse.x(), mouse.y()); for event in event_pump.poll_iter() { if let Event::KeyDown { keycode: Some(Keycode::Num9), diff --git a/src/lisp/eval.rs b/src/lisp/eval.rs index 75cb5c9..759cca0 100644 --- a/src/lisp/eval.rs +++ b/src/lisp/eval.rs @@ -14,15 +14,12 @@ use log::{error, info}; pub type Context = Vec; -pub struct Evaluator<'ctx, 'global> { - pub app: &'global mut AppState<'ctx>, +pub struct Evaluator<'global> { + pub app: &'global mut AppState, pub context: Context, } -impl<'ctx, 'global> Evaluator<'ctx, 'global> -where - 'ctx: 'global, -{ +impl<'global> Evaluator<'global> { pub fn eval(&mut self, expr: &LispExpr) -> Result { match expr { LispExpr::Unit => Ok(expr.clone()), diff --git a/src/main.rs b/src/main.rs index 225c37c..04b0f2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ mod grid; mod guide; mod lisp; mod message; +mod render; mod symmetry; mod undo; mod utils; @@ -27,12 +28,19 @@ use { app::AppState, cli::Config, error::{AppError, SdlTTFError}, + render::Signal, }; +use std::{sync::mpsc, thread}; + use log::{error, info}; +use sdl2::event::Event; pub fn error_sink() -> Result<(), AppError> { - let init = || { + let (render_tx, app_rx) = mpsc::channel(); + let (app_tx, render_rx) = mpsc::channel(); + + let render_handle = thread::spawn(move || -> Result<(), AppError> { let sdl_context = sdl2::init().map_err(AppError::Sdl)?; info!("Initialized SDL context"); @@ -40,8 +48,18 @@ pub fn error_sink() -> Result<(), AppError> { .map_err(SdlTTFError::Init) .map_err(AppError::SdlTTF)?; info!("Initialized SDL_ttf context"); - Ok((sdl_context, ttf_context)) - }; + while let Ok(signal) = render_rx.recv() { + match signal { + Signal::Quit => { + let ev = sdl_context.event().unwrap(); + ev.push_event(Event::Quit { timestamp: 0u32 }) + .expect("unable to quit ohno"); + } + _ => {} + } + } + Ok(()) + }); match cli::parse_args().map_err(AppError::Cli)? { Config::Help => { @@ -52,18 +70,16 @@ pub fn error_sink() -> Result<(), AppError> { file_name, dimensions: (width, height), } => { - let (sdl_context, ttf_context) = init()?; - AppState::init(width, height, &sdl_context, &ttf_context, None, file_name)?.run(); + AppState::init(width, height, app_tx, app_rx, None, file_name)?.run(); } Config::ExistingProject { file_name } => { - let (sdl_context, ttf_context) = init()?; let image = utils::load_file(&file_name).map_err(AppError::File)?; AppState::init( image.width(), image.height(), - &sdl_context, - &ttf_context, + app_tx, + app_rx, Some(image.data), Some(file_name), )? diff --git a/src/render.rs b/src/render.rs new file mode 100644 index 0000000..f642a04 --- /dev/null +++ b/src/render.rs @@ -0,0 +1,19 @@ +use sdl2::pixels::Color; +use sdl2::rect::{Point, Rect}; + +pub enum Signal { + Redraw, + Request(RequestData), + DrawStatusline, + DrawCommandBox, + DrawBrush, + DrawLineToGrid, + DrawSymmetry, + DrawApp, + Quit, +} + +pub enum RequestData { + WinSize, + MouseLoc, +} -- cgit v1.2.3