N3DS: Refactor joystick module to avoid globals.

main
Pierre Wendling 2022-09-20 23:04:20 -04:00 committed by Sam Lantinga
parent 392f3882d0
commit 86a8714fea
1 changed files with 63 additions and 53 deletions

View File

@ -45,18 +45,10 @@
*/ */
#define CORRECTION_FACTOR_Y -CORRECTION_FACTOR_X #define CORRECTION_FACTOR_Y -CORRECTION_FACTOR_X
typedef struct N3DSJoystickState SDL_FORCE_INLINE void UpdateN3DSPressedButtons(SDL_Joystick *joystick);
{ SDL_FORCE_INLINE void UpdateN3DSReleasedButtons(SDL_Joystick *joystick);
u32 kDown; SDL_FORCE_INLINE void UpdateN3DSCircle(SDL_Joystick *joystick);
u32 kUp; SDL_FORCE_INLINE void UpdateN3DSCStick(SDL_Joystick *joystick);
circlePosition circlePos;
circlePosition cStickPos;
} N3DSJoystickState;
SDL_FORCE_INLINE void UpdateAxis(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
SDL_FORCE_INLINE void UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state);
static N3DSJoystickState current_state;
static int static int
N3DS_JoystickInit(void) N3DS_JoystickInit(void)
@ -110,64 +102,82 @@ N3DS_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
static void static void
N3DS_JoystickUpdate(SDL_Joystick *joystick) N3DS_JoystickUpdate(SDL_Joystick *joystick)
{ {
N3DSJoystickState previous_state = current_state; UpdateN3DSPressedButtons(joystick);
UpdateN3DSReleasedButtons(joystick);
UpdateAxis(joystick, &previous_state); UpdateN3DSCircle(joystick);
UpdateButtons(joystick, &previous_state); UpdateN3DSCStick(joystick);
} }
SDL_FORCE_INLINE void SDL_FORCE_INLINE void
UpdateAxis(SDL_Joystick *joystick, N3DSJoystickState *previous_state) UpdateN3DSPressedButtons(SDL_Joystick *joystick)
{ {
hidCircleRead(&current_state.circlePos); static u32 previous_state = 0;
if (previous_state->circlePos.dx != current_state.circlePos.dx) { u32 updated_down;
SDL_PrivateJoystickAxis(joystick, u32 current_state = hidKeysDown();
0, updated_down = previous_state ^ current_state;
current_state.circlePos.dx * CORRECTION_FACTOR_X);
}
if (previous_state->circlePos.dy != current_state.circlePos.dy) {
SDL_PrivateJoystickAxis(joystick,
1,
current_state.circlePos.dy * CORRECTION_FACTOR_Y);
}
hidCstickRead(&current_state.cStickPos);
if (previous_state->cStickPos.dx != current_state.cStickPos.dx) {
SDL_PrivateJoystickAxis(joystick,
2,
current_state.cStickPos.dx * CORRECTION_FACTOR_X);
}
if (previous_state->cStickPos.dy != current_state.cStickPos.dy) {
SDL_PrivateJoystickAxis(joystick,
3,
current_state.cStickPos.dy * CORRECTION_FACTOR_Y);
}
}
SDL_FORCE_INLINE void
UpdateButtons(SDL_Joystick *joystick, N3DSJoystickState *previous_state)
{
u32 updated_down, updated_up;
current_state.kDown = hidKeysDown();
updated_down = previous_state->kDown ^ current_state.kDown;
if (updated_down) { if (updated_down) {
for (Uint8 i = 0; i < joystick->nbuttons; i++) { for (Uint8 i = 0; i < joystick->nbuttons; i++) {
if (current_state.kDown & BIT(i) & updated_down) { if (current_state & BIT(i) & updated_down) {
SDL_PrivateJoystickButton(joystick, i, SDL_PRESSED); SDL_PrivateJoystickButton(joystick, i, SDL_PRESSED);
} }
} }
} }
previous_state = current_state;
}
current_state.kUp = hidKeysUp(); SDL_FORCE_INLINE void
updated_up = previous_state->kUp ^ current_state.kUp; UpdateN3DSReleasedButtons(SDL_Joystick *joystick)
{
static u32 previous_state = 0;
u32 updated_up;
u32 current_state = hidKeysUp();
updated_up = previous_state ^ current_state;
if (updated_up) { if (updated_up) {
for (Uint8 i = 0; i < joystick->nbuttons; i++) { for (Uint8 i = 0; i < joystick->nbuttons; i++) {
if (current_state.kUp & BIT(i) & updated_up) { if (current_state & BIT(i) & updated_up) {
SDL_PrivateJoystickButton(joystick, i, SDL_RELEASED); SDL_PrivateJoystickButton(joystick, i, SDL_RELEASED);
} }
} }
} }
previous_state = current_state;
}
SDL_FORCE_INLINE void
UpdateN3DSCircle(SDL_Joystick *joystick)
{
static circlePosition previous_state = { 0, 0 };
circlePosition current_state;
hidCircleRead(&current_state);
if (previous_state.dx != current_state.dx) {
SDL_PrivateJoystickAxis(joystick,
0,
current_state.dx * CORRECTION_FACTOR_X);
}
if (previous_state.dy != current_state.dy) {
SDL_PrivateJoystickAxis(joystick,
1,
current_state.dy * CORRECTION_FACTOR_Y);
}
previous_state = current_state;
}
SDL_FORCE_INLINE void
UpdateN3DSCStick(SDL_Joystick *joystick)
{
static circlePosition previous_state = { 0, 0 };
circlePosition current_state;
hidCstickRead(&current_state);
if (previous_state.dx != current_state.dx) {
SDL_PrivateJoystickAxis(joystick,
2,
current_state.dx * CORRECTION_FACTOR_X);
}
if (previous_state.dy != current_state.dy) {
SDL_PrivateJoystickAxis(joystick,
3,
current_state.dy * CORRECTION_FACTOR_Y);
}
previous_state = current_state;
} }
static void static void