Browse Source

Fix 3DS Analog Values (#8581)

zoeyjodon 1 year ago
parent
commit
059e550e98
1 changed files with 17 additions and 7 deletions
  1. 17 7
      src/joystick/n3ds/SDL_sysjoystick.c

+ 17 - 7
src/joystick/n3ds/SDL_sysjoystick.c

@@ -31,18 +31,28 @@
 #define NB_BUTTONS 23
 #define NB_BUTTONS 23
 
 
 /*
 /*
-  N3DS sticks values are roughly within +/-160
+  N3DS sticks values are roughly within +/-170
   which is too small to pass the jitter tolerance.
   which is too small to pass the jitter tolerance.
   This correction is applied to axis values
   This correction is applied to axis values
   so they fit better in SDL's value range.
   so they fit better in SDL's value range.
 */
 */
-#define CORRECT_AXIS_X(X) ((X * SDL_JOYSTICK_AXIS_MAX) / 160)
+static inline int Correct_Axis_X(int X) {
+    if (X > 160) {
+        return SDL_JOYSTICK_AXIS_MAX;
+    }
+    else if (X < -160) {
+        return -SDL_JOYSTICK_AXIS_MAX;
+    }
+    return (X * SDL_JOYSTICK_AXIS_MAX) / 160;
+}
 
 
 /*
 /*
   The Y axis needs to be flipped because SDL's "up"
   The Y axis needs to be flipped because SDL's "up"
   is reversed compared to libctru's "up"
   is reversed compared to libctru's "up"
 */
 */
-#define CORRECT_AXIS_Y(Y) CORRECT_AXIS_X(-Y)
+static inline int Correct_Axis_Y(int Y) {
+    return Correct_Axis_X(-Y);
+}
 
 
 static void UpdateN3DSPressedButtons(Uint64 timestamp, SDL_Joystick *joystick);
 static void UpdateN3DSPressedButtons(Uint64 timestamp, SDL_Joystick *joystick);
 static void UpdateN3DSReleasedButtons(Uint64 timestamp, SDL_Joystick *joystick);
 static void UpdateN3DSReleasedButtons(Uint64 timestamp, SDL_Joystick *joystick);
@@ -141,12 +151,12 @@ static void UpdateN3DSCircle(Uint64 timestamp, SDL_Joystick *joystick)
     if (previous_state.dx != current_state.dx) {
     if (previous_state.dx != current_state.dx) {
         SDL_SendJoystickAxis(timestamp, joystick,
         SDL_SendJoystickAxis(timestamp, joystick,
                                 0,
                                 0,
-                                CORRECT_AXIS_X(current_state.dx));
+                                Correct_Axis_X(current_state.dx));
     }
     }
     if (previous_state.dy != current_state.dy) {
     if (previous_state.dy != current_state.dy) {
         SDL_SendJoystickAxis(timestamp, joystick,
         SDL_SendJoystickAxis(timestamp, joystick,
                                 1,
                                 1,
-                                CORRECT_AXIS_Y(current_state.dy));
+                                Correct_Axis_Y(current_state.dy));
     }
     }
     previous_state = current_state;
     previous_state = current_state;
 }
 }
@@ -159,12 +169,12 @@ static void UpdateN3DSCStick(Uint64 timestamp, SDL_Joystick *joystick)
     if (previous_state.dx != current_state.dx) {
     if (previous_state.dx != current_state.dx) {
         SDL_SendJoystickAxis(timestamp, joystick,
         SDL_SendJoystickAxis(timestamp, joystick,
                                 2,
                                 2,
-                                CORRECT_AXIS_X(current_state.dx));
+                                Correct_Axis_X(current_state.dx));
     }
     }
     if (previous_state.dy != current_state.dy) {
     if (previous_state.dy != current_state.dy) {
         SDL_SendJoystickAxis(timestamp, joystick,
         SDL_SendJoystickAxis(timestamp, joystick,
                                 3,
                                 3,
-                                CORRECT_AXIS_Y(current_state.dy));
+                                Correct_Axis_Y(current_state.dy));
     }
     }
     previous_state = current_state;
     previous_state = current_state;
 }
 }