home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quakeworld_src / client / in_amigajoy.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-17  |  10.1 KB  |  393 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. #pragma amiga-align
  38. #include <libraries/lowlevel.h> 
  39. #include <proto/exec.h> 
  40. #include <proto/lowlevel.h> 
  41. #include <intuition/intuition.h>
  42. #include <intuition/intuitionbase.h> 
  43. #pragma default-align
  44.  
  45. cvar_t  joy_advanced={"joy_advanced","0",true};
  46. cvar_t  joy_forwardthreshold={"joy_forwardthreshold","0.15",true};
  47. cvar_t  joy_sidethreshold={"joy_sidethreshold","0.15",true};
  48. cvar_t  joy_pitchthreshold={"joy_pitchthreshold","0.15",true};
  49. cvar_t  joy_pitchsensitivity={"joy_pitchsensitivity","1",true};
  50. cvar_t  joy_yawsensitivity={"joy_yawsensitivity","-1",true};
  51.  
  52. cvar_t  joy_side_reverse={"joy_side_reverse","0",true};
  53. cvar_t  joy_up_reverse={"joy_up_reverse","0",true};
  54. cvar_t  joy_forward_reverse={"joy_forward_reverse","0",true};
  55.  
  56. cvar_t joy_forwardsensitivity={"joy_forwardsensitivity","-1",true};
  57. cvar_t joy_sidesensitivity={"joy_sidesensitivity","-1",true};
  58.  
  59. cvar_t joy_force0={"joy_force0","AUTOSENSE",true};
  60. cvar_t joy_force1={"joy_force1","AUTOSENSE",true};
  61. cvar_t joy_force2={"joy_force2","AUTOSENSE",true};
  62. cvar_t joy_force3={"joy_force3","AUTOSENSE",true};
  63. cvar_t joy_onlypsx={"joy_onlypsx","0",true};
  64.  
  65. extern int psxused;
  66.  
  67. struct TheJoy
  68. {
  69.   int mv_a;
  70.   int mv_b;
  71.   int axis;
  72.   int value;
  73.   int nvalue;
  74. };
  75.  
  76. struct JoyType
  77.   char *Name;
  78.   ULONG Type;
  79. }; 
  80.  
  81.  
  82. static struct TheJoy tj;
  83. static int ack0 = -1, ack1 = -1;
  84. static int Port;
  85. static int JoyState[4];
  86. static int stick=1;
  87.  
  88. extern struct Screen *QuakeScreen;
  89. extern struct IntuitionBase *IntuitionBase;
  90.  
  91. static struct JoyType jt[] =
  92.   "GAMECTRL", SJA_TYPE_GAMECTLR,
  93.   "MOUSE",    SJA_TYPE_MOUSE,
  94.   "JOYSTICK", SJA_TYPE_JOYSTK,
  95.   "AUTOSENSE",SJA_TYPE_AUTOSENSE,
  96.   NULL,       0,
  97. }; 
  98.  
  99.  
  100.  
  101. int StringToType(char *type)
  102.   int i = 0;
  103.   while (i<4)
  104.   {
  105.     if (0 == stricmp(type, jt[i].Name)) return jt[i].Type;
  106.     i++;
  107.   }
  108.   return SJA_TYPE_AUTOSENSE;
  109.  
  110. void ResetJoystick(void)
  111.   int Port;
  112.   for (Port = 0; Port < 4; ++Port)
  113.   {
  114.     JoyState[Port] = (ULONG)JP_TYPE_NOTAVAIL;
  115.     SetJoyPortAttrs(Port, SJA_Type, SJA_TYPE_AUTOSENSE, TAG_DONE);
  116.   }
  117.  
  118.  
  119. void IN_InitJoy()
  120. {
  121.   int numdevs;
  122.   cvar_t *cv;
  123.   char strValue[256];
  124.   float f;
  125.  
  126.   Cvar_RegisterVariable(&joy_advanced);
  127.   Cvar_RegisterVariable(&joy_forwardthreshold);
  128.   Cvar_RegisterVariable(&joy_sidethreshold);
  129.   Cvar_RegisterVariable(&joy_pitchthreshold);
  130.   Cvar_RegisterVariable(&joy_pitchsensitivity);
  131.   Cvar_RegisterVariable(&joy_yawsensitivity);
  132.   Cvar_RegisterVariable(&joy_forward_reverse);
  133.   Cvar_RegisterVariable(&joy_side_reverse);
  134.   Cvar_RegisterVariable(&joy_up_reverse);
  135.   Cvar_RegisterVariable(&joy_sidesensitivity);
  136.   Cvar_RegisterVariable(&joy_forwardsensitivity);
  137.   Cvar_RegisterVariable(&joy_force0);
  138.   Cvar_RegisterVariable(&joy_force1);
  139.   Cvar_RegisterVariable(&joy_force2);
  140.   Cvar_RegisterVariable(&joy_force3);
  141.   Cvar_RegisterVariable(&joy_onlypsx);
  142.  
  143.   f = Cvar_VariableValue("in_initjoy");
  144.   if (!Cvar_FindVar("in_initjoy")) f=1.0;
  145.   if ( !f) return;
  146.   
  147.   if (joy_onlypsx.value) return;
  148.   
  149.   stick=1;
  150.  
  151.   numdevs=-1;
  152.   if (LowLevelBase)
  153.   {
  154.     int MyPort=0;
  155.     ResetJoystick();
  156.     for (MyPort=0;MyPort<4;MyPort++)
  157.     {
  158.       JoyState[MyPort] = (int)(ReadJoyPort(MyPort));
  159.       if ((JoyState[MyPort]!=JP_TYPE_NOTAVAIL)&&(!(JoyState[MyPort]&JP_TYPE_MOUSE)))
  160.       {
  161.         numdevs=MyPort;
  162.         break;
  163.       }
  164.     }
  165.   }
  166.   tj.mv_a=32768;
  167.   tj.mv_b=32768;
  168.  
  169.   if (numdevs == -1)
  170.     return;
  171.   else Port=numdevs;
  172.  
  173.   Con_Printf ("\nAmiga joystick available\n");
  174.   strcpy(strValue,Cvar_VariableString("joy_force0"));
  175.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  176.   SetJoyPortAttrs(0, SJA_Type,   StringToType(strValue), TAG_DONE);
  177.   Con_Printf("Joystick Port 0 is %s\n", strValue);
  178.  
  179.   strcpy(strValue,Cvar_VariableString("joy_force1"));
  180.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  181.   SetJoyPortAttrs(1, SJA_Type,   StringToType(strValue), TAG_DONE);
  182.   Con_Printf("Joystick Port 1 is %s\n", strValue);
  183.  
  184.   strcpy(strValue,Cvar_VariableString("joy_force2"));
  185.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  186.   SetJoyPortAttrs(2, SJA_Type,   StringToType(strValue), TAG_DONE);
  187.   Con_Printf("Joystick Port 2 is %s\n", strValue);
  188.  
  189.   strcpy(strValue,Cvar_VariableString("joy_force3"));
  190.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  191.   SetJoyPortAttrs(3, SJA_Type,   StringToType(strValue), TAG_DONE);
  192.   Con_Printf("Joystick Port 3 is %s\n", strValue);
  193. }
  194.  
  195. void IN_ShutdownJoystick(void) 
  196.   if (LowLevelBase)
  197.   {
  198.     if (!ack1) SystemControl(SCON_RemCreateKeys,1,TAG_END);
  199.     if (!ack0) SystemControl(SCON_RemCreateKeys,0,TAG_END);
  200.     ResetJoystick();
  201.   }
  202.  
  203. void RawValuePointer()
  204.   int button=0;
  205.   int dir=0;
  206.   int joytype;
  207.  
  208.   if (IntuitionBase->FirstScreen != QuakeScreen) return;
  209.  
  210.   if ( ( JoyState[ Port ] = ReadJoyPort(Port)) != JP_TYPE_NOTAVAIL )
  211.   {
  212.     button=(JoyState[ Port ]) & (JP_BUTTON_MASK);
  213.     dir = (JoyState[ Port ]) & (JP_DIRECTION_MASK);
  214.     tj.nvalue=0;
  215.     joytype=JoyState[Port]&JP_TYPE_MASK;
  216.     if ((joytype&JP_TYPE_JOYSTK)&&(joytype&JP_TYPE_GAMECTLR)) joytype=JP_TYPE_GAMECTLR;
  217.     if (((joytype&JP_TYPE_JOYSTK)||(joytype&JP_TYPE_GAMECTLR))==0) joytype=JP_TYPE_JOYSTK;
  218.     switch(joytype)
  219.     {
  220.       case JP_TYPE_GAMECTLR:
  221.       {
  222.         if (stick) stick=0;
  223.         if (button&JPF_BUTTON_RED) tj.value=tj.value|1;
  224.         else if (tj.value&1)
  225.         {
  226.           tj.value=tj.value&~1;
  227.           tj.nvalue|=1;
  228.         }
  229.         if (button&JPF_BUTTON_YELLOW) tj.value=tj.value|4;
  230.         else if (tj.value&4)
  231.         {
  232.           tj.value=tj.value&~4;
  233.           tj.nvalue|=4;
  234.         }
  235.         if (button&JPF_BUTTON_GREEN) tj.value=tj.value|8;
  236.         else if (tj.value&8)
  237.         {
  238.           tj.value=tj.value&~8;
  239.           tj.nvalue|=8;
  240.         }
  241.         if (button&JPF_BUTTON_BLUE) tj.value=tj.value|2;
  242.         else if (tj.value&2)
  243.         {
  244.           tj.value=tj.value&~2;
  245.           tj.nvalue|=2;
  246.         }
  247.         if (button&JPF_BUTTON_FORWARD) tj.value=tj.value|16;
  248.         else if (tj.value&16)
  249.         {
  250.           tj.value=tj.value&~16;
  251.           tj.nvalue|=16;
  252.         }
  253.         if (button&JPF_BUTTON_REVERSE) tj.value=tj.value|32;
  254.         else if (tj.value&32)
  255.         {
  256.           tj.value=tj.value&~32;
  257.           tj.nvalue|=32;
  258.         }
  259.         if (button&JPF_BUTTON_PLAY) tj.value=tj.value|64;
  260.         else if (tj.value&64)
  261.         {
  262.           tj.value=tj.value&~64;
  263.           tj.nvalue|=64;
  264.         }
  265.         if (dir&JPF_JOY_UP) tj.mv_a=0;
  266.         else if (dir&JPF_JOY_DOWN) tj.mv_a=65536;
  267.         else tj.mv_a=32768;
  268.         if (dir&JPF_JOY_LEFT) tj.mv_b=0;
  269.         else if (dir&JPF_JOY_RIGHT) tj.mv_b=65536;
  270.         else tj.mv_b=32768;
  271.       }
  272.       break;
  273.       case JP_TYPE_JOYSTK :
  274.       {
  275.         if (button&JPF_BUTTON_RED) tj.value=tj.value|1;
  276.         else if (tj.value&1)
  277.         {
  278.           tj.value=tj.value&~1;
  279.           tj.nvalue|=1;
  280.         }
  281.         if (dir&JPF_JOY_UP) tj.mv_a=0;
  282.         else if (dir&JPF_JOY_DOWN) tj.mv_a=65536;
  283.         else tj.mv_a=32768;
  284.         if (dir&JPF_JOY_LEFT) tj.mv_b=0;
  285.         else if (dir&JPF_JOY_RIGHT) tj.mv_b=65536;
  286.         else tj.mv_b=32768;
  287.       }
  288.       break;
  289.     }
  290.   }
  291. }
  292.  
  293. void IN_JoyMove (usercmd_t *cmd) 
  294.   float   speed, aspeed;
  295.   int   fAxisValue;
  296.   int key_index;
  297.  
  298.   if (!LowLevelBase) return;
  299.   if (joy_onlypsx.value) return;
  300.   
  301.   if (in_speed.state & 1) speed = cl_movespeedkey.value;
  302.   else speed = 1;
  303.   aspeed = speed * host_frametime;
  304.  
  305.   RawValuePointer();
  306.   fAxisValue= tj.mv_a;
  307.   fAxisValue -= 32768;
  308.   fAxisValue /= 32768;
  309.  
  310.   cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
  311. #if 0
  312.   if ((joy_advanced.value == 0.0) && (in_mlook.state & 1))
  313.   {
  314.     if (fabs(fAxisValue) > joy_pitchthreshold.value)
  315.     {
  316.       if (m_pitch.value < 0.0) cl.viewangles[PITCH] -= (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  317.       else cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  318.     }
  319.   }
  320.   else cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
  321. #endif
  322.  
  323.   fAxisValue = tj.mv_b;
  324.   fAxisValue -= 32768;
  325.   fAxisValue /= 32768;
  326.   cl.viewangles[YAW] -= (fAxisValue * 0.5 * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  327.   if (tj.value&1)
  328.   {
  329.     key_index = K_JOY1;
  330.     Key_Event(key_index+3,true);
  331.   }
  332.   else
  333.   {
  334.     if (tj.nvalue&1)
  335.     {
  336.       key_index = K_JOY1;
  337.       Key_Event(key_index+3,false);
  338.     }
  339.   }
  340.   if (stick) return;
  341.   if (tj.value&2)
  342.   {
  343.     key_index = K_JOY1;
  344.     Key_Event(key_index,true);
  345.   }
  346.   else
  347.   {
  348.     if (tj.nvalue&2)
  349.     {
  350.       key_index = K_JOY1;
  351.       Key_Event(key_index,false);
  352.     }
  353.   }
  354.   if (tj.value&4)
  355.   {
  356.     key_index = K_JOY1;
  357.     Key_Event(key_index+2,true);
  358.   }
  359.   else
  360.   {
  361.     if (tj.nvalue&4)
  362.     {
  363.       key_index = K_JOY1;
  364.       Key_Event(key_index+2,false);
  365.     }
  366.   }
  367.   if (tj.value&8)
  368.   {
  369.     key_index = K_JOY1;
  370.     Key_Event(key_index+1,true);
  371.   }
  372.   else
  373.   {
  374.     if (tj.nvalue&8)
  375.     {
  376.       key_index = K_JOY1;
  377.       Key_Event(key_index+1,false);
  378.     }
  379.   }
  380.   if (tj.value&16) cmd->sidemove -= (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  381.   if (tj.value&32) cmd->sidemove += (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  382.