Merge branch 'master' into play

This commit is contained in:
Themaister 2010-12-22 03:35:56 +01:00
commit 64239f5f47
4 changed files with 74 additions and 62 deletions

5
AUTHORS Normal file
View File

@ -0,0 +1,5 @@
Hans-Kristian Arntzen - <maister@archlinux.us>
- Main code, maintainer
Devin J. Pohly - <djpohly@djpohly.com>
- Joypad axis support

View File

@ -118,42 +118,49 @@ static const bool audio_sync = true;
// Keybinds, Joypad // 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. // 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 // Player 1
static const struct snes_keybind snes_keybinds_1[] = { static const struct snes_keybind snes_keybinds_1[] = {
// SNES button | keyboard key | joypad button | // SNES button | keyboard key | js btn | js axis |
{ SNES_DEVICE_ID_JOYPAD_A, 'X', 1 }, { SNES_DEVICE_ID_JOYPAD_A, 'X', 1, 0 },
{ SNES_DEVICE_ID_JOYPAD_B, 'Z', 0 }, { SNES_DEVICE_ID_JOYPAD_B, 'Z', 0, 0 },
{ SNES_DEVICE_ID_JOYPAD_X, 'S', 3 }, { SNES_DEVICE_ID_JOYPAD_X, 'S', 3, 0 },
{ SNES_DEVICE_ID_JOYPAD_Y, 'A', 2 }, { SNES_DEVICE_ID_JOYPAD_Y, 'A', 2, 0 },
{ SNES_DEVICE_ID_JOYPAD_L, 'Q', 4 }, { SNES_DEVICE_ID_JOYPAD_L, 'Q', 4, 0 },
{ SNES_DEVICE_ID_JOYPAD_R, 'W', 5 }, { SNES_DEVICE_ID_JOYPAD_R, 'W', 5, 0 },
{ SNES_DEVICE_ID_JOYPAD_LEFT, GLFW_KEY_LEFT, 11 }, { SNES_DEVICE_ID_JOYPAD_LEFT, GLFW_KEY_LEFT, 11, ~4 },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, GLFW_KEY_RIGHT, 12 }, { SNES_DEVICE_ID_JOYPAD_RIGHT, GLFW_KEY_RIGHT, 12, 4 },
{ SNES_DEVICE_ID_JOYPAD_UP, GLFW_KEY_UP, 13 }, { SNES_DEVICE_ID_JOYPAD_UP, GLFW_KEY_UP, 13, 5 },
{ SNES_DEVICE_ID_JOYPAD_DOWN, GLFW_KEY_DOWN, 14 }, { SNES_DEVICE_ID_JOYPAD_DOWN, GLFW_KEY_DOWN, 14, ~5 },
{ SNES_DEVICE_ID_JOYPAD_START, GLFW_KEY_ENTER, 6 }, { SNES_DEVICE_ID_JOYPAD_START, GLFW_KEY_ENTER, 6, 0 },
{ SNES_DEVICE_ID_JOYPAD_SELECT, GLFW_KEY_RSHIFT, 7 }, { SNES_DEVICE_ID_JOYPAD_SELECT, GLFW_KEY_RSHIFT, 7, 0 },
{ SNES_FAST_FORWARD_KEY, GLFW_KEY_SPACE, 9 }, { SNES_FAST_FORWARD_KEY, GLFW_KEY_SPACE, 9, 0 },
{ -1 } { -1 }
}; };
// Player 2 // Player 2
static const struct snes_keybind snes_keybinds_2[] = { static const struct snes_keybind snes_keybinds_2[] = {
// SNES button | keyboard key | joypad button | // SNES button | keyboard key | js btn | js axis |
{ SNES_DEVICE_ID_JOYPAD_A, 'B', 1 }, { SNES_DEVICE_ID_JOYPAD_A, 'B', 1, 0 },
{ SNES_DEVICE_ID_JOYPAD_B, 'V', 0 }, { SNES_DEVICE_ID_JOYPAD_B, 'V', 0, 0 },
{ SNES_DEVICE_ID_JOYPAD_X, 'G', 3 }, { SNES_DEVICE_ID_JOYPAD_X, 'G', 3, 0 },
{ SNES_DEVICE_ID_JOYPAD_Y, 'F', 2 }, { SNES_DEVICE_ID_JOYPAD_Y, 'F', 2, 0 },
{ SNES_DEVICE_ID_JOYPAD_L, 'R', 4 }, { SNES_DEVICE_ID_JOYPAD_L, 'R', 4, 0 },
{ SNES_DEVICE_ID_JOYPAD_R, 'T', 5 }, { SNES_DEVICE_ID_JOYPAD_R, 'T', 5, 0 },
{ SNES_DEVICE_ID_JOYPAD_LEFT, 'J', 11 }, { SNES_DEVICE_ID_JOYPAD_LEFT, 'J', 11, ~4 },
{ SNES_DEVICE_ID_JOYPAD_RIGHT, 'L', 12 }, { SNES_DEVICE_ID_JOYPAD_RIGHT, 'L', 12, 4 },
{ SNES_DEVICE_ID_JOYPAD_UP, 'I', 13 }, { SNES_DEVICE_ID_JOYPAD_UP, 'I', 13, 5 },
{ SNES_DEVICE_ID_JOYPAD_DOWN, 'K', 14 }, { SNES_DEVICE_ID_JOYPAD_DOWN, 'K', 14, ~5 },
{ SNES_DEVICE_ID_JOYPAD_START, 'P', 6 }, { SNES_DEVICE_ID_JOYPAD_START, 'P', 6, 0 },
{ SNES_DEVICE_ID_JOYPAD_SELECT, 'O', 7 }, { SNES_DEVICE_ID_JOYPAD_SELECT, 'O', 7, 0 },
{ -1 } { -1 }
}; };

View File

@ -32,6 +32,7 @@ struct snes_keybind
int id; int id;
int key; int key;
int joykey; int joykey;
int joyaxis;
}; };
typedef struct video_info typedef struct video_info

61
gl.c
View File

@ -75,9 +75,11 @@ static void glfw_input_poll(void *data)
} }
#define BUTTONS_MAX 128 #define BUTTONS_MAX 128
#define AXES_MAX 128
static int joypad_id[2]; static int joypad_id[2];
static int joypad_buttons[2]; static int joypad_buttons[2];
static int joypad_axes[2];
static bool joypad_inited = false; static bool joypad_inited = false;
static int joypad_count = 0; static int joypad_count = 0;
@ -93,6 +95,9 @@ static int init_joypads(int max_pads)
joypad_buttons[count] = glfwGetJoystickParam(i, GLFW_BUTTONS); joypad_buttons[count] = glfwGetJoystickParam(i, GLFW_BUTTONS);
if (joypad_buttons[count] > BUTTONS_MAX) if (joypad_buttons[count] > BUTTONS_MAX)
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++; count++;
} }
} }
@ -100,6 +105,19 @@ static int init_joypads(int max_pads)
return count; return count;
} }
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;
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) 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 ) if ( device != SNES_DEVICE_JOYPAD )
@ -110,9 +128,13 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b
int port_num = port ? 1 : 0; int port_num = port ? 1 : 0;
unsigned char buttons[BUTTONS_MAX]; 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]); 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; const struct snes_keybind *snes_keybinds;
@ -121,35 +143,15 @@ static int16_t glfw_input_state(void *data, const struct snes_keybind **binds, b
else else
snes_keybinds = binds[1]; snes_keybinds = binds[1];
// Finds fast forwarding state. // Checks if button is pressed, and sets fast-forwarding state
for ( int i = 0; snes_keybinds[i].id != -1; i++ )
{
if ( snes_keybinds[i].id == SNES_FAST_FORWARD_KEY )
{
bool pressed = false; 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;
}
}
// Checks if button is pressed
for ( int i = 0; snes_keybinds[i].id != -1; i++ ) for ( int i = 0; snes_keybinds[i].id != -1; i++ )
{ if ( snes_keybinds[i].id == SNES_FAST_FORWARD_KEY )
if ( snes_keybinds[i].id == (int)id ) set_fast_forward_button(glfw_is_pressed(port_num, &snes_keybinds[i], buttons, axes));
{ else if ( !pressed && snes_keybinds[i].id == (int)id )
if ( glfwGetKey(snes_keybinds[i].key) ) pressed = glfw_is_pressed(port_num, &snes_keybinds[i], buttons, axes);
return 1;
if ( (joypad_count > port_num) && (snes_keybinds[i].joykey < joypad_buttons[port_num]) && (buttons[snes_keybinds[i].joykey] == GLFW_PRESS) ) return pressed;
return 1;
}
}
return 0;
} }
static void glfw_free_input(void *data) static void glfw_free_input(void *data)
@ -213,7 +215,7 @@ static float tv_to_fps(const struct timeval *tv, const struct timeval *new_tv, i
static inline void show_fps(void) static inline void show_fps(void)
{ {
// Shows FPS in taskbar. // Shows FPS in taskbar.
static int frames = 0; static int frames = 0;
static struct timeval tv; static struct timeval tv;
struct timeval new_tv; struct timeval new_tv;
@ -224,10 +226,7 @@ static inline void show_fps(void)
if ((frames % 180) == 0 && frames > 0) if ((frames % 180) == 0 && frames > 0)
{ {
gettimeofday(&new_tv, NULL); gettimeofday(&new_tv, NULL);
struct timeval tmp_tv = { struct timeval tmp_tv = tv;
.tv_sec = tv.tv_sec,
.tv_usec = tv.tv_usec
};
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
char tmpstr[256] = {0}; char tmpstr[256] = {0};