diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 24 insertions, 8 deletions
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; | |||
18 | mod guide; | 18 | mod guide; |
19 | mod lisp; | 19 | mod lisp; |
20 | mod message; | 20 | mod message; |
21 | mod render; | ||
21 | mod symmetry; | 22 | mod symmetry; |
22 | mod undo; | 23 | mod undo; |
23 | mod utils; | 24 | mod utils; |
@@ -27,12 +28,19 @@ use { | |||
27 | app::AppState, | 28 | app::AppState, |
28 | cli::Config, | 29 | cli::Config, |
29 | error::{AppError, SdlTTFError}, | 30 | error::{AppError, SdlTTFError}, |
31 | render::Signal, | ||
30 | }; | 32 | }; |
31 | 33 | ||
34 | use std::{sync::mpsc, thread}; | ||
35 | |||
32 | use log::{error, info}; | 36 | use log::{error, info}; |
37 | use sdl2::event::Event; | ||
33 | 38 | ||
34 | pub fn error_sink() -> Result<(), AppError> { | 39 | pub fn error_sink() -> Result<(), AppError> { |
35 | let init = || { | 40 | let (render_tx, app_rx) = mpsc::channel(); |
41 | let (app_tx, render_rx) = mpsc::channel(); | ||
42 | |||
43 | let render_handle = thread::spawn(move || -> Result<(), AppError> { | ||
36 | let sdl_context = sdl2::init().map_err(AppError::Sdl)?; | 44 | let sdl_context = sdl2::init().map_err(AppError::Sdl)?; |
37 | info!("Initialized SDL context"); | 45 | info!("Initialized SDL context"); |
38 | 46 | ||
@@ -40,8 +48,18 @@ pub fn error_sink() -> Result<(), AppError> { | |||
40 | .map_err(SdlTTFError::Init) | 48 | .map_err(SdlTTFError::Init) |
41 | .map_err(AppError::SdlTTF)?; | 49 | .map_err(AppError::SdlTTF)?; |
42 | info!("Initialized SDL_ttf context"); | 50 | info!("Initialized SDL_ttf context"); |
43 | Ok((sdl_context, ttf_context)) | 51 | while let Ok(signal) = render_rx.recv() { |
44 | }; | 52 | match signal { |
53 | Signal::Quit => { | ||
54 | let ev = sdl_context.event().unwrap(); | ||
55 | ev.push_event(Event::Quit { timestamp: 0u32 }) | ||
56 | .expect("unable to quit ohno"); | ||
57 | } | ||
58 | _ => {} | ||
59 | } | ||
60 | } | ||
61 | Ok(()) | ||
62 | }); | ||
45 | 63 | ||
46 | match cli::parse_args().map_err(AppError::Cli)? { | 64 | match cli::parse_args().map_err(AppError::Cli)? { |
47 | Config::Help => { | 65 | Config::Help => { |
@@ -52,18 +70,16 @@ pub fn error_sink() -> Result<(), AppError> { | |||
52 | file_name, | 70 | file_name, |
53 | dimensions: (width, height), | 71 | dimensions: (width, height), |
54 | } => { | 72 | } => { |
55 | let (sdl_context, ttf_context) = init()?; | 73 | AppState::init(width, height, app_tx, app_rx, None, file_name)?.run(); |
56 | AppState::init(width, height, &sdl_context, &ttf_context, None, file_name)?.run(); | ||
57 | } | 74 | } |
58 | 75 | ||
59 | Config::ExistingProject { file_name } => { | 76 | Config::ExistingProject { file_name } => { |
60 | let (sdl_context, ttf_context) = init()?; | ||
61 | let image = utils::load_file(&file_name).map_err(AppError::File)?; | 77 | let image = utils::load_file(&file_name).map_err(AppError::File)?; |
62 | AppState::init( | 78 | AppState::init( |
63 | image.width(), | 79 | image.width(), |
64 | image.height(), | 80 | image.height(), |
65 | &sdl_context, | 81 | app_tx, |
66 | &ttf_context, | 82 | app_rx, |
67 | Some(image.data), | 83 | Some(image.data), |
68 | Some(file_name), | 84 | Some(file_name), |
69 | )? | 85 | )? |