mirror of
https://github.com/love2d/love.git
synced 2026-08-12 16:40:57 +02:00
Use SDL's rumble APIs for Joystick:setVibration, when available.
Fixes #1756
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user