Skip to content

Commit

Permalink
Refactor/fix joystick/controller support and implement player number …
Browse files Browse the repository at this point in the history
…enumeration for joysticks (address #36)
  • Loading branch information
nightmareci committed Dec 27, 2024
1 parent ba29870 commit 3316cad
Show file tree
Hide file tree
Showing 8 changed files with 807 additions and 572 deletions.
118 changes: 59 additions & 59 deletions src/game/gamestart.c
Original file line number Diff line number Diff line change
Expand Up @@ -15671,19 +15671,19 @@ int32_t getPressState(int32_t player, EButton key) { // パッドボタン割り
case BTN_GIVEUP:
conkey.type = YGS_CONKEY_BUTTON;
conkey.index = SDL_CONTROLLER_BUTTON_BACK;
ctmp = YGS2kIsPressConPlayerKey(playerCons[pl], &conkey);
ctmp = YGS2kIsPressConKey(playerCons[pl], &conkey);
break;

case BTN_PAUSE:
conkey.type = YGS_CONKEY_BUTTON;
conkey.index = SDL_CONTROLLER_BUTTON_START;
ctmp = YGS2kIsPressConPlayerKey(playerCons[pl], &conkey);
ctmp = YGS2kIsPressConKey(playerCons[pl], &conkey);
break;

default:
ctmp = inmenu ?
IsPressMenu(pl, key, YGS2kGetConPlayerType(playerCons[pl])) :
YGS2kIsPressConPlayerKey(playerCons[pl], &conKeyAssign[key + 8 * pl]);
IsPressMenu(pl, key, YGS2kGetConType(playerCons[pl])) :
YGS2kIsPressConKey(playerCons[pl], &conKeyAssign[key + 8 * pl]);
break;
}
#endif
Expand Down Expand Up @@ -15765,19 +15765,19 @@ int32_t getPushState(int32_t player, EButton key) { // パッドボタン割り
case BTN_GIVEUP:
conkey.type = YGS_CONKEY_BUTTON;
conkey.index = SDL_CONTROLLER_BUTTON_BACK;
ctmp = YGS2kIsPushConPlayerKey(playerCons[pl], &conkey);
ctmp = YGS2kIsPushConKey(playerCons[pl], &conkey);
break;

case BTN_PAUSE:
conkey.type = YGS_CONKEY_BUTTON;
conkey.index = SDL_CONTROLLER_BUTTON_START;
ctmp = YGS2kIsPushConPlayerKey(playerCons[pl], &conkey);
ctmp = YGS2kIsPushConKey(playerCons[pl], &conkey);
break;

default:
ctmp = inmenu ?
IsPushMenu(pl, key, YGS2kGetConPlayerType(playerCons[pl])) :
YGS2kIsPushConPlayerKey(playerCons[pl], &conKeyAssign[key + 8 * pl]);
IsPushMenu(pl, key, YGS2kGetConType(playerCons[pl])) :
YGS2kIsPushConKey(playerCons[pl], &conKeyAssign[key + 8 * pl]);
break;
}
#endif
Expand Down Expand Up @@ -15817,39 +15817,39 @@ int IsPressMenu(int32_t player, EButton button, YGS2kEInputType type)
{
case BTN_UP:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 3 };
pushed = YGS2kIsPressConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPressConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_UP };
return pushed || YGS2kIsPressConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPressConKey(playerCons[player], &key);
case BTN_DOWN:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 2 };
pushed = YGS2kIsPressConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPressConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_DOWN };
return pushed || YGS2kIsPressConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPressConKey(playerCons[player], &key);
case BTN_LEFT:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 1 };
pushed = YGS2kIsPressConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPressConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_LEFT };
return pushed || YGS2kIsPressConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPressConKey(playerCons[player], &key);
case BTN_RIGHT:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 0 };
pushed = YGS2kIsPressConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPressConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_RIGHT };
return pushed || YGS2kIsPressConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPressConKey(playerCons[player], &key);
case BTN_A:
key.type = YGS_CONKEY_BUTTON;
if (type == YGS_INPUT_NINTENDO) key.index = SDL_CONTROLLER_BUTTON_B;
else key.index = SDL_CONTROLLER_BUTTON_A;
return YGS2kIsPressConPlayerKey(playerCons[player], &key);
return YGS2kIsPressConKey(playerCons[player], &key);
case BTN_B:
key.type = YGS_CONKEY_BUTTON;
if (type == YGS_INPUT_NINTENDO) key.index = SDL_CONTROLLER_BUTTON_A;
else key.index = SDL_CONTROLLER_BUTTON_B;
return YGS2kIsPressConPlayerKey(playerCons[player], &key);
return YGS2kIsPressConKey(playerCons[player], &key);
break;
case BTN_C: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_X; return YGS2kIsPressConPlayerKey(playerCons[player], &key);
case BTN_D: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_Y; return YGS2kIsPressConPlayerKey(playerCons[player], &key);
case BTN_GIVEUP: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_BACK; return YGS2kIsPressConPlayerKey(playerCons[player], &key);
case BTN_PAUSE: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_START; return YGS2kIsPressConPlayerKey(playerCons[player], &key);
case BTN_C: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_X; return YGS2kIsPressConKey(playerCons[player], &key);
case BTN_D: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_Y; return YGS2kIsPressConKey(playerCons[player], &key);
case BTN_GIVEUP: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_BACK; return YGS2kIsPressConKey(playerCons[player], &key);
case BTN_PAUSE: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_START; return YGS2kIsPressConKey(playerCons[player], &key);
default: return 0;
}
break;
Expand Down Expand Up @@ -15880,39 +15880,39 @@ int IsPushMenu(int32_t player, EButton button, YGS2kEInputType type)
{
case BTN_UP:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 3 };
pushed = YGS2kIsPushConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPushConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_UP };
return pushed || YGS2kIsPushConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPushConKey(playerCons[player], &key);
case BTN_DOWN:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 2 };
pushed = YGS2kIsPushConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPushConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_DOWN };
return pushed || YGS2kIsPushConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPushConKey(playerCons[player], &key);
case BTN_LEFT:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 1 };
pushed = YGS2kIsPushConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPushConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_LEFT };
return pushed || YGS2kIsPushConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPushConKey(playerCons[player], &key);
case BTN_RIGHT:
key = (YGS2kSConKey) { .type = YGS_CONKEY_AXIS, .index = 0 };
pushed = YGS2kIsPushConPlayerKey(playerCons[player], &key);
pushed = YGS2kIsPushConKey(playerCons[player], &key);
key = (YGS2kSConKey) { .type = YGS_CONKEY_BUTTON, .index = SDL_CONTROLLER_BUTTON_DPAD_RIGHT };
return pushed || YGS2kIsPushConPlayerKey(playerCons[player], &key);
return pushed || YGS2kIsPushConKey(playerCons[player], &key);
case BTN_A:
key.type = YGS_CONKEY_BUTTON;
if (type == YGS_INPUT_NINTENDO) key.index = SDL_CONTROLLER_BUTTON_B;
else key.index = SDL_CONTROLLER_BUTTON_A;
return YGS2kIsPushConPlayerKey(playerCons[player], &key);
return YGS2kIsPushConKey(playerCons[player], &key);
case BTN_B:
key.type = YGS_CONKEY_BUTTON;
if (type == YGS_INPUT_NINTENDO) key.index = SDL_CONTROLLER_BUTTON_A;
else key.index = SDL_CONTROLLER_BUTTON_B;
return YGS2kIsPushConPlayerKey(playerCons[player], &key);
return YGS2kIsPushConKey(playerCons[player], &key);
break;
case BTN_C: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_X; return YGS2kIsPushConPlayerKey(playerCons[player], &key);
case BTN_D: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_Y; return YGS2kIsPushConPlayerKey(playerCons[player], &key);
case BTN_GIVEUP: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_BACK; return YGS2kIsPushConPlayerKey(playerCons[player], &key);
case BTN_PAUSE: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_START; return YGS2kIsPushConPlayerKey(playerCons[player], &key);
case BTN_C: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_X; return YGS2kIsPushConKey(playerCons[player], &key);
case BTN_D: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_Y; return YGS2kIsPushConKey(playerCons[player], &key);
case BTN_GIVEUP: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_BACK; return YGS2kIsPushConKey(playerCons[player], &key);
case BTN_PAUSE: key.type = YGS_CONKEY_BUTTON; key.index = SDL_CONTROLLER_BUTTON_START; return YGS2kIsPushConKey(playerCons[player], &key);
default: return 0;
}
break;
Expand Down Expand Up @@ -15941,8 +15941,8 @@ int IsPressConTypeKey(YGS2kEInputType type, YGS2kSConKey* key)
return 0;
}

for (int conPlayer = 0; conPlayer < YGS2kGetNumConPlayers(); conPlayer++) {
if (YGS2kGetConPlayerType(conPlayer) == type && YGS2kIsPressConPlayerKey(conPlayer, key)) return 1;
for (int player = 0; player < YGS2kGetNumPlayerSlots(); player++) {
if (YGS2kGetConType(player) == type && YGS2kIsPressConKey(player, key)) return 1;
}
return 0;
}
Expand All @@ -15959,8 +15959,8 @@ int IsPushConTypeKey(YGS2kEInputType type, YGS2kSConKey* key)
return 0;
}

for (int conPlayer = 0; conPlayer < YGS2kGetNumConPlayers(); conPlayer++) {
if (YGS2kGetConPlayerType(conPlayer) == type && YGS2kIsPushConPlayerKey(conPlayer, key)) return 1;
for (int player = 0; player < YGS2kGetNumPlayerSlots(); player++) {
if (YGS2kGetConType(player) == type && YGS2kIsPushConKey(player, key)) return 1;
}
return 0;
}
Expand Down Expand Up @@ -17459,11 +17459,11 @@ void spriteTime() {
case YGS_INPUT_XBOX:
case YGS_INPUT_PLAYSTATION:
case YGS_INPUT_NINTENDO: {
int conPlayer = YGS2kGetLastActiveConPlayer();
if (conPlayer == playerCons[0]) {
int player = YGS2kGetLastActiveCon();
if (player == playerCons[0]) {
lastPlayerInputType[0] = lastInputType;
}
else if (conPlayer == playerCons[1]) {
else if (player == playerCons[1]) {
lastPlayerInputType[1] = lastInputType;
}
break;
Expand Down Expand Up @@ -17518,16 +17518,16 @@ void spriteTime() {

#ifdef ENABLE_GAME_CONTROLLER
if (inmenu) {
up = up || IsPushMenu(pl, BTN_UP, YGS2kGetConPlayerType(playerCons[pl]));
down = down || IsPushMenu(pl, BTN_DOWN, YGS2kGetConPlayerType(playerCons[pl]));
left = left || IsPushMenu(pl, BTN_LEFT, YGS2kGetConPlayerType(playerCons[pl]));
right = right || IsPushMenu(pl, BTN_RIGHT, YGS2kGetConPlayerType(playerCons[pl]));
up = up || IsPushMenu(pl, BTN_UP, YGS2kGetConType(playerCons[pl]));
down = down || IsPushMenu(pl, BTN_DOWN, YGS2kGetConType(playerCons[pl]));
left = left || IsPushMenu(pl, BTN_LEFT, YGS2kGetConType(playerCons[pl]));
right = right || IsPushMenu(pl, BTN_RIGHT, YGS2kGetConType(playerCons[pl]));
}
else {
up = up || YGS2kIsPushConPlayerKey(playerCons[pl], &conKeyAssign[BTN_UP + 8 * pl]);
down = down || YGS2kIsPushConPlayerKey(playerCons[pl], &conKeyAssign[BTN_DOWN + 8 * pl]);
left = left || YGS2kIsPushConPlayerKey(playerCons[pl], &conKeyAssign[BTN_LEFT + 8 * pl]);
right = right || YGS2kIsPushConPlayerKey(playerCons[pl], &conKeyAssign[BTN_RIGHT + 8 * pl]);
up = up || YGS2kIsPushConKey(playerCons[pl], &conKeyAssign[BTN_UP + 8 * pl]);
down = down || YGS2kIsPushConKey(playerCons[pl], &conKeyAssign[BTN_DOWN + 8 * pl]);
left = left || YGS2kIsPushConKey(playerCons[pl], &conKeyAssign[BTN_LEFT + 8 * pl]);
right = right || YGS2kIsPushConKey(playerCons[pl], &conKeyAssign[BTN_RIGHT + 8 * pl]);
}
#endif

Expand Down Expand Up @@ -17568,16 +17568,16 @@ void spriteTime() {

#ifdef ENABLE_GAME_CONTROLLER
if (inmenu) {
up = up || IsPressMenu(pl, BTN_UP, YGS2kGetConPlayerType(playerCons[pl]));
down = down || IsPressMenu(pl, BTN_DOWN, YGS2kGetConPlayerType(playerCons[pl]));
left = left || IsPressMenu(pl, BTN_LEFT, YGS2kGetConPlayerType(playerCons[pl]));
right = right || IsPressMenu(pl, BTN_RIGHT, YGS2kGetConPlayerType(playerCons[pl]));
up = up || IsPressMenu(pl, BTN_UP, YGS2kGetConType(playerCons[pl]));
down = down || IsPressMenu(pl, BTN_DOWN, YGS2kGetConType(playerCons[pl]));
left = left || IsPressMenu(pl, BTN_LEFT, YGS2kGetConType(playerCons[pl]));
right = right || IsPressMenu(pl, BTN_RIGHT, YGS2kGetConType(playerCons[pl]));
}
else {
up = up || YGS2kIsPressConPlayerKey(playerCons[pl], &conKeyAssign[BTN_UP + 8 * pl]);
down = down || YGS2kIsPressConPlayerKey(playerCons[pl], &conKeyAssign[BTN_DOWN + 8 * pl]);
left = left || YGS2kIsPressConPlayerKey(playerCons[pl], &conKeyAssign[BTN_LEFT + 8 * pl]);
right = right || YGS2kIsPressConPlayerKey(playerCons[pl], &conKeyAssign[BTN_RIGHT + 8 * pl]);
up = up || YGS2kIsPressConKey(playerCons[pl], &conKeyAssign[BTN_UP + 8 * pl]);
down = down || YGS2kIsPressConKey(playerCons[pl], &conKeyAssign[BTN_DOWN + 8 * pl]);
left = left || YGS2kIsPressConKey(playerCons[pl], &conKeyAssign[BTN_LEFT + 8 * pl]);
right = right || YGS2kIsPressConKey(playerCons[pl], &conKeyAssign[BTN_RIGHT + 8 * pl]);
}
#endif

Expand Down
30 changes: 8 additions & 22 deletions src/main_sdl/ygs2kfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,16 +392,12 @@ bool YGS2kHalt()

/* イベント処理 || Process events */

#ifdef ENABLE_JOYSTICK
bool joysChanged = false;
#endif

#ifdef ENABLE_GAME_CONTROLLER
bool consChanged = false;
#endif
SDL_PumpEvents();
SDL_Event ev;
int showCursor = SDL_DISABLE;
#if defined(ENABLE_JOYSTICK) || defined(ENABLE_GAME_CONTROLLER)
bool slotsChanged = false;
#endif
while (SDL_PeepEvents(&ev, 1, SDL_GETEVENT, 0, SDL_LASTEVENT) == 1)
{
switch(ev.type){
Expand All @@ -418,13 +414,13 @@ bool YGS2kHalt()
#ifdef ENABLE_JOYSTICK
case SDL_JOYDEVICEADDED:
case SDL_JOYDEVICEREMOVED:
joysChanged = true;
slotsChanged = true;
#endif

#ifdef ENABLE_GAME_CONTROLLER
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
consChanged = true;
slotsChanged = true;
break;
#endif

Expand All @@ -451,19 +447,9 @@ bool YGS2kHalt()
SDL_ShowCursor(SDL_DISABLE);
}

#ifdef ENABLE_JOYSTICK
if (joysChanged)
{
YGS2kJoysClose();
if (!YGS2kJoysOpen()) {
YGS2kExit(EXIT_FAILURE);
}
}
#endif

#ifdef ENABLE_GAME_CONTROLLER
if (consChanged) {
if (!YGS2kConsChanged()) {
#if defined(ENABLE_JOYSTICK) || defined(ENABLE_GAME_CONTROLLER)
if (slotsChanged) {
if (!YGS2kPlayerSlotsChanged()) {
YGS2kExit(EXIT_FAILURE);
}
}
Expand Down
Loading

0 comments on commit 3316cad

Please sign in to comment.