From 973c6212f29b6cf71e92161df1c2d59c1d023f40 Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sat, 11 Dec 2010 13:54:26 -0500 Subject: [PATCH 1/6] eliminate extra loop in input_state also introduces an is_pressed helper method --- gl.c | 41 +++++++++++++++-------------------------- 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/gl.c b/gl.c index 6024236e3b..fac7c6946d 100644 --- a/gl.c +++ b/gl.c @@ -100,6 +100,15 @@ static int init_joypads(int max_pads) return count; } +static bool glfw_is_pressed(int port_num, const struct snes_keybind *key, unsigned char *buttons) +{ + if (glfwGetKey(key->key)) + return true; + if (port_num >= joypad_count) + return false; + return (key->joykey < joypad_buttons[port_num] && buttons[key->joykey] == GLFW_PRESS); +} + static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, bool port, unsigned device, unsigned index, unsigned id) { if ( device != SNES_DEVICE_JOYPAD ) @@ -121,35 +130,15 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b else snes_keybinds = binds[1]; - // Finds fast forwarding state. + // Checks if button is pressed, and sets fast-forwarding state + bool pressed = false; for ( int i = 0; snes_keybinds[i].id != -1; i++ ) - { if ( snes_keybinds[i].id == SNES_FAST_FORWARD_KEY ) - { - bool pressed = false; - if ( glfwGetKey(snes_keybinds[i].key) ) - pressed = true; - else if ( (joypad_count > port_num) && (snes_keybinds[i].joykey < joypad_buttons[port_num]) && (buttons[snes_keybinds[i].joykey] == GLFW_PRESS) ) - pressed = true; - set_fast_forward_button(pressed); - break; - } - } + set_fast_forward_button(glfw_is_pressed(port_num, &snes_keybinds[i], buttons)); + else if ( !pressed && snes_keybinds[i].id == (int)id ) + pressed = glfw_is_pressed(port_num, &snes_keybinds[i], buttons); - // Checks if button is pressed - for ( int i = 0; snes_keybinds[i].id != -1; i++ ) - { - if ( snes_keybinds[i].id == (int)id ) - { - if ( glfwGetKey(snes_keybinds[i].key) ) - return 1; - - if ( (joypad_count > port_num) && (snes_keybinds[i].joykey < joypad_buttons[port_num]) && (buttons[snes_keybinds[i].joykey] == GLFW_PRESS) ) - return 1; - } - } - - return 0; + return pressed; } static void glfw_free_input(void *data) From 19a9641f59819156d2c97126c99cfe63df077f2d Mon Sep 17 00:00:00 2001 From: "Devin J. Pohly" Date: Sat, 11 Dec 2010 13:55:34 -0500 Subject: [PATCH 2/6] add support for joystick axes axes pushed farther than AXIS_THRESHOLD will be counted as button presses --- config.h | 61 +++++++++++++++++++++++++++++++------------------------- driver.h | 1 + gl.c | 22 +++++++++++++++----- 3 files changed, 52 insertions(+), 32 deletions(-) diff --git a/config.h b/config.h index d6aabf4d52..6539d2c196 100644 --- a/config.h +++ b/config.h @@ -118,42 +118,49 @@ static const bool audio_sync = true; // Keybinds, Joypad //////////////////// +// Axis threshold (between 0.0 and 1.0) +// How far an axis must be tilted to result in a button press +#define AXIS_THRESHOLD 0.8 + // To figure out which joypad buttons to use, check jstest or similar. +// Axes are configured using the axis number for the positive (up, right) +// direction and the number's two's-complement (~) for negative directions. +// To use the axis, set the button to -1. // Player 1 static const struct snes_keybind snes_keybinds_1[] = { - // SNES button | keyboard key | joypad button | - { SNES_DEVICE_ID_JOYPAD_A, 'X', 1 }, - { SNES_DEVICE_ID_JOYPAD_B, 'Z', 0 }, - { SNES_DEVICE_ID_JOYPAD_X, 'S', 3 }, - { SNES_DEVICE_ID_JOYPAD_Y, 'A', 2 }, - { SNES_DEVICE_ID_JOYPAD_L, 'Q', 4 }, - { SNES_DEVICE_ID_JOYPAD_R, 'W', 5 }, - { SNES_DEVICE_ID_JOYPAD_LEFT, GLFW_KEY_LEFT, 11 }, - { SNES_DEVICE_ID_JOYPAD_RIGHT, GLFW_KEY_RIGHT, 12 }, - { SNES_DEVICE_ID_JOYPAD_UP, GLFW_KEY_UP, 13 }, - { SNES_DEVICE_ID_JOYPAD_DOWN, GLFW_KEY_DOWN, 14 }, - { SNES_DEVICE_ID_JOYPAD_START, GLFW_KEY_ENTER, 6 }, - { SNES_DEVICE_ID_JOYPAD_SELECT, GLFW_KEY_RSHIFT, 7 }, - { SNES_FAST_FORWARD_KEY, GLFW_KEY_SPACE, 9 }, + // SNES button | keyboard key | js btn | js axis | + { SNES_DEVICE_ID_JOYPAD_A, 'X', 1, 0 }, + { SNES_DEVICE_ID_JOYPAD_B, 'Z', 0, 0 }, + { SNES_DEVICE_ID_JOYPAD_X, 'S', 3, 0 }, + { SNES_DEVICE_ID_JOYPAD_Y, 'A', 2, 0 }, + { SNES_DEVICE_ID_JOYPAD_L, 'Q', 4, 0 }, + { SNES_DEVICE_ID_JOYPAD_R, 'W', 5, 0 }, + { SNES_DEVICE_ID_JOYPAD_LEFT, GLFW_KEY_LEFT, 11, ~4 }, + { SNES_DEVICE_ID_JOYPAD_RIGHT, GLFW_KEY_RIGHT, 12, 4 }, + { SNES_DEVICE_ID_JOYPAD_UP, GLFW_KEY_UP, 13, 5 }, + { SNES_DEVICE_ID_JOYPAD_DOWN, GLFW_KEY_DOWN, 14, ~5 }, + { SNES_DEVICE_ID_JOYPAD_START, GLFW_KEY_ENTER, 6, 0 }, + { SNES_DEVICE_ID_JOYPAD_SELECT, GLFW_KEY_RSHIFT, 7, 0 }, + { SNES_FAST_FORWARD_KEY, GLFW_KEY_SPACE, 9, 0 }, { -1 } }; // Player 2 static const struct snes_keybind snes_keybinds_2[] = { - // SNES button | keyboard key | joypad button | - { SNES_DEVICE_ID_JOYPAD_A, 'B', 1 }, - { SNES_DEVICE_ID_JOYPAD_B, 'V', 0 }, - { SNES_DEVICE_ID_JOYPAD_X, 'G', 3 }, - { SNES_DEVICE_ID_JOYPAD_Y, 'F', 2 }, - { SNES_DEVICE_ID_JOYPAD_L, 'R', 4 }, - { SNES_DEVICE_ID_JOYPAD_R, 'T', 5 }, - { SNES_DEVICE_ID_JOYPAD_LEFT, 'J', 11 }, - { SNES_DEVICE_ID_JOYPAD_RIGHT, 'L', 12 }, - { SNES_DEVICE_ID_JOYPAD_UP, 'I', 13 }, - { SNES_DEVICE_ID_JOYPAD_DOWN, 'K', 14 }, - { SNES_DEVICE_ID_JOYPAD_START, 'P', 6 }, - { SNES_DEVICE_ID_JOYPAD_SELECT, 'O', 7 }, + // SNES button | keyboard key | js btn | js axis | + { SNES_DEVICE_ID_JOYPAD_A, 'B', 1, 0 }, + { SNES_DEVICE_ID_JOYPAD_B, 'V', 0, 0 }, + { SNES_DEVICE_ID_JOYPAD_X, 'G', 3, 0 }, + { SNES_DEVICE_ID_JOYPAD_Y, 'F', 2, 0 }, + { SNES_DEVICE_ID_JOYPAD_L, 'R', 4, 0 }, + { SNES_DEVICE_ID_JOYPAD_R, 'T', 5, 0 }, + { SNES_DEVICE_ID_JOYPAD_LEFT, 'J', 11, ~4 }, + { SNES_DEVICE_ID_JOYPAD_RIGHT, 'L', 12, 4 }, + { SNES_DEVICE_ID_JOYPAD_UP, 'I', 13, 5 }, + { SNES_DEVICE_ID_JOYPAD_DOWN, 'K', 14, ~5 }, + { SNES_DEVICE_ID_JOYPAD_START, 'P', 6, 0 }, + { SNES_DEVICE_ID_JOYPAD_SELECT, 'O', 7, 0 }, { -1 } }; diff --git a/driver.h b/driver.h index 346b75ec64..366021bbde 100644 --- a/driver.h +++ b/driver.h @@ -32,6 +32,7 @@ struct snes_keybind int id; int key; int joykey; + int joyaxis; }; typedef struct video_info diff --git a/gl.c b/gl.c index fac7c6946d..64b173bef1 100644 --- a/gl.c +++ b/gl.c @@ -75,9 +75,11 @@ static void glfw_input_poll(void *data) } #define BUTTONS_MAX 128 +#define AXES_MAX 128 static int joypad_id[2]; static int joypad_buttons[2]; +static int joypad_axes[2]; static bool joypad_inited = false; static int joypad_count = 0; @@ -93,6 +95,9 @@ static int init_joypads(int max_pads) joypad_buttons[count] = glfwGetJoystickParam(i, GLFW_BUTTONS); if (joypad_buttons[count] > BUTTONS_MAX) joypad_buttons[count] = BUTTONS_MAX; + joypad_axes[count] = glfwGetJoystickParam(i, GLFW_AXES); + if (joypad_axes[count] > AXES_MAX) + joypad_axes[count] = AXES_MAX; count++; } } @@ -100,13 +105,17 @@ static int init_joypads(int max_pads) return count; } -static bool glfw_is_pressed(int port_num, const struct snes_keybind *key, unsigned char *buttons) +static bool glfw_is_pressed(int port_num, const struct snes_keybind *key, unsigned char *buttons, float *axes) { if (glfwGetKey(key->key)) return true; if (port_num >= joypad_count) return false; - return (key->joykey < joypad_buttons[port_num] && buttons[key->joykey] == GLFW_PRESS); + if (key->joykey >= 0) + return (key->joykey < joypad_buttons[port_num] && buttons[key->joykey] == GLFW_PRESS); + if (key->joyaxis >= 0) + return (key->joyaxis < joypad_axes[port_num] && axes[key->joyaxis] >= AXIS_THRESHOLD); + return (~key->joyaxis < joypad_axes[port_num] && axes[~key->joyaxis] <= -AXIS_THRESHOLD); } static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, bool port, unsigned device, unsigned index, unsigned id) @@ -119,9 +128,12 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b int port_num = port ? 1 : 0; unsigned char buttons[BUTTONS_MAX]; + float axes[AXES_MAX]; - if ( joypad_count > port_num ) + if ( joypad_count > port_num ) { glfwGetJoystickButtons(joypad_id[port_num], buttons, joypad_buttons[port_num]); + glfwGetJoystickPos(joypad_id[port_num], axes, joypad_axes[port_num]); + } const struct snes_keybind *snes_keybinds; @@ -134,9 +146,9 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b bool pressed = false; for ( int i = 0; snes_keybinds[i].id != -1; i++ ) if ( snes_keybinds[i].id == SNES_FAST_FORWARD_KEY ) - set_fast_forward_button(glfw_is_pressed(port_num, &snes_keybinds[i], buttons)); + set_fast_forward_button(glfw_is_pressed(port_num, &snes_keybinds[i], buttons, axes)); else if ( !pressed && snes_keybinds[i].id == (int)id ) - pressed = glfw_is_pressed(port_num, &snes_keybinds[i], buttons); + pressed = glfw_is_pressed(port_num, &snes_keybinds[i], buttons, axes); return pressed; } From 1ce718ed58772fa546a53e9ce669e9fa417cca84 Mon Sep 17 00:00:00 2001 From: Themaister Date: Tue, 14 Dec 2010 14:22:26 +0100 Subject: [PATCH 3/6] Add authors file --- AUTHORS | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 AUTHORS diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000000..7261b0220d --- /dev/null +++ b/AUTHORS @@ -0,0 +1,5 @@ +Hans-Kristian Arntzen - + - Main code, maintainer + +Devin J. Pohly - + - Joypad axis support From 8a030675a441189df7e4e83ba0bb7dbfa0753380 Mon Sep 17 00:00:00 2001 From: Themaister Date: Tue, 14 Dec 2010 14:27:29 +0100 Subject: [PATCH 4/6] Clean up indentation and one code style issue. --- gl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gl.c b/gl.c index 64b173bef1..a5a9118aed 100644 --- a/gl.c +++ b/gl.c @@ -130,7 +130,8 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b unsigned char buttons[BUTTONS_MAX]; float axes[AXES_MAX]; - if ( joypad_count > port_num ) { + if ( joypad_count > port_num ) + { glfwGetJoystickButtons(joypad_id[port_num], buttons, joypad_buttons[port_num]); glfwGetJoystickPos(joypad_id[port_num], axes, joypad_axes[port_num]); } From 81ba2daa04e49837e64a317c746ab0f043e84b3c Mon Sep 17 00:00:00 2001 From: Themaister Date: Tue, 14 Dec 2010 14:28:09 +0100 Subject: [PATCH 5/6] indent... --- gl.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gl.c b/gl.c index a5a9118aed..5f9ed843e0 100644 --- a/gl.c +++ b/gl.c @@ -215,11 +215,11 @@ static float tv_to_fps(const struct timeval *tv, const struct timeval *new_tv, i static inline void show_fps(void) { -// Shows FPS in taskbar. + // Shows FPS in taskbar. static int frames = 0; static struct timeval tv; struct timeval new_tv; - + if (frames == 0) gettimeofday(&tv, NULL); @@ -262,7 +262,7 @@ static bool gl_frame(void *data, const uint16_t* frame, int width, int height, i 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, frame); glDrawArrays(GL_QUADS, 0, 4); - + show_fps(); glfwSwapBuffers(); @@ -405,6 +405,6 @@ const video_driver_t video_gl = { .set_nonblock_state = gl_set_nonblock_state, .free = gl_free }; - + From c589d82596bf812669ead1c3df191a71d8bc3351 Mon Sep 17 00:00:00 2001 From: Themaister Date: Tue, 14 Dec 2010 15:19:13 +0100 Subject: [PATCH 6/6] Clean up some stupid code. :p --- gl.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/gl.c b/gl.c index 5f9ed843e0..f7bf756730 100644 --- a/gl.c +++ b/gl.c @@ -226,10 +226,7 @@ static inline void show_fps(void) if ((frames % 180) == 0 && frames > 0) { gettimeofday(&new_tv, NULL); - struct timeval tmp_tv = { - .tv_sec = tv.tv_sec, - .tv_usec = tv.tv_usec - }; + struct timeval tmp_tv = tv; gettimeofday(&tv, NULL); char tmpstr[256] = {0};