diff options
author | Kjetil Orbekk <kj@orbekk.com> | 2021-06-23 07:48:16 -0400 |
---|---|---|
committer | Kjetil Orbekk <kj@orbekk.com> | 2021-06-23 07:48:16 -0400 |
commit | 42e16dc95f05bc8da403ff7329bc815b4baf005b (patch) | |
tree | 260f2b0d0b24026748dadf36342703a1125d4fc1 /rust/controller/src | |
parent | cc268ccb4fa2f7573694af189089ca8f788b0597 (diff) |
Some SDL controller experimentation
Diffstat (limited to 'rust/controller/src')
-rw-r--r-- | rust/controller/src/main.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/rust/controller/src/main.rs b/rust/controller/src/main.rs new file mode 100644 index 0000000..fccfca0 --- /dev/null +++ b/rust/controller/src/main.rs @@ -0,0 +1,74 @@ +extern crate sdl2; + +use sdl2::controller::GameController; +use sdl2::event::Event; +use sdl2::keyboard::Keycode; +use sdl2::pixels::Color; +use sdl2::GameControllerSubsystem; +use sdl2::IntegerOrSdlError; +use std::time::Duration; + +fn get_controllers(game_controller: &GameControllerSubsystem) -> Vec<GameController> { + let mut result = vec![]; + for i in 0..game_controller.num_joysticks().unwrap() { + if game_controller.is_game_controller(i) { + result.push(game_controller.open(i).unwrap()); + } + } + result +} + +fn main() { + let sdl_context = sdl2::init().unwrap(); + let video_subsystem = sdl_context.video().unwrap(); + let game_controller = sdl_context.game_controller().unwrap(); + + println!("Game controller: {:?}", game_controller); + println!( + "I have {} joysticks", + game_controller.num_joysticks().unwrap() + ); + + let controllers = get_controllers(&game_controller); + for c in &controllers { + println!("=========="); + println!("Controller: {}", c.name()); + println!("Mapping: {}", c.name()); + println!("=========="); + } + + let window = video_subsystem + .window("rust-sdl2 demo", 800, 600) + .position_centered() + .build() + .unwrap(); + + let mut canvas = window.into_canvas().build().unwrap(); + + canvas.set_draw_color(Color::RGB(0, 255, 255)); + canvas.clear(); + canvas.present(); + let mut event_pump = sdl_context.event_pump().unwrap(); + let mut i = 0; + 'running: loop { + i = (i + 1) % 255; + canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); + canvas.clear(); + for event in event_pump.poll_iter() { + match event { + Event::Quit { .. } + | Event::KeyDown { + keycode: Some(Keycode::Escape), + .. + } => break 'running, + _ => { + println!("Got event: {:?}", event) + } + } + } + // The rest of the game loop goes here... + + canvas.present(); + ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); + } +} |