Skip to content

Commit

Permalink
Fix multiple gamepads connected causing buttons to constantly flip fl…
Browse files Browse the repository at this point in the history
…op between pressed and released, fix launcher not supporting holding gamepad buttons
  • Loading branch information
UnknownShadow200 committed Apr 29, 2024
1 parent cc18241 commit 5e5b468
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
31 changes: 21 additions & 10 deletions src/Input.c
Expand Up @@ -418,38 +418,49 @@ static void KeyBind_Init(void) {
*---------------------------------------------------------Gamepad---------------------------------------------------------*
*#########################################################################################################################*/
#define GAMEPAD_BEG_BTN CCPAD_A
#define GAMEPAD_BTN_COUNT (INPUT_COUNT - GAMEPAD_BEG_BTN)

int Gamepad_AxisBehaviour[2] = { AXIS_BEHAVIOUR_MOVEMENT, AXIS_BEHAVIOUR_CAMERA };
int Gamepad_AxisSensitivity[2] = { AXIS_SENSI_NORMAL, AXIS_SENSI_NORMAL };
static const float axis_sensiFactor[] = { 0.25f, 0.5f, 1.0f, 2.0f, 4.0f };

struct GamepadState {
float axisX[2], axisY[2];
/*cc_bool pressed[INPUT_COUNT - GAMEPAD_BEG_BTN];*/
float holdtime[INPUT_COUNT - GAMEPAD_BEG_BTN];
cc_bool pressed[GAMEPAD_BTN_COUNT];
float holdtime[GAMEPAD_BTN_COUNT];
};
static struct GamepadState gamepads[INPUT_MAX_GAMEPADS];

static void Gamepad_Update(struct GamepadState* pad, float delta) {
int btn;
for (btn = GAMEPAD_BEG_BTN; btn < INPUT_COUNT; btn++)
for (btn = 0; btn < GAMEPAD_BTN_COUNT; btn++)
{
if (!Input.Pressed[btn]) continue;
pad->holdtime[btn - GAMEPAD_BEG_BTN] += delta;
if (pad->holdtime[btn - GAMEPAD_BEG_BTN] < 1.0f) continue;
if (!pad->pressed[btn]) continue;
pad->holdtime[btn] += delta;
if (pad->holdtime[btn] < 1.0f) continue;

/* Held for over a second, trigger a fake press */
pad->holdtime[btn - GAMEPAD_BEG_BTN] = 0;
Input_SetPressed(btn);
pad->holdtime[btn] = 0;
Input_SetPressed(btn + GAMEPAD_BEG_BTN);
}
}


void Gamepad_SetButton(int port, int btn, int pressed) {
struct GamepadState* pad = &gamepads[port];
int i;
btn -= GAMEPAD_BEG_BTN;

/* Reset hold tracking time */
if (pressed && !Input.Pressed[btn]) pad->holdtime[btn - GAMEPAD_BEG_BTN] = 0;
if (pressed && !pad->pressed[btn]) pad->holdtime[btn] = 0;
pad->pressed[btn] = pressed != 0;;

/* Set pressed if button pressed on any gamepad, to avoid constant flip flopping */
/* between pressed and non-pressed when multiple controllers are plugged in */
for (i = 0; i < INPUT_MAX_GAMEPADS; i++)
pressed |= gamepads[i].pressed[btn];

Input_SetNonRepeatable(btn, pressed);
Input_SetNonRepeatable(btn + GAMEPAD_BEG_BTN, pressed);
}

void Gamepad_SetAxis(int port, int axis, float x, float y, float delta) {
Expand Down
7 changes: 4 additions & 3 deletions src/Launcher.c
Expand Up @@ -271,8 +271,9 @@ void Launcher_Run(void) {
#endif

for (;;) {
Window_ProcessEvents(10 / 1000.0);
Window_ProcessGamepads(10 / 1000.0);
Window_ProcessEvents(10 / 1000.0f);
Window_ProcessGamepads(10 / 1000.0f);
Gamepad_Tick(10 / 1000.0f);
if (!Window_Main.Exists || Launcher_ShouldExit) break;

Launcher_Active->Tick(Launcher_Active);
Expand Down Expand Up @@ -579,4 +580,4 @@ void Launcher_MakeTitleFont(struct FontDesc* font) {
Font_Make(font, 32, FONT_FLAGS_NONE);
Drawer2D.BitmappedText = false;
}
#endif
#endif

0 comments on commit 5e5b468

Please sign in to comment.