aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs65
1 files changed, 31 insertions, 34 deletions
diff --git a/src/main.rs b/src/main.rs
index 31f64f1..1b6e93a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@ mod brush;
4mod command; 4mod command;
5mod consts; 5mod consts;
6mod dither; 6mod dither;
7mod error;
7mod lisp; 8mod lisp;
8mod message; 9mod message;
9mod symmetry; 10mod symmetry;
@@ -11,6 +12,7 @@ mod undo;
11mod utils; 12mod utils;
12 13
13use app::AppState; 14use app::AppState;
15use error::{AppError, SdlTTFError};
14 16
15use std::{ 17use std::{
16 env, 18 env,
@@ -22,52 +24,47 @@ use std::{
22use log::{error, info}; 24use log::{error, info};
23use obi::Image; 25use obi::Image;
24 26
25pub fn main() { 27pub fn error_sink() -> Result<(), AppError> {
26 env_logger::init(); 28 env_logger::init();
27 29
28 let sdl_context = sdl2::init(); 30 let sdl_context = sdl2::init().map_err(AppError::Sdl)?;
29 if sdl_context.is_err() {
30 error!("Unable to find libsdl2 ... Exiting");
31 return;
32 }
33 let sdl_context = sdl_context.unwrap();
34 info!("Initialized SDL context"); 31 info!("Initialized SDL context");
35 32
36 let ttf_context = sdl2::ttf::init(); 33 let ttf_context = sdl2::ttf::init().map_err(|e| AppError::SdlTTF(SdlTTFError::Init(e)))?;
37 if ttf_context.is_err() {
38 error!("Unable to find SDL2_ttf ... Exiting");
39 return;
40 }
41 let ttf_context = ttf_context.unwrap();
42 info!("Initialized SDL_ttf context"); 34 info!("Initialized SDL_ttf context");
43 35
44 let args: Vec<_> = env::args().collect(); 36 let args: Vec<_> = env::args().collect();
45 if args.len() < 2 { 37 if args.len() < 2 {
46 AppState::init(160, 160, &sdl_context, &ttf_context, None, None).run(); 38 AppState::init(160, 160, &sdl_context, &ttf_context, None, None)?.run();
47 return; 39 return Ok(());
48 } else { 40 } else {
49 let path = PathBuf::from(args.get(1).unwrap()); 41 let path = PathBuf::from(args.get(1).unwrap());
50 let image_src = OpenOptions::new() 42 let mut image = OpenOptions::new()
51 .read(true) 43 .read(true)
52 .write(false) 44 .write(false)
53 .create(false) 45 .create(false)
54 .open(&path); 46 .open(&path)
55 if let Ok(mut image) = image_src { 47 .map_err(AppError::File)?;
56 let mut buf = Vec::new(); 48 let mut buf = Vec::new();
57 image.read_to_end(&mut buf).unwrap(); 49 image.read_to_end(&mut buf).map_err(AppError::File)?;
58 let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); 50 let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap(); // TODO: obi error
59 let (width, height) = (decoded.width(), decoded.height()); 51 let (width, height) = (decoded.width(), decoded.height());
60 AppState::init( 52 AppState::init(
61 width, 53 width,
62 height, 54 height,
63 &sdl_context, 55 &sdl_context,
64 &ttf_context, 56 &ttf_context,
65 Some(decoded.data), 57 Some(decoded.data),
66 Some(&path), 58 Some(&path),
67 ) 59 )?
68 .run(); 60 .run();
69 } else { 61 return Ok(());
70 AppState::init(500, 500, &sdl_context, &ttf_context, None, Some(&path)).run() 62 }
71 } 63}
64
65pub fn main() {
66 match error_sink() {
67 Err(e) => error!("{}", e),
68 _ => (),
72 } 69 }
73} 70}