Commit 43a2b0b1e5637409f93a5763e6704f87012def07

Pierre Wendling 2022-10-10T00:00:52

N3DS: Use macro to correct axis. Using `(value * SDL max) / 3DS max` allows for marginally better accuracy compared to `value * (SDL max / 3DS max)`.

diff --git a/src/joystick/n3ds/SDL_sysjoystick.c b/src/joystick/n3ds/SDL_sysjoystick.c
index 6525d51..ea00a6d 100644
--- a/src/joystick/n3ds/SDL_sysjoystick.c
+++ b/src/joystick/n3ds/SDL_sysjoystick.c
@@ -34,16 +34,16 @@
 /*
   N3DS sticks values are roughly within +/-160
   which is too small to pass the jitter tolerance.
-  This correction factor is applied to axis values
+  This correction is applied to axis values
   so they fit better in SDL's value range.
 */
-#define CORRECTION_FACTOR_X SDL_JOYSTICK_AXIS_MAX / 160
+#define CORRECT_AXIS_X(X) ((X * SDL_JOYSTICK_AXIS_MAX) / 160)
 
 /*
   The Y axis needs to be flipped because SDL's "up"
   is reversed compared to libctru's "up"
 */
-#define CORRECTION_FACTOR_Y -CORRECTION_FACTOR_X
+#define CORRECT_AXIS_Y(Y) CORRECT_AXIS_X(-Y)
 
 SDL_FORCE_INLINE void UpdateN3DSPressedButtons(SDL_Joystick *joystick);
 SDL_FORCE_INLINE void UpdateN3DSReleasedButtons(SDL_Joystick *joystick);
@@ -151,12 +151,12 @@ UpdateN3DSCircle(SDL_Joystick *joystick)
     if (previous_state.dx != current_state.dx) {
         SDL_PrivateJoystickAxis(joystick,
                                 0,
-                                current_state.dx * CORRECTION_FACTOR_X);
+                                CORRECT_AXIS_X(current_state.dx));
     }
     if (previous_state.dy != current_state.dy) {
         SDL_PrivateJoystickAxis(joystick,
                                 1,
-                                current_state.dy * CORRECTION_FACTOR_Y);
+                                CORRECT_AXIS_Y(current_state.dy));
     }
     previous_state = current_state;
 }
@@ -170,12 +170,12 @@ UpdateN3DSCStick(SDL_Joystick *joystick)
     if (previous_state.dx != current_state.dx) {
         SDL_PrivateJoystickAxis(joystick,
                                 2,
-                                current_state.dx * CORRECTION_FACTOR_X);
+                                CORRECT_AXIS_X(current_state.dx));
     }
     if (previous_state.dy != current_state.dy) {
         SDL_PrivateJoystickAxis(joystick,
                                 3,
-                                current_state.dy * CORRECTION_FACTOR_Y);
+                                CORRECT_AXIS_Y(current_state.dy));
     }
     previous_state = current_state;
 }