Use SDL's rumble APIs for Joystick:setVibration, when available.

Fixes #1756
This commit is contained in:
Alex Szpakowski
2021-12-11 11:52:42 -04:00
parent ebd5f13456
commit 7e271fc53e
+27 -4
View File
@@ -392,6 +392,11 @@ bool Joystick::checkCreateHaptic()
bool Joystick::isVibrationSupported()
{
#if SDL_VERSION_ATLEAST(2, 0, 18)
if (isConnected() && SDL_JoystickHasRumble(joyhandle) == SDL_TRUE)
return true;
#endif
if (!checkCreateHaptic())
return false;
@@ -442,8 +447,12 @@ bool Joystick::setVibration(float left, float right, float duration)
if (left == 0.0f && right == 0.0f)
return setVibration();
if (!checkCreateHaptic())
if (!isConnected())
{
vibration.left = vibration.right = 0.0f;
vibration.endtime = SDL_HAPTIC_INFINITY;
return false;
}
Uint32 length = SDL_HAPTIC_INFINITY;
if (duration >= 0.0f)
@@ -453,10 +462,19 @@ bool Joystick::setVibration(float left, float right, float duration)
}
bool success = false;
#if SDL_VERSION_ATLEAST(2, 0, 9)
if (SDL_JoystickRumble(joyhandle, (Uint16)(left * LOVE_UINT16_MAX), (Uint16)(right * LOVE_UINT16_MAX), length) == 0)
success = true;
#endif
if (!success && !checkCreateHaptic())
return false;
unsigned int features = SDL_HapticQuery(haptic);
int axes = SDL_HapticNumAxes(haptic);
if ((features & SDL_HAPTIC_LEFTRIGHT) != 0)
if (!success && (features & SDL_HAPTIC_LEFTRIGHT) != 0)
{
memset(&vibration.effect, 0, sizeof(SDL_HapticEffect));
vibration.effect.type = SDL_HAPTIC_LEFTRIGHT;
@@ -528,9 +546,14 @@ bool Joystick::setVibration(float left, float right, float duration)
bool Joystick::setVibration()
{
bool success = true;
bool success = false;
if (SDL_WasInit(SDL_INIT_HAPTIC) && haptic && SDL_HapticIndex(haptic) != -1)
#if SDL_VERSION_ATLEAST(2, 0, 9)
if (!success)
success = isConnected() && SDL_JoystickRumble(joyhandle, 0, 0, 0) == 0;
#endif
if (!success && SDL_WasInit(SDL_INIT_HAPTIC) && haptic && SDL_HapticIndex(haptic) != -1)
success = (SDL_HapticStopEffect(haptic, vibration.id) == 0);
if (success)