home *** CD-ROM | disk | FTP | other *** search
/ Freelog Special Issue 2 / Freelog_HS_3_Setp_Oct_Nov_2000_CD2.mdx / Arcade / Orbit / src / joystick.c < prev    next >
C/C++ Source or Header  |  1999-08-23  |  5KB  |  180 lines

  1. /*
  2.  
  3. ORBIT, a freeware space combat simulator
  4. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20. */
  21.  
  22. #include "orbit.h"
  23.  
  24. /*
  25.  *  Handle the joystick
  26.  */
  27. #if defined WIN32
  28.  
  29. JOYINFOEX joyInfoEx;
  30. JOYCAPS joyCaps;
  31.  
  32. void InitJoy(void)
  33. /*
  34.  *  See if there's a joystick
  35.  */
  36. {
  37.     ZeroMemory (&joyInfoEx, sizeof(joyInfoEx));
  38.     joyInfoEx.dwSize = sizeof(joyInfoEx);
  39.     joy_available = (JOYERR_NOERROR == joyGetPosEx (JOYSTICKID1, &joyInfoEx));
  40.  
  41.     /* Get joystick min-max values */
  42.     joyGetDevCaps (JOYSTICKID1, &joyCaps, sizeof(joyCaps));
  43.     joy_xmin = (double) joyCaps.wXmin;
  44.     joy_xmax = (double) joyCaps.wXmax;
  45.     joy_ymin = (double) joyCaps.wYmin;
  46.     joy_ymax = (double) joyCaps.wYmax;
  47.     joy_rmin = (double) joyCaps.wRmin;
  48.     joy_rmax = (double) joyCaps.wRmax;
  49.     joy_zmin = (double) joyCaps.wZmin;
  50.     joy_zmax = (double) joyCaps.wZmax;
  51. }
  52.  
  53. void JoyStick(void)
  54. /*
  55.  *  Process joystick
  56.  */
  57. {
  58.     joyInfoEx.dwSize = sizeof (joyInfoEx);
  59.     joyInfoEx.dwFlags = JOY_RETURNALL;
  60.     joyGetPosEx (JOYSTICKID1, &joyInfoEx);
  61.     joy_x = (double) joyInfoEx.dwXpos;
  62.     joy_y = (double) joyInfoEx.dwYpos;
  63.     joy_r = (double) joyInfoEx.dwRpos;
  64.     joy_z = (double) joyInfoEx.dwZpos;
  65.  
  66.     joy_buttons = joyInfoEx.dwButtonNumber;
  67.  
  68.     /* See if player fired a missile */
  69.     if (1 & joy_buttons)
  70.     {
  71.         PlayerFires();
  72.     }
  73.  
  74.     /* Ignore joystick motion if dead */
  75.     if ( (state == STATE_DEAD1) || (state == STATE_DEAD2) ) return;
  76.  
  77.     /* Convert joy values to (-1.0,1.0) */
  78.     joy_x = (-1.0) + 2.0 * (joy_x - joy_xmin) / (joy_xmax - joy_xmin);
  79.     joy_y = (-1.0) + 2.0 * (joy_y - joy_ymin) / (joy_ymax - joy_ymin);
  80.     joy_r = (-1.0) + 2.0 * (joy_r - joy_rmin) / (joy_rmax - joy_rmin);
  81.     joy_z = (-1.0) + 2.0 * (joy_z - joy_zmin) / (joy_zmax - joy_zmin);
  82.  
  83.     /* See if joystick is outside deadzone */
  84.     if (fabs(joy_x) < deadzone) joy_x = 0.0;
  85.     if (fabs(joy_y) < deadzone) joy_y = 0.0;
  86.     if (fabs(joy_r) < deadzone) joy_r = 0.0;
  87.     if (player.flightmodel == FLIGHT_NEWTONIAN) if (fabs(joy_z) < deadzone) joy_z = 0.0;
  88.  
  89.     if (2 & joy_buttons)
  90.     {
  91.         /* Second joy button allows player to pitch with X-axis */
  92.         if (joy_x > 0.0) player.move_pitchright = (joy_x - deadzone) / (1.0 - deadzone);
  93.         if (joy_x < 0.0) player.move_pitchleft = (joy_x + deadzone) / (deadzone - 1.0);
  94.     }
  95.     else
  96.     {
  97.         if (joy_x > 0.0) player.move_right = (joy_x - deadzone) / (1.0 - deadzone);
  98.         if (joy_x < 0.0) player.move_left = (joy_x + deadzone) / (deadzone - 1.0);
  99.     }
  100.  
  101.     if (joy_y > 0.0) player.move_up = (joy_y - deadzone) / (1.0 - deadzone);
  102.     if (joy_y < 0.0) player.move_down = (joy_y + deadzone) / (deadzone - 1.0);
  103.     if (joy_r > 0.0) player.move_pitchright = (joy_r - deadzone) / (1.0 - deadzone);
  104.     if (joy_r < 0.0) player.move_pitchleft = (joy_r + deadzone) / (deadzone - 1.0);
  105.  
  106.     if (joy_throttle)
  107.     {
  108.         if (player.flightmodel == FLIGHT_ARCADE)
  109.         {
  110.             joy_z = (-joy_z + 1.0) / 2.0;
  111.             if (joy_z < deadzone)
  112.             {
  113.                 player.throttle = 0.0;
  114.             }
  115.             else
  116.             {
  117.                 joy_z = (joy_z - deadzone) / (1.0 - deadzone);
  118.                 if (joy_z < 0.5)
  119.                     player.throttle = 2.0 * joy_z * MAX_THROTTLE;
  120.                 else
  121.                     player.throttle = (2.0 * (joy_z - 0.5)) * MAX_WARP_THROTTLE;
  122.             }
  123.         }
  124.         else
  125.         {
  126.             if (joy_z > 0.0) player.move_backward = (joy_z - deadzone) / (1.0 - deadzone);
  127.             if (joy_z < 0.0) player.move_forward = (joy_z + deadzone) / (deadzone - 1.0);
  128.         }
  129.     }
  130.  
  131. }
  132.  
  133. #elif defined __linux__ /* if defines WIN32 */
  134.  
  135. /*
  136. #include <linux/joystick.h>
  137.  
  138. int joy_fd = -1;
  139. char *joy_dev = "/dev/js0";
  140. */
  141. void InitJoy(void)
  142. /*
  143.  *  Sorry, no joystick in Unix yet
  144.  *  /usr/src/linux-2.2.10/Documentation/joystick-api.txt
  145.  *  man js
  146.  */
  147. {
  148.     int number_of_axes;
  149.     int number_of_buttons;
  150.  
  151.     joy_available = 0;
  152.     /*
  153.     joy_fd = open(joy_dev, O_RDONLY | O_NONBLOCK);
  154.     if(joy_fd < 0)
  155.     {
  156.         joy_available = 0;
  157.         return;
  158.     }
  159.     ioctl (fd, JSIOCGAXES, &number_of_axes);
  160.     ioctl (fd, JSIOCGBUTTONS, &number_of_buttons);
  161.     */
  162. }
  163.  
  164. void JoyStick(void)
  165. {
  166. /*
  167.     struct js_event joy_e;
  168.     read (joy_fd, &joy_e, sizeof(struct js_event));
  169. */
  170. }
  171.  
  172. #else /* if defined __linux__ */
  173.  
  174. void InitJoy(void)
  175. {}
  176. void JoyStick(void)
  177. {}
  178.  
  179. #endif
  180.