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