Skip to content

Commit

Permalink
Implement input device selection screen (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxong committed Nov 18, 2013
1 parent fdd4ee0 commit bbd49de
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 48 deletions.
11 changes: 8 additions & 3 deletions src/cdogs/gamedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,13 @@ void GetPlayerCmds(
int i;
for (i = 0; i < MAX_PLAYERS; i++)
{
if (playerDatas[i].inputDevice == INPUT_DEVICE_UNSET)
{
continue;
}
(*cmds)[i] = GetOnePlayerCmd(
&gConfig.Input.PlayerKeys[i], keyFunc, mouseFunc, joyFunc,
&gConfig.Input.PlayerKeys[playerDatas[i].deviceIndex],
keyFunc, mouseFunc, joyFunc,
playerDatas[i].inputDevice, playerDatas[i].deviceIndex);
}
}
Expand Down Expand Up @@ -446,7 +451,7 @@ int GetMenuCmd(struct PlayerData playerDatas[MAX_PLAYERS])

int GetGameCmd(
InputDevices *devices, InputConfig *config,
int player, struct PlayerData *playerData, Vec2i playerPos)
struct PlayerData *playerData, Vec2i playerPos)
{
int cmd = 0;
joystick_t *joystick = &devices->joysticks.joys[0];
Expand All @@ -456,7 +461,7 @@ int GetGameCmd(
case INPUT_DEVICE_KEYBOARD:
cmd = GetKeyboardCmd(
&devices->keyboard,
&config->PlayerKeys[player].Keys,
&config->PlayerKeys[playerData->deviceIndex].Keys,
KeyIsDown);
break;
case INPUT_DEVICE_MOUSE:
Expand Down
2 changes: 1 addition & 1 deletion src/cdogs/gamedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ void GetPlayerCmds(
int GetMenuCmd(struct PlayerData playerDatas[MAX_PLAYERS]);
int GetGameCmd(
InputDevices *devices, InputConfig *config,
int player, struct PlayerData *playerData, Vec2i playerPos);
struct PlayerData *playerData, Vec2i playerPos);
int GameIsMouseUsed(struct PlayerData playerDatas[MAX_PLAYERS]);

#endif
1 change: 1 addition & 0 deletions src/cdogs/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ double ToDegrees(double radians);

typedef enum
{
INPUT_DEVICE_UNSET,
INPUT_DEVICE_KEYBOARD,
INPUT_DEVICE_MOUSE,
INPUT_DEVICE_JOYSTICK,
Expand Down
3 changes: 1 addition & 2 deletions src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ int HandleKey(int cmd, int *isPaused)
{
cmd |= GetGameCmd(
&gInputDevices, &gConfig.Input,
i, &gPlayerDatas[i], Vec2iZero());
&gPlayerDatas[i], Vec2iZero());
}
}
}
Expand Down Expand Up @@ -630,7 +630,6 @@ int gameloop(void)
cmds[i] = GetGameCmd(
&gInputDevices,
&gConfig.Input,
i,
&gPlayerDatas[i],
GetPlayerCenter(&gGraphicsDevice, i));
cmdAll |= cmds[i];
Expand Down
62 changes: 33 additions & 29 deletions src/menu_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,41 @@ void MenuDisplayPlayerControls(
switch (d->pData->inputDevice)
{
case INPUT_DEVICE_KEYBOARD:
sprintf(s, "(%s, %s, %s, %s, %s and %s)",
SDL_GetKeyName(d->keys->Keys.left),
SDL_GetKeyName(d->keys->Keys.right),
SDL_GetKeyName(d->keys->Keys.up),
SDL_GetKeyName(d->keys->Keys.down),
SDL_GetKeyName(d->keys->Keys.button1),
SDL_GetKeyName(d->keys->Keys.button2));
textWidth = TextGetStringWidth(s);
if (textWidth < 125)
{
textPos.x = pos.x - textWidth / 2;
DrawTextString(s, g, textPos);
}
else
{
sprintf(s, "(%s, %s, %s,",
SDL_GetKeyName(d->keys->Keys.left),
SDL_GetKeyName(d->keys->Keys.right),
SDL_GetKeyName(d->keys->Keys.up));
textWidth = TextGetStringWidth(s);
textPos.x = pos.x - textWidth / 2;
textPos.y -= 10;
DrawTextString(s, g, textPos);
sprintf(s, "%s, %s and %s)",
SDL_GetKeyName(d->keys->Keys.down),
SDL_GetKeyName(d->keys->Keys.button1),
SDL_GetKeyName(d->keys->Keys.button2));
input_keys_t *keys =
&d->inputConfig->PlayerKeys[d->pData->deviceIndex].Keys;
sprintf(s, "(%s, %s, %s, %s, %s and %s)",
SDL_GetKeyName(keys->left),
SDL_GetKeyName(keys->right),
SDL_GetKeyName(keys->up),
SDL_GetKeyName(keys->down),
SDL_GetKeyName(keys->button1),
SDL_GetKeyName(keys->button2));
textWidth = TextGetStringWidth(s);
textPos.x = pos.x - textWidth / 2;
textPos.y += 10;
DrawTextString(s, g, textPos);
if (textWidth < 125)
{
textPos.x = pos.x - textWidth / 2;
DrawTextString(s, g, textPos);
}
else
{
sprintf(s, "(%s, %s, %s,",
SDL_GetKeyName(keys->left),
SDL_GetKeyName(keys->right),
SDL_GetKeyName(keys->up));
textWidth = TextGetStringWidth(s);
textPos.x = pos.x - textWidth / 2;
textPos.y -= 10;
DrawTextString(s, g, textPos);
sprintf(s, "%s, %s and %s)",
SDL_GetKeyName(keys->down),
SDL_GetKeyName(keys->button1),
SDL_GetKeyName(keys->button2));
textWidth = TextGetStringWidth(s);
textPos.x = pos.x - textWidth / 2;
textPos.y += 10;
DrawTextString(s, g, textPos);
}
}
break;
case INPUT_DEVICE_MOUSE:
Expand Down
2 changes: 1 addition & 1 deletion src/menu_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void MenuDisplayPlayer(GraphicsDevice *g, Vec2i pos, Vec2i size, void *data);
typedef struct
{
struct PlayerData *pData;
KeyConfig *keys;
InputConfig *inputConfig;
} MenuDisplayPlayerControlsData;
void MenuDisplayPlayerControls(
GraphicsDevice *g, Vec2i pos, Vec2i size, void *data);
Expand Down
4 changes: 2 additions & 2 deletions src/player_select_menus.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ static menu_t *CreateSaveTemplateMenu(
void PlayerSelectMenusCreate(
PlayerSelectMenu *menu,
int numPlayers, int player, Character *c, struct PlayerData *pData,
InputDevices *input, GraphicsDevice *graphics, KeyConfig *key)
InputDevices *input, GraphicsDevice *graphics, InputConfig *inputConfig)
{
MenuSystem *ms = &menu->ms;
PlayerSelectMenuData *data = &menu->data;
Expand All @@ -513,7 +513,7 @@ void PlayerSelectMenusCreate(
data->display.c = c;
data->display.currentMenu = &ms->current;
data->display.pData = pData;
data->controls.keys = key;
data->controls.inputConfig = inputConfig;
data->controls.pData = pData;

switch (numPlayers)
Expand Down
2 changes: 1 addition & 1 deletion src/player_select_menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ typedef struct
void PlayerSelectMenusCreate(
PlayerSelectMenu *menu,
int numPlayers, int player, Character *c, struct PlayerData *pData,
InputDevices *input, GraphicsDevice *graphics, KeyConfig *key);
InputDevices *input, GraphicsDevice *graphics, InputConfig *inputConfig);

// TODO: load templates from file
typedef struct
Expand Down
131 changes: 125 additions & 6 deletions src/prep.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,93 @@ int NumPlayersSelection(
return res;
}


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;
}
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;
}
}
}
}

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.PlayerKeys[i]);
&gInputDevices, graphics, &gConfig.Input);
hasInputDevice[i] = 0;
}

KeyInit(&gInputDevices.keyboard);
Expand All @@ -158,7 +235,10 @@ int PlayerSelection(int numPlayers, GraphicsDevice *graphics)
GetPlayerCmds(&cmds, gPlayerDatas);
for (i = 0; i < numPlayers; i++)
{
MenuProcessCmd(&menus[i].ms, cmds[i]);
if (hasInputDevice[i])
{
MenuProcessCmd(&menus[i].ms, cmds[i]);
}
}
for (i = 0; i < numPlayers; i++)
{
Expand All @@ -172,10 +252,40 @@ int PlayerSelection(int numPlayers, GraphicsDevice *graphics)
break;
}

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

GraphicsBlitBkg(graphics);
for (i = 0; i < numPlayers; i++)
{
MenuDisplay(&menus[i].ms);
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;
default:
assert(0 && "not implemented");
break;
}
DrawTextString(prompt, graphics, Vec2iAdd(center, offset));
}
}
BlitFlip(graphics, &gConfig.Graphics);
SDL_Delay(10);
Expand All @@ -197,7 +307,7 @@ int PlayerEquip(int numPlayers, GraphicsDevice *graphics)
WeaponMenuCreate(
&menus[i], numPlayers, i,
&gCampaign.Setting.characters.players[i], &gPlayerDatas[i],
&gInputDevices, graphics, &gConfig.Input.PlayerKeys[i]);
&gInputDevices, graphics, &gConfig.Input);
}

debug(D_NORMAL, "\n");
Expand All @@ -214,11 +324,20 @@ int PlayerEquip(int numPlayers, GraphicsDevice *graphics)
GetPlayerCmds(&cmds, gPlayerDatas);
for (i = 0; i < numPlayers; i++)
{
MenuProcessCmd(&menus[i].ms, cmds[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) || gPlayerDatas[i].weaponCount == 0)
if (!MenuIsExit(&menus[i].ms))
{
isDone = 0;
}
Expand Down
4 changes: 4 additions & 0 deletions src/prep.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
int NumPlayersSelection(
int *numPlayers, campaign_mode_e mode,
GraphicsDevice *graphics, InputDevices *input);

// Allow input devices to take control players,
// and select player name and appearance
int PlayerSelection(int numPlayers, GraphicsDevice *graphics);

int PlayerEquip(int numPlayers, GraphicsDevice *graphics);

#endif
4 changes: 2 additions & 2 deletions src/weapon_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static void DisplayEquippedWeapons(
void WeaponMenuCreate(
WeaponMenu *menu,
int numPlayers, int player, Character *c, struct PlayerData *pData,
InputDevices *input, GraphicsDevice *graphics, KeyConfig *key)
InputDevices *input, GraphicsDevice *graphics, InputConfig *inputConfig)
{
MenuSystem *ms = &menu->ms;
WeaponMenuData *data = &menu->data;
Expand All @@ -115,7 +115,7 @@ void WeaponMenuCreate(
data->display.c = c;
data->display.currentMenu = &ms->current;
data->display.pData = pData;
data->controls.keys = key;
data->controls.inputConfig = inputConfig;
data->controls.pData = pData;

switch (numPlayers)
Expand Down
2 changes: 1 addition & 1 deletion src/weapon_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ typedef struct
void WeaponMenuCreate(
WeaponMenu *menu,
int numPlayers, int player, Character *c, struct PlayerData *pData,
InputDevices *input, GraphicsDevice *graphics, KeyConfig *key);
InputDevices *input, GraphicsDevice *graphics, InputConfig *inputConfig);

#endif

0 comments on commit bbd49de

Please sign in to comment.