Exemplo n.º 1
0
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;
			}
		}
	}
}
Exemplo n.º 2
0
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;
			}
		}
	}
}