1extern crate sdl2_mt;
2
3use sdl2_mt::event::Event::*;
4use sdl2_mt::event::WindowEvent;
5use sdl2_mt::keyboard::Keycode;
6use sdl2_mt::pixels::Color;
7
8use std::sync::mpsc;
9use std::thread::sleep;
10use std::time::Duration;
11
12fn main() {
13 let sdlh = sdl2_mt::init();
15
16 let window = sdlh.create_simple_window("2D plot", 720, 720).unwrap();
17
18 sdlh.run_on_ui_thread(Box::new(move |_sdl, windows| {
20 let canvas = windows.get_mut(&window).unwrap();
21 canvas.set_draw_color(Color::RGBA(128, 128, 128, 255));
22 canvas.clear();
23 canvas.present();
24 })).unwrap();
25
26 let (tx, rx) = mpsc::channel();
29 while rx.try_recv().is_err() {
30 let tx = tx.clone();
31
32 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
34 match event {
35 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
36 tx.send(()).unwrap();
38 },
39
40 &KeyDown { keycode: Some(keycode), .. } => {
41 use sdl2_mt::video::WindowPos::Positioned;
42 let mut canvas = windows.get_mut(&window).unwrap();
43 let (mut x, mut y) = canvas.window().position();
44 match keycode {
45 Keycode::Up => y -= 5,
46 Keycode::Down => y += 5,
47 Keycode::Left => x -= 5,
48 Keycode::Right => x += 5,
49 _ => {}
50 }
51 canvas.window_mut().set_position(Positioned(x), Positioned(y));
52 },
53
54 &Window { win_event: WindowEvent::Resized(new_w, new_h), .. } => {
55 let mut canvas = windows.get_mut(&window).unwrap();
56 canvas.set_draw_color(Color::RGBA(128, (new_h % 256) as u8, (new_w % 256) as u8, 255));
57 canvas.clear();
58 canvas.present();
59 },
60
61 _ => return false
65 }
66 true
68 })).unwrap();
69
70 sleep(Duration::from_millis(15));
72 }
73
74 sdlh.exit().unwrap();
77}