aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 34f087a5eafd343aa794c9c9677ad309eaf459d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
mod app;
mod bitmap;
mod brush;
mod command;
mod consts;
mod dither;
mod lisp;
mod message;
mod symmetry;
mod undo;
mod utils;

use app::AppState;

use std::{
    env,
    fs::OpenOptions,
    io::{Cursor, Read},
    path::{Path, PathBuf},
};

use log::{error, info};
use obi::Image;

pub fn main() {
    env_logger::init();

    let sdl_context = sdl2::init();
    if sdl_context.is_err() {
        error!("Unable to find libsdl2 ... Exiting");
        return;
    }
    let sdl_context = sdl_context.unwrap();
    info!("Initialized SDL context");

    let ttf_context = sdl2::ttf::init();
    if ttf_context.is_err() {
        error!("Unable to find SDL2_ttf ... Exiting");
        return;
    }
    let ttf_context = ttf_context.unwrap();
    info!("Initialized SDL_ttf context");

    let args: Vec<_> = env::args().collect();
    if args.len() < 2 {
        AppState::init(160, 160, &sdl_context, &ttf_context, None, None).run();
        return;
    } else {
        let path = PathBuf::from(args.get(1).unwrap());
        let image_src = OpenOptions::new()
            .read(true)
            .write(false)
            .create(false)
            .open(&path);
        if let Ok(mut image) = image_src {
            let mut buf = Vec::new();
            image.read_to_end(&mut buf).unwrap();
            let decoded = Image::decode(&mut (Cursor::new(buf))).unwrap();
            let (width, height) = (decoded.width(), decoded.height());
            AppState::init(
                width,
                height,
                &sdl_context,
                &ttf_context,
                Some(decoded.data),
                Some(&path),
            )
            .run();
        } else {
            AppState::init(500, 500, &sdl_context, &ttf_context, None, Some(&path)).run()
        }
    }
}