Browse Source

Add Android accelerometer value clamping. Closes #350.

Lasse Öörni 11 years ago
parent
commit
21eb3d8bc9
1 changed files with 7 additions and 0 deletions
  1. 7 0
      Source/ThirdParty/SDL/src/joystick/android/SDL_sysjoystick.c

+ 7 - 0
Source/ThirdParty/SDL/src/joystick/android/SDL_sysjoystick.c

@@ -19,6 +19,8 @@
   3. This notice may not be removed or altered from any source distribution.
   3. This notice may not be removed or altered from any source distribution.
 */
 */
 
 
+// Modified by Lasse Oorni for Urho3D
+
 #include "../../SDL_internal.h"
 #include "../../SDL_internal.h"
 
 
 #ifdef SDL_JOYSTICK_ANDROID
 #ifdef SDL_JOYSTICK_ANDROID
@@ -519,6 +521,11 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
             if (item->joystick) {
             if (item->joystick) {
                 if (Android_JNI_GetAccelerometerValues(values)) {
                 if (Android_JNI_GetAccelerometerValues(values)) {
                     for ( i = 0; i < 3; i++ ) {
                     for ( i = 0; i < 3; i++ ) {
+                        // Urho3D: clamp values to avoid short integer overflow
+                        if (values[i] < -1.0f)
+                            values[i] = -1.0f;
+                        else if (values[i] > 1.0f)
+                            values[i] = 1.0f;
                         value = (Sint16)(values[i] * 32767.0f);
                         value = (Sint16)(values[i] * 32767.0f);
                         SDL_PrivateJoystickAxis(item->joystick, i, value);
                         SDL_PrivateJoystickAxis(item->joystick, i, value);
                     }
                     }