pub struct Sdl2Mt(/* private fields */);Implementations§
Source§impl Sdl2Mt
impl Sdl2Mt
Sourcepub fn create_simple_window<IntoString: Into<String>>(
&self,
name: IntoString,
width: u32,
height: u32,
) -> Result<u32, UiThreadExited>
pub fn create_simple_window<IntoString: Into<String>>( &self, name: IntoString, width: u32, height: u32, ) -> Result<u32, UiThreadExited>
A quick, simple way to create a window. Just give it a name, width, and height.
This function executes synchronously. It will block until the window_creator function has completed.
§Panics
This function will panic if the Window or the Canvas build() functions
do not succeed.
Examples found in repository?
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 let window = sdlh.create_simple_window("2D plot", 720, 720).unwrap();
17
18 // example of running arbitrary code on the UI thread
19 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 // create a channel we can use to easily break the loop
27 // from inside the closure.
28 let (tx, rx) = mpsc::channel();
29 while rx.try_recv().is_err() {
30 let tx = tx.clone();
31
32 // handle any new UI events that have happened
33 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
34 match event {
35 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
36 // send a message to rx to cancel the loop
37 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 // false means "this event handler function did not handle this event"
62 // in a multithreaded application, you might have an event handler per window.
63 // this makes it easier to juggle events between handlers.
64 _ => return false
65 }
66 // true means we handled this event
67 true
68 })).unwrap();
69
70 // keep the CPU usage down
71 sleep(Duration::from_millis(15));
72 }
73
74 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
75 // the exit() function has the effect of terminating the SDL2 UI thread.
76 sdlh.exit().unwrap();
77}Sourcepub fn create_window(
&self,
window_creator: Box<dyn FnMut(&mut Sdl, &mut VideoSubsystem) -> Option<WindowCanvas> + Send>,
) -> Result<Option<u32>, UiThreadExited>
pub fn create_window( &self, window_creator: Box<dyn FnMut(&mut Sdl, &mut VideoSubsystem) -> Option<WindowCanvas> + Send>, ) -> Result<Option<u32>, UiThreadExited>
Executes a window_creator function that accepts &mut VideoSubsystem
and returns an Option
This function executes synchronously. It will block until the window_creator function has completed.
Examples found in repository?
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 // create_window() allows you to run arbitrary code to create a window
17 // and then return the drawable Canvas from that window.
18 let window = sdlh.create_window(Box::new(|_sdl, video_subsystem| {
19 let canvas = video_subsystem.window("2D plot", 720, 720)
20 .position_centered()
21 .resizable()
22 .build()
23 .unwrap()
24 .into_canvas()
25 .software()
26 .build()
27 .unwrap();
28
29 Some(canvas)
30 })).unwrap().unwrap();
31
32 sleep(Duration::from_millis(20));
33
34 // example of running arbitrary code on the UI thread
35 sdlh.run_on_ui_thread(Box::new(move |_sdl, windows| {
36 let canvas = windows.get_mut(&window).unwrap();
37 canvas.set_draw_color(Color::RGBA(128, 128, 128, 255));
38 canvas.clear();
39 canvas.present();
40 })).unwrap();
41
42 // create a channel we can use to easily break the loop
43 // from inside the closure.
44 let (tx, rx) = mpsc::channel();
45 while rx.try_recv().is_err() {
46 let tx = tx.clone();
47
48 // handle any new UI events that have happened
49 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
50 match event {
51 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
52 // send a message to rx to cancel the loop
53 tx.send(()).unwrap();
54 },
55
56 &KeyDown { keycode: Some(keycode), .. } => {
57 use sdl2_mt::video::WindowPos::Positioned;
58 let mut canvas = windows.get_mut(&window).unwrap();
59 let (mut x, mut y) = canvas.window().position();
60 match keycode {
61 Keycode::Up => y -= 5,
62 Keycode::Down => y += 5,
63 Keycode::Left => x -= 5,
64 Keycode::Right => x += 5,
65 _ => {}
66 }
67 canvas.window_mut().set_position(Positioned(x), Positioned(y));
68 },
69
70 &Window { win_event: WindowEvent::Resized(new_w, new_h), .. } => {
71 let mut canvas = windows.get_mut(&window).unwrap();
72 canvas.set_draw_color(Color::RGBA(128, (new_h % 256) as u8, (new_w % 256) as u8, 255));
73 canvas.clear();
74 canvas.present();
75 },
76 // false means "this event handler function did not handle this event"
77 // in a multithreaded application, you might have an event handler per window.
78 // this makes it easier to juggle events between handlers.
79 _ => return false
80 }
81 // true means we handled this event
82 true
83 })).unwrap();
84
85 // keep the CPU usage down
86 sleep(Duration::from_millis(15));
87 }
88
89 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
90 // the exit() function has the effect of terminating the SDL2 UI thread.
91 sdlh.exit().unwrap();
92}Sourcepub fn run_on_ui_thread(
&self,
lambda: Box<dyn FnMut(&mut Sdl, &mut HashMap<u32, WindowCanvas>) + Send>,
) -> Result<(), UiThreadExited>
pub fn run_on_ui_thread( &self, lambda: Box<dyn FnMut(&mut Sdl, &mut HashMap<u32, WindowCanvas>) + Send>, ) -> Result<(), UiThreadExited>
This function executes asynchronously. It will not block the calling thread.
Examples found in repository?
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 let window = sdlh.create_simple_window("2D plot", 720, 720).unwrap();
17
18 // example of running arbitrary code on the UI thread
19 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 // create a channel we can use to easily break the loop
27 // from inside the closure.
28 let (tx, rx) = mpsc::channel();
29 while rx.try_recv().is_err() {
30 let tx = tx.clone();
31
32 // handle any new UI events that have happened
33 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
34 match event {
35 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
36 // send a message to rx to cancel the loop
37 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 // false means "this event handler function did not handle this event"
62 // in a multithreaded application, you might have an event handler per window.
63 // this makes it easier to juggle events between handlers.
64 _ => return false
65 }
66 // true means we handled this event
67 true
68 })).unwrap();
69
70 // keep the CPU usage down
71 sleep(Duration::from_millis(15));
72 }
73
74 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
75 // the exit() function has the effect of terminating the SDL2 UI thread.
76 sdlh.exit().unwrap();
77}More examples
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 // create_window() allows you to run arbitrary code to create a window
17 // and then return the drawable Canvas from that window.
18 let window = sdlh.create_window(Box::new(|_sdl, video_subsystem| {
19 let canvas = video_subsystem.window("2D plot", 720, 720)
20 .position_centered()
21 .resizable()
22 .build()
23 .unwrap()
24 .into_canvas()
25 .software()
26 .build()
27 .unwrap();
28
29 Some(canvas)
30 })).unwrap().unwrap();
31
32 sleep(Duration::from_millis(20));
33
34 // example of running arbitrary code on the UI thread
35 sdlh.run_on_ui_thread(Box::new(move |_sdl, windows| {
36 let canvas = windows.get_mut(&window).unwrap();
37 canvas.set_draw_color(Color::RGBA(128, 128, 128, 255));
38 canvas.clear();
39 canvas.present();
40 })).unwrap();
41
42 // create a channel we can use to easily break the loop
43 // from inside the closure.
44 let (tx, rx) = mpsc::channel();
45 while rx.try_recv().is_err() {
46 let tx = tx.clone();
47
48 // handle any new UI events that have happened
49 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
50 match event {
51 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
52 // send a message to rx to cancel the loop
53 tx.send(()).unwrap();
54 },
55
56 &KeyDown { keycode: Some(keycode), .. } => {
57 use sdl2_mt::video::WindowPos::Positioned;
58 let mut canvas = windows.get_mut(&window).unwrap();
59 let (mut x, mut y) = canvas.window().position();
60 match keycode {
61 Keycode::Up => y -= 5,
62 Keycode::Down => y += 5,
63 Keycode::Left => x -= 5,
64 Keycode::Right => x += 5,
65 _ => {}
66 }
67 canvas.window_mut().set_position(Positioned(x), Positioned(y));
68 },
69
70 &Window { win_event: WindowEvent::Resized(new_w, new_h), .. } => {
71 let mut canvas = windows.get_mut(&window).unwrap();
72 canvas.set_draw_color(Color::RGBA(128, (new_h % 256) as u8, (new_w % 256) as u8, 255));
73 canvas.clear();
74 canvas.present();
75 },
76 // false means "this event handler function did not handle this event"
77 // in a multithreaded application, you might have an event handler per window.
78 // this makes it easier to juggle events between handlers.
79 _ => return false
80 }
81 // true means we handled this event
82 true
83 })).unwrap();
84
85 // keep the CPU usage down
86 sleep(Duration::from_millis(15));
87 }
88
89 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
90 // the exit() function has the effect of terminating the SDL2 UI thread.
91 sdlh.exit().unwrap();
92}Sourcepub fn handle_ui_events(
&self,
event_handler: Box<dyn FnMut(&mut Sdl, &mut HashMap<u32, WindowCanvas>, &Event) -> bool + Send>,
) -> Result<(), UiThreadExited>
pub fn handle_ui_events( &self, event_handler: Box<dyn FnMut(&mut Sdl, &mut HashMap<u32, WindowCanvas>, &Event) -> bool + Send>, ) -> Result<(), UiThreadExited>
Executes an event_handler function.
This function executes synchronously. It will block until the event_handler function has completed.
Examples found in repository?
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 let window = sdlh.create_simple_window("2D plot", 720, 720).unwrap();
17
18 // example of running arbitrary code on the UI thread
19 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 // create a channel we can use to easily break the loop
27 // from inside the closure.
28 let (tx, rx) = mpsc::channel();
29 while rx.try_recv().is_err() {
30 let tx = tx.clone();
31
32 // handle any new UI events that have happened
33 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
34 match event {
35 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
36 // send a message to rx to cancel the loop
37 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 // false means "this event handler function did not handle this event"
62 // in a multithreaded application, you might have an event handler per window.
63 // this makes it easier to juggle events between handlers.
64 _ => return false
65 }
66 // true means we handled this event
67 true
68 })).unwrap();
69
70 // keep the CPU usage down
71 sleep(Duration::from_millis(15));
72 }
73
74 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
75 // the exit() function has the effect of terminating the SDL2 UI thread.
76 sdlh.exit().unwrap();
77}More examples
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 // create_window() allows you to run arbitrary code to create a window
17 // and then return the drawable Canvas from that window.
18 let window = sdlh.create_window(Box::new(|_sdl, video_subsystem| {
19 let canvas = video_subsystem.window("2D plot", 720, 720)
20 .position_centered()
21 .resizable()
22 .build()
23 .unwrap()
24 .into_canvas()
25 .software()
26 .build()
27 .unwrap();
28
29 Some(canvas)
30 })).unwrap().unwrap();
31
32 sleep(Duration::from_millis(20));
33
34 // example of running arbitrary code on the UI thread
35 sdlh.run_on_ui_thread(Box::new(move |_sdl, windows| {
36 let canvas = windows.get_mut(&window).unwrap();
37 canvas.set_draw_color(Color::RGBA(128, 128, 128, 255));
38 canvas.clear();
39 canvas.present();
40 })).unwrap();
41
42 // create a channel we can use to easily break the loop
43 // from inside the closure.
44 let (tx, rx) = mpsc::channel();
45 while rx.try_recv().is_err() {
46 let tx = tx.clone();
47
48 // handle any new UI events that have happened
49 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
50 match event {
51 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
52 // send a message to rx to cancel the loop
53 tx.send(()).unwrap();
54 },
55
56 &KeyDown { keycode: Some(keycode), .. } => {
57 use sdl2_mt::video::WindowPos::Positioned;
58 let mut canvas = windows.get_mut(&window).unwrap();
59 let (mut x, mut y) = canvas.window().position();
60 match keycode {
61 Keycode::Up => y -= 5,
62 Keycode::Down => y += 5,
63 Keycode::Left => x -= 5,
64 Keycode::Right => x += 5,
65 _ => {}
66 }
67 canvas.window_mut().set_position(Positioned(x), Positioned(y));
68 },
69
70 &Window { win_event: WindowEvent::Resized(new_w, new_h), .. } => {
71 let mut canvas = windows.get_mut(&window).unwrap();
72 canvas.set_draw_color(Color::RGBA(128, (new_h % 256) as u8, (new_w % 256) as u8, 255));
73 canvas.clear();
74 canvas.present();
75 },
76 // false means "this event handler function did not handle this event"
77 // in a multithreaded application, you might have an event handler per window.
78 // this makes it easier to juggle events between handlers.
79 _ => return false
80 }
81 // true means we handled this event
82 true
83 })).unwrap();
84
85 // keep the CPU usage down
86 sleep(Duration::from_millis(15));
87 }
88
89 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
90 // the exit() function has the effect of terminating the SDL2 UI thread.
91 sdlh.exit().unwrap();
92}Sourcepub fn exit(self) -> Result<(), UiThreadExited>
pub fn exit(self) -> Result<(), UiThreadExited>
Terminates the UI thread. Not strictly necessary if the program will exit anyways, such as when the main program thread returns from main.
Examples found in repository?
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 let window = sdlh.create_simple_window("2D plot", 720, 720).unwrap();
17
18 // example of running arbitrary code on the UI thread
19 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 // create a channel we can use to easily break the loop
27 // from inside the closure.
28 let (tx, rx) = mpsc::channel();
29 while rx.try_recv().is_err() {
30 let tx = tx.clone();
31
32 // handle any new UI events that have happened
33 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
34 match event {
35 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
36 // send a message to rx to cancel the loop
37 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 // false means "this event handler function did not handle this event"
62 // in a multithreaded application, you might have an event handler per window.
63 // this makes it easier to juggle events between handlers.
64 _ => return false
65 }
66 // true means we handled this event
67 true
68 })).unwrap();
69
70 // keep the CPU usage down
71 sleep(Duration::from_millis(15));
72 }
73
74 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
75 // the exit() function has the effect of terminating the SDL2 UI thread.
76 sdlh.exit().unwrap();
77}More examples
12fn main() {
13 //sdlh is "sdl handle"
14 let sdlh = sdl2_mt::init();
15
16 // create_window() allows you to run arbitrary code to create a window
17 // and then return the drawable Canvas from that window.
18 let window = sdlh.create_window(Box::new(|_sdl, video_subsystem| {
19 let canvas = video_subsystem.window("2D plot", 720, 720)
20 .position_centered()
21 .resizable()
22 .build()
23 .unwrap()
24 .into_canvas()
25 .software()
26 .build()
27 .unwrap();
28
29 Some(canvas)
30 })).unwrap().unwrap();
31
32 sleep(Duration::from_millis(20));
33
34 // example of running arbitrary code on the UI thread
35 sdlh.run_on_ui_thread(Box::new(move |_sdl, windows| {
36 let canvas = windows.get_mut(&window).unwrap();
37 canvas.set_draw_color(Color::RGBA(128, 128, 128, 255));
38 canvas.clear();
39 canvas.present();
40 })).unwrap();
41
42 // create a channel we can use to easily break the loop
43 // from inside the closure.
44 let (tx, rx) = mpsc::channel();
45 while rx.try_recv().is_err() {
46 let tx = tx.clone();
47
48 // handle any new UI events that have happened
49 sdlh.handle_ui_events(Box::new(move |_sdl, windows, event| {
50 match event {
51 &Quit { .. } | &KeyDown { keycode: Some(Keycode::Escape), .. } => {
52 // send a message to rx to cancel the loop
53 tx.send(()).unwrap();
54 },
55
56 &KeyDown { keycode: Some(keycode), .. } => {
57 use sdl2_mt::video::WindowPos::Positioned;
58 let mut canvas = windows.get_mut(&window).unwrap();
59 let (mut x, mut y) = canvas.window().position();
60 match keycode {
61 Keycode::Up => y -= 5,
62 Keycode::Down => y += 5,
63 Keycode::Left => x -= 5,
64 Keycode::Right => x += 5,
65 _ => {}
66 }
67 canvas.window_mut().set_position(Positioned(x), Positioned(y));
68 },
69
70 &Window { win_event: WindowEvent::Resized(new_w, new_h), .. } => {
71 let mut canvas = windows.get_mut(&window).unwrap();
72 canvas.set_draw_color(Color::RGBA(128, (new_h % 256) as u8, (new_w % 256) as u8, 255));
73 canvas.clear();
74 canvas.present();
75 },
76 // false means "this event handler function did not handle this event"
77 // in a multithreaded application, you might have an event handler per window.
78 // this makes it easier to juggle events between handlers.
79 _ => return false
80 }
81 // true means we handled this event
82 true
83 })).unwrap();
84
85 // keep the CPU usage down
86 sleep(Duration::from_millis(15));
87 }
88
89 // not strictly necessary, since when the main thread exits in Rust the entire program is killed.
90 // the exit() function has the effect of terminating the SDL2 UI thread.
91 sdlh.exit().unwrap();
92}