aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 04b0f2b0ec4f5f9ee249815a87e28329d7bd6ab1 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#![allow(
    clippy::upper_case_acronyms,
    clippy::vec_init_then_push,
    clippy::unsound_collection_transmute,
    clippy::new_without_default
)]

mod app;
mod bitmap;
mod brush;
mod cache;
mod cli;
mod command;
mod consts;
mod dither;
mod error;
mod grid;
mod guide;
mod lisp;
mod message;
mod render;
mod symmetry;
mod undo;
mod utils;
mod widget;

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 (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");

        let ttf_context = sdl2::ttf::init()
            .map_err(SdlTTFError::Init)
            .map_err(AppError::SdlTTF)?;
        info!("Initialized SDL_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 => {
            println!("{}", cli::HELP_TEXT);
        }

        Config::NewProject {
            file_name,
            dimensions: (width, height),
        } => {
            AppState::init(width, height, app_tx, app_rx, None, file_name)?.run();
        }

        Config::ExistingProject { file_name } => {
            let image = utils::load_file(&file_name).map_err(AppError::File)?;
            AppState::init(
                image.width(),
                image.height(),
                app_tx,
                app_rx,
                Some(image.data),
                Some(file_name),
            )?
            .run();
        }
    }
    Ok(())
}

pub fn main() {
    env_logger::init();
    if let Err(e) = error_sink() {
        error!("{}", e);
    }
}