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_joystick.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 /*
23  * Joystick support written by Alexander Rawass <alexannika@users.sourceforge.net>
24  */
25 #include <list>
26 #include <lin_time.h>
27 #include "vegastrike.h"
28 #include "vs_globals.h"
29 
30 #include "in_handler.h"
31 #include "in_joystick.h"
32 #include "config_xml.h"
33 #include "in_mouse.h"
34 #ifndef HAVE_SDL
35 #include "gldrv/gl_include.h"
36 #if (GLUT_API_VERSION >= 4 || GLUT_XLIB_IMPLEMENTATION >= 13)
37 #else
38 #define NO_SDL_JOYSTICK
39 #endif
40 #endif
41 
42 #include "options.h"
43 
45 
46 //Used for storing the max and min values of the tree Joystick Axes - Okona
47 static int maxx = 1;
48 static int minx = -1;
49 static int maxy = 1;
50 static int miny = -1;
51 static int maxz = 1;
52 static int minz = -1;
53 
54 JoyStick *joystick[MAX_JOYSTICKS]; //until I know where I place it
55 int num_joysticks = 0;
57 {
58  for (int a = 0; a < j->nr_of_axes; a++) {
59  if (fabs( j->joy_axis[a] ) <= j->deadzone)
60  j->joy_axis[a] = 0.0;
61  else if (j->joy_axis[a] > 0)
62  j->joy_axis[a] -= j->deadzone;
63  else
64  j->joy_axis[a] += j->deadzone;
65  if (j->deadzone < .999)
66  j->joy_axis[a] /= (1-j->deadzone);
67  }
68 }
70 {
71  if ( (game_options.joystick_exponent != 1.0) && (game_options.joystick_exponent > 0) ) {
72  for (int a = 0; a < j->nr_of_axes; a++)
73  j->joy_axis[a] =
74  ( (j->joy_axis[a] < 0) ? -pow( -j->joy_axis[a], game_options.joystick_exponent ) : pow( j->joy_axis[a],
75  game_options.
76  joystick_exponent ) );
77  }
78 }
79 static bool JoyStickToggle = true;
81 {
82  JoyStickToggle = false;
83 }
84 void JoyStickToggleKey( const KBData &key, KBSTATE a )
85 {
86  if (a == PRESS)
88 }
89 void myGlutJoystickCallback( unsigned int buttonmask, int x, int y, int z )
90 {
91  unsigned int i;
92  for (i = 0; i < MAX_AXES; i++)
93  joystick[0]->joy_axis[i] = 0.0;
94  joystick[0]->joy_buttons = 0;
95  if (JoyStickToggle) {
96  joystick[0]->joy_buttons = buttonmask;
97  if (joystick[0]->nr_of_axes > 0)
98  //Set the max and min of each axis - Okona
99  if (x < minx) minx = x;
100  if (x > maxx) maxx = x;
101  //Calculate an autocalibrated value based on the max min values - Okona
102  joystick[0]->joy_axis[0] = ( (float) x-( ( (float) (maxx+minx) )/2.0 ) )/( ( (float) (maxx-minx) )/2.0 );
103  if (joystick[0]->nr_of_axes > 1)
104  if (y < miny) miny = y;
105  if (y > maxy) maxy = y;
106  joystick[0]->joy_axis[1] = ( (float) y-( ( (float) (maxy+miny) )/2.0 ) )/( ( (float) (maxy-miny) )/2.0 );
107  if (joystick[0]->nr_of_axes > 2)
108  if (z < minz) minz = z;
109  if (z > maxz) maxz = z;
110  joystick[0]->joy_axis[2] = ( (float) z-( ( (float) (maxz+minz) )/2.0 ) )/( ( (float) (maxz-minz) )/2.0 );
111  modifyDeadZone( joystick[0] );
112  modifyExponent( joystick[0] );
113  }
114 }
115 
117 {
118  for (int j = 0; j < MAX_AXES; ++j) {
119  axis_axis[j] = -1;
120  axis_inverse[j] = false;
121  joy_axis[j] = axis_axis[j] = 0;
122  }
123  joy_buttons = 0;
124 }
126 {
127  return game_options.polling_rate;
128 }
130 {
131  int i;
132  for (i = 0; i < NUMJBUTTONS; i++)
133  for (int j = 0; j < MAX_JOYSTICKS; j++)
134  UnbindJoyKey( j, i );
135  for (int h = 0; h < MAX_HATSWITCHES; h++)
136  for (int v = 0; v < MAX_VALUES; v++)
137  UnbindHatswitchKey( h, v );
138  for (int j = 0; j < MAX_JOYSTICKS; j++)
139  for (int h = 0; h < MAX_DIGITAL_HATSWITCHES; h++)
140  for (int v = 0; v < MAX_DIGITAL_VALUES; v++)
142 #ifndef NO_SDL_JOYSTICK
143 #ifdef HAVE_SDL
144  num_joysticks = SDL_NumJoysticks();
145  printf( "%i joysticks were found.\n\n", num_joysticks );
146  printf( "The names of the joysticks are:\n" );
147 #else
148  //use glut
149  if (glutDeviceGet( GLUT_HAS_JOYSTICK ) || game_options.force_use_of_joystick) {
150  printf( "setting joystick functionality:: joystick online" );
151  glutJoystickFunc( myGlutJoystickCallback, JoystickPollingRate() );
152  num_joysticks = 1;
153  }
154 #endif
155 #endif
156  for (i = 0; i < MAX_JOYSTICKS; i++) {
157 #ifndef NO_SDL_JOYSTICK
158 #ifdef HAVE_SDL
159  if (i < num_joysticks)
160  printf( " %s\n", SDL_JoystickName( i ) );
161 #else
162  if (i < num_joysticks)
163  //SDL_EventState (SDL_JOYBUTTONDOWN,SDL_ENABLE);
164  //SDL_EventState (SDL_JOYBUTTONUP,SDL_ENABLE);
165  printf( "Glut detects %d joystick", i+1 );
166 #endif
167 #endif
168  joystick[i] = new JoyStick( i ); //SDL_Init is done in main.cpp
169  }
170 }
171 
173 {
174  for (int i = 0; i < MAX_JOYSTICKS; i++)
175  delete joystick[i];
176 }
177 JoyStick::JoyStick( int which ) : mouse( which == MOUSE_JOYSTICK )
178 {
179  for (int j = 0; j < MAX_AXES; ++j) {
180  axis_axis[j] = -1;
181  axis_inverse[j] = false;
182  joy_axis[j] = 0;
183  }
184  joy_buttons = 0;
185 
186  player = which; //by default bind players to whichever joystick it is
188  if (which != MOUSE_JOYSTICK)
189  deadzone = game_options.deadband;
190 
191  else
192  deadzone = game_options.mouse_deadband;
193  ;
194  joy_available = 0;
195  joy_x = joy_y = joy_z = 0;
196  if (which == MOUSE_JOYSTICK)
197  InitMouse( which );
198 #if defined (NO_SDL_JOYSTICK)
199  return;
200 
201 #else
202 #ifdef HAVE_SDL
203  num_joysticks = SDL_NumJoysticks();
204  if (which >= num_joysticks) {
205  if (which != MOUSE_JOYSTICK)
206  joy_available = false;
207  return;
208  }
209  joy = SDL_JoystickOpen( which ); //joystick nr should be configurable
210  if (joy == NULL) {
211  printf( "warning: no joystick nr %d\n", which );
212  joy_available = false;
213  return;
214  }
215  joy_available = true;
216  nr_of_axes = SDL_JoystickNumAxes( joy );
217  nr_of_buttons = SDL_JoystickNumButtons( joy );
218  nr_of_hats = SDL_JoystickNumHats( joy );
219 #else
220  //WE HAVE GLUT
221  if (which > 0 && which != MOUSE_JOYSTICK) {
222  joy_available = false;
223  return;
224  }
225  joy_available = true;
226  nr_of_axes = 3; //glutDeviceGet(GLUT_JOYSTICK_AXES);
227  nr_of_buttons = 15; //glutDeviceGet(GLUT_JOYSTICK_BUTTONS);
228  nr_of_hats = 0;
229 #endif //we have GLUT
230 #endif
231  printf( "axes: %d buttons: %d hats: %d\n", nr_of_axes, nr_of_buttons, nr_of_hats );
232 }
233 void JoyStick::InitMouse( int which )
234 {
235  player = 0; //default to first player
236  joy_available = true;
237  nr_of_axes = 2; //x and y for mouse
238  nr_of_buttons = 15;
239  nr_of_hats = 0;
240 }
241 
243 {
244  return joy_available;
245 }
246 struct mouseData
247 {
248  int dx;
249  int dy;
250  float time;
252  {
253  dx = dy = 0;
254  time = 0;
255  }
256  mouseData( int ddx, int ddy, float ttime )
257  {
258  dx = ddx;
259  dy = ddy;
260  time = ttime;
261  }
262 };
263 extern void GetMouseXY( int &mousex, int &mousey );
264 void JoyStick::GetMouse( float &x, float &y, float &z, int &buttons )
265 {
266  int def_mouse_sens = 1;
267  int _dx, _dy;
268  float fdx, fdy;
269  int _mx, _my;
270  GetMouseXY( _mx, _my );
271  GetMouseDelta( _dx, _dy );
272  if ( 0 && (_dx || _dy) )
273  printf( "x:%d y:%d\n", _dx, _dy );
274  if (!game_options.warp_mouse) {
275  fdx = (float) (_dx = _mx-g_game.x_resolution/2);
276  def_mouse_sens = 25;
277  fdy = (float) (_dy = _my-g_game.y_resolution/2);
278  } else {
279  static std::list< mouseData > md;
280  std::list< mouseData >::iterator i = md.begin();
281  float ttime = getNewTime();
282  float lasttime = ttime-game_options.mouse_blur;
283  int avg = (_dx || _dy) ? 1 : 0;
284  float valx = _dx;
285  float valy = _dy;
286  for (; i != md.end();) {
287  if ( (*i).time >= lasttime ) {
288  bool found = false;
289  int ldx = (*i).dx;
290  int ldy = (*i).dy;
291  if ( (ldx >= 0)*_dx*ldx == (_dx >= 0)*_dx*ldx ) {
292  //make sure same sign or zero
293  valx += (*i).dx;
294  found = true;
295  }
296  if ( (ldy >= 0)*_dy*ldy == (_dy >= 0)*_dy*ldy ) {
297  //make sure same sign or zero
298  valy += (*i).dy;
299  found = true;
300  }
301  if (found)
302  avg++;
303  ++i;
304  } else if ( ( i = md.erase( i ) ) == md.end() ) {
305  break;
306  }
307  }
308  if (_dx || _dy)
309  md.push_back( mouseData( _dx, _dy, ttime ) );
310  if (avg) {
311  _dx = float_to_int( valx/avg );
312  _dy = float_to_int( valy/avg );
313  }
314  fdx = float(valx)/game_options.mouse_blur;
315  fdy = float(valy)/game_options.mouse_blur;
316  }
317  joy_axis[0] = fdx/(g_game.x_resolution*def_mouse_sens/game_options.mouse_sensitivity);
318  joy_axis[1] = fdy/(g_game.y_resolution*def_mouse_sens/game_options.mouse_sensitivity);
319  if (!game_options.warp_mouse)
320  modifyDeadZone( this );
321  joy_axis[0] *= game_options.mouse_exponent;
322  joy_axis[1] *= game_options.mouse_exponent;
323  x = joy_axis[0];
324  y = joy_axis[1];
325 
326  joy_axis[2] = z = 0;
327  buttons = getMouseButtonStatus();
328 }
329 void JoyStick::GetJoyStick( float &x, float &y, float &z, int &buttons )
330 {
331  //int status;
332  if (joy_available == false) {
333  for (int a = 0; a < MAX_AXES; a++)
334  joy_axis[a] = 0;
335  x = y = z = 0;
336  joy_buttons = buttons = 0;
337  return;
338  } else if (mouse) {
339  GetMouse( x, y, z, buttons );
340  return;
341  }
342  int a;
343 #ifndef NO_SDL_JOYSTICK
344 #if defined (HAVE_SDL)
345  int numaxes = SDL_JoystickNumAxes( joy );
346 
347  vector< Sint16 >axi( numaxes < MAX_AXES ? MAX_AXES : numaxes );
348  for (a = 0; a < numaxes; a++)
349  axi[a] = SDL_JoystickGetAxis( joy, a );
350  joy_buttons = 0;
351  nr_of_buttons = SDL_JoystickNumButtons( joy );
352  for (int i = 0; i < nr_of_buttons; i++) {
353  int butt = SDL_JoystickGetButton( joy, i );
354  if (butt == 1)
355  joy_buttons |= (1<<i);
356  }
357  for (int h = 0; h < nr_of_hats; h++)
358  digital_hat[h] = SDL_JoystickGetHat( joy, h );
359  for (a = 0; a < MAX_AXES; a++)
360  joy_axis[a] = ( (float) axi[a]/32768.0 );
361  modifyDeadZone( this );
362  modifyExponent( this );
363 #else //we have glut
364  if (JoystickPollingRate() <= 0)
365  glutForceJoystickFunc();
366 #endif
367  x = joy_axis[0];
368  y = joy_axis[1];
369  z = joy_axis[2];
370  buttons = joy_buttons;
371 #endif //we have no joystick
372 }
373 
375 {
376  return nr_of_buttons;
377 }
378