home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / glquake_src / in_amigapsx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-17  |  15.2 KB  |  615 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 <proto/lowlevel.h> 
  38. #include <intuition/intuition.h>
  39. #include <intuition/intuitionbase.h> 
  40. #include <devices/gameport.h> 
  41. #include <libraries/lowlevel.h>
  42. #include <proto/exec.h>
  43. #include <proto/lowlevel.h>
  44. #include "psxport.h" 
  45.  
  46. extern cvar_t  joy_advanced;
  47. extern cvar_t  joy_forwardthreshold;
  48. extern cvar_t  joy_sidethreshold;
  49. extern cvar_t  joy_pitchthreshold;
  50. extern cvar_t  joy_pitchsensitivity;
  51. extern cvar_t  joy_sensitivity;
  52.  
  53. extern cvar_t  joy_side_reverse;
  54. extern cvar_t  joy_up_reverse;
  55. extern cvar_t  joy_forward_reverse;
  56.  
  57. extern cvar_t joy_forwardsensitivity;
  58. extern cvar_t joy_sidesensitivity;
  59. cvar_t joy_psxdeadzone={"joy_psxdeadzone","48",true};
  60.  
  61. int analog_centered=FALSE;
  62. int analog_clx, analog_cly;
  63. int analog_lx, analog_ly;
  64. int analog_crx, analog_cry;
  65. int analog_rx, analog_ry;
  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. static struct TheJoy tj;
  77.  
  78. extern struct Screen *QuakeScreen;
  79. extern struct IntuitionBase *IntuitionBase;
  80.  
  81. struct GamePortTrigger gameport_gpt = {
  82.   GPTF_UPKEYS | GPTF_DOWNKEYS,    /* gpt_Keys */
  83.   0,                /* gpt_Timeout */
  84.   1,                /* gpt_XDelta */
  85.   1                /* gpt_YDelta */
  86. };
  87.  
  88. static struct InputEvent gameport_ie;
  89.  
  90. int psxport=0; 
  91.  
  92. static struct MsgPort *gameport_mp = NULL;
  93. static struct IOStdReq *gameport_io = NULL;
  94. static BYTE gameport_ct;
  95. static int gameport_is_open = FALSE;
  96. static int gameport_io_in_progress = FALSE;
  97. static int gameport_curr,gameport_prev;
  98.  
  99. void ResetPsx(void)
  100.   int ix;
  101.  
  102. #ifdef __STORM__
  103.   if ((gameport_mp = CreateMsgPort ()) == NULL ||
  104.       (gameport_io = (struct IOStdReq *)CreateIORequest (gameport_mp, sizeof(struct IOStdReq))) == NULL)
  105. #else
  106.   if ((gameport_mp = CreatePort(NULL,0)) ==NULL ||
  107.       (gameport_io = CreateStdIO(gameport_mp)) == NULL)
  108. #endif
  109.     Sys_Error ("Can't create MsgPort or IoRequest");
  110.  
  111.   for (ix=0; ix<4; ix++) 
  112.   {
  113.     if (OpenDevice ("psxport.device", ix, (struct IORequest *)gameport_io, 0) == 0) 
  114.     {
  115.       Con_Printf ("\npsxport.device unit %d opened.\n",ix);
  116.       gameport_io->io_Command = GPD_ASKCTYPE;
  117.       gameport_io->io_Length = 1;
  118.       gameport_io->io_Data = &gameport_ct;
  119.       DoIO ((struct IORequest *)gameport_io);
  120.       if (gameport_ct == GPCT_NOCONTROLLER) 
  121.       {
  122.         gameport_is_open = TRUE;
  123.         ix=4;
  124.       } 
  125.       else 
  126.       {
  127.         CloseDevice ((struct IORequest *)gameport_io);
  128.       }
  129.     } 
  130.   }
  131.  
  132.   if (!gameport_is_open) 
  133.   {
  134.     Con_Printf ("No psxport, or no free psx controllers!  Joystick disabled.\n");
  135.   } 
  136.   else 
  137.   {
  138.  
  139.     gameport_ct = GPCT_ALLOCATED;
  140.     gameport_io->io_Command = GPD_SETCTYPE;
  141.     gameport_io->io_Length = 1;
  142.     gameport_io->io_Data = &gameport_ct;
  143.     DoIO ((struct IORequest *)gameport_io);
  144.  
  145.     gameport_io->io_Command = GPD_SETTRIGGER;
  146.     gameport_io->io_Length = sizeof(struct GamePortTrigger);
  147.     gameport_io->io_Data = &gameport_gpt;
  148.     DoIO ((struct IORequest *)gameport_io);
  149.  
  150.     gameport_io->io_Command = GPD_READEVENT;
  151.     gameport_io->io_Length = sizeof (struct InputEvent);
  152.     gameport_io->io_Data = &gameport_ie;
  153.     SendIO ((struct IORequest *)gameport_io);
  154.     gameport_io_in_progress = TRUE;
  155.   }
  156.  
  157.  
  158. void IN_InitPsx()
  159. {
  160.   int numdevs;
  161.   cvar_t *cv;
  162.   char strValue[256];
  163.   float f;
  164.  
  165.   joy_advanced.value=Cvar_VariableValue("joy_advanced");
  166.   if (!Cvar_FindVar("joy_advanced")) joy_advanced.value=0;
  167.   joy_forwardthreshold.value=Cvar_VariableValue("joy_forwardthreshold");
  168.   if (!Cvar_FindVar("joy_forwardthreshold")) joy_forwardthreshold.value=0.15;
  169.   joy_sidethreshold.value=Cvar_VariableValue("joy_sidethreshold");
  170.   if (!Cvar_FindVar("joy_sidethreshold")) joy_sidethreshold.value=0.15;
  171.   joy_pitchthreshold.value=Cvar_VariableValue("joy_pitchthreshold");
  172.   if (!Cvar_FindVar("joy_pitchthreshold")) joy_pitchthreshold.value=0.15;
  173.   joy_pitchsensitivity.value=Cvar_VariableValue("joy_pitchsensitivity");
  174.   if (!Cvar_FindVar("joy_pitchsensitivity")) joy_pitchsensitivity.value=1;
  175.   joy_forward_reverse.value= Cvar_VariableValue("joy_forward_reverse");
  176.   if (!Cvar_FindVar("joy_forward_reverse")) joy_forward_reverse.value=0;
  177.   joy_side_reverse.value = Cvar_VariableValue("joy_side_reverse");
  178.   if (!Cvar_FindVar("joy_side_reverse")) joy_side_reverse.value=0;
  179.   joy_forwardsensitivity.value=Cvar_VariableValue("joy_forwardsensitivity");
  180.   if (!Cvar_FindVar("joy_forwardsensitivity")) joy_forwardsensitivity.value=-1;
  181.   joy_sidesensitivity.value=Cvar_VariableValue("joy_sidesensitivity");
  182.   if (!Cvar_FindVar("joy_sidesensitivity")) joy_sidesensitivity.value=-1;
  183.   joy_psxdeadzone.value=Cvar_VariableValue("joy_psxdeadzone");
  184.   if (!Cvar_FindVar("joy_psxdeadzone")) joy_psxdeadzone.value=48;
  185.  
  186.   f = Cvar_VariableValue("in_initjoy");
  187.   if (!Cvar_FindVar("in_initjoy")) f=1.0;
  188.   if ( !f) return;
  189.   
  190.   ResetPsx();
  191.   tj.mv_a=32768;
  192.   tj.mv_b=32768;
  193. }
  194.  
  195. void IN_ShutdownPsx(void) 
  196.  if (gameport_io_in_progress) 
  197.  {
  198.    AbortIO ((struct IORequest *)gameport_io);
  199.    WaitIO ((struct IORequest *)gameport_io);
  200.    gameport_io_in_progress = FALSE;
  201.    gameport_ct = GPCT_NOCONTROLLER;
  202.    gameport_io->io_Command = GPD_SETCTYPE;
  203.    gameport_io->io_Length = 1;
  204.    gameport_io->io_Data = &gameport_ct;
  205.    DoIO ((struct IORequest *)gameport_io);
  206.  }
  207.  if (gameport_is_open) 
  208.  {
  209.    CloseDevice ((struct IORequest *)gameport_io);
  210.    gameport_is_open = FALSE;
  211.  }
  212.  if (gameport_io != NULL) 
  213.  {
  214. #ifdef __STORM__
  215.    DeleteIORequest ((struct IORequest *)gameport_io);
  216. #else
  217.    DeleteStdIO(gameport_io);
  218. #endif
  219.    gameport_io = NULL;
  220.  }
  221.  if (gameport_mp != NULL) 
  222.  {
  223. #ifdef __STORM__
  224.    DeleteMsgPort (gameport_mp);
  225. #else
  226.    DeletePort(gameport_mp);
  227. #endif
  228.    gameport_mp = NULL;
  229.  }
  230.  
  231. void RawValuePointerPsx(usercmd_t *cmd)
  232. {
  233.   int x,y; 
  234. #ifndef GLQUAKE
  235.   if (IntuitionBase->FirstScreen != QuakeScreen) return;
  236. #endif
  237.   if (GetMsg (gameport_mp) != NULL) 
  238.   {
  239.  
  240.     if ((gameport_ie.ie_Class == PSX_CLASS_JOYPAD) 
  241.      || (gameport_ie.ie_Class == PSX_CLASS_WHEEL)) 
  242.       analog_centered = false;
  243.       
  244.     if (PSX_CLASS(gameport_ie) != PSX_CLASS_MOUSE)
  245.     {
  246.       gameport_curr = ~PSX_BUTTONS(gameport_ie);
  247.       if (gameport_curr !=gameport_prev)
  248.       {
  249.  
  250.         if (gameport_curr & PSX_TRIANGLE) tj.value = tj.value|1;
  251.         else if (tj.value&1)
  252.         {
  253.           tj.value=tj.value&~1;
  254.           tj.nvalue|=1;
  255.         }
  256.  
  257.         if (gameport_curr & PSX_SQUARE) tj.value=tj.value|4;
  258.         else if (tj.value&4)
  259.         {
  260.           tj.value=tj.value&~4;
  261.           tj.nvalue|=4;
  262.         }
  263.  
  264.         if (gameport_curr & PSX_CIRCLE) tj.value=tj.value|8;
  265.         else if (tj.value&8)
  266.         {
  267.           tj.value=tj.value&~8;
  268.           tj.nvalue|=8;
  269.         }
  270.  
  271.         if (gameport_curr & PSX_CROSS) tj.value=tj.value|2;
  272.         else if (tj.value&2)
  273.         {
  274.           tj.value=tj.value&~2;
  275.           tj.nvalue|=2;
  276.         }
  277.   
  278.         if (gameport_curr & PSX_LEFT) tj.mv_b=0;
  279.         else if (gameport_curr & PSX_RIGHT) tj.mv_b=65536;
  280.         else tj.mv_b=32768;
  281.        
  282.         if (gameport_curr & PSX_UP) tj.mv_a=0;
  283.         else if (gameport_curr & PSX_DOWN) tj.mv_a=65536;
  284.         else tj.mv_a=32768;
  285.  
  286.         if (gameport_curr & PSX_START) tj.value=tj.value|64;
  287.         else if (tj.value&64)
  288.         {
  289.           tj.value=tj.value&~64;
  290.           tj.nvalue|=64;
  291.         }
  292.  
  293.         if (gameport_curr & PSX_SELECT) tj.value=tj.value|128;
  294.         else if (tj.value&128)
  295.         {
  296.           tj.value=tj.value&~128;
  297.           tj.nvalue|=128;
  298.         }
  299.  
  300.         if (gameport_curr & PSX_R1) tj.value=tj.value|16;
  301.         else if (tj.value&16)
  302.         {
  303.           tj.value=tj.value&~16;
  304.           tj.nvalue|=16;
  305.         }
  306.  
  307.         if (gameport_curr & PSX_L1) tj.value=tj.value|32;
  308.         else if (tj.value&32)
  309.         {
  310.           tj.value=tj.value&~32;
  311.           tj.nvalue|=32;
  312.         }
  313.  
  314.         if (gameport_curr & PSX_R2) tj.value=tj.value|256;
  315.         else if (tj.value&256)
  316.         {
  317.           tj.value=tj.value&~256;
  318.           tj.nvalue|=256;
  319.         }
  320.  
  321.         if (gameport_curr & PSX_L2) tj.value=tj.value|512;
  322.         else if (tj.value&512)
  323.         {
  324.           tj.value=tj.value&~512;
  325.           tj.nvalue|=512;
  326.         }
  327.  
  328.         gameport_prev = gameport_curr;
  329.  
  330.       }
  331.  
  332.     }
  333.  
  334.     if ((PSX_CLASS(gameport_ie) == PSX_CLASS_ANALOG) 
  335.      || (PSX_CLASS(gameport_ie) == PSX_CLASS_ANALOG2) 
  336.      || (PSX_CLASS(gameport_ie) == PSX_CLASS_ANALOG_MODE2)) 
  337.     {
  338.       analog_lx = PSX_LEFTX(gameport_ie);
  339.       analog_ly = PSX_LEFTY(gameport_ie);
  340.       analog_rx = PSX_RIGHTX(gameport_ie);
  341.       analog_ry = PSX_RIGHTY(gameport_ie);
  342.  
  343.       if (!analog_centered) 
  344.       {
  345.         analog_clx = analog_lx;
  346.         analog_cly = analog_ly;
  347.         analog_crx = analog_rx;
  348.         analog_cry = analog_ry;
  349.         analog_centered = true;
  350.       }          
  351.  
  352.       x=analog_lx-analog_clx;
  353.       y=analog_cly-analog_ly;
  354.       if ((abs(x)>joy_psxdeadzone.value) || (abs(y)>joy_psxdeadzone.value)) 
  355.       {
  356.         x<<=1;
  357.         y<<=1;
  358.         x=x*sensitivity.value;
  359.         y=y*sensitivity.value;
  360.       if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
  361.         cmd->sidemove += m_side.value * x;
  362.     else cl.viewangles[YAW] -= m_yaw.value * x;
  363.     
  364.         if ((in_strafe.state & 1) && noclip_anglehack) cmd->upmove += m_forward.value * y;
  365.         else cmd->forwardmove += m_forward.value * y;
  366.       }
  367.       
  368.       x=analog_rx-analog_crx;
  369.       y=analog_cry-analog_ry;
  370.       if ((abs(x)>joy_psxdeadzone.value) || (abs(y)>joy_psxdeadzone.value)) 
  371.       {
  372.         x<<=1;
  373.         y<<=1;
  374.         x=x*sensitivity.value;
  375.         y=y*sensitivity.value;
  376.         if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
  377.         cmd->sidemove += m_side.value * x;
  378.     else cl.viewangles[YAW] -= m_yaw.value * x;
  379.     
  380.     V_StopPitchDrift ();
  381.         
  382.     cl.viewangles[PITCH] += m_pitch.value * y;
  383.         if (cl.viewangles[PITCH] > 80) cl.viewangles[PITCH] = 80;
  384.         if (cl.viewangles[PITCH] < -70) cl.viewangles[PITCH] = -70;
  385.       }
  386.  
  387.       if (gameport_curr & PSX_R3) tj.value=tj.value|1024;
  388.       else if (tj.value&1024)
  389.       {
  390.         tj.value=tj.value&~1024;
  391.         tj.nvalue|=1024;
  392.       }
  393.  
  394.       if (gameport_curr & PSX_L3) tj.value=tj.value|2048;
  395.       else if (tj.value&2048)
  396.       {
  397.         tj.value=tj.value&~2048;
  398.         tj.nvalue|=2048;
  399.       }
  400.     }
  401.  
  402.     if (gameport_ie.ie_Class == PSX_CLASS_MOUSE) 
  403.     {
  404.       gameport_curr = ~PSX_BUTTONS(gameport_ie);
  405.  
  406.       if (gameport_curr & PSX_LMB) tj.value = tj.value|1;
  407.       else if (tj.value&1)
  408.       {
  409.         tj.value=tj.value&~1;
  410.         tj.nvalue|=1;
  411.       }
  412.  
  413.       if (gameport_curr & PSX_RMB) tj.value=tj.value|4;
  414.       else if (tj.value&4)
  415.       {
  416.         tj.value=tj.value&~4;
  417.         tj.nvalue|=4;
  418.       }
  419.  
  420.  
  421.       x=PSX_MOUSEDX(gameport_ie);
  422.       y=-PSX_MOUSEDY(gameport_ie);
  423.       x<<=2;
  424.       y<<=2;
  425.       x=x*sensitivity.value;
  426.       y=y*sensitivity.value;
  427.       if ( (in_strafe.state & 1) || (lookstrafe.value && (in_mlook.state & 1) ))
  428.       cmd->sidemove += m_side.value * x;
  429.       else cl.viewangles[YAW] -= m_yaw.value * x;
  430.     
  431.       if (in_mlook.state & 1) V_StopPitchDrift ();
  432.      
  433.       if ( (in_mlook.state & 1) && !(in_strafe.state & 1)) 
  434.       {
  435.         cl.viewangles[PITCH] += m_pitch.value * y;
  436.         if (cl.viewangles[PITCH] > 80) cl.viewangles[PITCH] = 80;
  437.         if (cl.viewangles[PITCH] < -70) cl.viewangles[PITCH] = -70;
  438.       }
  439.       else 
  440.       {
  441.         if ((in_strafe.state & 1) && noclip_anglehack) cmd->upmove -= m_forward.value * y;
  442.         else cmd->forwardmove -= m_forward.value * y;
  443.       }
  444.  
  445.     }
  446.  
  447.     gameport_io->io_Command = GPD_READEVENT;
  448.     gameport_io->io_Length = sizeof (struct InputEvent);
  449.     gameport_io->io_Data = &gameport_ie;
  450.     SendIO ((struct IORequest *)gameport_io);
  451.   }
  452. }
  453.  
  454. void IN_PsxMove (usercmd_t *cmd) 
  455.   float   speed, aspeed;
  456.   int   fAxisValue;
  457.   int key_index;
  458.   
  459.   if (!gameport_is_open) return;
  460.  
  461.   if (in_speed.state & 1) speed = cl_movespeedkey.value;
  462.   else speed = 1;
  463.   aspeed = speed * host_frametime;
  464.  
  465.   RawValuePointerPsx(cmd);
  466.   fAxisValue= tj.mv_a;
  467.   fAxisValue -= 32768;
  468.   fAxisValue /= 32768;
  469.  
  470.   cmd->forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value;
  471.  
  472.   fAxisValue = tj.mv_b;
  473.   fAxisValue -= 32768;
  474.   fAxisValue /= 32768;
  475.   cl.viewangles[YAW] -= (fAxisValue * 0.5 * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value;
  476.   if (tj.value&1)
  477.   {
  478.     key_index = K_JOY1;
  479.     Key_Event(key_index+3,true);
  480.   }
  481.   else
  482.   {
  483.     if (tj.nvalue&1)
  484.     {
  485.       key_index = K_JOY1;
  486.       Key_Event(key_index+3,false);
  487.     }
  488.   }
  489.   if (tj.value&2)
  490.   {
  491.     key_index = K_JOY1;
  492.     Key_Event(key_index,true);
  493.   }
  494.   else
  495.   {
  496.     if (tj.nvalue&2)
  497.     {
  498.       key_index = K_JOY1;
  499.       Key_Event(key_index,false);
  500.     }
  501.   }
  502.   if (tj.value&4)
  503.   {
  504.     key_index = K_JOY1;
  505.     Key_Event(key_index+2,true);
  506.   }
  507.   else
  508.   {
  509.     if (tj.nvalue&4)
  510.     {
  511.       key_index = K_JOY1;
  512.       Key_Event(key_index+2,false);
  513.     }
  514.   }
  515.   if (tj.value&8)
  516.   {
  517.     key_index = K_JOY1;
  518.     Key_Event(key_index+1,true);
  519.   }
  520.   else
  521.   {
  522.     if (tj.nvalue&8)
  523.     {
  524.       key_index = K_JOY1;
  525.       Key_Event(key_index+1,false);
  526.     }
  527.   }
  528.   if (tj.value&16) cmd->sidemove -= (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  529.   if (tj.value&32) cmd->sidemove += (joy_sidesensitivity.value) * speed * cl_sidespeed.value;
  530.  
  531.   if (tj.value&64)
  532.   {
  533.     key_index = K_JOY1;
  534.     Key_Event(key_index+4,true);
  535.   }
  536.   else
  537.   {
  538.     if (tj.nvalue&64)
  539.     {
  540.       key_index=K_JOY1;
  541.       Key_Event(key_index+4,false);
  542.     }
  543.   }
  544.   if (tj.value&128)
  545.   {
  546.     key_index = K_JOY1;
  547.     Key_Event(key_index+5,true);
  548.   }
  549.   else
  550.   {
  551.     if (tj.nvalue&128)
  552.     {
  553.       key_index=K_JOY1;
  554.       Key_Event(key_index+5,false);
  555.     }
  556.   }
  557.   if (tj.value&256)
  558.   {
  559.     key_index = K_JOY1;
  560.     Key_Event(key_index+6,true);
  561.   }
  562.   else
  563.   {
  564.     if (tj.nvalue&256)
  565.     {
  566.       key_index=K_JOY1;
  567.       Key_Event(key_index+6,false);
  568.     }
  569.   }
  570.   if (tj.value&512)
  571.   {
  572.     key_index = K_JOY1;
  573.     Key_Event(key_index+7,true);
  574.   }
  575.   else
  576.   {
  577.     if (tj.nvalue&512)
  578.     {
  579.       key_index=K_JOY1;
  580.       Key_Event(key_index+7,false);
  581.     }
  582.   }
  583.   if (tj.value&1024)
  584.   {
  585.     key_index = K_JOY1;
  586.     Key_Event(key_index+8,true);
  587.   }
  588.   else
  589.   {
  590.     if (tj.nvalue&1024)
  591.     {
  592.       key_index=K_JOY1;
  593.       Key_Event(key_index+8,false);
  594.     }
  595.   }
  596.   if (tj.value&2048)
  597.   {
  598.     key_index = K_JOY1;
  599.     Key_Event(key_index+9,true);
  600.   }
  601.   else
  602.   {
  603.     if (tj.nvalue&2048)
  604.     {
  605.       key_index=K_JOY1;
  606.       Key_Event(key_index+9,false);
  607.     }
  608.   }
  609.