mirror of
https://github.com/encounter/SDL.git
synced 2025-12-10 05:57:44 +00:00
Automatically assign player indexes to game controllers, and allow changing the player index for game controllers and joysticks.
Added the functions SDL_JoystickFromPlayerIndex(), SDL_JoystickSetPlayerIndex(), SDL_GameControllerFromPlayerIndex(), and SDL_GameControllerSetPlayerIndex()
This commit is contained in:
@@ -469,6 +469,15 @@ IOS_JoystickGetDevicePlayerIndex(int device_index)
|
||||
return device ? (int)device->controller.playerIndex : -1;
|
||||
}
|
||||
|
||||
static void
|
||||
IOS_JoystickSetDevicePlayerIndex(int device_index, int player_index)
|
||||
{
|
||||
SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
|
||||
if (device) {
|
||||
device->controller.playerIndex = player_index;
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_JoystickGUID
|
||||
IOS_JoystickGetDeviceGUID( int device_index )
|
||||
{
|
||||
@@ -616,7 +625,6 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
||||
GCController *controller = joystick->hwdata->controller;
|
||||
Uint8 hatstate = SDL_HAT_CENTERED;
|
||||
int i;
|
||||
int updateplayerindex = 0;
|
||||
int pause_button_index = 0;
|
||||
|
||||
if (controller.extendedGamepad) {
|
||||
@@ -670,17 +678,10 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
||||
hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
|
||||
|
||||
for (i = 0; i < SDL_arraysize(axes); i++) {
|
||||
/* The triggers (axes 2 and 5) are resting at -32768 but SDL
|
||||
* initializes its values to 0. We only want to make sure the
|
||||
* player index is up to date if the user actually moves an axis. */
|
||||
if ((i != 2 && i != 5) || axes[i] != -32768) {
|
||||
updateplayerindex |= (joystick->axes[i].value != axes[i]);
|
||||
}
|
||||
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < button_count; i++) {
|
||||
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
|
||||
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
|
||||
}
|
||||
} else if (controller.gamepad) {
|
||||
@@ -701,7 +702,6 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
||||
hatstate = IOS_MFIJoystickHatStateForDPad(gamepad.dpad);
|
||||
|
||||
for (i = 0; i < button_count; i++) {
|
||||
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
|
||||
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
|
||||
}
|
||||
}
|
||||
@@ -715,7 +715,6 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
||||
};
|
||||
|
||||
for (i = 0; i < SDL_arraysize(axes); i++) {
|
||||
updateplayerindex |= (joystick->axes[i].value != axes[i]);
|
||||
SDL_PrivateJoystickAxis(joystick, i, axes[i]);
|
||||
}
|
||||
|
||||
@@ -737,14 +736,12 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
for (i = 0; i < button_count; i++) {
|
||||
updateplayerindex |= (joystick->buttons[i] != buttons[i]);
|
||||
SDL_PrivateJoystickButton(joystick, i, buttons[i]);
|
||||
}
|
||||
}
|
||||
#endif /* TARGET_OS_TV */
|
||||
|
||||
if (joystick->nhats > 0) {
|
||||
updateplayerindex |= (joystick->hats[0] != hatstate);
|
||||
SDL_PrivateJoystickHat(joystick, 0, hatstate);
|
||||
}
|
||||
|
||||
@@ -752,31 +749,9 @@ IOS_MFIJoystickUpdate(SDL_Joystick * joystick)
|
||||
for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
|
||||
SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_PRESSED);
|
||||
SDL_PrivateJoystickButton(joystick, pause_button_index, SDL_RELEASED);
|
||||
updateplayerindex = YES;
|
||||
}
|
||||
joystick->hwdata->num_pause_presses = 0;
|
||||
}
|
||||
|
||||
if (updateplayerindex && controller.playerIndex == -1) {
|
||||
BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
|
||||
|
||||
/* Find the player index of all other connected controllers. */
|
||||
for (GCController *c in [GCController controllers]) {
|
||||
if (c != controller && c.playerIndex >= 0) {
|
||||
usedPlayerIndexSlots[c.playerIndex] = YES;
|
||||
}
|
||||
}
|
||||
|
||||
/* Set this controller's player index to the first unused index.
|
||||
* FIXME: This logic isn't great... but SDL doesn't expose this
|
||||
* concept in its external API, so we don't have much to go on. */
|
||||
for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
|
||||
if (!usedPlayerIndexSlots[i]) {
|
||||
controller.playerIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* SDL_JOYSTICK_MFI */
|
||||
}
|
||||
@@ -874,6 +849,7 @@ SDL_JoystickDriver SDL_IOS_JoystickDriver =
|
||||
IOS_JoystickDetect,
|
||||
IOS_JoystickGetDeviceName,
|
||||
IOS_JoystickGetDevicePlayerIndex,
|
||||
IOS_JoystickSetDevicePlayerIndex,
|
||||
IOS_JoystickGetDeviceGUID,
|
||||
IOS_JoystickGetDeviceInstanceID,
|
||||
IOS_JoystickOpen,
|
||||
|
||||
Reference in New Issue
Block a user