Skip to content

Commit

Permalink
Move inputdevice from config to playerdata (#163)
Browse files Browse the repository at this point in the history
Set player 1 controls as keyboard 1 for menus
Remove controls options entries
  • Loading branch information
cxong committed Nov 18, 2013
1 parent 17a3704 commit baa181a
Show file tree
Hide file tree
Showing 21 changed files with 287 additions and 344 deletions.
15 changes: 0 additions & 15 deletions src/cdogs/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,12 @@ void ConfigLoadDefault(Config *config)
config->Graphics.ScaleFactor = 1;
config->Graphics.ShakeMultiplier = 1;
config->Graphics.ScaleMode = SCALE_MODE_NN;
config->Input.PlayerKeys[0].Device = INPUT_DEVICE_KEYBOARD;
config->Input.PlayerKeys[0].Keys.left = SDLK_LEFT;
config->Input.PlayerKeys[0].Keys.right = SDLK_RIGHT;
config->Input.PlayerKeys[0].Keys.up = SDLK_UP;
config->Input.PlayerKeys[0].Keys.down = SDLK_DOWN;
config->Input.PlayerKeys[0].Keys.button1 = SDLK_RSHIFT;
config->Input.PlayerKeys[0].Keys.button2 = SDLK_RETURN;
config->Input.PlayerKeys[1].Device = INPUT_DEVICE_KEYBOARD;
config->Input.PlayerKeys[1].Keys.left = SDLK_KP4;
config->Input.PlayerKeys[1].Keys.right = SDLK_KP6;
config->Input.PlayerKeys[1].Keys.up = SDLK_KP8;
Expand Down Expand Up @@ -334,16 +332,3 @@ void ConfigLoadDefault(Config *config)
config->QuickPlay.EnemiesWithExplosives = 1;
config->QuickPlay.ItemCount = QUICKPLAY_QUANTITY_ANY;
}

int ConfigIsMouseUsed(InputConfig *config)
{
int i;
for (i = 0; i < 2; i++)
{
if (config->PlayerKeys[i].Device == INPUT_DEVICE_MOUSE)
{
return 1;
}
}
return 0;
}
3 changes: 0 additions & 3 deletions src/cdogs/config_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,11 @@ static void AddKeysNode(input_keys_t *keys, json_t *parent)

static void LoadKeyConfigNode(KeyConfig *config, json_t *node)
{
config->Device = StrInputDevice(json_find_first_label(node, "Device")->child->text);
LoadKeysNode(&config->Keys, json_find_first_label(node, "Keys"));
}
static void AddKeyConfigNode(KeyConfig *config, json_t *parent)
{
json_t *subConfig = json_new_object();
json_insert_pair_into_object(
subConfig, "Device", json_new_string(InputDeviceStr(config->Device)));
AddKeysNode(&config->Keys, subConfig);
json_insert_child(parent, subConfig);
}
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/config_old.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ void ConfigLoadOld(Config *config, const char *filename)
for (i = 0; i < 2; i++)
{
fscanfres = fscanf(f, "%d\n%d %d %d %d %d %d\n",
(int *)&config->Input.PlayerKeys[i].Device,
&dummy,
&config->Input.PlayerKeys[i].Keys.left,
&config->Input.PlayerKeys[i].Keys.right,
&config->Input.PlayerKeys[i].Keys.up,
Expand Down Expand Up @@ -143,7 +143,7 @@ void ConfigSaveOld(Config *config, const char *filename)
for (i = 0; i < 2; i++)
{
fprintf(f, "%d\n%d %d %d %d %d %d\n",
config->Input.PlayerKeys[i].Device,
INPUT_DEVICE_KEYBOARD,
config->Input.PlayerKeys[i].Keys.left,
config->Input.PlayerKeys[i].Keys.right,
config->Input.PlayerKeys[i].Keys.up,
Expand Down
207 changes: 207 additions & 0 deletions src/cdogs/gamedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include <tinydir/tinydir.h>

#include "actors.h"
#include "config.h"
#include "defs.h"
#include "keyboard.h"
#include "input.h"
Expand Down Expand Up @@ -89,6 +90,14 @@ void PlayerDataInitialize(void)
{
struct PlayerData *d = &gPlayerDatas[i];
memset(d, 0, sizeof *d);

// Set default player 1 controls, as it's used in menus
if (i == 0)
{
d->inputDevice = INPUT_DEVICE_KEYBOARD;
d->deviceIndex = 0;
}

switch (i)
{
case 0:
Expand Down Expand Up @@ -280,3 +289,201 @@ int IsTileInExit(TTileItem *tile, struct MissionOptions *options)
tile->y >= options->exitTop &&
tile->y <= options->exitBottom;
}


static int GetKeyboardCmd(
keyboard_t *keyboard, input_keys_t *keys,
int(*keyFunc)(keyboard_t *, int))
{
int cmd = 0;

if (keyFunc(keyboard, keys->left)) cmd |= CMD_LEFT;
else if (keyFunc(keyboard, keys->right)) cmd |= CMD_RIGHT;

if (keyFunc(keyboard, keys->up)) cmd |= CMD_UP;
else if (keyFunc(keyboard, keys->down)) cmd |= CMD_DOWN;

if (keyFunc(keyboard, keys->button1)) cmd |= CMD_BUTTON1;

if (keyFunc(keyboard, keys->button2)) cmd |= CMD_BUTTON2;

return cmd;
}

#define MOUSE_MOVE_DEAD_ZONE 12
static int GetMouseCmd(
Mouse *mouse, int(*mouseFunc)(Mouse *, int), int useMouseMove, Vec2i pos)
{
int cmd = 0;

if (useMouseMove)
{
int dx = abs(mouse->currentPos.x - pos.x);
int dy = abs(mouse->currentPos.y - pos.y);
if (dx > MOUSE_MOVE_DEAD_ZONE || dy > MOUSE_MOVE_DEAD_ZONE)
{
if (2 * dx > dy)
{
if (pos.x < mouse->currentPos.x) cmd |= CMD_RIGHT;
else if (pos.x > mouse->currentPos.x) cmd |= CMD_LEFT;
}
if (2 * dy > dx)
{
if (pos.y < mouse->currentPos.y) cmd |= CMD_DOWN;
else if (pos.y > mouse->currentPos.y) cmd |= CMD_UP;
}
}
}
else
{
if (mouseFunc(mouse, SDL_BUTTON_WHEELUP)) cmd |= CMD_UP;
else if (mouseFunc(mouse, SDL_BUTTON_WHEELDOWN)) cmd |= CMD_DOWN;
}

if (mouseFunc(mouse, SDL_BUTTON_LEFT)) cmd |= CMD_BUTTON1;
if (mouseFunc(mouse, SDL_BUTTON_RIGHT)) cmd |= CMD_BUTTON2;
if (mouseFunc(mouse, SDL_BUTTON_MIDDLE)) cmd |= CMD_BUTTON3;

return cmd;
}

static int GetJoystickCmd(
joystick_t *joystick, int(*joyFunc)(joystick_t *, int))
{
int cmd = 0;

if (joyFunc(joystick, CMD_LEFT)) cmd |= CMD_LEFT;
else if (joyFunc(joystick, CMD_RIGHT)) cmd |= CMD_RIGHT;

if (joyFunc(joystick, CMD_UP)) cmd |= CMD_UP;
else if (joyFunc(joystick, CMD_DOWN)) cmd |= CMD_DOWN;

if (joyFunc(joystick, CMD_BUTTON1)) cmd |= CMD_BUTTON1;

if (joyFunc(joystick, CMD_BUTTON2)) cmd |= CMD_BUTTON2;

if (joyFunc(joystick, CMD_BUTTON3)) cmd |= CMD_BUTTON3;

if (joyFunc(joystick, CMD_BUTTON4)) cmd |= CMD_BUTTON4;

return cmd;
}

static int GetOnePlayerCmd(
KeyConfig *config,
int(*keyFunc)(keyboard_t *, int),
int(*mouseFunc)(Mouse *, int),
int(*joyFunc)(joystick_t *, int),
input_device_e device,
int deviceIndex)
{
int cmd = 0;
switch (device)
{
case INPUT_DEVICE_KEYBOARD:
cmd = GetKeyboardCmd(
&gInputDevices.keyboard, &config->Keys, keyFunc);
break;
case INPUT_DEVICE_MOUSE:
cmd = GetMouseCmd(&gInputDevices.mouse, mouseFunc, 0, Vec2iZero());
break;
case INPUT_DEVICE_JOYSTICK:
{
joystick_t *joystick = &gInputDevices.joysticks.joys[deviceIndex];
cmd = GetJoystickCmd(joystick, joyFunc);
}
break;
default:
assert(0 && "unknown input device");
break;
}
return cmd;
}

void GetPlayerCmds(
int(*cmds)[MAX_PLAYERS], struct PlayerData playerDatas[MAX_PLAYERS])
{
int(*keyFunc)(keyboard_t *, int) = KeyIsPressed;
int(*mouseFunc)(Mouse *, int) = MouseIsPressed;
int(*joyFunc)(joystick_t *, int) = JoyIsPressed;
int i;
for (i = 0; i < MAX_PLAYERS; i++)
{
(*cmds)[i] = GetOnePlayerCmd(
&gConfig.Input.PlayerKeys[i], keyFunc, mouseFunc, joyFunc,
playerDatas[i].inputDevice, playerDatas[i].deviceIndex);
}
}

int GetMenuCmd(struct PlayerData playerDatas[MAX_PLAYERS])
{
int cmd;
keyboard_t *kb = &gInputDevices.keyboard;
if (KeyIsPressed(kb, SDLK_ESCAPE))
{
return CMD_ESC;
}

cmd = GetOnePlayerCmd(
&gConfig.Input.PlayerKeys[0],
KeyIsPressed, MouseIsPressed, JoyIsPressed,
playerDatas[0].inputDevice, playerDatas[0].deviceIndex);
if (!cmd)
{
if (KeyIsPressed(kb, SDLK_LEFT)) cmd |= CMD_LEFT;
else if (KeyIsPressed(kb, SDLK_RIGHT)) cmd |= CMD_RIGHT;

if (KeyIsPressed(kb, SDLK_UP)) cmd |= CMD_UP;
else if (KeyIsPressed(kb, SDLK_DOWN)) cmd |= CMD_DOWN;

if (KeyIsPressed(kb, SDLK_RETURN)) cmd |= CMD_BUTTON1;

if (KeyIsPressed(kb, SDLK_BACKSPACE)) cmd |= CMD_BUTTON2;
}

return cmd;
}

int GetGameCmd(
InputDevices *devices, InputConfig *config,
int player, struct PlayerData *playerData, Vec2i playerPos)
{
int cmd = 0;
joystick_t *joystick = &devices->joysticks.joys[0];

switch (playerData->inputDevice)
{
case INPUT_DEVICE_KEYBOARD:
cmd = GetKeyboardCmd(
&devices->keyboard,
&config->PlayerKeys[player].Keys,
KeyIsDown);
break;
case INPUT_DEVICE_MOUSE:
cmd = GetMouseCmd(&devices->mouse, MouseIsDown, 1, playerPos);
break;
case INPUT_DEVICE_JOYSTICK:
joystick =
&devices->joysticks.joys[playerData->deviceIndex];
cmd = GetJoystickCmd(joystick, JoyIsDown);
break;
default:
assert(0 && "unknown input device");
break;
}

return cmd;
}

int GameIsMouseUsed(struct PlayerData playerDatas[MAX_PLAYERS])
{
int i;
for (i = 0; i < MAX_PLAYERS; i++)
{
if (playerDatas[i].inputDevice == INPUT_DEVICE_MOUSE)
{
return 1;
}
}
return 0;
}
11 changes: 11 additions & 0 deletions src/cdogs/gamedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ struct PlayerData
int allTime, today;
int kills;
int friendlies;

input_device_e inputDevice;
int deviceIndex;
};

extern struct PlayerData gPlayerDatas[MAX_PLAYERS];
Expand Down Expand Up @@ -284,4 +287,12 @@ int AreKeysAllowed(campaign_mode_e mode);

int IsTileInExit(TTileItem *tile, struct MissionOptions *options);

void GetPlayerCmds(
int(*cmds)[MAX_PLAYERS], struct PlayerData playerDatas[MAX_PLAYERS]);
int GetMenuCmd(struct PlayerData playerDatas[MAX_PLAYERS]);
int GetGameCmd(
InputDevices *devices, InputConfig *config,
int player, struct PlayerData *playerData, Vec2i playerPos);
int GameIsMouseUsed(struct PlayerData playerDatas[MAX_PLAYERS]);

#endif

0 comments on commit baa181a

Please sign in to comment.