summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Howard2014-04-30 22:56:04 -0400
committerSimon Howard2014-04-30 22:56:04 -0400
commit4890591ba50bfc8b11cda684b223cb7c551e6dd3 (patch)
tree589edddb7f25d68f2f7f9b990b82078eca728491 /src
parent96ff3f7bc127889c9f47e4ec17faa445ade623f1 (diff)
downloadchocolate-doom-4890591ba50bfc8b11cda684b223cb7c551e6dd3.tar.gz
chocolate-doom-4890591ba50bfc8b11cda684b223cb7c551e6dd3.tar.bz2
chocolate-doom-4890591ba50bfc8b11cda684b223cb7c551e6dd3.zip
joystick: Add virtual-physical button mapping.
The solution to solving #386 is to add a layer of indirection: the game code can only support up to ~20 joystick buttons, but this doesn't matter as long as we never want to bind more than 20 buttons to actions anyway. Redefine the game's notion of buttons to be based on "virtual" joystick buttons, and map these buttons to physical (SDL) buttons based on configuration file variables.
Diffstat (limited to 'src')
-rw-r--r--src/i_joystick.c79
-rw-r--r--src/i_joystick.h5
-rw-r--r--src/m_config.c60
-rw-r--r--src/m_controls.h2
-rw-r--r--src/setup/joystick.c15
5 files changed, 144 insertions, 17 deletions
diff --git a/src/i_joystick.c b/src/i_joystick.c
index de10fa08..da93ce41 100644
--- a/src/i_joystick.c
+++ b/src/i_joystick.c
@@ -37,6 +37,7 @@
#include "i_system.h"
#include "m_config.h"
+#include "m_misc.h"
// When an axis is within the dead zone, it is set to zero.
// This is 5% of the full range:
@@ -72,6 +73,12 @@ static int joystick_y_invert = 0;
static int joystick_strafe_axis = -1;
static int joystick_strafe_invert = 0;
+// Virtual to physical button joystick button mapping. By default this
+// is a straight mapping.
+static int joystick_physical_buttons[NUM_VIRTUAL_BUTTONS] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
void I_ShutdownJoystick(void)
{
if (joystick != NULL)
@@ -159,49 +166,78 @@ void I_InitJoystick(void)
I_AtExit(I_ShutdownJoystick, true);
}
-static int ButtonAxisMask(void)
+static boolean IsAxisButton(int physbutton)
{
- int result = 0;
-
if (IS_BUTTON_AXIS(joystick_x_axis))
{
- result |= 1 << BUTTON_AXIS_NEG(joystick_x_axis);
- result |= 1 << BUTTON_AXIS_POS(joystick_x_axis);
+ if (physbutton == BUTTON_AXIS_NEG(joystick_x_axis)
+ || physbutton == BUTTON_AXIS_POS(joystick_x_axis))
+ {
+ return true;
+ }
}
if (IS_BUTTON_AXIS(joystick_y_axis))
{
- result |= 1 << BUTTON_AXIS_NEG(joystick_y_axis);
- result |= 1 << BUTTON_AXIS_POS(joystick_y_axis);
+ if (physbutton == BUTTON_AXIS_NEG(joystick_y_axis)
+ || physbutton == BUTTON_AXIS_POS(joystick_y_axis))
+ {
+ return true;
+ }
}
if (IS_BUTTON_AXIS(joystick_strafe_axis))
{
- result |= 1 << BUTTON_AXIS_NEG(joystick_strafe_axis);
- result |= 1 << BUTTON_AXIS_POS(joystick_strafe_axis);
+ if (physbutton == BUTTON_AXIS_NEG(joystick_strafe_axis)
+ || physbutton == BUTTON_AXIS_POS(joystick_strafe_axis))
+ {
+ return true;
+ }
}
- return result;
+ return false;
+}
+
+// Get the state of the given virtual button.
+
+static int ReadButtonState(int vbutton)
+{
+ int physbutton;
+
+ // Map from virtual button to physical (SDL) button.
+ if (vbutton < NUM_VIRTUAL_BUTTONS)
+ {
+ physbutton = joystick_physical_buttons[vbutton];
+ }
+ else
+ {
+ physbutton = vbutton;
+ }
+
+ // Never read axis buttons as buttons.
+ if (IsAxisButton(physbutton))
+ {
+ return 0;
+ }
+
+ return SDL_JoystickGetButton(joystick, physbutton);
}
// Get a bitmask of all currently-pressed buttons
-static int GetButtonState(void)
+static int GetButtonsState(void)
{
int i;
int result;
result = 0;
- for (i=0; i<SDL_JoystickNumButtons(joystick); ++i)
+ for (i = 0; i < 20; ++i)
{
- if (SDL_JoystickGetButton(joystick, i))
+ if (ReadButtonState(i))
{
result |= 1 << i;
}
}
- // Mask out any buttons that are actually used in axes.
- result &= ~ButtonAxisMask();
-
return result;
}
@@ -287,7 +323,7 @@ void I_UpdateJoystick(void)
event_t ev;
ev.type = ev_joystick;
- ev.data1 = GetButtonState();
+ ev.data1 = GetButtonsState();
ev.data2 = GetAxisState(joystick_x_axis, joystick_x_invert);
ev.data3 = GetAxisState(joystick_y_axis, joystick_y_invert);
ev.data4 = GetAxisState(joystick_strafe_axis, joystick_strafe_invert);
@@ -298,6 +334,8 @@ void I_UpdateJoystick(void)
void I_BindJoystickVariables(void)
{
+ int i;
+
M_BindVariable("use_joystick", &usejoystick);
M_BindVariable("joystick_index", &joystick_index);
M_BindVariable("joystick_x_axis", &joystick_x_axis);
@@ -306,5 +344,12 @@ void I_BindJoystickVariables(void)
M_BindVariable("joystick_x_invert", &joystick_x_invert);
M_BindVariable("joystick_y_invert", &joystick_y_invert);
M_BindVariable("joystick_strafe_invert",&joystick_strafe_invert);
+
+ for (i = 0; i < NUM_VIRTUAL_BUTTONS; ++i)
+ {
+ char name[32];
+ M_snprintf(name, sizeof(name), "joystick_physical_button%i", i);
+ M_BindVariable(name, &joystick_physical_buttons[i]);
+ }
}
diff --git a/src/i_joystick.h b/src/i_joystick.h
index 7c28eb6c..c9e5b380 100644
--- a/src/i_joystick.h
+++ b/src/i_joystick.h
@@ -27,6 +27,11 @@
#ifndef __I_JOYSTICK__
#define __I_JOYSTICK__
+// Number of "virtual" joystick buttons defined in configuration files.
+// This needs to be at least as large as the number of different key
+// bindings supported by the higher-level game code (joyb* variables).
+#define NUM_VIRTUAL_BUTTONS 10
+
// If this bit is set in a configuration file axis value, the axis is
// not actually a joystick axis, but instead is a "button axis". This
// means that instead of reading an SDL joystick axis, we read the
diff --git a/src/m_config.c b/src/m_config.c
index 898c9368..90d3776f 100644
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -939,6 +939,66 @@ static default_t extra_defaults_list[] =
CONFIG_VARIABLE_INT(joystick_strafe_invert),
//!
+ // The physical joystick button that corresponds to button #0.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button0),
+
+ //!
+ // The physical joystick button that corresponds to button #1.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button1),
+
+ //!
+ // The physical joystick button that corresponds to button #2.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button2),
+
+ //!
+ // The physical joystick button that corresponds to button #3.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button3),
+
+ //!
+ // The physical joystick button that corresponds to button #4.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button4),
+
+ //!
+ // The physical joystick button that corresponds to button #5.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button5),
+
+ //!
+ // The physical joystick button that corresponds to button #6.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button6),
+
+ //!
+ // The physical joystick button that corresponds to button #7.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button7),
+
+ //!
+ // The physical joystick button that corresponds to button #8.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button8),
+
+ //!
+ // The physical joystick button that corresponds to button #9.
+ //
+
+ CONFIG_VARIABLE_INT(joystick_physical_button9),
+
+ //!
// Joystick button to strafe left.
//
diff --git a/src/m_controls.h b/src/m_controls.h
index 5b2f34d6..6ae15880 100644
--- a/src/m_controls.h
+++ b/src/m_controls.h
@@ -157,6 +157,8 @@ extern int joybstraferight;
extern int joybprevweapon;
extern int joybnextweapon;
+extern int joy_physical_buttons[10];
+
extern int joybmenu;
extern int dclick_use;
diff --git a/src/setup/joystick.c b/src/setup/joystick.c
index ab442fe2..cc5c4a8e 100644
--- a/src/setup/joystick.c
+++ b/src/setup/joystick.c
@@ -26,6 +26,7 @@
#include "i_joystick.h"
#include "m_config.h"
#include "m_controls.h"
+#include "m_misc.h"
#include "textscreen.h"
#include "execute.h"
@@ -88,6 +89,11 @@ static int joystick_y_invert = 0;
static int joystick_strafe_axis = -1;
static int joystick_strafe_invert = 0;
+// Virtual to physical mapping.
+static int joystick_physical_buttons[NUM_VIRTUAL_BUTTONS] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
+};
+
static txt_button_t *joystick_button;
static int *all_joystick_buttons[] = {
@@ -682,6 +688,8 @@ void ConfigJoystick(void)
void BindJoystickVariables(void)
{
+ int i;
+
M_BindVariable("use_joystick", &usejoystick);
M_BindVariable("joystick_index", &joystick_index);
M_BindVariable("joystick_x_axis", &joystick_x_axis);
@@ -690,5 +698,12 @@ void BindJoystickVariables(void)
M_BindVariable("joystick_x_invert", &joystick_x_invert);
M_BindVariable("joystick_y_invert", &joystick_y_invert);
M_BindVariable("joystick_strafe_invert",&joystick_strafe_invert);
+
+ for (i = 0; i < NUM_VIRTUAL_BUTTONS; ++i)
+ {
+ char name[32];
+ M_snprintf(name, sizeof(name), "joystick_physical_button%i", i);
+ M_BindVariable(name, &joystick_physical_buttons[i]);
+ }
}