Skip to content

Commit

Permalink
Lock inputs after unpausing to prevent error inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Aug 11, 2024
1 parent cf5fc8b commit c52af7d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
13 changes: 11 additions & 2 deletions src/cdogs/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2015, 2018 Cong Xu
Copyright (c) 2013-2015, 2018, 2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -75,6 +75,13 @@ void JoyReset(CArray *joys)
CA_FOREACH_END()
}

void JoyLock(CArray *joys)
{
CA_FOREACH(Joystick, j, *joys)
j->lockedCmd = j->currentCmd;
CA_FOREACH_END()
}

static void JoyTerminateOne(Joystick *j)
{
SDL_GameControllerClose(j->gc);
Expand All @@ -99,7 +106,8 @@ static Joystick *GetJoystick(const SDL_JoystickID id)

bool JoyIsDown(const SDL_JoystickID id, const int cmd)
{
return !!(GetJoystick(id)->currentCmd & cmd);
const Joystick *j = GetJoystick(id);
return !!(j->currentCmd & ~j->lockedCmd & cmd);
}

bool JoyIsPressed(const SDL_JoystickID id, const int cmd)
Expand Down Expand Up @@ -277,6 +285,7 @@ static void JoyOnCmd(Joystick *j, const int cmd, const bool isDown)
j->pressedCmd |= cmd;
}
j->currentCmd &= ~cmd;
j->lockedCmd &= ~cmd;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/cdogs/joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2015, Cong Xu
Copyright (c) 2013-2015, 2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -48,10 +48,14 @@ typedef struct
int currentCmd;
int previousCmd;
int pressedCmd;
// When switching between states, lock cmds that were already down,
// and prevent isDown until they are released
int lockedCmd;
} Joystick;

void JoyInit(CArray *joys);
void JoyReset(CArray *joys);
void JoyLock(CArray *joys);
void JoyTerminate(CArray *joys);

bool JoyIsDown(const SDL_JoystickID id, const int cmd);
Expand Down
15 changes: 12 additions & 3 deletions src/cdogs/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2015, 2018, 2021 Cong Xu, davidrgmcb
Copyright (c) 2013-2015, 2018, 2021, 2024 Cong Xu, davidrgmcb
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -82,6 +82,14 @@ InputKeys KeyLoadPlayerKeys(Config *c)
return k;
}

void KeyLockKeys(keyboard_t *k)
{
memcpy(
k->lockedKeys,
k->currentKeys,
sizeof k->lockedKeys);
}

void KeyPrePoll(keyboard_t *keyboard)
{
memcpy(
Expand All @@ -106,7 +114,8 @@ void KeyOnKeyDown(keyboard_t *keyboard, const SDL_Keysym s)
}
void KeyOnKeyUp(keyboard_t *keyboard, const SDL_Keysym s)
{
keyboard->currentKeys[s.scancode].isPressed = 0;
keyboard->currentKeys[s.scancode].isPressed = false;
keyboard->lockedKeys[s.scancode].isPressed = false;
}

void DiagonalHold(keyboard_t *keyboard, int currentPlayer)
Expand Down Expand Up @@ -256,7 +265,7 @@ void KeyPostPoll(keyboard_t *keyboard, const Uint32 ticks)

bool KeyIsDown(const keyboard_t *k, const int key)
{
return k->currentKeys[key].isPressed;
return !k->lockedKeys[key].isPressed && k->currentKeys[key].isPressed;
}

bool KeyIsPressed(const keyboard_t *k, const int key)
Expand Down
6 changes: 5 additions & 1 deletion src/cdogs/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2015, 2018, 2021 Cong Xu
Copyright (c) 2013-2015, 2018, 2021, 2024 Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -87,6 +87,9 @@ typedef struct
KeyPress previousKeys[SDL_NUM_SCANCODES];
KeyPress currentKeys[SDL_NUM_SCANCODES];
KeyPress pressedKeys[SDL_NUM_SCANCODES];
// When switching between states, lock keys that were already down,
// and prevent isDown until they are released
KeyPress lockedKeys[SDL_NUM_SCANCODES];
SDL_Keymod modState;
char Typed[32];
Uint32 repeatedTicks;
Expand All @@ -97,6 +100,7 @@ typedef struct

void KeyInit(keyboard_t *keyboard);
InputKeys KeyLoadPlayerKeys(Config *c);
void KeyLockKeys(keyboard_t *k);
void KeyPrePoll(keyboard_t *keyboard);
void KeyOnKeyDown(keyboard_t *keyboard, const SDL_Keysym s);
void KeyOnKeyUp(keyboard_t *keyboard, const SDL_Keysym s);
Expand Down
3 changes: 2 additions & 1 deletion src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ static void RunGameInput(GameLoopData *data)
// Clear all user inputs if we're using the pause menu
memset(rData->cmds, 0, sizeof rData->cmds);
memset(rData->lastCmds, 0, sizeof rData->lastCmds);
// TODO: require a button up before allowing firing again
KeyLockKeys(&gEventHandlers.keyboard);
JoyLock(&gEventHandlers.joysticks);
}

// Update and check if we want to quit
Expand Down

0 comments on commit c52af7d

Please sign in to comment.