Skip to content

Commit 67be483

Browse files
authored
Merge pull request Rust-SDL2#824 from Manorhos/rumble
Add wrappers for SDL 2.0.9 rumble functions
2 parents 996236a + c8209de commit 67be483

File tree

6 files changed

+123
-10
lines changed

6 files changed

+123
-10
lines changed

changelog.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
In this file will be listed the changes, especially the breaking ones that one should be careful of
22
when upgrading from a version of rust-sdl2 to another.
33

4+
### v0.32.1 (unreleased)
5+
6+
[PR #824](https://github.com/Rust-SDL2/rust-sdl2/pull/824):
7+
Added `controller::set_rumble` and `joystick::set_rumble`, wrappers for `SDL_GameControllerRumble` and `SDL_JoystickRumble` respectively.
8+
49
### v0.32
510

611
[PR #790](https://github.com/Rust-SDL2/rust-sdl2/pull/790): Added missing `window_id` field to `Event::DropFile`
@@ -41,7 +46,7 @@ Fix `ClipboardUtil::set_clipboard_text` to return an Ok when it went well.
4146
Add `video::border_size -> Result<(u16, u16, u16, u16), String>` equivalent of `SDL_GetWindowBorderSize()`
4247

4348
[PR #732](https://github.com/Rust-SDL2/rust-sdl2/pull/732):
44-
Implemented `From<(u8, u8, u8)>` and `From<(u8, u8, u8, u8)>` for `pixels::Color`.
49+
Implemented `From<(u8, u8, u8)>` and `From<(u8, u8, u8, u8)>` for `pixels::Color`.
4550
`Canvas.set_draw_color` can now be called with tuples or other types which implements `Into<pixels::Color>`
4651

4752
[PR #279](https://github.com/Rust-SDL2/rust-sdl2/pull/729)

examples/game-controller.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,37 @@ fn main() {
3636
}
3737
}
3838

39-
let controller =
39+
let mut controller =
4040
match controller {
4141
Some(c) => c,
4242
None => panic!("Couldn't open any controller"),
4343
};
4444

4545
println!("Controller mapping: {}", controller.mapping());
4646

47+
let (mut lo_freq, mut hi_freq) = (0, 0);
48+
4749
for event in sdl_context.event_pump().unwrap().wait_iter() {
4850
use sdl2::event::Event;
51+
use sdl2::controller::Axis;
4952

5053
match event {
54+
Event::ControllerAxisMotion{ axis: Axis::TriggerLeft, value: val, .. } => {
55+
// Trigger axes go from 0 to 32767, so this should be okay
56+
lo_freq = (val as u16) * 2;
57+
match controller.set_rumble(lo_freq, hi_freq, 15000) {
58+
Ok(()) => println!("Set rumble to ({}, {})", lo_freq, hi_freq),
59+
Err(e) => println!("Error setting rumble to ({}, {}): {:?}", lo_freq, hi_freq, e),
60+
}
61+
}
62+
Event::ControllerAxisMotion{ axis: Axis::TriggerRight, value: val, .. } => {
63+
// Trigger axes go from 0 to 32767, so this should be okay
64+
hi_freq = (val as u16) * 2;
65+
match controller.set_rumble(lo_freq, hi_freq, 15000) {
66+
Ok(()) => println!("Set rumble to ({}, {})", lo_freq, hi_freq),
67+
Err(e) => println!("Error setting rumble to ({}, {}): {:?}", lo_freq, hi_freq, e),
68+
}
69+
}
5170
Event::ControllerAxisMotion{ axis, value: val, .. } => {
5271
// Axis motion is an absolute value in the range
5372
// [-32768, 32767]. Let's simulate a very rough dead

examples/joystick.rs

+33-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ fn main() {
2828
}
2929

3030
// Print the joystick's power level, if a joystick was found.
31-
match joystick {
31+
let mut joystick = match joystick {
3232
Some(j) => {
3333
println!("\"{}\" power level: {:?}", j.name(), j.power_level().unwrap());
34+
j
3435
},
3536
None => panic!("Couldn't open any joystick"),
36-
}
37+
};
38+
39+
let (mut lo_freq, mut hi_freq) = (0, 0);
3740

3841
for event in sdl_context.event_pump().unwrap().wait_iter() {
3942
use sdl2::event::Event;
@@ -48,10 +51,34 @@ fn main() {
4851
println!("Axis {} moved to {}", axis_idx, val);
4952
}
5053
}
51-
Event::JoyButtonDown{ button_idx, .. } =>
52-
println!("Button {} down", button_idx),
53-
Event::JoyButtonUp{ button_idx, .. } =>
54-
println!("Button {} up", button_idx),
54+
Event::JoyButtonDown{ button_idx, .. } => {
55+
println!("Button {} down", button_idx);
56+
if button_idx == 0 {
57+
lo_freq = 65535;
58+
} else if button_idx == 1 {
59+
hi_freq = 65535;
60+
}
61+
if button_idx < 2 {
62+
match joystick.set_rumble(lo_freq, hi_freq, 15000) {
63+
Ok(()) => println!("Set rumble to ({}, {})", lo_freq, hi_freq),
64+
Err(e) => println!("Error setting rumble to ({}, {}): {:?}", lo_freq, hi_freq, e),
65+
}
66+
}
67+
}
68+
Event::JoyButtonUp{ button_idx, .. } => {
69+
println!("Button {} up", button_idx);
70+
if button_idx == 0 {
71+
lo_freq = 0;
72+
} else if button_idx == 1 {
73+
hi_freq = 0;
74+
}
75+
if button_idx < 2 {
76+
match joystick.set_rumble(lo_freq, hi_freq, 15000) {
77+
Ok(()) => println!("Set rumble to ({}, {})", lo_freq, hi_freq),
78+
Err(e) => println!("Error setting rumble to ({}, {}): {:?}", lo_freq, hi_freq, e),
79+
}
80+
}
81+
}
5582
Event::JoyHatMotion{ hat_idx, state, .. } =>
5683
println!("Hat {} moved to {:?}", hat_idx, state),
5784
Event::Quit{..} => break,

scripts/travis-install-sdl2.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -xueo pipefail
44

5-
wget https://www.libsdl.org/release/SDL2-2.0.8.tar.gz -O sdl2.tar.gz
5+
wget https://www.libsdl.org/release/SDL2-2.0.9.tar.gz -O sdl2.tar.gz
66
tar xzf sdl2.tar.gz
77
pushd SDL2-* && ./configure && make && sudo make install && popd
88
wget -q https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.tar.gz

src/sdl2/controller.rs

+31
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,37 @@ impl GameController {
402402

403403
unsafe { sys::SDL_GameControllerGetButton(self.raw, raw_button) != 0 }
404404
}
405+
406+
/// Set the rumble motors to their specified intensities, if supported.
407+
/// Automatically resets back to zero after `duration_ms` milliseconds have passed.
408+
///
409+
/// # Notes
410+
///
411+
/// The value range for the intensities is 0 to 0xFFFF.
412+
///
413+
/// Do *not* use `std::u32::MAX` or similar for `duration_ms` if you want
414+
/// the rumble effect to keep playing for a long time, as this results in
415+
/// the effect ending immediately after starting due to an overflow.
416+
/// Use some smaller, "huge enough" number instead.
417+
pub fn set_rumble(&mut self,
418+
low_frequency_rumble: u16,
419+
high_frequency_rumble: u16,
420+
duration_ms: u32)
421+
-> Result<(), IntegerOrSdlError>
422+
{
423+
let result = unsafe {
424+
sys::SDL_GameControllerRumble(self.raw,
425+
low_frequency_rumble,
426+
high_frequency_rumble,
427+
duration_ms)
428+
};
429+
430+
if result != 0 {
431+
Err(IntegerOrSdlError::SdlError(get_error()))
432+
} else {
433+
Ok(())
434+
}
435+
}
405436
}
406437

407438
impl Drop for GameController {

src/sdl2/joystick.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl JoystickSubsystem {
2121
}
2222

2323
/// Attempt to open the joystick at index `joystick_index` and return it.
24-
pub fn open(&self, joystick_index: u32)
24+
pub fn open(&self, joystick_index: u32)
2525
-> Result<Joystick, IntegerOrSdlError> {
2626
use common::IntegerOrSdlError::*;
2727
let joystick_index = try!(validate_int(joystick_index, "joystick_index"));
@@ -344,6 +344,37 @@ impl Joystick {
344344
}
345345
}
346346
}
347+
348+
/// Set the rumble motors to their specified intensities, if supported.
349+
/// Automatically resets back to zero after `duration_ms` milliseconds have passed.
350+
///
351+
/// # Notes
352+
///
353+
/// The value range for the intensities is 0 to 0xFFFF.
354+
///
355+
/// Do *not* use `std::u32::MAX` or similar for `duration_ms` if you want
356+
/// the rumble effect to keep playing for a long time, as this results in
357+
/// the effect ending immediately after starting due to an overflow.
358+
/// Use some smaller, "huge enough" number instead.
359+
pub fn set_rumble(&mut self,
360+
low_frequency_rumble: u16,
361+
high_frequency_rumble: u16,
362+
duration_ms: u32)
363+
-> Result<(), IntegerOrSdlError>
364+
{
365+
let result = unsafe {
366+
sys::SDL_JoystickRumble(self.raw,
367+
low_frequency_rumble,
368+
high_frequency_rumble,
369+
duration_ms)
370+
};
371+
372+
if result != 0 {
373+
Err(IntegerOrSdlError::SdlError(get_error()))
374+
} else {
375+
Ok(())
376+
}
377+
}
347378
}
348379

349380
impl Drop for Joystick {

0 commit comments

Comments
 (0)