home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 377b.lha / devices / gameport / joy.c next >
Encoding:
C/C++ Source or Header  |  1980-02-03  |  8.1 KB  |  246 lines

  1. /* joy.c - gameport.device joystick example - 3/26/90 DART
  2.  * ANSI code, compiled and linked with LATTICE 5.04: lc -L joy
  3.  *
  4.  * Copyright (c) 1990 Commodore-Amiga, Inc.
  5.  *
  6.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  7.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  8.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  9.  * information on the correct usage of the techniques and operating system
  10.  * functions presented in this example.  The source and executable code of
  11.  * this example may only be distributed in free electronic form, via bulletin
  12.  * board or as part of a fully non-commercial and freely redistributable
  13.  * diskette.  Both the source and executable code (including comments) must
  14.  * be included, without modification, in any copy.  This example may not be
  15.  * published in printed form or distributed with any commercial product.
  16.  * However, the programming techniques and support routines set forth in
  17.  * this example may be used in the development of original executable
  18.  * software products for Commodore Amiga computers.
  19.  * All other rights reserved.
  20.  * This example is provided "as-is" and is subject to change; no warranties
  21.  * are made.  All use is at your own risk.  No liability or responsibility
  22.  * is assumed.
  23.  */
  24.  
  25. #include <exec/types.h>
  26. #include <exec/execbase.h>
  27. #include <exec/devices.h>
  28. #include <devices/gameport.h>
  29. #include <devices/inputevent.h>
  30. #include <libraries/dos.h>
  31.  
  32. #ifdef LATTICE
  33. #include <proto/all.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. int CXBRK(void) { return(0); }   /* Disable Lattice CTRL/C handling */
  37. #endif
  38.  
  39. #define XMOVE 1
  40. #define YMOVE 1
  41.  
  42. /* our functions */
  43. BOOL set_controller_type(BYTE);
  44. VOID set_trigger_conditions(struct GamePortTrigger *);
  45. VOID send_read_request(VOID);
  46. VOID check_move(VOID);      
  47. VOID flush_buffer(VOID);
  48. VOID free_gp_unit(VOID);
  49.  
  50. extern struct ExecBase *SysBase; /* for vertical blank frequency info */
  51. struct InputEvent game_event;   /* where input event will be stored */
  52. struct IOStdReq *game_io_msg = NULL;
  53. struct MsgPort  *game_msg_port = NULL;
  54.  
  55.  
  56. /* GLOBALS */
  57.  
  58. /* trigger on all joystick key transitions */
  59. struct GamePortTrigger joytrigger =
  60. {
  61.    GPTF_UPKEYS+GPTF_DOWNKEYS,
  62.    0,
  63.    XMOVE,
  64.    YMOVE
  65. };
  66.  
  67. UBYTE      hertz;   /* vertical blank frequency */
  68. WORD      codeval,error; 
  69. WORD      button_count, timeouts = 0;
  70. BOOL       not_finished = TRUE;
  71.  
  72. VOID main(int argc,char **argv)
  73. {
  74. /* Create port for gameport device communications */
  75. if (game_msg_port = CreatePort("RKM_game_port",0))
  76.    {
  77.    /* Create message block for device IO */
  78.    if (game_io_msg = CreateStdIO(game_msg_port))
  79.       {
  80.       /* Open the right/back (unit 1, number 2) gameport.device unit */
  81.       if (!(error=OpenDevice("gameport.device",1,game_io_msg,0)))
  82.          {
  83.          /* Set controller type to joystick */
  84.          if (set_controller_type(GPCT_ABSJOYSTICK)) 
  85.             {
  86.             /* Specify the trigger conditions */
  87.             set_trigger_conditions(&joytrigger);
  88.  
  89.             printf("\n >>> gameport.device joystick Demo <<<\n\n");
  90.  
  91.             if (hertz==60)printf(" Running on NTSC system (%u Hz).\n",hertz);
  92.             else if (hertz==50)printf(" Running on PAL system (%u Hz).\n",hertz);
  93.  
  94.             printf(" Attach joystick to right port (A2000) or  rear port (A1000).\n");
  95.             printf(" Then move joystick and click its button(s).\n\n");
  96.             printf(" To exit program press and release fire button 3 times. \n");
  97.             printf(" The program also exists if no activity occurs for 1 minute.\n\n");
  98.  
  99.             /* Clear device buffer. There might still be events left */
  100.             flush_buffer(); /* To start from a known state */
  101.  
  102.             /* From now on, just read input events into the event buffer, 
  103.              * one at a time.  READEVENT waits for the preset conditions. */
  104.  
  105.             send_read_request(); /* Send the initial gameport read request */
  106.  
  107.             while( (timeouts < 3) && (not_finished))
  108.                {
  109.                Wait(1L << game_msg_port->mp_SigBit);  /* Wait for joystick action */
  110.                GetMsg(game_msg_port); /* Remove message from message port */
  111.  
  112.                codeval = game_event.ie_Code;
  113.                switch(codeval) 
  114.                   {
  115.                   case IECODE_LBUTTON:   printf(" FIRE BUTTON PRESSED \n");
  116.                                          break;
  117.  
  118.                   case (IECODE_LBUTTON | IECODE_UP_PREFIX):
  119.                                          printf(" FIRE BUTTON RELEASED \n");
  120.                                          button_count++;
  121.                                          if (button_count==3)not_finished = FALSE; 
  122.                                          break;
  123.  
  124.                   case IECODE_NOBUTTON:  timeouts++;   /* Program will timeout after 1 minute */
  125.                                          button_count = 0;
  126.                                          break;
  127.  
  128.                   default:               break;
  129.                   }
  130.                check_move();    /* Check for change in position */ 
  131.  
  132.                /* We can now re-use our game_event... Send the next read request */
  133.                send_read_request();
  134.                }
  135.             /********************************************************* 
  136.              * Free gameport unit so other applications can use it ! *
  137.              ********************************************************/
  138.             free_gp_unit();
  139.             }
  140.          CloseDevice( game_io_msg);
  141.          }
  142.       DeleteStdIO(game_io_msg);
  143.       }
  144.    DeletePort(game_msg_port);
  145.    }
  146. exit(0);      
  147. }
  148. /* end of main */
  149.  
  150. /************************
  151. *  function definitions * --------------------------------------------
  152. ************************/
  153. BOOL set_controller_type(BYTE type)
  154. {
  155. BOOL success = FALSE;
  156. BYTE controller_type = 0;
  157.  
  158.    game_io_msg->io_Command = GPD_ASKCTYPE;    /* inquire current status */
  159.    game_io_msg->io_Length = 1; 
  160.    game_io_msg->io_Flags = IOF_QUICK;
  161.    game_io_msg->io_Data = (APTR)&controller_type; /* put answer in here */
  162.  
  163.    Forbid(); /* critical section start */
  164.    DoIO(game_io_msg);
  165.  
  166.    /* No one is using this device unit, let's claim it */
  167.    if (controller_type == GPCT_NOCONTROLLER)
  168.       {
  169.       game_io_msg->io_Command = GPD_SETCTYPE;
  170.       game_io_msg->io_Flags = IOF_QUICK;   
  171.       game_io_msg->io_Length = 1;
  172.       game_io_msg->io_Data = (APTR)&type;
  173.       DoIO( game_io_msg);   
  174.       success = TRUE;
  175.       }
  176.    Permit(); /* critical section end */
  177.    return(success);
  178. }    
  179.  
  180. VOID set_trigger_conditions(struct GamePortTrigger *gpt)
  181. {
  182.    /* get vertical blank frequency 
  183.     * US = 60 Hz, PAL = 50 Hz */
  184.    hertz = SysBase->VBlankFrequency;
  185.  
  186.    /* trigger every 20 seconds */
  187.    joytrigger.gpt_Timeout = (UWORD)hertz * 20;
  188.  
  189.    game_io_msg->io_Command = GPD_SETTRIGGER;   
  190.    game_io_msg->io_Length = (LONG)sizeof(struct GamePortTrigger);   
  191.    game_io_msg->io_Data = (APTR)gpt;   
  192.    DoIO(game_io_msg);   
  193. }
  194.  
  195. VOID check_move(VOID)      
  196. {
  197.    WORD xmove, ymove;
  198.  
  199.    xmove = game_event.ie_X;
  200.    ymove = game_event.ie_Y;
  201.  
  202.    if((xmove) || (ymove))
  203.       {
  204.       printf(" x = %2ld , y = %2ld -->",xmove, ymove);
  205.  
  206.       if (xmove == 1 && ymove == 0) printf(" RIGHT \n");
  207.       else if (xmove ==-1 && ymove == 0) printf(" LEFT \n");
  208.       else if (xmove == 0 && ymove == 1) printf(" DOWN \n");
  209.       else if (xmove == 0 && ymove ==-1) printf(" UP \n");
  210.  
  211.       else if (xmove == 1 && ymove == 1) printf(" RIGHT DOWN \n");
  212.       else if (xmove ==-1 && ymove == 1) printf(" LEFT DOWN \n");
  213.       else if (xmove == 1 && ymove ==-1) printf(" RIGHT UP \n");
  214.       else if (xmove ==-1 && ymove ==-1) printf(" LEFT UP \n");
  215.  
  216.       timeouts = 0;
  217.       }
  218. }
  219.  
  220. VOID flush_buffer(VOID)
  221. {
  222.    game_io_msg->io_Command = CMD_CLEAR;
  223.    game_io_msg->io_Flags = IOF_QUICK;
  224.    DoIO(game_io_msg);
  225. }
  226.  
  227. VOID send_read_request(VOID)
  228. {
  229.    game_io_msg->io_Command = GPD_READEVENT;   
  230.    game_io_msg->io_Length = sizeof(struct InputEvent); 
  231.    game_io_msg->io_Data = (APTR)&game_event;
  232.    SendIO(game_io_msg);  /* Asynchronous - message will return later */
  233. }
  234.  
  235. VOID free_gp_unit(VOID)
  236. {
  237. BYTE type = GPCT_NOCONTROLLER;
  238.  
  239.    game_io_msg->io_Command = GPD_SETCTYPE;
  240.    game_io_msg->io_Flags = IOF_QUICK;   
  241.    game_io_msg->io_Length = 1;
  242.    game_io_msg->io_Data = (APTR)&type;
  243.    DoIO( game_io_msg);   
  244. }
  245. /* eof joy.c */
  246.