diff options
author | Simon Howard | 2007-05-31 23:16:23 +0000 |
---|---|---|
committer | Simon Howard | 2007-05-31 23:16:23 +0000 |
commit | 7127f6c844ee24e3629f292a8301395111266a7e (patch) | |
tree | 1fc7378f54432279f70517f098c33666f66bd317 /src | |
parent | f37794b0e04ba617e2908f445858803abbf576e9 (diff) | |
download | chocolate-doom-7127f6c844ee24e3629f292a8301395111266a7e.tar.gz chocolate-doom-7127f6c844ee24e3629f292a8301395111266a7e.tar.bz2 chocolate-doom-7127f6c844ee24e3629f292a8301395111266a7e.zip |
Initial joystick support.
Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 887
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/i_joystick.c | 166 | ||||
-rw-r--r-- | src/i_joystick.h | 41 | ||||
-rw-r--r-- | src/i_main.c | 2 | ||||
-rw-r--r-- | src/i_system.c | 2 | ||||
-rw-r--r-- | src/m_misc.c | 7 |
6 files changed, 218 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index db446819..da9a35f8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,6 +48,7 @@ hu_lib.c hu_lib.h \ hu_stuff.c hu_stuff.h \ i_main.c \ info.c info.h \ +i_joystick.c i_joystick.h \ i_scale.c i_scale.h \ i_swap.h \ i_system.c i_system.h \ diff --git a/src/i_joystick.c b/src/i_joystick.c new file mode 100644 index 00000000..4d73a3af --- /dev/null +++ b/src/i_joystick.c @@ -0,0 +1,166 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// SDL Joystick code. +// +//----------------------------------------------------------------------------- + + +#include "SDL.h" +#include "SDL_joystick.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "doomdef.h" +#include "doomtype.h" +#include "d_event.h" +#include "d_main.h" +#include "i_joystick.h" + +static SDL_Joystick *joystick = NULL; + +// Configuration variables: + +// Standard default.cfg Joystick enable/disable + +extern int usejoystick; + +// Joystick to use, as an SDL joystick index: + +int joystick_index = -1; + +// Which joystick axis to use for horizontal movement, and whether to +// invert the direction: + +int joystick_x_axis = 0; +int joystick_x_invert = 0; + +// Which joystick axis to use for vertical movement, and whether to +// invert the direction: + +int joystick_y_axis = 1; +int joystick_y_invert = 0; + +void I_InitJoystick(void) +{ + int num_axes; + + if (!usejoystick) + { + return; + } + + if (SDL_Init(SDL_INIT_JOYSTICK) < 0) + { + return; + } + + // Open the joystick + + joystick = SDL_JoystickOpen(joystick_index); + + if (joystick == NULL) + { + printf("I_InitJoystick: Failed to open joystick #%i\n", + joystick_index); + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + return; + } + + num_axes = SDL_JoystickNumAxes(joystick); + + if (joystick_x_axis < 0 || joystick_x_axis >= num_axes + || joystick_y_axis < 0 || joystick_y_axis >= num_axes) + { + printf("I_InitJoystick: Invalid joystick axis for joystick #%i " + "(run joystick setup again)\n", + joystick_index); + + SDL_JoystickClose(joystick); + joystick = NULL; + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + } + + // Initialised okay! + + printf("I_InitJoystick: %s\n", SDL_JoystickName(joystick_index)); +} + +void I_ShutdownJoystick(void) +{ + if (joystick != NULL) + { + SDL_JoystickClose(joystick); + joystick = NULL; + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); + } +} + +// Get a bitmask of all currently-pressed buttons + +static int GetButtonState(void) +{ + int i; + int result; + + result = 0; + + for (i=0; i<SDL_JoystickNumButtons(joystick); ++i) + { + if (SDL_JoystickGetButton(joystick, i)) + { + result |= 1 << i; + } + } + + return result; +} + +// Read the state of an axis, inverting if necessary. + +static int GetAxisState(int axis, int invert) +{ + int result; + + result = SDL_JoystickGetAxis(joystick, axis); + + if (invert) + { + result = -result; + } + + return result; +} + +void I_UpdateJoystick(void) +{ + event_t ev; + + ev.type = ev_joystick; + ev.data1 = GetButtonState(); + ev.data2 = GetAxisState(joystick_x_axis, joystick_x_invert); + ev.data3 = GetAxisState(joystick_y_axis, joystick_y_invert); + + D_PostEvent(&ev); +} + diff --git a/src/i_joystick.h b/src/i_joystick.h new file mode 100644 index 00000000..bedf5437 --- /dev/null +++ b/src/i_joystick.h @@ -0,0 +1,41 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// Copyright(C) 2007 Simon Howard +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// +// DESCRIPTION: +// System-specific joystick interface. +// +//----------------------------------------------------------------------------- + + +#ifndef __I_JOYSTICK__ +#define __I_JOYSTICK__ + +extern int joystick_index; +extern int joystick_x_axis; +extern int joystick_x_invert; +extern int joystick_y_axis; +extern int joystick_y_invert; + +void I_InitJoystick(void); +void I_ShutdownJoystick(void); +void I_UpdateJoystick(void); + +#endif /* #ifndef __I_JOYSTICK__ */ + diff --git a/src/i_main.c b/src/i_main.c index 9da06442..da6c5474 100644 --- a/src/i_main.c +++ b/src/i_main.c @@ -36,7 +36,6 @@ int main(int argc, char **argv) { - // save arguments myargc = argc; @@ -48,3 +47,4 @@ int main(int argc, char **argv) return 0; } + diff --git a/src/i_system.c b/src/i_system.c index 024d858f..13a68ee0 100644 --- a/src/i_system.c +++ b/src/i_system.c @@ -36,6 +36,7 @@ #include "doomstat.h" #include "m_argv.h" #include "m_misc.h" +#include "i_joystick.h" #include "i_timer.h" #include "i_video.h" #include "s_sound.h" @@ -114,6 +115,7 @@ void I_Init (void) { I_CheckIsScreensaver(); I_InitTimer(); + I_InitJoystick(); } // diff --git a/src/m_misc.c b/src/m_misc.c index 6e0af575..ee3aae95 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -54,6 +54,7 @@ #include "w_wad.h" +#include "i_joystick.h" #include "i_swap.h" #include "i_system.h" #include "i_video.h" @@ -413,6 +414,12 @@ static default_t extra_defaults_list[] = #ifdef FEATURE_MULTIPLAYER {"player_name", &net_player_name, DEFAULT_STRING, 0, 0}, #endif + + {"joystick_index", &joystick_index, DEFAULT_INT, 0, 0}, + {"joystick_x_axis", &joystick_x_axis, DEFAULT_INT, 0, 0}, + {"joystick_x_invert", &joystick_x_invert, DEFAULT_INT, 0, 0}, + {"joystick_y_axis", &joystick_y_axis, DEFAULT_INT, 0, 0}, + {"joystick_y_invert", &joystick_y_invert, DEFAULT_INT, 0, 0}, }; static default_collection_t extra_defaults = |