home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 98 / af098sub.adf / asteroids2.LZX / asteroids2 / rotx / input.c < prev    next >
C/C++ Source or Header  |  2010-01-17  |  8KB  |  417 lines

  1. #include <exec/types.h>
  2. #include <dos/dos.h>
  3. #include <exec/memory.h>
  4. #include <intuition/intuition.h>
  5. #include <devices/gameport.h>
  6. #include <devices/input.h>
  7. #include <exec/interrupts.h>
  8. #include <h/rot.h>
  9. #include <h/extern.h>
  10.  
  11. struct MsgPort *timeport = NULL;
  12. struct MsgPort *tport = NULL;
  13. struct timerequest *timereq;
  14. struct timerequest tr;
  15.  
  16. struct MsgPort *gameport = NULL;
  17. struct IOStdReq *gamereq;
  18. struct GamePortTrigger gpt = { GPTF_DOWNKEYS | GPTF_UPKEYS,30000,1,1 };
  19. struct InputEvent joyevent;
  20.  
  21. struct MsgPort *inputDevPort;
  22. struct IOStdReq *inputRequestBlock;
  23. struct Interrupt handler;
  24.  
  25. ULONG class,code,qual;
  26. ULONG gameportmask, timermask;
  27.  
  28.  
  29. struct InputEvent *__saveds __asm __interrupt inputhandlercode(register __a0 struct InputEvent *);
  30.  
  31.  
  32.  
  33. HandleInput(player,type)
  34. LONG player,type;
  35. {
  36.  
  37. if (in.FIUP[type] == FALSE)
  38.     if (--control.firecount[player] < 0)
  39.         {
  40.         in.FI[type] = TRUE;
  41.         control.firecount[player] = control.firedelay[player];
  42.         }
  43.  
  44. if (in.RT[type] == TRUE)
  45.     {
  46.     if (++ship[player].pos > 31)
  47.         ship[player].pos= 0;
  48.     }
  49. if (in.LT[type] == TRUE)
  50.     {
  51.     if (--ship[player].pos <  0)
  52.         ship[player].pos=31;
  53.     }
  54. if (in.TH[type] == TRUE) IncVelocity(player);
  55. if (in.THUP[type] == TRUE)
  56.     {
  57.     FlushSound(1);
  58.     in.THUP[type] = FALSE;
  59.     }
  60. if (in.FI[type] == TRUE)
  61.     {
  62.     control.firecount[player] = control.firedelay[player];
  63.  
  64.     if (control.fire[player] == 0) InitFire(player);
  65.     else
  66.     if (control.fire[player] == 1) InitDoubleFire(player);
  67.     else
  68.     if (control.fire[player] == 2) InitSuperFire(player);
  69.     else                         InitBarrageFire(player);
  70.     }
  71.  
  72. if (in.HY[type] == TRUE)
  73.     {
  74.     Hyperspace(player);
  75.     in.HY[type] = FALSE;
  76.     }
  77. }
  78.  
  79.  
  80. struct InputEvent *__asm __saveds __interrupt
  81. inputhandlercode(register __a0 struct InputEvent *keyevent)
  82. {
  83.  
  84. Forbid();
  85. class = keyevent->ie_Class;
  86. code = keyevent->ie_Code;
  87. qual = keyevent->ie_Qualifier;
  88.  
  89. if ((class == IECLASS_RAWKEY) && ((qual&0xF00) != IEQUALIFIER_REPEAT))
  90.     {
  91.     in.KEY = code;
  92.  
  93.     if (code == k.fire)
  94.         {
  95.         in.FI[0] = TRUE;
  96.         in.FIUP[0] = FALSE;
  97.         }
  98.     else
  99.     if (code == k.fire+0x80) in.FIUP[0] = TRUE;
  100.     else
  101.     if (code == k.hyperspace) in.HY[0] = TRUE;
  102.     else
  103.     if (code == k.left+0x80)
  104.         {
  105.         in.LT[0] = FALSE;
  106.         }
  107.     else
  108.     if (code == k.right+0x80)
  109.         {
  110.         in.RT[0] = FALSE;
  111.         }
  112.     else
  113.     if (code == k.right)
  114.         {
  115.         in.RT[0]=TRUE;
  116.         in.LT[0]=FALSE;
  117.         }
  118.     else
  119.     if (code == k.left)
  120.         {
  121.         in.LT[0]=TRUE;
  122.         in.RT[0]=FALSE;
  123.         }
  124.     else
  125.     if (code == k.thrust) in.TH[0] = TRUE;
  126.     else
  127.     if (code == k.thrust+0x80)
  128.         {
  129.         in.TH[0] = FALSE;
  130.         in.THUP[0] = TRUE;
  131.         }
  132.     else
  133.     if (code == ESC) in.EXIT=TRUE;
  134.     else
  135.     if (code == k.pause) in.PAUSE=TRUE;
  136.     else
  137.     if (code == RET) in.NEXT = TRUE;
  138.     }
  139. Permit();
  140.  
  141. return(keyevent);
  142. }
  143.  
  144.  
  145. getjoystickinput()
  146. {
  147. LONG code,xx,yy;
  148.  
  149. if (GetMsg(gameport) != NULL)
  150. {
  151. code = joyevent.ie_Code;
  152.  
  153. gamereq->io_Command = GPD_READEVENT;
  154. gamereq->io_Flags    = NULL;
  155. gamereq->io_Data = (APTR)&joyevent;
  156. gamereq->io_Length = (LONG)sizeof(struct InputEvent);
  157. SendIO(gamereq);
  158.  
  159. if (code == IECODE_LBUTTON)
  160.     {
  161.     in.FI[1] = TRUE;
  162.     in.FIUP[1] = FALSE;
  163.     }
  164. else
  165. if (code == (IECODE_LBUTTON | IECODE_UP_PREFIX))
  166.     {
  167.     in.FIUP[1] = TRUE;
  168.     }
  169.  
  170. xx = joyevent.ie_X;
  171. yy = joyevent.ie_Y;
  172.  
  173. if (xx == 0)
  174.     {
  175.     in.LT[1] = FALSE;
  176.     in.RT[1] = FALSE;
  177.     in.HY[1] = FALSE;
  178.     }
  179. else
  180. if (xx == 1)
  181.     {
  182.     in.RT[1] =TRUE;
  183.     in.LT[1] =FALSE;
  184.     }
  185. else
  186. if (xx == -1)
  187.     {
  188.     in.LT[1]=TRUE;
  189.     in.RT[1]=FALSE;
  190.     }
  191.  
  192. if (yy == -1) in.TH[1] = TRUE;
  193. else
  194. if (yy ==  1) 
  195.     {
  196.     if (in.HY[1] == FALSE) in.HY[1] = TRUE;
  197.     }
  198. else
  199. if (yy ==  0)
  200.     {
  201.     in.TH[1] = FALSE;
  202.     in.THUP[1] = TRUE;
  203.     }
  204. }
  205. }
  206.  
  207.  
  208.  
  209. addgameport()
  210. {
  211. LONG error;
  212. UBYTE joystick = GPCT_ABSJOYSTICK;
  213. UBYTE type = 0;
  214.  
  215. gameport = (struct MsgPort *)CreatePort("test",NULL);
  216. if (gameport == NULL)
  217.     {
  218.     makerequest("Gameport Error");
  219.     Cleanup();
  220.     }
  221.  
  222. gamereq = (struct IOStdReq *)CreateExtIO(gameport,sizeof(struct IOStdReq));
  223. if (gamereq == NULL)
  224.     {
  225.     makerequest("Gameport IOStdReq Error");
  226.     Cleanup();
  227.     }
  228.  
  229. error = OpenDevice("gameport.device",1,(struct IORequest *)gamereq,NULL);
  230. if (error != NULL)
  231.     {
  232.     makerequest("gameport.device Error");
  233.     Cleanup();
  234.     }
  235.  
  236. gamereq->io_Command = GPD_ASKCTYPE;
  237. gamereq->io_Flags = IOF_QUICK;
  238. gamereq->io_Length = 1;
  239. gamereq->io_Data = (APTR)&type;
  240. DoIO(gamereq);
  241.  
  242. if (type == GPCT_NOCONTROLLER)
  243.     {
  244.     gamereq->io_Command = GPD_SETCTYPE;
  245.     gamereq->io_Flags = IOF_QUICK;
  246.     gamereq->io_Data = (APTR)&joystick;
  247.     gamereq->io_Length = 1;
  248.     DoIO(gamereq);
  249.  
  250.     gamereq->io_Command = GPD_SETTRIGGER;
  251.     gamereq->io_Flags = IOF_QUICK;
  252.     gamereq->io_Data = (APTR)&gpt;
  253.     gamereq->io_Length = (LONG)sizeof(struct GamePortTrigger);
  254.     DoIO(gamereq);
  255.  
  256.     gamereq->io_Command = CMD_CLEAR;
  257.     gamereq->io_Flags = IOF_QUICK;
  258.     gamereq->io_Data    = NULL;
  259.     gamereq->io_Length    = 0;
  260.     DoIO(gamereq);
  261.  
  262.     gamereq->io_Command = GPD_READEVENT;
  263.     gamereq->io_Flags    = NULL;
  264.     gamereq->io_Data = (APTR)&joyevent;
  265.     gamereq->io_Length = (LONG)sizeof(struct InputEvent);
  266.     SendIO(gamereq);
  267.  
  268.     gameportmask = 1L << gameport->mp_SigBit;
  269.     }
  270. else
  271.     {
  272.     printf ("couldnt lock joystick port 1\n");
  273.     }
  274. }
  275.  
  276.  
  277. removegameport()
  278. {
  279. UBYTE joystick = GPCT_NOCONTROLLER;
  280.  
  281. if (gamereq)
  282.     {
  283.     AbortIO(gamereq);
  284.     WaitIO(gamereq);
  285.  
  286.     gamereq->io_Command = GPD_SETCTYPE;
  287.     gamereq->io_Flags = IOF_QUICK;
  288.     gamereq->io_Data = (APTR)&joystick;
  289.     gamereq->io_Length = 1;
  290.     DoIO(gamereq);
  291.  
  292.     CloseDevice((struct IORequest *)gamereq);
  293.     DeleteExtIO((struct IORequest *)gamereq);
  294.     }
  295.  
  296. if (gameport) DeletePort(gameport);
  297. }
  298.  
  299.  
  300. addhandler()
  301. {
  302. LONG error;
  303.  
  304. inputDevPort = (struct MsgPort *)CreatePort(NULL,NULL);
  305. if (inputDevPort == NULL)
  306.     {
  307.     makerequest("Input Device Port Error");
  308.     Cleanup();
  309.     }
  310.  
  311. inputRequestBlock = (struct IOStdReq *)CreateExtIO(inputDevPort,sizeof(struct IOStdReq));
  312. if (inputRequestBlock == NULL)
  313.     {
  314.     makerequest("Input IOStdReq Error");
  315.     Cleanup();
  316.     }
  317.  
  318. error = OpenDevice("input.device",NULL,(struct IORequest *)inputRequestBlock,NULL);
  319. if (error != NULL)
  320.     {
  321.     makerequest("input.device Error");
  322.     Cleanup();
  323.     }
  324.  
  325. handler.is_Code = (void(*)())inputhandlercode;
  326. handler.is_Data = (APTR)NULL;
  327. handler.is_Node.ln_Pri = 60;
  328.  
  329. inputRequestBlock->io_Data = (APTR)&handler;
  330. inputRequestBlock->io_Command= IND_ADDHANDLER;
  331. DoIO((struct IORequest *)inputRequestBlock);
  332. }
  333.  
  334.  
  335.  
  336. removehandler()
  337. {
  338.  
  339. if (inputRequestBlock)
  340.     {
  341.     inputRequestBlock->io_Command= IND_REMHANDLER;
  342.     inputRequestBlock->io_Data = (APTR)&handler;
  343.     DoIO((struct IORequest *)inputRequestBlock);
  344.  
  345.     CloseDevice((struct IORequest *)inputRequestBlock);
  346.     DeleteExtIO((struct IORequest *)inputRequestBlock);
  347.     }
  348.  
  349. if (inputDevPort) DeletePort(inputDevPort);
  350. }
  351.  
  352. addtimer()
  353. {
  354. LONG error;
  355.  
  356. timeport = (struct MsgPort *)CreatePort(NULL,NULL);
  357. if (timeport == NULL)
  358.     {
  359.     makerequest("Timerport Error");
  360.     Cleanup();
  361.     }
  362. timermask = 1L << timeport->mp_SigBit;
  363.  
  364. timereq = (struct timerequest *)CreateExtIO(timeport,sizeof(struct timerequest));
  365. if (timereq == NULL)
  366.     {
  367.     makerequest("timerequest Error");
  368.     Cleanup();
  369.     }
  370.  
  371. error = OpenDevice(TIMERNAME,UNIT_MICROHZ,(struct IORequest *)timereq,NULL);
  372. if (error != NULL)
  373.     {
  374.     makerequest("Cant Open Timer Devicer");
  375.     Cleanup();
  376.     }
  377. }
  378.  
  379.  
  380.  
  381. removetimer()
  382. {
  383. if (timereq)
  384.     {
  385.     CloseDevice((struct IORequest *)timereq);
  386.     DeleteExtIO((struct IORequest *)timereq);
  387.     }
  388.  
  389. if (timeport) DeletePort(timeport);
  390. }
  391.  
  392.  
  393. timedelay(msecs)
  394. LONG msecs;
  395. {
  396. timereq->tr_time.tv_secs = 0;
  397. timereq->tr_time.tv_micro = msecs;
  398. timereq->tr_node.io_Command = TR_ADDREQUEST;
  399. SendIO((struct IORequest *)timereq);
  400. }
  401.  
  402.  
  403. waitfortimer()
  404. {
  405. ULONG actual=NULL;
  406.  
  407. while (!(actual & timermask))
  408.     {
  409.     actual = Wait(gameportmask | timermask);
  410.     if (actual & gameportmask)
  411.         {
  412.         getjoystickinput();
  413.         }
  414.     }
  415. WaitIO(timereq);
  416. }
  417.