home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quake_src / in_amigajoy.c < prev    next >
C/C++ Source or Header  |  2000-06-17  |  10KB  |  376 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.   f = Cvar_VariableValue("in_initjoy");
  127.   if (!Cvar_FindVar("in_initjoy")) f=1.0;
  128.   if ( !f) return;
  129.   
  130.   if (joy_onlypsx.value) return;
  131.   
  132.   stick=1;
  133.  
  134.   numdevs=-1;
  135.   if (LowLevelBase)
  136.   {
  137.     int MyPort=0;
  138.     ResetJoystick();
  139.     for (MyPort=0;MyPort<4;MyPort++)
  140.     {
  141.       JoyState[MyPort] = (int)(ReadJoyPort(MyPort));
  142.       if ((JoyState[MyPort]!=JP_TYPE_NOTAVAIL)&&(!(JoyState[MyPort]&JP_TYPE_MOUSE)))
  143.       {
  144.         numdevs=MyPort;
  145.         break;
  146.       }
  147.     }
  148.   }
  149.   tj.mv_a=32768;
  150.   tj.mv_b=32768;
  151.  
  152.   if (numdevs == -1)
  153.     return;
  154.   else Port=numdevs;
  155.  
  156.   Con_Printf ("\nAmiga joystick available\n");
  157.   strcpy(strValue,Cvar_VariableString("joy_force0"));
  158.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  159.   SetJoyPortAttrs(0, SJA_Type,   StringToType(strValue), TAG_DONE);
  160.   Con_Printf("Joystick Port 0 is %s\n", strValue);
  161.  
  162.   strcpy(strValue,Cvar_VariableString("joy_force1"));
  163.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  164.   SetJoyPortAttrs(1, SJA_Type,   StringToType(strValue), TAG_DONE);
  165.   Con_Printf("Joystick Port 1 is %s\n", strValue);
  166.  
  167.   strcpy(strValue,Cvar_VariableString("joy_force2"));
  168.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  169.   SetJoyPortAttrs(2, SJA_Type,   StringToType(strValue), TAG_DONE);
  170.   Con_Printf("Joystick Port 2 is %s\n", strValue);
  171.  
  172.   strcpy(strValue,Cvar_VariableString("joy_force3"));
  173.   if (strlen(strValue)==0) strcpy(strValue,"AUTOSENSE");
  174.   SetJoyPortAttrs(3, SJA_Type,   StringToType(strValue), TAG_DONE);
  175.   Con_Printf("Joystick Port 3 is %s\n", strValue);
  176. }
  177.  
  178. void IN_ShutdownJoystick(void) 
  179.   if (LowLevelBase)
  180.   {
  181.     if (!ack1) SystemControl(SCON_RemCreateKeys,1,TAG_END);
  182.     if (!ack0) SystemControl(SCON_RemCreateKeys,0,TAG_END);
  183.     ResetJoystick();
  184.   }
  185.  
  186. void RawValuePointer()
  187.   int button=0;
  188.   int dir=0;
  189.   int joytype;
  190.  
  191.   if (IntuitionBase->FirstScreen != QuakeScreen) return;
  192.  
  193.   if ( ( JoyState[ Port ] = ReadJoyPort(Port)) != JP_TYPE_NOTAVAIL )
  194.   {
  195.     button=(JoyState[ Port ]) & (JP_BUTTON_MASK);
  196.     dir = (JoyState[ Port ]) & (JP_DIRECTION_MASK);
  197.     tj.nvalue=0;
  198.     joytype=JoyState[Port]&JP_TYPE_MASK;
  199.     if ((joytype&JP_TYPE_JOYSTK)&&(joytype&JP_TYPE_GAMECTLR)) joytype=JP_TYPE_GAMECTLR;
  200.     if (((joytype&JP_TYPE_JOYSTK)||(joytype&JP_TYPE_GAMECTLR))==0) joytype=JP_TYPE_JOYSTK;
  201.     switch(joytype)
  202.     {
  203.       case JP_TYPE_GAMECTLR:
  204.       {
  205.         if (stick) stick=0;
  206.         if (button&JPF_BUTTON_RED) tj.value=tj.value|1;
  207.         else if (tj.value&1)
  208.         {
  209.           tj.value=tj.value&~1;
  210.           tj.nvalue|=1;
  211.         }
  212.         if (button&JPF_BUTTON_YELLOW) tj.value=tj.value|4;
  213.         else if (tj.value&4)
  214.         {
  215.           tj.value=tj.value&~4;
  216.           tj.nvalue|=4;
  217.         }
  218.         if (button&JPF_BUTTON_GREEN) tj.value=tj.value|8;
  219.         else if (tj.value&8)
  220.         {
  221.           tj.value=tj.value&~8;
  222.           tj.nvalue|=8;
  223.         }
  224.         if (button&JPF_BUTTON_BLUE) tj.value=tj.value|2;
  225.         else if (tj.value&2)
  226.         {
  227.           tj.value=tj.value&~2;
  228.           tj.nvalue|=2;
  229.         }
  230.         if (button&JPF_BUTTON_FORWARD) tj.value=tj.value|16;
  231.         else if (tj.value&16)
  232.         {
  233.           tj.value=tj.value&~16;
  234.           tj.nvalue|=16;
  235.         }
  236.         if (button&JPF_BUTTON_REVERSE) tj.value=tj.value|32;
  237.         else if (tj.value&32)
  238.         {
  239.           tj.value=tj.value&~32;
  240.           tj.nvalue|=32;
  241.         }
  242.         if (button&JPF_BUTTON_PLAY) tj.value=tj.value|64;
  243.         else if (tj.value&64)
  244.         {
  245.           tj.value=tj.value&~64;
  246.           tj.nvalue|=64;
  247.         }
  248.         if (dir&JPF_JOY_UP) tj.mv_a=0;
  249.         else if (dir&JPF_JOY_DOWN) tj.mv_a=65536;
  250.         else tj.mv_a=32768;
  251.         if (dir&JPF_JOY_LEFT) tj.mv_b=0;
  252.         else if (dir&JPF_JOY_RIGHT) tj.mv_b=65536;
  253.         else tj.mv_b=32768;
  254.       }
  255.       break;
  256.       case JP_TYPE_JOYSTK :
  257.       {
  258.         if (button&JPF_BUTTON_RED) tj.value=tj.value|1;
  259.         else if (tj.value&1)
  260.         {
  261.           tj.value=tj.value&~1;
  262.           tj.nvalue|=1;
  263.         }
  264.         if (dir&JPF_JOY_UP) tj.mv_a=0;
  265.         else if (dir&JPF_JOY_DOWN) tj.mv_a=65536;
  266.         else tj.mv_a=32768;
  267.         if (dir&JPF_JOY_LEFT) tj.mv_b=0;
  268.         else if (dir&JPF_JOY_RIGHT) tj.mv_b=65536;
  269.         else tj.mv_b=32768;
  270.       }
  271.       break;
  272.     }
  273.   }
  274. }
  275.  
  276. void IN_JoyMove (usercmd_t *cmd) 
  277.   float   speed, aspeed;
  278.   int   fAxisValue;
  279.   int key_index;
  280.  
  281.   if (!LowLevelBase) return;
  282.   if (joy_onlypsx.value) return;
  283.   
  284.   if (in_speed.state & 1) speed = cl_movespeedkey.value;
  285.   else speed = 1;
  286.   aspeed = speed * host_frametime;
  287.  
  288.   RawValuePointer();
  289.   fAxisValue= tj.mv_a;
  290.   fAxisValue -= 32768;
  291.   fAxisValue /= 32768;
  292.  
  293.   cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
  294. #if 0
  295.   if ((joy_advanced.value == 0.0) && (in_mlook.state & 1))
  296.   {
  297.     if (fabs(fAxisValue) > joy_pitchthreshold.value)
  298.     {
  299.       if (m_pitch.value < 0.0) cl.viewangles[PITCH] -= (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  300.       else cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  301.     }
  302.   }
  303.   else cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
  304. #endif
  305.  
  306.   fAxisValue = tj.mv_b;
  307.   fAxisValue -= 32768;
  308.   fAxisValue /= 32768;
  309.   cl.viewangles[YAW] -= (fAxisValue * 0.5 * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  310.   if (tj.value&1)
  311.   {
  312.     key_index = K_JOY1;
  313.     Key_Event(key_index+3,true);
  314.   }
  315.   else
  316.   {
  317.     if (tj.nvalue&1)
  318.     {
  319.       key_index = K_JOY1;
  320.       Key_Event(key_index+3,false);
  321.     }
  322.   }
  323.   if (stick) return;
  324.   if (tj.value&2)
  325.   {
  326.     key_index = K_JOY1;
  327.     Key_Event(key_index,true);
  328.   }
  329.   else
  330.   {
  331.     if (tj.nvalue&2)
  332.     {
  333.       key_index = K_JOY1;
  334.       Key_Event(key_index,false);
  335.     }
  336.   }
  337.   if (tj.value&4)
  338.   {
  339.     key_index = K_JOY1;
  340.     Key_Event(key_index+2,true);
  341.   }
  342.   else
  343.   {
  344.     if (tj.nvalue&4)
  345.     {
  346.       key_index = K_JOY1;
  347.       Key_Event(key_index+2,false);
  348.     }
  349.   }
  350.   if (tj.value&8)
  351.   {
  352.     key_index = K_JOY1;
  353.     Key_Event(key_index+1,true);
  354.   }
  355.   else
  356.   {
  357.     if (tj.nvalue&8)
  358.     {
  359.       key_index = K_JOY1;
  360.       Key_Event(key_index+1,false);
  361.     }
  362.   }
  363.   if (tj.value&16) cmd->sidemove -= (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  364.   if (tj.value&32) cmd->sidemove += (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  365.