Skip to content

Commit 9886f7d

Browse files
Fix apple controller to support four players (#2009)
1 parent d79a202 commit 9886f7d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

core/base/Controller-apple.mm

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,32 @@ - (void)onControllerDisconnected:(NSNotification*)connectedNotification
140140
observerConnection:^(GCController* gcController) {
141141
auto controller = new Controller();
142142
controller->_impl->_gcController = gcController;
143-
controller->_deviceName = [gcController.vendorName UTF8String];
143+
144+
// get the player index
145+
GCControllerPlayerIndex playerIndex = gcController.playerIndex;
146+
if (playerIndex == GCControllerPlayerIndexUnset) {
147+
// apple support up to 4 players
148+
// ref: https://developer.apple.com/documentation/gamecontroller/gccontrollerplayerindex
149+
for (int i = 0; i < 4; ++i) {
150+
bool indexInUse = false;
151+
152+
for (GCController *existingController in [GCController controllers]) {
153+
if (existingController.playerIndex == i) {
154+
indexInUse = true;
155+
break;
156+
}
157+
}
158+
159+
if (!indexInUse) {
160+
gcController.playerIndex = static_cast<GCControllerPlayerIndex>(i);
161+
playerIndex = static_cast<GCControllerPlayerIndex>(i);
162+
break;
163+
}
164+
}
165+
}
166+
167+
controller->_deviceId = static_cast<int>(playerIndex);
168+
controller->_deviceName = [gcController.vendorName UTF8String];
144169

145170
s_allController.push_back(controller);
146171

0 commit comments

Comments
 (0)