1 extern crate sdl2;
2
3 use std::env;
4 use std::path::Path;
5 use sdl2::image::{LoadTexture, InitFlag};
6 use sdl2::event::Event;
7 use sdl2::keyboard::Keycode;
8
run(png: &Path) -> Result<(), String>9 pub fn run(png: &Path) -> Result<(), String> {
10 let sdl_context = sdl2::init()?;
11 let video_subsystem = sdl_context.video()?;
12 let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG)?;
13 let window = video_subsystem.window("rust-sdl2 demo: Video", 800, 600)
14 .position_centered()
15 .build()
16 .map_err(|e| e.to_string())?;
17
18 let mut canvas = window.into_canvas().software().build().map_err(|e| e.to_string())?;
19 let texture_creator = canvas.texture_creator();
20 let texture = texture_creator.load_texture(png)?;
21
22 canvas.copy(&texture, None, None)?;
23 canvas.present();
24
25 'mainloop: loop {
26 for event in sdl_context.event_pump()?.poll_iter() {
27 match event {
28 Event::Quit{..} |
29 Event::KeyDown {keycode: Option::Some(Keycode::Escape), ..} =>
30 break 'mainloop,
31 _ => {}
32 }
33 }
34 }
35
36 Ok(())
37 }
38
main() -> Result<(), String>39 fn main() -> Result<(), String> {
40 let args: Vec<_> = env::args().collect();
41
42 if args.len() < 2 {
43 println!("Usage: cargo run /path/to/image.(png|jpg)")
44 } else {
45 run(Path::new(&args[1]))?;
46 }
47
48 Ok(())
49 }
50