home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / glquake_src / in_amigajoy.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-30  |  10.0 KB  |  388 lines

  1. /* 
  2. Copyright (C) 1996-1997 Id Software, Inc. 
  3.  
  4. This program is free software; you can redistribute it and/or 
  5. modify it under the terms of the GNU General Public License 
  6. as published by the Free Software Foundation; either version 2 
  7. of the License, or (at your option) any later version. 
  8.  
  9. This program is distributed in the hope that it will be useful, 
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   
  12.  
  13. See the GNU General Public License for more details. 
  14.  
  15. You should have received a copy of the GNU General Public License 
  16. along with this program; if not, write to the Free Software 
  17. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
  18.  
  19. */ 
  20.  
  21. // Input-Sub-Module for Joystick
  22.  
  23. /*
  24.             Portability
  25.             ===========
  26.  
  27.             This file should compile under all Amiga-based Kernels,
  28.             including WarpUP, 68k and PowerUP. There might be some
  29.             changes in the OS-Includes included needed (for example
  30.             adding inline-includes for EGCS or something like that).
  31.  
  32.             Steffen Haeuser (MagicSN@Birdland.es.bawue.de)
  33. */
  34.  
  35. #include "quakedef.h" 
  36.  
  37. #include <libraries/lowlevel.h> 
  38. #include <proto/exec.h> 
  39. #include <proto/lowlevel.h> 
  40. #include <intuition/intuition.h>
  41. #include <intuition/intuitionbase.h> 
  42.  
  43. cvar_t  joy_advanced={"joy_advanced","0",true};
  44. cvar_t  joy_forwardthreshold={"joy_forwardthreshold","0.15",true};
  45. cvar_t  joy_sidethreshold={"joy_sidethreshold","0.15",true};
  46. cvar_t  joy_pitchthreshold={"joy_pitchthreshold","0.15",true};
  47. cvar_t  joy_pitchsensitivity={"joy_pitchsensitivity","1",true};
  48. cvar_t  joy_yawsensitivity={"joy_yawsensitivity","-1",true};
  49.  
  50. cvar_t  joy_side_reverse={"joy_side_reverse","0",true};
  51. cvar_t  joy_up_reverse={"joy_up_reverse","0",true};
  52. cvar_t  joy_forward_reverse={"joy_forward_reverse","0",true};
  53.  
  54. cvar_t joy_forwardsensitivity={"joy_forwardsensitivity","-1",true};
  55. cvar_t joy_sidesensitivity={"joy_sidesensitivity","-1",true};
  56.  
  57. cvar_t joy_force0={"joy_force0","AUTOSENSE",true};
  58. cvar_t joy_force1={"joy_force1","AUTOSENSE",true};
  59. cvar_t joy_force2={"joy_force2","AUTOSENSE",true};
  60. cvar_t joy_force3={"joy_force3","AUTOSENSE",true};
  61. cvar_t joy_onlypsx={"joy_onlypsx","0",true};
  62.  
  63. extern int psxused;
  64.  
  65. struct TheJoy
  66. {
  67.   int mv_a;
  68.   int mv_b;
  69.   int axis;
  70.   int value;
  71.   int nvalue;
  72. };
  73.  
  74. struct JoyType
  75.   char *Name;
  76.   ULONG Type;
  77. }; 
  78.  
  79.  
  80. static struct TheJoy tj;
  81. static int ack0 = -1, ack1 = -1;
  82. static int Port;
  83. static int JoyState[4];
  84. static int stick=1;
  85.  
  86. extern struct Screen *QuakeScreen;
  87. extern struct IntuitionBase *IntuitionBase;
  88.  
  89. static struct JoyType jt[] =
  90.   "GAMECTRL", SJA_TYPE_GAMECTLR,
  91.   "MOUSE",    SJA_TYPE_MOUSE,
  92.   "JOYSTICK", SJA_TYPE_JOYSTK,
  93.   "AUTOSENSE",SJA_TYPE_AUTOSENSE,
  94.   NULL,       0,
  95. }; 
  96.  
  97.  
  98.  
  99. int StringToType(char *type)
  100.   int i = 0;
  101.   while (i<4)
  102.   {
  103.     if (0 == stricmp(type, jt[i].Name)) return jt[i].Type;
  104.     i++;
  105.   }
  106.   return SJA_TYPE_AUTOSENSE;
  107.  
  108. void ResetJoystick(void)
  109.   int Port;
  110.   for (Port = 0; Port < 4; ++Port)
  111.   {
  112.     JoyState[Port] = (ULONG)JP_TYPE_NOTAVAIL;
  113.     SetJoyPortAttrs(Port, SJA_Type, SJA_TYPE_AUTOSENSE, TAG_DONE);
  114.   }
  115.  
  116.  
  117. void IN_InitJoy()
  118. {
  119.   int numdevs;
  120.   cvar_t *cv;
  121.   char strValue[256];
  122.   float f;
  123.  
  124.   Cvar_RegisterVariable(&joy_advanced);
  125.   Cvar_RegisterVariable(&joy_forwardthreshold);
  126.   Cvar_RegisterVariable(&joy_sidethreshold);
  127.   Cvar_RegisterVariable(&joy_pitchthreshold);
  128.   Cvar_RegisterVariable(&joy_pitchsensitivity);
  129.   Cvar_RegisterVariable(&joy_yawsensitivity);
  130.   Cvar_RegisterVariable(&joy_forward_reverse);
  131.   Cvar_RegisterVariable(&joy_side_reverse);
  132.   Cvar_RegisterVariable(&joy_up_reverse);
  133.   Cvar_RegisterVariable(&joy_sidesensitivity);
  134.   Cvar_RegisterVariable(&joy_forwardsensitivity);
  135.   Cvar_RegisterVariable(&joy_force0);
  136.   Cvar_RegisterVariable(&joy_force1);
  137.   Cvar_RegisterVariable(&joy_force2);
  138.   Cvar_RegisterVariable(&joy_force3);
  139.   Cvar_RegisterVariable(&joy_onlypsx);
  140.  
  141.   f = Cvar_VariableValue("in_initjoy");
  142.   if (!Cvar_FindVar("in_initjoy")) f=1.0;
  143.   if ( !f) return;
  144.   
  145.   if (joy_onlypsx.value) return;
  146.   
  147.   stick=1;
  148.  
  149.   numdevs=-1;
  150.   if (LowLevelBase)
  151.   {
  152.     int MyPort=0;
  153.     ResetJoystick();
  154.     for (MyPort=0;MyPort<4;MyPort++)
  155.     {
  156.       JoyState[MyPort] = (int)(ReadJoyPort(MyPort));
  157.       if ((JoyState[MyPort]!=JP_TYPE_NOTAVAIL)&&(!(JoyState[MyPort]&JP_TYPE_MOUSE)))
  158.       {
  159.         numdevs=MyPort;
  160.         break;
  161.       }
  162.     }
  163.   }
  164.   tj.mv_a=32768;
  165.   tj.mv_b=32768;
  166.  
  167.   if (numdevs == -1)
  168.     return;
  169.   else Port=numdevs;
  170.  
  171.   Con_Printf ("\nAmiga joystick available\n");
  172.   strcpy(strValue,Cvar_VariableString("joy_force0"));
  173.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  174.   SetJoyPortAttrs(0, SJA_Type,   StringToType(strValue), TAG_DONE);
  175.   Con_Printf("Joystick Port 0 is %s\n", strValue);
  176.  
  177.   strcpy(strValue,Cvar_VariableString("joy_force1"));
  178.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  179.   SetJoyPortAttrs(1, SJA_Type,   StringToType(strValue), TAG_DONE);
  180.   Con_Printf("Joystick Port 1 is %s\n", strValue);
  181.  
  182.   strcpy(strValue,Cvar_VariableString("joy_force2"));
  183.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  184.   SetJoyPortAttrs(2, SJA_Type,   StringToType(strValue), TAG_DONE);
  185.   Con_Printf("Joystick Port 2 is %s\n", strValue);
  186.  
  187.   strcpy(strValue,Cvar_VariableString("joy_force3"));
  188.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  189.   SetJoyPortAttrs(3, SJA_Type,   StringToType(strValue), TAG_DONE);
  190.   Con_Printf("Joystick Port 3 is %s\n", strValue);
  191. }
  192.  
  193. void IN_ShutdownJoystick(void) 
  194.   if (LowLevelBase)
  195.   {
  196.     if (!ack1) SystemControl(SCON_RemCreateKeys,1,TAG_END);
  197.     if (!ack0) SystemControl(SCON_RemCreateKeys,0,TAG_END);
  198.     ResetJoystick();
  199.   }
  200.  
  201. void RawValuePointer()
  202.   int button=0;
  203.   int dir=0;
  204.   int joytype;
  205. #ifndef GLQUAKE
  206.   if (IntuitionBase->FirstScreen != QuakeScreen) return;
  207. #endif 
  208.   if ( ( JoyState[ Port ] = ReadJoyPort(Port)) != JP_TYPE_NOTAVAIL )
  209.   {
  210.     button=(JoyState[ Port ]) & (JP_BUTTON_MASK);
  211.     dir = (JoyState[ Port ]) & (JP_DIRECTION_MASK);
  212.     tj.nvalue=0;
  213.     joytype=JoyState[Port]&JP_TYPE_MASK;
  214.     if ((joytype&JP_TYPE_JOYSTK)&&(joytype&JP_TYPE_GAMECTLR)) joytype=JP_TYPE_GAMECTLR;
  215.     if (((joytype&JP_TYPE_JOYSTK)||(joytype&JP_TYPE_GAMECTLR))==0) joytype=JP_TYPE_JOYSTK;
  216.     switch(joytype)
  217.     {
  218.       case JP_TYPE_GAMECTLR:
  219.       {
  220.         if (stick) stick=0;
  221.         if (button&JPF_BUTTON_RED) tj.value=tj.value|1;
  222.         else if (tj.value&1)
  223.         {
  224.           tj.value=tj.value&~1;
  225.           tj.nvalue|=1;
  226.         }
  227.         if (button&JPF_BUTTON_YELLOW) tj.value=tj.value|4;
  228.         else if (tj.value&4)
  229.         {
  230.           tj.value=tj.value&~4;
  231.           tj.nvalue|=4;
  232.         }
  233.         if (button&JPF_BUTTON_GREEN) tj.value=tj.value|8;
  234.         else if (tj.value&8)
  235.         {
  236.           tj.value=tj.value&~8;
  237.           tj.nvalue|=8;
  238.         }
  239.         if (button&JPF_BUTTON_BLUE) tj.value=tj.value|2;
  240.         else if (tj.value&2)
  241.         {
  242.           tj.value=tj.value&~2;
  243.           tj.nvalue|=2;
  244.         }
  245.         if (button&JPF_BUTTON_FORWARD) tj.value=tj.value|16;
  246.         else if (tj.value&16)
  247.         {
  248.           tj.value=tj.value&~16;
  249.           tj.nvalue|=16;
  250.         }
  251.         if (button&JPF_BUTTON_REVERSE) tj.value=tj.value|32;
  252.         else if (tj.value&32)
  253.         {
  254.           tj.value=tj.value&~32;
  255.           tj.nvalue|=32;
  256.         }
  257.         if (button&JPF_BUTTON_PLAY) tj.value=tj.value|64;
  258.         else if (tj.value&64)
  259.         {
  260.           tj.value=tj.value&~64;
  261.           tj.nvalue|=64;
  262.         }
  263.         if (dir&JPF_JOY_UP) tj.mv_a=0;
  264.         else if (dir&JPF_JOY_DOWN) tj.mv_a=65536;
  265.         else tj.mv_a=32768;
  266.         if (dir&JPF_JOY_LEFT) tj.mv_b=0;
  267.         else if (dir&JPF_JOY_RIGHT) tj.mv_b=65536;
  268.         else tj.mv_b=32768;
  269.       }
  270.       break;
  271.       case JP_TYPE_JOYSTK :
  272.       {
  273.         if (button&JPF_BUTTON_RED) tj.value=tj.value|1;
  274.         else if (tj.value&1)
  275.         {
  276.           tj.value=tj.value&~1;
  277.           tj.nvalue|=1;
  278.         }
  279.         if (dir&JPF_JOY_UP) tj.mv_a=0;
  280.         else if (dir&JPF_JOY_DOWN) tj.mv_a=65536;
  281.         else tj.mv_a=32768;
  282.         if (dir&JPF_JOY_LEFT) tj.mv_b=0;
  283.         else if (dir&JPF_JOY_RIGHT) tj.mv_b=65536;
  284.         else tj.mv_b=32768;
  285.       }
  286.       break;
  287.     }
  288.   }
  289. }
  290.  
  291. void IN_JoyMove (usercmd_t *cmd) 
  292.   float   speed, aspeed;
  293.   int   fAxisValue;
  294.   int key_index;
  295.  
  296.   if (!LowLevelBase) return;
  297.   if (joy_onlypsx.value) return;
  298.   
  299.   if (in_speed.state & 1) speed = cl_movespeedkey.value;
  300.   else speed = 1;
  301.   aspeed = speed * host_frametime;
  302.  
  303.   RawValuePointer();
  304.   fAxisValue= tj.mv_a;
  305.   fAxisValue -= 32768;
  306.   fAxisValue /= 32768;
  307.  
  308.   if ((joy_advanced.value == 0.0) && (in_mlook.state & 1))
  309.   {
  310.     if (fabs(fAxisValue) > joy_pitchthreshold.value)
  311.     {
  312.       if (m_pitch.value < 0.0) cl.viewangles[PITCH] -= (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  313.       else cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  314.     }
  315.   }
  316.   else cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
  317.  
  318.   fAxisValue = tj.mv_b;
  319.   fAxisValue -= 32768;
  320.   fAxisValue /= 32768;
  321.   cl.viewangles[YAW] -= (fAxisValue * 0.5 * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  322.   if (tj.value&1)
  323.   {
  324.     key_index = K_JOY1;
  325.     Key_Event(key_index+3,true);
  326.   }
  327.   else
  328.   {
  329.     if (tj.nvalue&1)
  330.     {
  331.       key_index = K_JOY1;
  332.       Key_Event(key_index+3,false);
  333.     }
  334.   }
  335.   if (stick) return;
  336.   if (tj.value&2)
  337.   {
  338.     key_index = K_JOY1;
  339.     Key_Event(key_index,true);
  340.   }
  341.   else
  342.   {
  343.     if (tj.nvalue&2)
  344.     {
  345.       key_index = K_JOY1;
  346.       Key_Event(key_index,false);
  347.     }
  348.   }
  349.   if (tj.value&4)
  350.   {
  351.     key_index = K_JOY1;
  352.     Key_Event(key_index+2,true);
  353.   }
  354.   else
  355.   {
  356.     if (tj.nvalue&4)
  357.     {
  358.       key_index = K_JOY1;
  359.       Key_Event(key_index+2,false);
  360.     }
  361.   }
  362.   if (tj.value&8)
  363.   {
  364.     key_index = K_JOY1;
  365.     Key_Event(key_index+1,true);
  366.   }
  367.   else
  368.   {
  369.     if (tj.nvalue&8)
  370.     {
  371.       key_index = K_JOY1;
  372.       Key_Event(key_index+1,false);
  373.     }
  374.   }
  375.   if (tj.value&16) cmd->sidemove -= (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  376.   if (tj.value&32) cmd->sidemove += (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  377.