Browse Source

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

Fixes #1756
Alex Szpakowski 3 years ago
parent
commit
7e271fc53e
1 changed files with 27 additions and 4 deletions
  1. 27 4
      src/modules/joystick/sdl/Joystick.cpp

+ 27 - 4
src/modules/joystick/sdl/Joystick.cpp

@@ -392,6 +392,11 @@ bool Joystick::checkCreateHaptic()
 
 
 bool Joystick::isVibrationSupported()
 bool Joystick::isVibrationSupported()
 {
 {
+#if SDL_VERSION_ATLEAST(2, 0, 18)
+	if (isConnected() && SDL_JoystickHasRumble(joyhandle) == SDL_TRUE)
+		return true;
+#endif
+
 	if (!checkCreateHaptic())
 	if (!checkCreateHaptic())
 		return false;
 		return false;
 
 
@@ -442,8 +447,12 @@ bool Joystick::setVibration(float left, float right, float duration)
 	if (left == 0.0f && right == 0.0f)
 	if (left == 0.0f && right == 0.0f)
 		return setVibration();
 		return setVibration();
 
 
-	if (!checkCreateHaptic())
+	if (!isConnected())
+	{
+		vibration.left = vibration.right = 0.0f;
+		vibration.endtime = SDL_HAPTIC_INFINITY;
 		return false;
 		return false;
+	}
 
 
 	Uint32 length = SDL_HAPTIC_INFINITY;
 	Uint32 length = SDL_HAPTIC_INFINITY;
 	if (duration >= 0.0f)
 	if (duration >= 0.0f)
@@ -453,10 +462,19 @@ bool Joystick::setVibration(float left, float right, float duration)
 	}
 	}
 
 
 	bool success = false;
 	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);
 	unsigned int features = SDL_HapticQuery(haptic);
 	int axes = SDL_HapticNumAxes(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));
 		memset(&vibration.effect, 0, sizeof(SDL_HapticEffect));
 		vibration.effect.type = SDL_HAPTIC_LEFTRIGHT;
 		vibration.effect.type = SDL_HAPTIC_LEFTRIGHT;
@@ -528,9 +546,14 @@ bool Joystick::setVibration(float left, float right, float duration)
 
 
 bool Joystick::setVibration()
 bool Joystick::setVibration()
 {
 {
-	bool success = true;
+	bool success = false;
+
+#if SDL_VERSION_ATLEAST(2, 0, 9)
+	if (!success)
+		success = isConnected() && SDL_JoystickRumble(joyhandle, 0, 0, 0) == 0;
+#endif
 
 
-	if (SDL_WasInit(SDL_INIT_HAPTIC) && haptic && SDL_HapticIndex(haptic) != -1)
+	if (!success && SDL_WasInit(SDL_INIT_HAPTIC) && haptic && SDL_HapticIndex(haptic) != -1)
 		success = (SDL_HapticStopEffect(haptic, vibration.id) == 0);
 		success = (SDL_HapticStopEffect(haptic, vibration.id) == 0);
 
 
 	if (success)
 	if (success)