diff --git a/src/cdogs/config.c b/src/cdogs/config.c index f37ee9a50..9515b6e3f 100644 --- a/src/cdogs/config.c +++ b/src/cdogs/config.c @@ -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; @@ -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; -} diff --git a/src/cdogs/config_json.c b/src/cdogs/config_json.c index 7dd3ffa31..8e5c69da5 100644 --- a/src/cdogs/config_json.c +++ b/src/cdogs/config_json.c @@ -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); } diff --git a/src/cdogs/config_old.c b/src/cdogs/config_old.c index 03e208821..5bd060378 100644 --- a/src/cdogs/config_old.c +++ b/src/cdogs/config_old.c @@ -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, @@ -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, diff --git a/src/cdogs/gamedata.c b/src/cdogs/gamedata.c index 3b2ed9e57..075ace5e1 100644 --- a/src/cdogs/gamedata.c +++ b/src/cdogs/gamedata.c @@ -59,6 +59,7 @@ #include #include "actors.h" +#include "config.h" #include "defs.h" #include "keyboard.h" #include "input.h" @@ -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: @@ -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; +} diff --git a/src/cdogs/gamedata.h b/src/cdogs/gamedata.h index 9d39670d0..c6a5eff5a 100644 --- a/src/cdogs/gamedata.h +++ b/src/cdogs/gamedata.h @@ -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]; @@ -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 diff --git a/src/cdogs/input.c b/src/cdogs/input.c index c1f8d76a3..8c8999e4e 100644 --- a/src/cdogs/input.c +++ b/src/cdogs/input.c @@ -58,224 +58,9 @@ #include "sounds.h" #include "gamedata.h" -#define MOUSE_MOVE_DEAD_ZONE 12 - InputDevices gInputDevices; -void InputChangeDevice( - InputDevices *devices, input_device_e *d, input_device_e *dOther) -{ - int numJoys = devices->joysticks.numJoys; - input_device_e newDevice; - int isFirst = 1; - int available[INPUT_DEVICE_COUNT]; - available[INPUT_DEVICE_KEYBOARD] = 1; - available[INPUT_DEVICE_MOUSE] = *dOther != INPUT_DEVICE_MOUSE; - available[INPUT_DEVICE_JOYSTICK_1] = - numJoys >= 1 && *dOther != INPUT_DEVICE_JOYSTICK_1; - available[INPUT_DEVICE_JOYSTICK_2] = - numJoys >= 2 && *dOther != INPUT_DEVICE_JOYSTICK_2; - for (newDevice = *d; isFirst || newDevice != *d;) - { - if (!isFirst && available[newDevice]) - { - break; - } - isFirst = 0; - newDevice++; - if (newDevice == INPUT_DEVICE_COUNT) - { - newDevice = INPUT_DEVICE_KEYBOARD; - } - } - *d = newDevice; - debug(D_NORMAL, "change control to: %s\n", InputDeviceStr(*d)); -} - -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; -} - -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; -} - -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; -} - -int GetOnePlayerCmd( - KeyConfig *config, - int (*keyFunc)(keyboard_t *, int), - int (*mouseFunc)(Mouse *, int), - int (*joyFunc)(joystick_t *, int)) -{ - int cmd = 0; - if (config->Device == INPUT_DEVICE_KEYBOARD) - { - cmd = GetKeyboardCmd(&gInputDevices.keyboard, &config->Keys, keyFunc); - } - else if (config->Device == INPUT_DEVICE_MOUSE) - { - cmd = GetMouseCmd(&gInputDevices.mouse, mouseFunc, 0, Vec2iZero()); - } - else - { - joystick_t *joystick = &gInputDevices.joysticks.joys[0]; - - if (config->Device == INPUT_DEVICE_JOYSTICK_1) - { - joystick = &gInputDevices.joysticks.joys[0]; - } - else if (config->Device == INPUT_DEVICE_JOYSTICK_2) - { - joystick = &gInputDevices.joysticks.joys[1]; - } - - cmd = GetJoystickCmd(joystick, joyFunc); - } - return cmd; -} - - -void GetPlayerCmds(int (*cmds)[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); - } -} - -int InputGetGameCmd( - InputDevices *devices, InputConfig *config, int player, Vec2i playerPos) -{ - int cmd = 0; - joystick_t *joystick = &devices->joysticks.joys[0]; - - switch (config->PlayerKeys[player].Device) - { - 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_1: - joystick = &devices->joysticks.joys[0]; - cmd = GetJoystickCmd(joystick, JoyIsDown); - break; - case INPUT_DEVICE_JOYSTICK_2: - joystick = &devices->joysticks.joys[1]; - cmd = GetJoystickCmd(joystick, JoyIsDown); - break; - default: - assert(0 && "unknown input device"); - break; - } - - return cmd; -} - -int GetMenuCmd(void) -{ - int cmds[MAX_PLAYERS]; - keyboard_t *kb = &gInputDevices.keyboard; - if (KeyIsPressed(kb, SDLK_ESCAPE)) - { - return CMD_ESC; - } - - GetPlayerCmds(&cmds); - if (!cmds[0]) - { - if (KeyIsPressed(kb, SDLK_LEFT)) cmds[0] |= CMD_LEFT; - else if (KeyIsPressed(kb, SDLK_RIGHT)) cmds[0] |= CMD_RIGHT; - - if (KeyIsPressed(kb, SDLK_UP)) cmds[0] |= CMD_UP; - else if (KeyIsPressed(kb, SDLK_DOWN)) cmds[0] |= CMD_DOWN; - - if (KeyIsPressed(kb, SDLK_RETURN)) cmds[0] |= CMD_BUTTON1; - - if (KeyIsPressed(kb, SDLK_BACKSPACE)) cmds[0] |= CMD_BUTTON2; - } - - return cmds[0]; -} - void InputInit(InputDevices *devices, PicPaletted *mouseCursor) { KeyInit(&devices->keyboard); @@ -375,7 +160,7 @@ void InputTerminate(InputDevices *devices) JoyTerminate(&devices->joysticks); } -const char *InputDeviceName(int d) +const char *InputDeviceName(int d, int deviceIndex) { switch (d) { @@ -383,10 +168,8 @@ const char *InputDeviceName(int d) return "Keyboard"; case INPUT_DEVICE_MOUSE: return "Mouse"; - case INPUT_DEVICE_JOYSTICK_1: - return SDL_JoystickName(0); - case INPUT_DEVICE_JOYSTICK_2: - return SDL_JoystickName(1); + case INPUT_DEVICE_JOYSTICK: + return SDL_JoystickName(deviceIndex); default: return ""; } diff --git a/src/cdogs/input.h b/src/cdogs/input.h index 3df988b14..016164fb6 100644 --- a/src/cdogs/input.h +++ b/src/cdogs/input.h @@ -71,7 +71,6 @@ typedef struct typedef struct { - input_device_e Device; input_keys_t Keys; } KeyConfig; @@ -102,20 +101,12 @@ typedef struct extern InputDevices gInputDevices; -void InputChangeDevice( - InputDevices *devices, input_device_e *d, input_device_e *dOther); - -void GetPlayerCmds(int (*cmds)[MAX_PLAYERS]); -int GetMenuCmd(void); - void InputInit(InputDevices *devices, PicPaletted *mouseCursor); int InputGetKey(input_keys_t *keys, key_code_e keyCode); -int InputGetGameCmd( - InputDevices *devices, InputConfig *config, int player, Vec2i playerPos); void InputSetKey(input_keys_t *keys, int key, key_code_e keyCode); void InputPoll(InputDevices *devices, Uint32 ticks); void InputTerminate(InputDevices *devices); -const char *InputDeviceName(int d); +const char *InputDeviceName(int d, int deviceIndex); #endif diff --git a/src/cdogs/utils.c b/src/cdogs/utils.c index 6ea8da7cd..a9b010a41 100644 --- a/src/cdogs/utils.c +++ b/src/cdogs/utils.c @@ -100,10 +100,8 @@ char *InputDeviceStr(int d) return "Keyboard"; case INPUT_DEVICE_MOUSE: return "Mouse"; - case INPUT_DEVICE_JOYSTICK_1: - return "Joystick 1"; - case INPUT_DEVICE_JOYSTICK_2: - return "Joystick 2"; + case INPUT_DEVICE_JOYSTICK: + return "Joystick"; default: return ""; } @@ -118,13 +116,9 @@ input_device_e StrInputDevice(const char *str) { return INPUT_DEVICE_MOUSE; } - else if (strcmp(str, "Joystick 1") == 0) + else if (strcmp(str, "Joystick") == 0) { - return INPUT_DEVICE_JOYSTICK_1; - } - else if (strcmp(str, "Joystick 1") == 0) - { - return INPUT_DEVICE_JOYSTICK_2; + return INPUT_DEVICE_JOYSTICK; } else { diff --git a/src/cdogs/utils.h b/src/cdogs/utils.h index 4d5f3e74f..17ee87faf 100644 --- a/src/cdogs/utils.h +++ b/src/cdogs/utils.h @@ -121,8 +121,7 @@ typedef enum { INPUT_DEVICE_KEYBOARD, INPUT_DEVICE_MOUSE, - INPUT_DEVICE_JOYSTICK_1, - INPUT_DEVICE_JOYSTICK_2, + INPUT_DEVICE_JOYSTICK, INPUT_DEVICE_COUNT } input_device_e; diff --git a/src/game.c b/src/game.c index 76f1babb2..3e0ca348d 100644 --- a/src/game.c +++ b/src/game.c @@ -476,7 +476,7 @@ int HandleKey(int cmd, int *isPaused) &gInputDevices.keyboard, gConfig.Input.PlayerKeys[0].Keys.map) || (cmd & CMD_BUTTON3) != 0) { - int cmd1 = 0, cmd2 = 0; + int i; if (!hasDisplayedAutomap) { AutomapDraw(0); @@ -485,11 +485,13 @@ int HandleKey(int cmd, int *isPaused) } SDL_Delay(10); InputPoll(&gInputDevices, SDL_GetTicks()); - cmd1 = InputGetGameCmd( - &gInputDevices, &gConfig.Input, 0, Vec2iZero()); - cmd2 = InputGetGameCmd( - &gInputDevices, &gConfig.Input, 1, Vec2iZero()); - cmd = cmd1 | cmd2; + cmd = 0; + for (i = 0; i < MAX_PLAYERS; i++) + { + cmd |= GetGameCmd( + &gInputDevices, &gConfig.Input, + i, &gPlayerDatas[i], Vec2iZero()); + } } } @@ -625,10 +627,11 @@ int gameloop(void) { if (gPlayers[i] && !gPlayers[i]->dead) { - cmds[i] = InputGetGameCmd( + cmds[i] = GetGameCmd( &gInputDevices, &gConfig.Input, i, + &gPlayerDatas[i], GetPlayerCenter(&gGraphicsDevice, i)); cmdAll |= cmds[i]; } @@ -718,7 +721,7 @@ int gameloop(void) HUDUpdate(&hud, ticks_now - ticks_then); HUDDraw(&hud, isPaused); - if (ConfigIsMouseUsed(&gConfig.Input)) + if (GameIsMouseUsed(gPlayerDatas)) { MouseDraw(&gInputDevices.mouse); } diff --git a/src/mainmenu.c b/src/mainmenu.c index 45f0c2d24..c8f436969 100644 --- a/src/mainmenu.c +++ b/src/mainmenu.c @@ -350,8 +350,6 @@ menu_t *MenuCreateOptionsGraphics(const char *name) return menu; } -menu_t *MenuCreateOptionChangeControl( - const char *name, input_device_e *device0, input_device_e *device1); menu_t *MenuCreateKeys(const char *name); menu_t *MenuCreateOptionsControls(const char *name) @@ -361,26 +359,6 @@ menu_t *MenuCreateOptionsControls(const char *name) "Configure Controls:", MENU_TYPE_OPTIONS, 0); - MenuAddSubmenu( - menu, - MenuCreateOptionChangeControl( - "Player 1", &gConfig.Input.PlayerKeys[0].Device, &gConfig.Input.PlayerKeys[1].Device)); - MenuAddSubmenu( - menu, - MenuCreateOptionChangeControl( - "Player 2", &gConfig.Input.PlayerKeys[1].Device, &gConfig.Input.PlayerKeys[0].Device)); - MenuAddSubmenu( - menu, - MenuCreateOptionToggle( - "Swap buttons joystick 1", - &gConfig.Input.SwapButtonsJoystick1, - MENU_OPTION_DISPLAY_STYLE_YES_NO)); - MenuAddSubmenu( - menu, - MenuCreateOptionToggle( - "Swap buttons joystick 2", - &gConfig.Input.SwapButtonsJoystick2, - MENU_OPTION_DISPLAY_STYLE_YES_NO)); #ifndef __ANDROID__ MenuAddSubmenu(menu, MenuCreateKeys("Redefine keys...")); #endif @@ -548,10 +526,10 @@ menu_t *MenuCreateKeys(const char *name) MENU_TYPE_KEYS, 0); MenuCreateKeysSingleSection( - menu, "Player 1", + menu, "Keyboard 1", &gConfig.Input.PlayerKeys[0].Keys, &gConfig.Input.PlayerKeys[1].Keys); MenuCreateKeysSingleSection( - menu, "Player 2", + menu, "Keyboard 2", &gConfig.Input.PlayerKeys[1].Keys, &gConfig.Input.PlayerKeys[0].Keys); MenuAddSubmenu( menu, diff --git a/src/menu.c b/src/menu.c index 512b3507c..66d2871d8 100644 --- a/src/menu.c +++ b/src/menu.c @@ -166,7 +166,7 @@ void MenuLoop(MenuSystem *menu) } else { - int cmd = GetMenuCmd(); + int cmd = GetMenuCmd(gPlayerDatas); MenuProcessCmd(menu, cmd); } if (MenuIsExit(menu)) @@ -415,17 +415,6 @@ menu_t *MenuCreateCustom( return menu; } -menu_t *MenuCreateOptionChangeControl( - const char *name, input_device_e *device0, input_device_e *device1) -{ - menu_t *menu = MenuCreate(name, MENU_TYPE_SET_OPTION_CHANGE_CONTROL); - menu->u.option.uHook.changeControl.device0 = device0; - menu->u.option.uHook.changeControl.device1 = device1; - menu->u.option.displayStyle = MENU_OPTION_DISPLAY_STYLE_INT_TO_STR_FUNC; - menu->u.option.uFunc.intToStr = InputDeviceName; - return menu; -} - void MenuDisplayItems(MenuSystem *ms); void MenuDisplaySubmenus(MenuSystem *ms); @@ -557,7 +546,6 @@ void MenuDisplaySubmenus(MenuSystem *ms) subMenu->type == MENU_TYPE_SET_OPTION_SEED || subMenu->type == MENU_TYPE_SET_OPTION_UP_DOWN_VOID_FUNC_VOID || subMenu->type == MENU_TYPE_SET_OPTION_RANGE_GET_SET || - subMenu->type == MENU_TYPE_SET_OPTION_CHANGE_CONTROL || subMenu->type == MENU_TYPE_VOID_FUNC_VOID) { int optionInt = MenuOptionGetIntValue(subMenu); @@ -760,8 +748,6 @@ int MenuOptionGetIntValue(menu_t *menu) return (int)*menu->u.option.uHook.seed; case MENU_TYPE_SET_OPTION_RANGE_GET_SET: return menu->u.option.uHook.optionRangeGetSet.getFunc(); - case MENU_TYPE_SET_OPTION_CHANGE_CONTROL: - return *menu->u.option.uHook.changeControl.device0; case MENU_TYPE_VOID_FUNC_VOID: if (menu->u.option.uHook.toggleFuncs.get) { @@ -1163,12 +1149,6 @@ void MenuActivate(MenuSystem *ms, menu_t *menu, int cmd) menu->u.option.uHook.optionRangeGetSet.setFunc(option); } break; - case MENU_TYPE_SET_OPTION_CHANGE_CONTROL: - InputChangeDevice( - &gInputDevices, - menu->u.option.uHook.changeControl.device0, - menu->u.option.uHook.changeControl.device1); - break; case MENU_TYPE_VOID_FUNC_VOID: menu->u.option.uHook.toggleFuncs.toggle(); break; diff --git a/src/menu.h b/src/menu.h index 9f843f0a2..b51260cdc 100644 --- a/src/menu.h +++ b/src/menu.h @@ -68,7 +68,6 @@ typedef enum MENU_TYPE_SET_OPTION_SEED, // set random seed MENU_TYPE_SET_OPTION_UP_DOWN_VOID_FUNC_VOID, // set option using up/down functions MENU_TYPE_SET_OPTION_RANGE_GET_SET, // set option range low-high using get/set functions - MENU_TYPE_SET_OPTION_CHANGE_CONTROL, // change control device MENU_TYPE_SET_OPTION_CHANGE_KEY, // redefine key MENU_TYPE_VOID_FUNC_VOID, // call a void(*f)(void) function MENU_TYPE_CAMPAIGN_ITEM, diff --git a/src/menu_utils.c b/src/menu_utils.c index 7bf97bfb8..d86dcd517 100644 --- a/src/menu_utils.c +++ b/src/menu_utils.c @@ -28,6 +28,8 @@ */ #include "menu_utils.h" +#include + #include #include @@ -64,19 +66,20 @@ void MenuDisplayPlayerControls( GraphicsDevice *g, Vec2i pos, Vec2i size, void *data) { char s[256]; - KeyConfig *config = data; + MenuDisplayPlayerControlsData *d = data; Vec2i textPos = Vec2iNew(0, pos.y + size.y - (size.y / 6)); int textWidth = 0; - if (config->Device == INPUT_DEVICE_KEYBOARD) + switch (d->pData->inputDevice) { + case INPUT_DEVICE_KEYBOARD: sprintf(s, "(%s, %s, %s, %s, %s and %s)", - SDL_GetKeyName(config->Keys.left), - SDL_GetKeyName(config->Keys.right), - SDL_GetKeyName(config->Keys.up), - SDL_GetKeyName(config->Keys.down), - SDL_GetKeyName(config->Keys.button1), - SDL_GetKeyName(config->Keys.button2)); + 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) { @@ -86,35 +89,38 @@ void MenuDisplayPlayerControls( else { sprintf(s, "(%s, %s, %s,", - SDL_GetKeyName(config->Keys.left), - SDL_GetKeyName(config->Keys.right), - SDL_GetKeyName(config->Keys.up)); + 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(config->Keys.down), - SDL_GetKeyName(config->Keys.button1), - SDL_GetKeyName(config->Keys.button2)); + SDL_GetKeyName(d->keys->Keys.down), + SDL_GetKeyName(d->keys->Keys.button1), + SDL_GetKeyName(d->keys->Keys.button2)); textWidth = TextGetStringWidth(s); textPos.x = pos.x - textWidth / 2; textPos.y += 10; DrawTextString(s, g, textPos); } - } - else if (config->Device == INPUT_DEVICE_MOUSE) - { + break; + case INPUT_DEVICE_MOUSE: sprintf(s, "(mouse wheel to scroll, left and right click)"); textWidth = TextGetStringWidth(s); textPos.x = pos.x - textWidth / 2; DrawTextString(s, g, textPos); - } - else - { - sprintf(s, "(%s)", InputDeviceName(config->Device)); + break; + case INPUT_DEVICE_JOYSTICK: + sprintf(s, "(%s)", + InputDeviceName(d->pData->inputDevice, d->pData->deviceIndex)); textWidth = TextGetStringWidth(s); textPos.x = pos.x - textWidth / 2; DrawTextString(s, g, textPos); + break; + default: + assert(0 && "unknown device"); + break; } } diff --git a/src/menu_utils.h b/src/menu_utils.h index fcf52a00d..563008c7a 100644 --- a/src/menu_utils.h +++ b/src/menu_utils.h @@ -41,7 +41,11 @@ typedef struct } MenuDisplayPlayerData; void MenuDisplayPlayer(GraphicsDevice *g, Vec2i pos, Vec2i size, void *data); -// data is of type KeyConfig * +typedef struct +{ + struct PlayerData *pData; + KeyConfig *keys; +} MenuDisplayPlayerControlsData; void MenuDisplayPlayerControls( GraphicsDevice *g, Vec2i pos, Vec2i size, void *data); diff --git a/src/password.c b/src/password.c index 4916c7207..4597fe06b 100644 --- a/src/password.c +++ b/src/password.c @@ -234,7 +234,7 @@ static int EnterCode(GraphicsDevice *graphics, const char *password) int cmd; InputPoll(&gInputDevices, SDL_GetTicks()); GraphicsBlitBkg(graphics); - cmd = GetMenuCmd(); + cmd = GetMenuCmd(gPlayerDatas); if (!PasswordEntry(cmd, buffer)) { if (!buffer[0]) diff --git a/src/player_select_menus.c b/src/player_select_menus.c index 2949c30fe..bf8e6bcd3 100644 --- a/src/player_select_menus.c +++ b/src/player_select_menus.c @@ -513,6 +513,8 @@ void PlayerSelectMenusCreate( data->display.c = c; data->display.currentMenu = &ms->current; data->display.pData = pData; + data->controls.keys = key; + data->controls.pData = pData; switch (numPlayers) { @@ -597,7 +599,7 @@ void PlayerSelectMenusCreate( MenuAddSubmenu(ms->root, MenuCreateReturn("Done", 0)); MenuAddExitType(ms, MENU_TYPE_RETURN); MenuSystemAddCustomDisplay(ms, MenuDisplayPlayer, data); - MenuSystemAddCustomDisplay(ms, MenuDisplayPlayerControls, key); + MenuSystemAddCustomDisplay(ms, MenuDisplayPlayerControls, &data->controls); SetPlayer(c, pData); } diff --git a/src/player_select_menus.h b/src/player_select_menus.h index 39739daa1..c117025ec 100644 --- a/src/player_select_menus.h +++ b/src/player_select_menus.h @@ -46,6 +46,7 @@ typedef struct typedef struct { MenuDisplayPlayerData display; + MenuDisplayPlayerControlsData controls; int nameMenuSelection; AppearanceMenuData faceData; AppearanceMenuData skinData; diff --git a/src/prep.c b/src/prep.c index 332289a2a..687c4ae25 100644 --- a/src/prep.c +++ b/src/prep.c @@ -113,7 +113,7 @@ int NumPlayersSelection( res = 0; break; // hack to allow exit } - cmd = GetMenuCmd(); + cmd = GetMenuCmd(gPlayerDatas); MenuProcessCmd(&ms, cmd); if (MenuIsExit(&ms)) { @@ -155,7 +155,7 @@ int PlayerSelection(int numPlayers, GraphicsDevice *graphics) // TODO: destroy menus return 0; // hack to allow exit } - GetPlayerCmds(&cmds); + GetPlayerCmds(&cmds, gPlayerDatas); for (i = 0; i < numPlayers; i++) { MenuProcessCmd(&menus[i].ms, cmds[i]); @@ -211,7 +211,7 @@ int PlayerEquip(int numPlayers, GraphicsDevice *graphics) { return 0; // hack to exit from menu } - GetPlayerCmds(&cmds); + GetPlayerCmds(&cmds, gPlayerDatas); for (i = 0; i < numPlayers; i++) { MenuProcessCmd(&menus[i].ms, cmds[i]); diff --git a/src/weapon_menu.c b/src/weapon_menu.c index b816db5da..3d026efcb 100644 --- a/src/weapon_menu.c +++ b/src/weapon_menu.c @@ -115,6 +115,8 @@ void WeaponMenuCreate( data->display.c = c; data->display.currentMenu = &ms->current; data->display.pData = pData; + data->controls.keys = key; + data->controls.pData = pData; switch (numPlayers) { @@ -152,5 +154,5 @@ void WeaponMenuCreate( MenuAddExitType(ms, MENU_TYPE_RETURN); MenuSystemAddCustomDisplay(ms, MenuDisplayPlayer, &data->display); MenuSystemAddCustomDisplay(ms, DisplayEquippedWeapons, data); - MenuSystemAddCustomDisplay(ms, MenuDisplayPlayerControls, key); + MenuSystemAddCustomDisplay(ms, MenuDisplayPlayerControls, &data->controls); } diff --git a/src/weapon_menu.h b/src/weapon_menu.h index 6ce6cf88b..87557f08c 100644 --- a/src/weapon_menu.h +++ b/src/weapon_menu.h @@ -35,6 +35,7 @@ typedef struct { MenuDisplayPlayerData display; + MenuDisplayPlayerControlsData controls; } WeaponMenuData; typedef struct {