Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
in_sdl.cpp File Reference
#include "in_joystick.h"
#include "vs_globals.h"
#include "config_xml.h"
#include "in_kb_data.h"
#include <assert.h>

Go to the source code of this file.

Classes

struct  JSHandlerCall
 

Macros

#define MAXOR(A, B)   ( ( (A) < (B) ) ? (B) : (A) )
 

Enumerations

enum  JSSwitches { JOYSTICK_SWITCH, HATSWITCH, DIGHATSWITCH, NUMSWITCHES }
 

Functions

void DefaultJoyHandler (const KBData &, KBSTATE newState)
 needed for assert() calls. More...
 
static void GenUnbindJoyKey (JSSwitches whichswitch, int joystick, int key)
 
static void GenBindJoyKey (JSSwitches whichswitch, int joystick, int key, KBHandler handler, const KBData &data)
 
void UnbindJoyKey (int joystick, int key)
 
void BindJoyKey (int joystick, int key, KBHandler handler, const KBData &data)
 
void UnbindHatswitchKey (int joystick, int key)
 
void BindHatswitchKey (int joystick, int key, KBHandler handler, const KBData &data)
 
void UnbindDigitalHatswitchKey (int joystick, int key, int dir)
 
void BindDigitalHatswitchKey (int joystick, int key, int dir, KBHandler handler, const KBData &data)
 
void ProcessJoystick (int whichplayer)
 

Variables

JSHandlerCall JoystickBindings [NUMSWITCHES][MAXOR(MAX_HATSWITCHES, MAX_JOYSTICKS)][MAXOR(NUMJBUTTONS, MAXOR(MAX_VALUES, MAX_DIGITAL_HATSWITCHES *MAX_DIGITAL_VALUES))]
 
KBSTATE JoystickState [NUMSWITCHES][MAXOR(MAX_HATSWITCHES, MAX_JOYSTICKS)][MAXOR(MAX_VALUES, MAXOR(NUMJBUTTONS, MAX_DIGITAL_HATSWITCHES *MAX_DIGITAL_VALUES))]
 

Macro Definition Documentation

#define MAXOR (   A,
 
)    ( ( (A) < (B) ) ? (B) : (A) )

Definition at line 31 of file in_sdl.cpp.

Referenced by GenUnbindJoyKey().

Enumeration Type Documentation

enum JSSwitches
Enumerator
JOYSTICK_SWITCH 
HATSWITCH 
DIGHATSWITCH 
NUMSWITCHES 

Definition at line 24 of file in_sdl.cpp.

25 {
27  HATSWITCH,
30 };

Function Documentation

void BindDigitalHatswitchKey ( int  joystick,
int  key,
int  dir,
KBHandler  handler,
const KBData data 
)

Definition at line 82 of file in_sdl.cpp.

References DIGHATSWITCH, GenBindJoyKey(), and MAX_DIGITAL_VALUES.

83 {
84  GenBindJoyKey( DIGHATSWITCH, joystick, key*MAX_DIGITAL_VALUES+dir, handler, data );
85 }
void BindHatswitchKey ( int  joystick,
int  key,
KBHandler  handler,
const KBData data 
)

Definition at line 72 of file in_sdl.cpp.

References GenBindJoyKey(), and HATSWITCH.

73 {
74  GenBindJoyKey( HATSWITCH, joystick, key, handler, data );
75 }
void BindJoyKey ( int  joystick,
int  key,
KBHandler  handler,
const KBData data 
)

Definition at line 62 of file in_sdl.cpp.

References GenBindJoyKey(), and JOYSTICK_SWITCH.

63 {
64  GenBindJoyKey( JOYSTICK_SWITCH, joystick, key, handler, data );
65 }
void DefaultJoyHandler ( const KBData ,
KBSTATE  newState 
)

needed for assert() calls.

Definition at line 6 of file in_sdl.cpp.

Referenced by JSHandlerCall::JSHandlerCall().

7 {
8 }
static void GenBindJoyKey ( JSSwitches  whichswitch,
int  joystick,
int  key,
KBHandler  handler,
const KBData data 
)
static

Definition at line 50 of file in_sdl.cpp.

References joystick, MAX_JOYSTICKS, NUMJBUTTONS, and RESET.

Referenced by BindDigitalHatswitchKey(), BindHatswitchKey(), and BindJoyKey().

51 {
52  assert( key < NUMJBUTTONS && joystick < MAX_JOYSTICKS );
53  JoystickBindings[whichswitch][joystick][key] = JSHandlerCall( handler, data );
54  handler( KBData(), RESET );
55 }
static void GenUnbindJoyKey ( JSSwitches  whichswitch,
int  joystick,
int  key 
)
static
void ProcessJoystick ( int  whichplayer)

Definition at line 87 of file in_sdl.cpp.

References JSHandlerCall::data, DIGHATSWITCH, JoyStick::digital_hat, DOWN, JSHandlerCall::function, JoyStick::GetJoyStick(), h, HATSWITCH, VegaConfig::hatswitch, VegaConfig::hatswitch_axis, VegaConfig::hatswitch_joystick, VegaConfig::hatswitch_margin, i, j, JoyStick::joy_axis, joystick, JOYSTICK_SWITCH, MAX_DIGITAL_VALUES, MAX_HATSWITCHES, MAX_JOYSTICKS, MAX_VALUES, JoyStick::nr_of_hats, NUMJBUTTONS, PRESS, RELEASE, UP, v, vs_config, VS_HAT_CENTERED, VS_HAT_DOWN, VS_HAT_LEFT, VS_HAT_LEFTDOWN, VS_HAT_LEFTUP, VS_HAT_RIGHT, VS_HAT_RIGHTDOWN, VS_HAT_RIGHTUP, VS_HAT_UP, x, y, and z.

Referenced by ProcessInput().

88 {
89  float x, y, z;
90  int buttons;
91 #ifdef HAVE_SDL
92 #ifndef NO_SDL_JOYSTICK
93  SDL_JoystickUpdate(); //FIXME isn't this supposed to be called already by SDL?
94 #endif
95 #endif
96  for (int i = whichplayer; i < whichplayer+1 && i < MAX_JOYSTICKS; i++) {
97  buttons = 0;
98  if ( joystick[i]->isAvailable() ) {
99  joystick[i]->GetJoyStick( x, y, z, buttons );
100  for (int h = 0; h < joystick[i]->nr_of_hats; h++) {
101 #ifdef HAVE_SDL
102  Uint8
103 #else
104  unsigned char
105 #endif
106  hsw = joystick[i]->digital_hat[h];
107  if (joystick[i]->debug_digital_hatswitch) {
108  char buf[100];
109  sprintf( buf, "hsw: %d", hsw );
110  std::cout<<buf<<std::endl;
111  }
112  for (int dir_index = 0; dir_index < MAX_DIGITAL_VALUES; dir_index++) {
113  bool press = false;
114 #ifdef HAVE_SDL
115 #ifndef NO_SDL_JOYSTICK
116  //CENTERED is an exact position.
117  if ( dir_index == VS_HAT_CENTERED && (hsw == SDL_HAT_CENTERED) ) {
118  if (joystick[i]->debug_digital_hatswitch)
119  std::cout<<"center"<<std::endl;
120  press = true;
121  }
122  if ( dir_index == VS_HAT_LEFT && (hsw&SDL_HAT_LEFT) )
123  press = true;
124  if ( dir_index == VS_HAT_RIGHT && (hsw&SDL_HAT_RIGHT) )
125  press = true;
126  if ( dir_index == VS_HAT_DOWN && (hsw&SDL_HAT_DOWN) )
127  press = true;
128  if ( dir_index == VS_HAT_UP && (hsw&SDL_HAT_UP) )
129  press = true;
130  if ( dir_index == VS_HAT_RIGHTUP && (hsw&SDL_HAT_RIGHTUP) )
131  press = true;
132  if ( dir_index == VS_HAT_RIGHTDOWN && (hsw&SDL_HAT_RIGHTDOWN) )
133  press = true;
134  if ( dir_index == VS_HAT_LEFTUP && (hsw&SDL_HAT_LEFTUP) )
135  press = true;
136  if ( dir_index == VS_HAT_LEFTDOWN && (hsw&SDL_HAT_LEFTDOWN) )
137  press = true;
138 #endif
139 #endif
140  KBSTATE *state =
141  &JoystickState[DIGHATSWITCH][i][h*MAX_DIGITAL_VALUES+dir_index];
142  JSHandlerCall *handler =
143  &JoystickBindings[DIGHATSWITCH][i][h*MAX_DIGITAL_VALUES+dir_index];
144  if (press == true) {
145  if (*state == UP) {
146  (*handler->function)
147  ( handler->data, PRESS );
148  *state = DOWN;
149  }
150  } else {
151  if (*state == DOWN)
152  (*handler->function)
153  ( handler->data, RELEASE );
154  *state = UP;
155  }
156  (*handler->function)( handler->data, *state );
157  }
158  } //digital_hatswitch
159  for (int j = 0; j < NUMJBUTTONS; j++) {
160  KBSTATE *state = &JoystickState[JOYSTICK_SWITCH][i][j];
162  if ( ( buttons&(1<<j) ) ) {
163  if (*state == UP) {
164  (*handler->function)
165  ( handler->data, PRESS );
166  *state = DOWN;
167  }
168  } else {
169  if (*state == DOWN)
170  (*handler->function)( handler->data, RELEASE );
171  *state = UP;
172  }
173  (*handler->function)( handler->data, *state );
174  }
175  } //is available
176  } //for nr joysticks
177  for (int h = 0; h < MAX_HATSWITCHES; h++) {
178  float margin = fabs( vs_config->hatswitch_margin[h] );
179  if (margin < 1.0) {
180  //we have hatswitch nr. h
181  int hs_axis = vs_config->hatswitch_axis[h];
182  int hs_joy = vs_config->hatswitch_joystick[h];
183  if ( joystick[hs_joy]->isAvailable() ) {
184  float axevalue = joystick[hs_joy]->joy_axis[hs_axis];
185  for (int v = 0; v < MAX_VALUES; v++) {
186  float hs_val = vs_config->hatswitch[h][v];
187  if (fabs( hs_val ) <= 1.0) {
188  //this is set
189  JSHandlerCall *handler = &JoystickBindings[HATSWITCH][h][v];
190  KBSTATE *state = &JoystickState[HATSWITCH][h][v];
191  if (hs_val-margin <= axevalue && axevalue <= hs_val+margin) {
192  //hatswitch pressed
193  if (*state == UP) {
194  (*handler->function)( handler->data, PRESS );
195  *state = DOWN;
196  }
197  } else {
198  //not pressed
199  if (*state == DOWN)
200  (*handler->function)( handler->data, RELEASE );
201  *state = UP;
202  }
203  (*handler->function)( handler->data, *state );
204  }
205  } //for all values
206  } //is available
207  }
208  }
209 }
void UnbindDigitalHatswitchKey ( int  joystick,
int  key,
int  dir 
)

Definition at line 77 of file in_sdl.cpp.

References DIGHATSWITCH, GenUnbindJoyKey(), and MAX_DIGITAL_VALUES.

Referenced by InitJoystick().

78 {
80 }
void UnbindHatswitchKey ( int  joystick,
int  key 
)

Definition at line 67 of file in_sdl.cpp.

References GenUnbindJoyKey(), and HATSWITCH.

Referenced by InitJoystick().

68 {
70 }
void UnbindJoyKey ( int  joystick,
int  key 
)

Definition at line 57 of file in_sdl.cpp.

References GenUnbindJoyKey(), and JOYSTICK_SWITCH.

Referenced by InitJoystick().

58 {
60 }

Variable Documentation