From 42e16dc95f05bc8da403ff7329bc815b4baf005b Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Wed, 23 Jun 2021 07:48:16 -0400 Subject: Some SDL controller experimentation --- rust/controller/Cargo.toml | 10 ++++++ rust/controller/flake.lock | 25 +++++++++++++++ rust/controller/flake.nix | 22 ++++++++++++++ rust/controller/src/main.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 rust/controller/Cargo.toml create mode 100644 rust/controller/flake.lock create mode 100644 rust/controller/flake.nix create mode 100644 rust/controller/src/main.rs diff --git a/rust/controller/Cargo.toml b/rust/controller/Cargo.toml new file mode 100644 index 0000000..2825ff3 --- /dev/null +++ b/rust/controller/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "controller" +version = "0.1.0" +authors = ["Kjetil Orbekk "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sdl2 = "0.34" diff --git a/rust/controller/flake.lock b/rust/controller/flake.lock new file mode 100644 index 0000000..ab8956d --- /dev/null +++ b/rust/controller/flake.lock @@ -0,0 +1,25 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1624445739, + "narHash": "sha256-92lLwqUFTxbzU7oscb8L7WZZEZpI7IujV0zbqFC7w9M=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "652966c8815bfb8da1db531854fb1f1f7406070c", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/rust/controller/flake.nix b/rust/controller/flake.nix new file mode 100644 index 0000000..7f8cad7 --- /dev/null +++ b/rust/controller/flake.nix @@ -0,0 +1,22 @@ +{ + description = "A very basic flake"; + + outputs = { self, nixpkgs }: + let + system = "x86_64-linux"; + pkgs = nixpkgs.legacyPackages.${system}; + in { + devShell.${system} = pkgs.mkShell { + buildInputs = with pkgs; [ + SDL2 + glibc + pkgconfig + ]; + }; + + packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello; + + defaultPackage.x86_64-linux = self.packages.x86_64-linux.hello; + + }; +} 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 { + 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)); + } +} -- cgit v1.2.3