Пример #1
0
bool EventIsEscape(
	EventHandlers *handlers,
	const int cmds[MAX_LOCAL_PLAYERS], const int menuCmd)
{
	for (int i = 0; i < MAX_LOCAL_PLAYERS; i++)
	{
		if (cmds[i] & CMD_ESC)
		{
			return true;
		}
	}

	// Check keyboard escape
	if (KeyIsPressed(&handlers->keyboard, SDL_SCANCODE_ESCAPE) ||
		handlers->HasQuit)
	{
		return true;
	}

	// Check menu commands
	if (menuCmd & CMD_ESC)
	{
		return true;
	}

	return false;
}
Пример #2
0
bool
cmd_is_active (enum _cmds cmd)
{
  if (cmd >= CMD_LAST)
    {
      DebugPrintf (0, "ERROR: Illegal command '%d'\n", cmd);
      Terminate (ERR);
    }

  if ( KeyIsPressed( key_cmds[cmd][0] ) || 
       KeyIsPressed( key_cmds[cmd][1] ) || 
       KeyIsPressed( key_cmds[cmd][2] ))
    return (TRUE);
  else
    return (FALSE);

} // cmd_is_active()
Пример #3
0
int NumPlayersSelection(
	int *numPlayers, campaign_mode_e mode,
	GraphicsDevice *graphics, InputDevices *input)
{
	MenuSystem ms;
	int i;
	int res = 0;
	MenuSystemInit(
		&ms, input, graphics,
		Vec2iZero(),
		Vec2iNew(
			graphics->cachedConfig.ResolutionWidth,
			graphics->cachedConfig.ResolutionHeight));
	ms.root = ms.current = MenuCreateNormal(
		"",
		"Select number of players",
		MENU_TYPE_NORMAL,
		0);
	for (i = 0; i < MAX_PLAYERS; i++)
	{
		if (mode == CAMPAIGN_MODE_DOGFIGHT && i == 0)
		{
			// At least two players for dogfights
			continue;
		}
		char buf[2];
		sprintf(buf, "%d", i + 1);
		MenuAddSubmenu(ms.current, MenuCreateReturn(buf, i + 1));
	}
	MenuAddExitType(&ms, MENU_TYPE_RETURN);

	for (;;)
	{
		int cmd;
		InputPoll(&gInputDevices, SDL_GetTicks());
		if (KeyIsPressed(&gInputDevices.keyboard, SDLK_ESCAPE))
		{
			res = 0;
			break;	// hack to allow exit
		}
		cmd = GetMenuCmd(gPlayerDatas);
		MenuProcessCmd(&ms, cmd);
		if (MenuIsExit(&ms))
		{
			*numPlayers = ms.current->u.returnCode;
			res = 1;
			break;
		}

		GraphicsBlitBkg(graphics);
		MenuDisplay(&ms);
		BlitFlip(graphics, &gConfig.Graphics);
		SDL_Delay(10);
	}

	MenuSystemTerminate(&ms);
	return res;
}
Пример #4
0
// does the same as KeyIsPressed, but automatically releases the key as well..
bool
KeyIsPressedR (SDLKey key)
{
  bool ret;

  ret = KeyIsPressed (key);

  ReleaseKey (key);
  return (ret);
}
Пример #5
0
SDL_Scancode KeyGetPressed(const keyboard_t *k)
{
	for (SDL_Scancode i = 0; i < SDL_NUM_SCANCODES; i++)
	{
		if (KeyIsPressed(k, i))
		{
			return i;
		}
	}
	return 0;
}
Пример #6
0
int KeyGetPressed(keyboard_t *keyboard)
{
	int i;
	for (i = 0; i < 512; i++)
	{
		if (KeyIsPressed(keyboard, i))
		{
			return i;
		}
	}
	return 0;
}
Пример #7
0
int KeyGetTyped(keyboard_t *keyboard)
{
	int i;
	for (i = 0; i < 128; i++)
	{
		Uint16 unicode = keyboard->currentKeys[i].unicode;
		if (KeyIsPressed(keyboard, i) &&
			unicode >= (Uint16)' ' &&
			unicode <= (Uint16)'~')
		{
			return unicode;
		}
	}
	return 0;
}
Пример #8
0
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;
}
Пример #9
0
int PlayerEquip(void *bkg)
{
	int done1 = 0, done2;

	debug(D_NORMAL, "\n");

	done2 = gOptions.twoPlayers ? 0 : 1;
	while (!done1 || !done2)
	{
		int cmd1 = 0;
		int cmd2 = 0;
		InputPoll(&gJoysticks, &gKeyboard);
		memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
		GetPlayerCmd(&cmd1, &cmd2, 1);

		if (KeyIsPressed(&gKeyboard, keyEsc)) return 0; // hack to exit from menu

		if (gOptions.twoPlayers)
		{
			done1 = !WeaponSelection(CenterOfLeft(50), CHARACTER_PLAYER1, &gPlayer1Data, cmd1, done1);
			ShowSelection(CenterOfLeft(50), &gPlayer1Data,CHARACTER_PLAYER1);
			ShowPlayerControls(CenterOfLeft(100), &gConfig.Input.PlayerKeys[0]);

			done2 = !WeaponSelection(CenterOfRight(50), CHARACTER_PLAYER2, &gPlayer2Data, cmd2, done2);
			ShowSelection(CenterOfRight(50), &gPlayer2Data, CHARACTER_PLAYER2);
			ShowPlayerControls(CenterOfRight(100), &gConfig.Input.PlayerKeys[1]);
		}
		else
		{
			done1 = !WeaponSelection(CenterX(80), CHARACTER_PLAYER1, &gPlayer1Data, cmd1, done1);
			ShowSelection(CenterX(80), &gPlayer1Data, CHARACTER_PLAYER1);
			ShowPlayerControls(CenterX(100), &gConfig.Input.PlayerKeys[0]);
		}

		CopyToScreen();
		SDL_Delay(10);
	}

	return 1;
}
Пример #10
0
int PlayerSelection(int twoPlayers, void *bkg)
{
	int mode1, mode2;

	mode1 = MODE_MAIN;
	mode2 = twoPlayers ? MODE_MAIN : MODE_DONE;

	SetPlayer(0, &gPlayer1Data);
	SetPlayer(1, &gPlayer2Data);

	KeyInit(&gKeyboard);
	while (mode1 != MODE_DONE || mode2 != MODE_DONE)
	{
		int cmd1 = 0;
		int cmd2 = 0;
		InputPoll(&gJoysticks, &gKeyboard);
		memcpy(GetDstScreen(), bkg, GraphicsGetMemSize(&gGraphicsDevice.cachedConfig));
		GetPlayerCmd(&cmd1, &cmd2, 1);

		if (KeyIsPressed(&gKeyboard, keyEsc)) return 0; // hack to allow exit

		if (twoPlayers)
		{
			mode1 = MakeSelection(mode1, CenterOfLeft(50), CHARACTER_PLAYER1, &gPlayer1Data, cmd1);
			mode2 = MakeSelection(mode2, CenterOfRight(50), CHARACTER_PLAYER2, &gPlayer2Data, cmd2);
		}
		else
		{
			mode1 = MakeSelection(mode1, CenterX(50), CHARACTER_PLAYER1, &gPlayer1Data, cmd1);
		}

		CopyToScreen();
		SDL_Delay(10);
	}

	return 1;
}
Пример #11
0
void NPServerProcessInput (void)
{
	static uint8	header = 0;
	static uint32	pos    = 0;
	KeyMap  		myKeys;

	if (npserver.phasecount == 0)
	{
		for (int c = 1; c <= NP_MAX_CLIENTS; c++)
			if (npplayer[c].ready)
				sem_wait(sememu[c]);

		for (uint32 j = 0; j < npserver.phasespan; j++)
			nprecvpad[0][j] = npcachpad[j];

		for (int i = 0; i < npserver.numplayers; i++)
		{
			for (uint32 j = 0; j < npserver.phasespan; j++)
			{
				npactvpad[i][j] = npsendpad[i][j];
				npsendpad[i][j] = nprecvpad[i][j];
			}
		}

		if (npserver.header & 0x80)
		{
			for (int i = 0; i < npserver.numplayers; i++)
			{
				npactvpad[i][npserver.phasespan] = 0;
				npsendpad[i][npserver.phasespan] = 0;
			}

			npserver.phasespan++;
			if (npserver.phasespan > (uint32) Memory.ROMFramesPerSecond)
				npserver.phasespan = (uint32) Memory.ROMFramesPerSecond;

			char	str[256];
			sprintf(str, "delay: %d", npserver.phasespan);
			S9xMessage(0, 0, str);
		}
		else
		if (npserver.header & 0x40)
		{
			npserver.phasespan--;
			if (npserver.phasespan == 0)
				npserver.phasespan = 1;

			char	str[256];
			sprintf(str, "delay: %d", npserver.phasespan);
			S9xMessage(0, 0, str);
		}

		npserver.header = header;
		header = 0;
		pos = 0;
	}

 	for (int i = 0; i < npserver.numplayers; i++)
	{
		controlPad[i] = npactvpad[i][pos];
		ControlPadFlagsToS9xReportButtons(i, controlPad[i]);
	}

	GetKeys(myKeys);

	if (ISpKeyIsPressed(kISpEsc) || KeyIsPressed(myKeys, keyCode[kKeyEsc]))
	{
		if (s9xthreadrunning)
		{
			if (!eventQueued)
			{
				PostQueueToSubEventLoop();
				eventQueued = true;
			}
		}
		else
			running = false;

		return;
	}

	uint32	pad = 0;

	JoypadScanDirection(0, &pad);
	if (ISpKeyIsPressed(kISp1PR     ))	pad |= 0x0010;
	if (ISpKeyIsPressed(kISp1PL     ))	pad |= 0x0020;
	if (ISpKeyIsPressed(kISp1PX     ))	pad |= 0x0040;
	if (ISpKeyIsPressed(kISp1PA     ))	pad |= 0x0080;
	if (ISpKeyIsPressed(kISp1PStart ))	pad |= 0x1000;
	if (ISpKeyIsPressed(kISp1PSelect))	pad |= 0x2000;
	if (ISpKeyIsPressed(kISp1PY     ))	pad |= 0x4000;
	if (ISpKeyIsPressed(kISp1PB     ))	pad |= 0x8000;

	if (KeyIsPressed(myKeys, keyCode[k1PR]     ))	pad |= 0x0010;
	if (KeyIsPressed(myKeys, keyCode[k1PL]     ))	pad |= 0x0020;
	if (KeyIsPressed(myKeys, keyCode[k1PX]     ))	pad |= 0x0040;
	if (KeyIsPressed(myKeys, keyCode[k1PA]     ))	pad |= 0x0080;
	if (KeyIsPressed(myKeys, keyCode[k1PRight] ))	pad |= 0x0100;
	if (KeyIsPressed(myKeys, keyCode[k1PLeft]  ))	pad |= 0x0200;
	if (KeyIsPressed(myKeys, keyCode[k1PDown]  ))	pad |= 0x0400;
	if (KeyIsPressed(myKeys, keyCode[k1PUp]    ))	pad |= 0x0800;
	if (KeyIsPressed(myKeys, keyCode[k1PStart] ))	pad |= 0x1000;
	if (KeyIsPressed(myKeys, keyCode[k1PSelect]))	pad |= 0x2000;
	if (KeyIsPressed(myKeys, keyCode[k1PY]     ))	pad |= 0x4000;
	if (KeyIsPressed(myKeys, keyCode[k1PB]     ))	pad |= 0x8000;

	npcachpad[pos] = pad;

	if (KeyIsPressed(myKeys, keyCode[k2PR]))	header |= 0x80;
	if (KeyIsPressed(myKeys, keyCode[k2PL]))	header |= 0x40;

	if (npserver.phasecount == 0)
	{
		npserver.phasecount = npserver.phasespan;

		for (int c = 1; c <= NP_MAX_CLIENTS; c++)
			if (npplayer[c].ready)
				sem_post(sempad[c]);
	}

	npserver.phasecount--;
	pos++;
}
Пример #12
0
int main(int argc, char *argv[])
{
	UNUSED(argc);
	UNUSED(argv);

	if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) != 0)
	{
		fprintf(stderr, "Could not initialise SDL: %s\n", SDL_GetError());
		return -1;
	}
	if (SDLNet_Init() == -1)
	{
		fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
		exit(EXIT_FAILURE);
	}
	if (!SDL_SetVideoMode(320, 200, 0, 0))
	{
		fprintf(stderr, "Could not set video mode: %s\n", SDL_GetError());
		SDL_Quit();
		exit(-1);
	}

	ConfigLoadDefault(&gConfig);
	ConfigLoad(&gConfig, GetConfigFilePath(CONFIG_FILE));
	EventInit(&gEventHandlers, NULL, false);

	NetInputClient client;
	NetInputClientInit(&client);
	NetInputClientConnect(&client, 0x7F000001);	// localhost

	printf("Press esc to exit\n");

	Uint32 ticksNow = SDL_GetTicks();
	Uint32 ticksElapsed = 0;
	for (;;)
	{
		Uint32 ticksThen = ticksNow;
		ticksNow = SDL_GetTicks();
		ticksElapsed += ticksNow - ticksThen;
		if (ticksElapsed < 1000 / FPS_FRAMELIMIT)
		{
			SDL_Delay(1);
			debug(D_VERBOSE, "Delaying 1 ticksNow %u elapsed %u\n", ticksNow, ticksElapsed);
			continue;
		}
		EventPoll(&gEventHandlers, SDL_GetTicks());
		int cmd = GetOnePlayerCmd(
			&gEventHandlers,
			&gConfig.Input.PlayerKeys[0],
			false,
			INPUT_DEVICE_KEYBOARD,
			0);
		if (cmd)
		{
			printf("Sending %s + %s\n",
				CmdStr(cmd & (CMD_LEFT | CMD_RIGHT | CMD_UP | CMD_DOWN)),
				CmdStr(cmd & (CMD_BUTTON1 | CMD_BUTTON2 | CMD_BUTTON3 | CMD_BUTTON4 | CMD_ESC)));
			NetInputClientSend(&client, cmd);
		}

		// Check keyboard escape
		if (KeyIsPressed(&gEventHandlers.keyboard, SDLK_ESCAPE))
		{
			break;
		}

		ticksElapsed -= 1000 / FPS_FRAMELIMIT;
	}

	NetInputClientTerminate(&client);
	EventTerminate(&gEventHandlers);
	SDLNet_Quit();
	SDL_Quit();
	return 0;
}
Пример #13
0
void NPClientProcessInput(void)
{
	KeyMap			myKeys;	
	static uint32	pos = 0;
	
	#define KeyIsPressed(km, k)	(1 & (((unsigned char *) km) [(k) >> 3] >> ((k) & 7)))
	
	if (npclient.exitsgn)
	{
		if (s9xthreadrunning)
		{
			if (!eventQueued)
			{
				PostQueueToSubEventLoop();
				eventQueued = true;
			}
		}
		else
			running = false;

		return;
	}
	
	if (npclient.phasecount == 0)
	{
		sem_wait(csememu);

		for (int i = 0; i < npclient.numplayers; i++)
			for (uint32 j = 0; j < npclient.phasespan; j++)
				npcactvpad[i][j] = npcrecvpad[i][j];
				
		for (uint32 j = 0; j < npclient.phasespan; j++)
			npcsendpad[j] = npccachpad[j];
		
		if (npclient.header & 0x80)
		{
			npcsendpad[npclient.phasespan] = 0;
			for (int i = 0; i < npclient.numplayers; i++)
				npcactvpad[i][npclient.phasespan] = 0;
						
			npclient.phasespan++;
			if (npclient.phasespan > (uint32) Memory.ROMFramesPerSecond)
				npclient.phasespan = (uint32) Memory.ROMFramesPerSecond;
				
			char	str[256];
			sprintf(str, "delay: %d", npclient.phasespan);
			S9xMessage(0, 0, str);
		}
		else
		if (npclient.header & 0x40)
		{
			npclient.phasespan--;
			if (npclient.phasespan == 0)
				npclient.phasespan = 1;

			char	str[256];
			sprintf(str, "delay: %d", npclient.phasespan);
			S9xMessage(0, 0, str);
		}

		npclient.header = 0;
		pos = 0;
	}

	for (int i = 0; i < npclient.numplayers; i++)
	{
 		controlPad[i] = npcactvpad[i][pos];
		ControlPadFlagsToS9xReportButtons(i, controlPad[i]);
	}

	GetKeys(myKeys);
	
	uint32  pad = 0;

	JoypadScanDirection(0, &pad);
	if (ISpKeyIsPressed(kISp1PR     ))	pad |= 0x0010;
	if (ISpKeyIsPressed(kISp1PL     ))	pad |= 0x0020;
	if (ISpKeyIsPressed(kISp1PX     ))	pad |= 0x0040;
	if (ISpKeyIsPressed(kISp1PA     ))	pad |= 0x0080;
	if (ISpKeyIsPressed(kISp1PStart ))	pad |= 0x1000;
	if (ISpKeyIsPressed(kISp1PSelect))	pad |= 0x2000;
	if (ISpKeyIsPressed(kISp1PY     ))	pad |= 0x4000;
	if (ISpKeyIsPressed(kISp1PB     ))	pad |= 0x8000;

	if (KeyIsPressed(myKeys, keyCode[k1PR]     ))	pad |= 0x0010;
	if (KeyIsPressed(myKeys, keyCode[k1PL]     ))	pad |= 0x0020;
	if (KeyIsPressed(myKeys, keyCode[k1PX]     ))	pad |= 0x0040;
	if (KeyIsPressed(myKeys, keyCode[k1PA]     ))	pad |= 0x0080;
	if (KeyIsPressed(myKeys, keyCode[k1PRight] ))	pad |= 0x0100;
	if (KeyIsPressed(myKeys, keyCode[k1PLeft]  ))	pad |= 0x0200;
	if (KeyIsPressed(myKeys, keyCode[k1PDown]  ))	pad |= 0x0400;
	if (KeyIsPressed(myKeys, keyCode[k1PUp]    ))	pad |= 0x0800;
	if (KeyIsPressed(myKeys, keyCode[k1PStart] ))	pad |= 0x1000;
	if (KeyIsPressed(myKeys, keyCode[k1PSelect]))	pad |= 0x2000;
	if (KeyIsPressed(myKeys, keyCode[k1PY]     ))	pad |= 0x4000;
	if (KeyIsPressed(myKeys, keyCode[k1PB]     ))	pad |= 0x8000;
	
	npccachpad[pos] = pad;

	if (npclient.phasecount == 0)
	{
		npclient.phasecount = npclient.phasespan;
		sem_post(csempad);
	}

	npclient.phasecount--;
	pos++;
}
Пример #14
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;
			}
		}
	}
}
Пример #15
0
int PlayerEquip(int numPlayers, GraphicsDevice *graphics)
{
	int i;
	WeaponMenu menus[MAX_PLAYERS];
	for (i = 0; i < numPlayers; i++)
	{
		WeaponMenuCreate(
			&menus[i], numPlayers, i,
			&gCampaign.Setting.characters.players[i], &gPlayerDatas[i],
			&gInputDevices, graphics, &gConfig.Input);
	}

	debug(D_NORMAL, "\n");

	for (;;)
	{
		int cmds[MAX_PLAYERS];
		int isDone = 1;
		InputPoll(&gInputDevices, SDL_GetTicks());
		if (KeyIsPressed(&gInputDevices.keyboard, SDLK_ESCAPE))
		{
			return 0; // hack to exit from menu
		}
		GetPlayerCmds(&cmds, gPlayerDatas);
		for (i = 0; i < numPlayers; i++)
		{
			if (!MenuIsExit(&menus[i].ms))
			{
				MenuProcessCmd(&menus[i].ms, cmds[i]);
			}
			else if (gPlayerDatas[i].weaponCount == 0)
			{
				// Check exit condition; must have selected at least one weapon
				// Otherwise reset the current menu
				menus[i].ms.current = menus[i].ms.root;
			}
		}
		for (i = 0; i < numPlayers; i++)
		{
			if (!MenuIsExit(&menus[i].ms))
			{
				isDone = 0;
			}
		}
		if (isDone)
		{
			break;
		}

		GraphicsBlitBkg(graphics);
		for (i = 0; i < numPlayers; i++)
		{
			MenuDisplay(&menus[i].ms);
		}
		BlitFlip(graphics, &gConfig.Graphics);
		SDL_Delay(10);
	}

	for (i = 0; i < numPlayers; i++)
	{
		MenuSystemTerminate(&menus[i].ms);
	}
	return 1;
}
Пример #16
0
int PlayerSelection(int numPlayers, GraphicsDevice *graphics)
{
	int i;
	int hasInputDevice[MAX_PLAYERS];
	PlayerSelectMenu menus[MAX_PLAYERS];
	for (i = 0; i < numPlayers; i++)
	{
		PlayerSelectMenusCreate(
			&menus[i], numPlayers, i,
			&gCampaign.Setting.characters.players[i], &gPlayerDatas[i],
			&gInputDevices, graphics, &gConfig.Input);
		hasInputDevice[i] = 0;
	}

	KeyInit(&gInputDevices.keyboard);
	for (;;)
	{
		int cmds[MAX_PLAYERS];
		int isDone = 1;
		InputPoll(&gInputDevices, SDL_GetTicks());
		if (KeyIsPressed(&gInputDevices.keyboard, SDLK_ESCAPE))
		{
			// TODO: destroy menus
			return 0; // hack to allow exit
		}
		GetPlayerCmds(&cmds, gPlayerDatas);
		for (i = 0; i < numPlayers; i++)
		{
			if (hasInputDevice[i] && !MenuIsExit(&menus[i].ms))
			{
				MenuProcessCmd(&menus[i].ms, cmds[i]);
			}
		}
		for (i = 0; i < numPlayers; i++)
		{
			if (!MenuIsExit(&menus[i].ms))
			{
				isDone = 0;
			}
		}
		if (isDone)
		{
			break;
		}

		AssignPlayerInputDevices(
			hasInputDevice, numPlayers,
			gPlayerDatas, &gInputDevices, &gConfig.Input);

		GraphicsBlitBkg(graphics);
		for (i = 0; i < numPlayers; i++)
		{
			if (hasInputDevice[i])
			{
				MenuDisplay(&menus[i].ms);
			}
			else
			{
				Vec2i center;
				const char *prompt = "Press Fire to join...";
				Vec2i offset = Vec2iScaleDiv(TextGetSize(prompt), -2);
				int w = graphics->cachedConfig.ResolutionWidth;
				int h = graphics->cachedConfig.ResolutionHeight;
				switch (numPlayers)
				{
				case 1:
					// Center of screen
					center = Vec2iNew(w / 2, h / 2);
					break;
				case 2:
					// Side by side
					center = Vec2iNew(i * w / 2 + w / 4, h / 2);
					break;
				case 3:
				case 4:
					// Four corners
					center = Vec2iNew(
						(i & 1) * w / 2 + w / 4, (i / 2) * h / 2 + h / 4);
					break;
				default:
					assert(0 && "not implemented");
					break;
				}
				DrawTextString(prompt, graphics, Vec2iAdd(center, offset));
			}
		}
		BlitFlip(graphics, &gConfig.Graphics);
		SDL_Delay(10);
	}

	for (i = 0; i < numPlayers; i++)
	{
		MenuSystemTerminate(&menus[i].ms);
	}
	return 1;
}
Пример #17
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;
			}
		}
	}
}
Пример #18
0
void Keys_t::ProcessKeysState(bool *PCurrentState) {
//    Uart.Printf("\r%A", PCurrentState, KEYS_CNT, ' ');
    // Iterate keys
    for(uint8_t i=0; i<KEYS_CNT; i++) {
        bool PressedNow = KeyIsPressed(PCurrentState[i]);

        // ==== Key Press ====
        if(PressedNow and !Key[i].IsPressed) {
            Key[i].IsPressed = true;
#if KEY_LONGPRESS
            Key[i].IsLongPress = false;
            Key[i].IsRepeating = false;
#endif
            KeyEvtInfo_t IEvt;
            IEvt.KeysCnt = 0;
            // Check if combo
            for(uint8_t j=0; j<KEYS_CNT; j++) {
                if(Key[j].IsPressed) {
                    IEvt.KeyID[IEvt.KeysCnt] = j;
                    IEvt.KeysCnt++;
                    if((j != i) and !IsCombo) {
                        IsCombo = true;
                        AddEvtToQueue(keCancel, j);
                    }
                }
            } // for j
            if(IEvt.KeysCnt == 1) {   // Single key pressed, no combo
                IsCombo = false;
                IEvt.Type = kePress;
#if KEY_LONGPRESS   // Reset timers
                RepeatTimer = chTimeNow();
                LongPressTimer = chTimeNow();
#endif
            }
            else IEvt.Type = keCombo;
            AddEvtToQueue(IEvt);
        } // if became pressed

        // ==== Key Release ====
        else if(!PressedNow and Key[i].IsPressed) {
            Key[i].IsPressed = false;
            // Check if combo completely released
            if(IsCombo) {
                IsCombo = false;
                for(uint8_t j=0; j<KEYS_CNT; j++) {
                    if(Key[j].IsPressed) {
                        IsCombo = true;
                        break;
                    }
                }
            } // if combo
#if KEY_RELEASE // Send evt if not combo
            else AddEvtToQueue(keRelease, i);
#endif
        }

#if KEY_LONGPRESS // ==== Long Press ====
        else if(PressedNow and Key[i].IsPressed and !IsCombo) {
            // Check if long press
            if(!Key[i].IsLongPress) {
                if(TimeElapsed(&LongPressTimer, KEY_LONGPRESS_DELAY_MS)) {
                    Key[i].IsLongPress = true;
                    AddEvtToQueue(keLongPress, i);
                }
            }
            // Check if repeat
            if(!Key[i].IsRepeating) {
                if(TimeElapsed(&RepeatTimer, KEYS_DELAY_BEFORE_REPEAT_MS)) {
                    Key[i].IsRepeating = true;
                    AddEvtToQueue(keRepeat, i);
                }
            }
            else {
                if(TimeElapsed(&RepeatTimer, KEY_REPEAT_PERIOD_MS)) {
                    AddEvtToQueue(keRepeat, i);
                }
            }
        } // if still pressed
#endif
    } // for i
}
Пример #19
0
	bool KeyIsNotPressed( Key key ) const		{ return !KeyIsPressed( key ); }
// Message - сообщение, Scan_code - клавиша, Modifiers - клавиши Ctrl, Alt и Shift.
// Post_marker_message означает, что в очередь окна надо направить сообщение WM_MARK.
// Если переменная Message не задана - надо сбросить клавиши в исходное состояние.
VOID KbdState_ChangePMKeyboardState( PQMSG Message, SHORT Scan_code, SHORT Modifiers, BYTE Post_marker_message = 1 )
{
 // Узнаем состояние клавиатуры.
 HWND Desktop = QueryDesktopWindow();

 BYTE Keyboard_state[ 256 ]; bzero( Keyboard_state, sizeof( BYTE ) * 256 );
 WinSetKeyboardStateTable( Desktop, Keyboard_state, 0 );

 // Включаем или выключаем Ctrl, Alt и Shift.
 if( Message != NULL )
  {
   if( Scan_code == SC_INSERT ) Keyboard_state[ VK_INSERT ] = SET_KEY_PRESSED; else Keyboard_state[ VK_INSERT ] = 0;
   if( Scan_code == SC_DELETE ) Keyboard_state[ VK_DELETE ] = SET_KEY_PRESSED; else Keyboard_state[ VK_DELETE ] = 0;
   if( Scan_code == SC_BACKSPACE ) Keyboard_state[ VK_BACKSPACE ] = SET_KEY_PRESSED; else Keyboard_state[ VK_BACKSPACE ] = 0;

   if( Modifiers & KC_CTRL ) Keyboard_state[ VK_CTRL ] = SET_KEY_PRESSED; else Keyboard_state[ VK_CTRL ] = 0;
   if( Modifiers & KC_ALT ) Keyboard_state[ KC_ALT ] = SET_KEY_PRESSED; else Keyboard_state[ KC_ALT ] = 0;
   if( Modifiers & KC_SHIFT ) Keyboard_state[ VK_SHIFT ] = SET_KEY_PRESSED; else Keyboard_state[ VK_SHIFT ] = 0;
  }
 // Или восстанавливаем настоящие значения.
 else
  {
   if( KeyIsPressed( SC_INSERT ) ) Keyboard_state[ VK_INSERT ] = SET_KEY_PRESSED; else Keyboard_state[ VK_INSERT ] = 0;
   if( KeyIsPressed( SC_DELETE ) ) Keyboard_state[ VK_DELETE ] = SET_KEY_PRESSED; else Keyboard_state[ VK_DELETE ] = 0;
   if( KeyIsPressed( SC_BACKSPACE ) ) Keyboard_state[ VK_BACKSPACE ] = SET_KEY_PRESSED; else Keyboard_state[ VK_BACKSPACE ] = 0;

   if( CtrlIsPressed() ) Keyboard_state[ VK_CTRL ] = SET_KEY_PRESSED; else Keyboard_state[ VK_CTRL ] = 0;
   if( AltIsPressed() ) Keyboard_state[ KC_ALT ] = SET_KEY_PRESSED; else Keyboard_state[ KC_ALT ] = 0;
   if( ShiftIsPressed() ) Keyboard_state[ VK_SHIFT ] = SET_KEY_PRESSED; else Keyboard_state[ VK_SHIFT ] = 0;
  }

 // Сбрасываем все сведения о кнопках мыши. Это необходимо при вставке текста с помощью
 // средней кнопки, когда сообщения сбрасываются, и данные, что кнопка нажата, остаются.
 Keyboard_state[ VK_BUTTON1 ] = 0;
 Keyboard_state[ VK_BUTTON2 ] = 0;
 Keyboard_state[ VK_BUTTON3 ] = 0;

 // Устанавливаем новое состояние клавиатуры.
 WinSetKeyboardStateTable( Desktop, Keyboard_state, 1 );

 // Запоминаем, что состояние клавиатуры изменено.
 if( Message != NULL )
  {
   // Запоминаем, что состояние клавиатуры изменено.
   KbdState.Keyboard_state_is_changed = 1;

   // Если в очередь окна надо направить сообщение:
   if( Post_marker_message )
    {
     // Посылаем в очередь окна сообщение WM_MARK. Когда оно будет получено, состояние можно будет вернуть назад.
     HMQ Message_queue = WinQueryWindowULong( Message->hwnd, QWL_HMQ );
     WinPostQueueMsg( Message_queue, WM_MARK, (MPARAM) MRK_KBD_MODIFIERS, 0 );
    }
  }
 // Или запоминаем, что состояние клавиатуры восстановлено.
 else
  {
   // Запоминаем, что состояние клавиатуры восстановлено.
   KbdState.Keyboard_state_is_changed = 0;
  }

 // Возврат.
 return;
}