From a4861de89f5659cda462713016bfe5c16b1cb076 Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 30 May 2014 20:11:37 +0100 Subject: [PATCH] [Change] Much better handling of joystick axes. --- src/conf.c | 18 ++---- src/input.c | 163 ++++++++++++++++++++++++++------------------------ src/input.h | 11 ++-- src/options.c | 20 ++++--- src/player.c | 14 +++-- 5 files changed, 115 insertions(+), 111 deletions(-) diff --git a/src/conf.c b/src/conf.c index 6a97a14..f57fff7 100644 --- a/src/conf.c +++ b/src/conf.c @@ -105,7 +105,7 @@ int conf_loadConfig(const char* file) { double d; char* str, *mod; SDLKey key; - int type, reverse; + int type; int w, h, fsaa; SDLMod m; @@ -230,17 +230,6 @@ int conf_loadConfig(const char* file) { } lua_pop(L, 1); - /* Is it reversed? Only used for axis. */ - lua_pushstring(L, "reverse"); - lua_gettable(L, -2); - if(lua_isnumber(L, -1)) - reverse = !!(int)lua_tonumber(L, -1); - else if(lua_isboolean(L, -1)) - reverse = lua_toboolean(L, -1); - else - reverse = 0; - lua_pop(L, 1); - /* Get the modifier. */ lua_pushstring(L, "mod"); lua_gettable(L, -2); @@ -254,7 +243,8 @@ int conf_loadConfig(const char* file) { /* Then the keybind is valid. Get the type. */ if(strcmp(str, "null") ==0) type = KEYBIND_NULL; else if(strcmp(str, "keyboard") ==0) type = KEYBIND_KEYBOARD; - else if(strcmp(str, "jaxis") ==0) type = KEYBIND_JAXIS; + else if(strcmp(str, "jaxispos") ==0) type = KEYBIND_JAXISPOS; + else if(strcmp(str, "jaxisneg") ==0) type = KEYBIND_JAXISNEG; else if(strcmp(str, "jbutton") ==0) type = KEYBIND_JBUTTON; else { WARN("Unknown keybinding of type %s", str); @@ -280,7 +270,7 @@ int conf_loadConfig(const char* file) { } else m = KMOD_NONE; /* Set the keybind. */ - input_setKeybind((char*)keybindNames[i], type, key, m, reverse); + input_setKeybind((char*)keybindNames[i], type, key, m); } else { WARN("Malformed keybind for %s in '%s'", keybindNames[i], file); } diff --git a/src/input.c b/src/input.c index 2272905..5e1abe5 100644 --- a/src/input.c +++ b/src/input.c @@ -29,7 +29,6 @@ typedef struct Keybind_ { char* name; /**< Keybinding name, taken from keybindNames[] */ KeybindType type; /**< type, defined in player.h. */ SDLKey key; /**< Key/axis/button event number. */ - double reverse; /**< 1. if normal, -1 if reversed, only useful for joystick axis. */ SDLMod mod; /**< Key modifiers (where applicable). */ } Keybind; @@ -110,8 +109,8 @@ const char* keybindDescription[] = { static unsigned int input_accelLast = 0; /**< Used to see if double tap. */ unsigned int input_afterburnSensibility = 200; /**< ms between taps to afterburn. */ -/* From player.c */ -extern double player_turn; +extern double player_left; /**< player.c */ +extern double player_right; /**< player.c */ #if SDL_VERSION_ATLEAST(1,3,0) # define INPUT_NUMKEYS SDL_NUM_SCANCODES /**< Number of keys available. */ @@ -128,46 +127,46 @@ static void input_keyConvDestroy(void); */ void input_setDefault(void) { /* Movement. */ - input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, KMOD_ALL, 0); - input_setKeybind("afterburn", KEYBIND_NULL, SDLK_UNKNOWN, KMOD_NONE, 0); - input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, KMOD_ALL, 0); - input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, KMOD_ALL, 0); - input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, KMOD_ALL, 0); + input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, KMOD_ALL); + input_setKeybind("afterburn", KEYBIND_NULL, SDLK_UNKNOWN, KMOD_NONE); + input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, KMOD_ALL); + input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, KMOD_ALL); + input_setKeybind("reverse", KEYBIND_KEYBOARD, SDLK_s, KMOD_ALL); /* Targetting. */ - input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_NONE, 0); - input_setKeybind("target_prev", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_RCTRL, 0); - input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, KMOD_NONE, 0); - input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, KMOD_NONE, 0); + input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_NONE); + input_setKeybind("target_prev", KEYBIND_KEYBOARD, SDLK_TAB, KMOD_RCTRL); + input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_t, KMOD_NONE); + input_setKeybind("target_hostile", KEYBIND_KEYBOARD, SDLK_r, KMOD_NONE); /* Combat. */ - input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, KMOD_ALL, 0); - input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, KMOD_NONE, 0); - input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, KMOD_NONE, 0); + input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, KMOD_ALL); + input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, KMOD_NONE); + input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, KMOD_NONE); /* Escorts. */ - input_setKeybind("e_attack", KEYBIND_KEYBOARD, SDLK_1, KMOD_NONE, 0); - input_setKeybind("e_hold", KEYBIND_KEYBOARD, SDLK_2, KMOD_NONE, 0); - input_setKeybind("e_return", KEYBIND_KEYBOARD, SDLK_3, KMOD_NONE, 0); - input_setKeybind("e_clear", KEYBIND_KEYBOARD, SDLK_4, KMOD_NONE, 0); + input_setKeybind("e_attack", KEYBIND_KEYBOARD, SDLK_1, KMOD_NONE); + input_setKeybind("e_hold", KEYBIND_KEYBOARD, SDLK_2, KMOD_NONE); + input_setKeybind("e_return", KEYBIND_KEYBOARD, SDLK_3, KMOD_NONE); + input_setKeybind("e_clear", KEYBIND_KEYBOARD, SDLK_4, KMOD_NONE); /* Secondary weapon. */ - input_setKeybind("secondary", KEYBIND_KEYBOARD, SDLK_LALT, KMOD_ALL, 0); - input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_e, KMOD_NONE, 0); - input_setKeybind("secondary_prev", KEYBIND_KEYBOARD, SDLK_e, KMOD_LCTRL, 0); + input_setKeybind("secondary", KEYBIND_KEYBOARD, SDLK_LALT, KMOD_ALL); + input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_e, KMOD_NONE); + input_setKeybind("secondary_prev", KEYBIND_KEYBOARD, SDLK_e, KMOD_LCTRL); /* Space */ - input_setKeybind("autonav", KEYBIND_KEYBOARD, SDLK_j, KMOD_LCTRL, 0); - input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, KMOD_NONE, 0); - input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, KMOD_NONE, 0); - input_setKeybind("thyperspace", KEYBIND_KEYBOARD, SDLK_h, KMOD_NONE, 0); - input_setKeybind("starmap", KEYBIND_KEYBOARD, SDLK_m, KMOD_NONE, 0); - input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, KMOD_NONE, 0); + input_setKeybind("autonav", KEYBIND_KEYBOARD, SDLK_j, KMOD_LCTRL); + input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, KMOD_NONE); + input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, KMOD_NONE); + input_setKeybind("thyperspace", KEYBIND_KEYBOARD, SDLK_h, KMOD_NONE); + input_setKeybind("starmap", KEYBIND_KEYBOARD, SDLK_m, KMOD_NONE); + input_setKeybind("jump", KEYBIND_KEYBOARD, SDLK_j, KMOD_NONE); /* Communication. */ - input_setKeybind("hail", KEYBIND_KEYBOARD, SDLK_y, KMOD_NONE, 0); + input_setKeybind("hail", KEYBIND_KEYBOARD, SDLK_y, KMOD_NONE); /* Misc. */ - input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_KP_PLUS, KMOD_ALL, 0); - input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, KMOD_ALL, 0); - input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, KMOD_ALL, 0); - input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, KMOD_NONE, 0); - input_setKeybind("speed", KEYBIND_KEYBOARD, SDLK_BACKQUOTE, KMOD_ALL, 0); - input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, KMOD_ALL, 0); - input_setKeybind("info", KEYBIND_KEYBOARD, SDLK_i, KMOD_NONE, 0); + input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_KP_PLUS, KMOD_ALL); + input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_KP_MINUS, KMOD_ALL); + input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_KP_MULTIPLY, KMOD_ALL); + input_setKeybind("pause", KEYBIND_KEYBOARD, SDLK_F1, KMOD_NONE); + input_setKeybind("speed", KEYBIND_KEYBOARD, SDLK_BACKQUOTE, KMOD_ALL); + input_setKeybind("menu", KEYBIND_KEYBOARD, SDLK_ESCAPE, KMOD_ALL); + input_setKeybind("info", KEYBIND_KEYBOARD, SDLK_i, KMOD_NONE); } /** @@ -218,7 +217,6 @@ void input_init(void) { tmp->type = KEYBIND_NULL; tmp->key = SDLK_UNKNOWN; tmp->mod = KMOD_NONE; - tmp->reverse = 1.; input_keybinds[i] = tmp; } @@ -305,10 +303,8 @@ SDLKey input_keyConv(char* name) { * @param type The type of the keybind. * @param key The key to bind to. * @param mod Modifiers to check for. - * @param reverse Whether to reverse it or not. */ -void input_setKeybind(char* keybind, KeybindType type, int key, - SDLMod mod, int reverse) { +void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(strcmp(keybind, input_keybinds[i]->name)==0) { @@ -316,7 +312,6 @@ void input_setKeybind(char* keybind, KeybindType type, int key, input_keybinds[i]->key = key; /* Non-keyboards get mod KMOD_ALL to always match. */ input_keybinds[i]->mod = (type == KEYBIND_KEYBOARD) ? mod : KMOD_ALL; - input_keybinds[i]->reverse = reverse ? -1. : 1.; return; } WARN("Unable to set keybind '%s', That command does not exist.", keybind); @@ -324,14 +319,17 @@ void input_setKeybind(char* keybind, KeybindType type, int key, /** * @brief Get the value of a keybind. + * @param keybind Name of the keybinding to get. + * @param[out] type Store the type of the keybinding. + * @param[out] mod Store the modifiers used with the keybinding. + * @return The key assosciated with the keybinding. */ -SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod, int* reverse) { +SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod) { int i; for(i = 0; strcmp(keybindNames[i], "end"); i++) if(strcmp(keybind, input_keybinds[i]->name)==0) { if(type != NULL) (*type) = input_keybinds[i]->type; if(mod != NULL) (*mod) = input_keybinds[i]->mod; - if(reverse != NULL) (*reverse) = input_keybinds[i]->reverse; return input_keybinds[i]->key; } @@ -364,9 +362,9 @@ const char* input_getKeybindDescription(char* keybind) { * @brief Run the input command. * @param keynum The index of the keybind. * @param value The value of the keypress (defined above). - * @param kabs Whether or not it's an absolute value (for the joystick). + * @param kabs The absolute value. */ -static void input_key(int keynum, double value, int kabs) { +static void input_key(int keynum, double value, double kabs) { unsigned int t; /* Accelerating. */ @@ -403,37 +401,41 @@ static void input_key(int keynum, double value, int kabs) { } /* Turning left. */ else if(KEY("left")) { - if(kabs) - player_turn = -value; - else { + if(kabs) { + player_abortAutonav(NULL); + player_setFlag(PLAYER_TURN_LEFT); + player_left = value; + } else { /* Set flags for facing correction. */ if(value == KEY_PRESS) { player_abortAutonav(NULL); player_setFlag(PLAYER_TURN_LEFT); + player_left = 1.; } - else if(value == KEY_RELEASE) + else if(value == KEY_RELEASE) { player_rmFlag(PLAYER_TURN_LEFT); - player_turn -= value; + player_left = 0.; + } } - /* Make sure the value is sane. */ - if(player_turn < -1.) player_turn = -1.; } /* Turning right. */ else if(KEY("right")) { - if(kabs) - player_turn = value; - else { + if(kabs) { + player_abortAutonav(NULL); + player_setFlag(PLAYER_TURN_RIGHT); + player_right = value; + } else { /* Set flags for facing correction. */ if(value == KEY_PRESS) { player_abortAutonav(NULL); player_setFlag(PLAYER_TURN_RIGHT); + player_right = 1.; } - else if(value == KEY_RELEASE) + else if(value == KEY_RELEASE) { player_rmFlag(PLAYER_TURN_RIGHT); - player_turn += value; + player_right = 0.; + } } - /* Make sure the value is sane. */ - if(player_turn > 1.) player_turn = 1.; } /* Turn around to face vel. */ else if(KEY("reverse")) { @@ -441,14 +443,11 @@ static void input_key(int keynum, double value, int kabs) { player_abortAutonav(NULL); player_setFlag(PLAYER_REVERSE); } - else if((value == KEY_RELEASE) && player_isFlag(PLAYER_REVERSE)) { + else if((value == KEY_RELEASE) && player_isFlag(PLAYER_REVERSE)) player_rmFlag(PLAYER_REVERSE); - player_turn = 0; /* Turning corrections. */ - if(player_isFlag(PLAYER_TURN_LEFT)) { player_turn -= 1; } - if(player_isFlag(PLAYER_TURN_RIGHT)) { player_turn += 1; } - } } - /* Shoot primary weapon. BOOM BOOM. */ + /* Combat. */ + /* Shooting primary weapon. */ else if(KEY("primary")) { if(value == KEY_PRESS) { player_abortAutonav(NULL); @@ -475,14 +474,8 @@ static void input_key(int keynum, double value, int kabs) { player_abortAutonav(NULL); player_setFlag(PLAYER_FACE); } - else if((value == KEY_RELEASE) && player_isFlag(PLAYER_FACE)) { + else if((value == KEY_RELEASE) && player_isFlag(PLAYER_FACE)) player_rmFlag(PLAYER_FACE); - - /* Turning corrections. */ - player_turn = 0; - if(player_isFlag(PLAYER_TURN_LEFT)) { player_turn -= 1; } - if(player_isFlag(PLAYER_TURN_RIGHT)) { player_turn += 1; } - } } /* Board those ships. */ else if(KEY("board") && INGAME() && NOHYP()) { @@ -609,11 +602,23 @@ static void input_keyevent(const int event, const SDLKey key, const SDLMod mod); * @param value Value of the axis. */ static void input_joyaxis(const SDLKey axis, const int value) { - int i; - for(i = 0; strcmp(keybindNames[i], "end"); i++) - if(input_keybinds[i]->type == KEYBIND_JAXIS && - input_keybinds[i]->key == axis) - input_key(i, -(input_keybinds[i]->reverse) * (double)value / 32767., 1); + int i, k; + for(i = 0; strcmp(keybindNames[i], "end"); i++) { + if(input_keybinds[i]->key == axis) { + /* Positive axis keybinding. */ + if((input_keybinds[i]->type == KEYBIND_JAXISPOS) + && (value >= 0)) { + k = (value > 0) ? KEY_PRESS : KEY_RELEASE; + input_key(i, k, fabs(((double)value)/32767.)); + } + /* Negative axis keybinding. */ + if((input_keybinds[i]->type == KEYBIND_JAXISNEG) + && (value <= 0)) { + k = (value < 0) ? KEY_PRESS : KEY_RELEASE; + input_key(i, k, fabs(((double)value)/32767.)); + } + } + } } /** @@ -626,7 +631,7 @@ static void input_joyevent(const int event, const SDLKey button) { for(i = 0; strcmp(keybindNames[i], "end");i++) if(input_keybinds[i]->type == KEYBIND_JBUTTON && input_keybinds[i]->key == button) - input_key(i, event, 0); + input_key(i, event, 0.); } /* Keyboard. */ @@ -650,7 +655,7 @@ static void input_keyevent(const int event, SDLKey key, const SDLMod mod) { if((input_keybinds[i]->mod == mod_filtered) || (input_keybinds[i]->mod == KMOD_ALL) || (event == KEY_RELEASE)) /**< Release always gets through. */ - input_key(i, event, 0); + input_key(i, event, 0.); /* No break so all keys get pressed if needed. */ } } diff --git a/src/input.h b/src/input.h index b2a1e61..5b9eef8 100644 --- a/src/input.h +++ b/src/input.h @@ -5,17 +5,18 @@ /* Input types. */ typedef enum { - KEYBIND_NULL, - KEYBIND_KEYBOARD, - KEYBIND_JAXIS, + KEYBIND_NULL, /**< Null keybinding. */ + KEYBIND_KEYBOARD, /**< Keyboard keybinding. */ + KEYBIND_JAXISPOS, /**< Joystick axis positive side keybinding. */ + KEYBIND_JAXISNEG, /**< Joystick axis negative side keybinding. */ KEYBIND_JBUTTON } KeybindType; /* Set input. */ void input_setDefault(void); SDLKey input_keyConv(char* name); -void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod, int reverse); -SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod, int* reverse); +void input_setKeybind(char* keybind, KeybindType type, int key, SDLMod mod); +SDLKey input_getKeybind(const char* keybind, KeybindType* type, SDLMod* mod); const char* input_getKeybindDescription(char* keybind); /* Handle the events. */ diff --git a/src/options.c b/src/options.c index 0a4777c..96bf73d 100644 --- a/src/options.c +++ b/src/options.c @@ -44,7 +44,6 @@ void opt_menuKeybinds(void) { SDLKey key; KeybindType type; SDLMod mod; - int reverse; /* Create the window. */ wid = window_create("Keybindings", -1, -1, KEYBINDS_WIDTH, KEYBINDS_HEIGHT); @@ -63,7 +62,7 @@ void opt_menuKeybinds(void) { str = malloc(sizeof(char*) * i); for(j = 0; j < i; j++) { str[j] = malloc(sizeof(char) * 64); - key = input_getKeybind(keybindNames[j], &type, &mod, &reverse); + key = input_getKeybind(keybindNames[j], &type, &mod); switch(type) { case KEYBIND_KEYBOARD: /* SDL_GetKeyName returns lowercase which is ugly. */ @@ -72,11 +71,13 @@ void opt_menuKeybinds(void) { else snprintf(str[j], 64, "%s <%s>", keybindNames[j], SDL_GetKeyName(key)); break; - case KEYBIND_JAXIS: - snprintf(str[j], 64, "%s ", keybindNames[j], key); + case KEYBIND_JAXISPOS: + snprintf(str[j], 64, "%s ", keybindNames[j], key); break; + case KEYBIND_JAXISNEG: + snprintf(str[j], 64, "%s ", keybindNames[j], key); case KEYBIND_JBUTTON: - snprintf(str[j], 64, "%s ", keybindNames[j], key); + snprintf(str[j], 64, "%s ", keybindNames[j], key); break; default: snprintf(str[j], 64, "%s", keybindNames[j]); @@ -124,7 +125,6 @@ static void menuKeybinds_update(unsigned int wid, char* name) { SDLKey key; KeybindType type; SDLMod mod; - int reverse; char buf[1024]; char bind[32]; @@ -139,7 +139,7 @@ static void menuKeybinds_update(unsigned int wid, char* name) { /* Get information. */ desc = input_getKeybindDescription(keybind); - key = input_getKeybind(keybind, &type, &mod, &reverse); + key = input_getKeybind(keybind, &type, &mod); /* Create the text. */ switch(type) { @@ -159,9 +159,11 @@ static void menuKeybinds_update(unsigned int wid, char* name) { (mod != KMOD_NONE) ? " + " : "", SDL_GetKeyName(key)); break; - case KEYBIND_JAXIS: - snprintf(bind, 64, "joy axis: <%d>%s", key, (reverse) ? " rev" : ""); + case KEYBIND_JAXISPOS: + snprintf(bind, 64, "joy axis pos: <%d>", key); break; + case KEYBIND_JAXISNEG: + snprintf(bind, 64, "joy axis neg: <%d>", key); case KEYBIND_JBUTTON: snprintf(bind, 64, "joy button: <%d>", key); break; diff --git a/src/player.c b/src/player.c index 9be6006..aa52a10 100644 --- a/src/player.c +++ b/src/player.c @@ -89,7 +89,8 @@ double player_crating = 0; /**< Player combar rating. */ unsigned int player_flags = 0; /**< Player flags. */ /* Input.c */ -double player_turn = 0.; /**< Turn velocity from input. */ +double player_left = 0.; /**< Player left turn velocity from input.c. */ +double player_right = 0.; /**< Player right turn velocity from input.c */ static double player_acc = 0.; /**< Accel velocity from input. */ /* Internal */ @@ -1804,6 +1805,7 @@ void player_abortAutonav(char* reason) { void player_think(Pilot* pplayer) { Pilot* target; double d; + double turn; /* Last I checked, the dead didn't think.. */ if(pilot_isFlag(pplayer, PILOT_DEAD)) { @@ -1864,11 +1866,15 @@ void player_think(Pilot* pplayer) { pilot_face(pplayer, VANGLE(player->solid->vel) + M_PI); } - /* Normal navigation sheme. */ + /* Normal turning sheme. */ else { pplayer->solid->dir_vel = 0.; - if(player_turn) - pplayer->solid->dir_vel -= player->turn * player_turn; + turn = 0; + if(player_isFlag(PLAYER_TURN_LEFT)) + turn -= player_left; + if(player_isFlag(PLAYER_TURN_RIGHT)) + turn += player_right; + pplayer->solid->dir_vel -= pplayer->turn * turn; } /* Weapon shooting stuff. */