diff options
-rw-r--r-- | src/bin/viewer.rs | 79 | ||||
-rw-r--r-- | src/lib.rs | 1 |
2 files changed, 79 insertions, 1 deletions
diff --git a/src/bin/viewer.rs b/src/bin/viewer.rs new file mode 100644 index 0000000..e600aee --- /dev/null +++ b/src/bin/viewer.rs | |||
@@ -0,0 +1,79 @@ | |||
1 | use std::env; | ||
2 | use std::fs::{File, OpenOptions}; | ||
3 | use std::io::{self, prelude, Cursor, Read}; | ||
4 | |||
5 | const HELP_TEXT: &'static str = r#" | ||
6 | OBI Viewer | ||
7 | "#; | ||
8 | |||
9 | use obi::Image; | ||
10 | use sdl2::{event::Event, keyboard::Keycode, pixels::Color, rect::Rect}; | ||
11 | |||
12 | fn main() { | ||
13 | let args = env::args().collect::<Vec<_>>(); | ||
14 | |||
15 | let sdl_context = sdl2::init().unwrap(); | ||
16 | let video_subsystem = sdl_context.video().unwrap(); | ||
17 | |||
18 | match &args[..] { | ||
19 | [_, path] => { | ||
20 | eprintln!("{}", path); | ||
21 | let image_src = OpenOptions::new() | ||
22 | .read(true) | ||
23 | .write(false) | ||
24 | .create(false) | ||
25 | .open(path); | ||
26 | if let Ok(mut image) = image_src { | ||
27 | let mut buf = Vec::new(); | ||
28 | image.read_to_end(&mut buf); | ||
29 | let img = Image::decode(&mut (Cursor::new(buf))).unwrap(); | ||
30 | |||
31 | let window = video_subsystem | ||
32 | .window("OBI Viewer", img.width(), img.height()) | ||
33 | .position_centered() | ||
34 | .opengl() | ||
35 | .build() | ||
36 | .map_err(|e| e.to_string()) | ||
37 | .unwrap(); | ||
38 | |||
39 | let mut canvas = window | ||
40 | .into_canvas() | ||
41 | .build() | ||
42 | .map_err(|e| e.to_string()) | ||
43 | .unwrap(); | ||
44 | |||
45 | let mut event_pump = sdl_context.event_pump().unwrap(); | ||
46 | |||
47 | 'running: loop { | ||
48 | for event in event_pump.poll_iter() { | ||
49 | match event { | ||
50 | Event::Quit { .. } | ||
51 | | Event::KeyDown { | ||
52 | keycode: Some(Keycode::Escape), | ||
53 | .. | ||
54 | } => break 'running, | ||
55 | _ => {} | ||
56 | } | ||
57 | } | ||
58 | |||
59 | canvas.set_draw_color(Color::RGB(0, 0, 0)); | ||
60 | canvas.clear(); | ||
61 | canvas.set_draw_color(Color::RGB(255, 255, 255)); | ||
62 | |||
63 | for x in 0..img.width() { | ||
64 | for y in 0..img.height() { | ||
65 | if img.data[img.index(x, y).unwrap()] { | ||
66 | canvas.fill_rect(Rect::new(x as i32, y as i32, 1, 1)); | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | canvas.present(); | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | _ => { | ||
76 | eprintln!("requires and argument!"); | ||
77 | } | ||
78 | } | ||
79 | } | ||
@@ -10,7 +10,6 @@ pub mod error; | |||
10 | mod rle; | 10 | mod rle; |
11 | 11 | ||
12 | use error::{OBIError, OBIResult}; | 12 | use error::{OBIError, OBIResult}; |
13 | use rle::RLE; | ||
14 | 13 | ||
15 | #[non_exhaustive] | 14 | #[non_exhaustive] |
16 | #[derive(Copy, Clone, Debug, PartialEq)] | 15 | #[derive(Copy, Clone, Debug, PartialEq)] |