int JoyGetPressed(joystick_t *joystick) { int cmd = 0; int i; for (i = 0; i < joystick->numButtons; i++) { int mask = 1 << i; if (JoyIsPressed(joystick, mask)) { cmd |= mask; } } return cmd; }
int GetMenuCmd(EventHandlers *handlers) { keyboard_t *kb = &handlers->keyboard; bool firstJoyPressedEsc = false; SDL_JoystickID firstJoyId = 0; if (handlers->joysticks.size > 0) { const Joystick *firstJoy = CArrayGet(&handlers->joysticks, 0); firstJoyId = firstJoy->id; firstJoyPressedEsc = JoyIsPressed(firstJoyId, CMD_ESC); } if (KeyIsPressed(kb, SDL_SCANCODE_ESCAPE) || firstJoyPressedEsc) { return CMD_ESC; } // Check first player keyboard int cmd = GetOnePlayerCmd(handlers, true, INPUT_DEVICE_KEYBOARD, 0); if (!cmd) { // Check keyboard if (KeyIsPressed(kb, SDL_SCANCODE_LEFT)) cmd |= CMD_LEFT; else if (KeyIsPressed(kb, SDL_SCANCODE_RIGHT)) cmd |= CMD_RIGHT; if (KeyIsPressed(kb, SDL_SCANCODE_UP)) cmd |= CMD_UP; else if (KeyIsPressed(kb, SDL_SCANCODE_DOWN)) cmd |= CMD_DOWN; if (KeyIsPressed(kb, SDL_SCANCODE_RETURN)) cmd |= CMD_BUTTON1; if (KeyIsPressed(kb, SDL_SCANCODE_BACKSPACE)) cmd |= CMD_BUTTON2; } if (!cmd && handlers->joysticks.size > 0) { // Check joystick 1 cmd = GetOnePlayerCmd( handlers, true, INPUT_DEVICE_JOYSTICK, firstJoyId); } if (!cmd) { // Check mouse cmd = GetOnePlayerCmd(handlers, true, INPUT_DEVICE_MOUSE, 0); } return cmd; }
static void AssignPlayerInputDevices( int hasInputDevice[MAX_PLAYERS], int numPlayers, struct PlayerData playerDatas[MAX_PLAYERS], InputDevices *inputDevices, InputConfig *inputConfig) { int i; int assignedKeyboards[MAX_KEYBOARD_CONFIGS]; int assignedMouse = 0; int assignedJoysticks[MAX_JOYSTICKS]; memset(assignedKeyboards, 0, sizeof assignedKeyboards); memset(assignedJoysticks, 0, sizeof assignedJoysticks); for (i = 0; i < numPlayers; i++) { int j; if (hasInputDevice[i]) { // Find all the assigned devices switch (playerDatas[i].inputDevice) { case INPUT_DEVICE_KEYBOARD: assignedKeyboards[playerDatas[i].deviceIndex] = 1; break; case INPUT_DEVICE_MOUSE: assignedMouse = 1; break; case INPUT_DEVICE_JOYSTICK: assignedJoysticks[playerDatas[i].deviceIndex] = 1; break; default: // do nothing break; } continue; } // Try to assign devices to players // For each unassigned player, check if any device has button 1 pressed for (j = 0; j < MAX_KEYBOARD_CONFIGS; j++) { if (KeyIsPressed( &inputDevices->keyboard, inputConfig->PlayerKeys[j].Keys.button1) && !assignedKeyboards[j]) { hasInputDevice[i] = 1; playerDatas[i].inputDevice = INPUT_DEVICE_KEYBOARD; playerDatas[i].deviceIndex = j; assignedKeyboards[j] = 1; continue; } } if (MouseIsPressed(&inputDevices->mouse, SDL_BUTTON_LEFT) && !assignedMouse) { hasInputDevice[i] = 1; playerDatas[i].inputDevice = INPUT_DEVICE_MOUSE; playerDatas[i].deviceIndex = 0; assignedMouse = 1; continue; } for (j = 0; j < inputDevices->joysticks.numJoys; j++) { if (JoyIsPressed( &inputDevices->joysticks.joys[j], CMD_BUTTON1) && !assignedJoysticks[j]) { hasInputDevice[i] = 1; playerDatas[i].inputDevice = INPUT_DEVICE_JOYSTICK; playerDatas[i].deviceIndex = j; assignedJoysticks[j] = 1; continue; } } } }
static void AssignPlayerInputDevices(EventHandlers *handlers) { bool assignedKeyboards[MAX_KEYBOARD_CONFIGS]; bool assignedMouse = false; bool assignedJoysticks[MAX_JOYSTICKS]; memset(assignedKeyboards, 0, sizeof assignedKeyboards); memset(assignedJoysticks, 0, sizeof assignedJoysticks); for (int i = 0; i < (int)gPlayerDatas.size; i++) { PlayerData *p = CArrayGet(&gPlayerDatas, i); if (!p->IsLocal) { continue; } if (p->inputDevice != INPUT_DEVICE_UNSET) { // Find all the assigned devices switch (p->inputDevice) { case INPUT_DEVICE_KEYBOARD: assignedKeyboards[p->deviceIndex] = true; break; case INPUT_DEVICE_MOUSE: assignedMouse = true; break; case INPUT_DEVICE_JOYSTICK: assignedJoysticks[p->deviceIndex] = true; break; default: // do nothing break; } continue; } // Try to assign devices to players // For each unassigned player, check if any device has button 1 pressed for (int j = 0; j < MAX_KEYBOARD_CONFIGS; j++) { char buf[256]; sprintf(buf, "Input.PlayerKeys%d.button1", j); if (KeyIsPressed(&handlers->keyboard, ConfigGetInt(&gConfig, buf)) && !assignedKeyboards[j]) { PlayerSetInputDevice(p, INPUT_DEVICE_KEYBOARD, j); assignedKeyboards[j] = true; SoundPlay(&gSoundDevice, StrSound("hahaha")); break; } } if (MouseIsPressed(&handlers->mouse, SDL_BUTTON_LEFT) && !assignedMouse) { PlayerSetInputDevice(p, INPUT_DEVICE_MOUSE, 0); assignedMouse = true; SoundPlay(&gSoundDevice, StrSound("hahaha")); continue; } for (int j = 0; j < handlers->joysticks.numJoys; j++) { if (JoyIsPressed( &handlers->joysticks.joys[j], CMD_BUTTON1) && !assignedJoysticks[j]) { PlayerSetInputDevice(p, INPUT_DEVICE_JOYSTICK, j); assignedJoysticks[j] = true; SoundPlay(&gSoundDevice, StrSound("hahaha")); break; } } } }