home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / cmanual / devices / gameportdevice / example1.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  16KB  |  470 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Gameport Device             Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-27                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /* This program will read 20 joystick events,   */
  23. /* using port 2 (the "Joystick Port"), before   */
  24. /* it will terminate. This example is "nice"    */
  25. /* to the system and does everything according  */
  26. /* to the "rules".                              */
  27. /*                                              */
  28. /* While we are waiting for something to happen */
  29. /* our task is put to sleep, so we do not waste */
  30. /* any computer time. However, if you want to   */
  31. /* do something while you are waiting, see next */
  32. /* example.                                     */
  33.  
  34.  
  35.  
  36. #include <exec/types.h>         /* UWORD, STRPTR */
  37. #include <devices/gameport.h>   /* GPCT_ABSJOYSTICK  */
  38. #include <devices/inputevent.h> /* struct InputEvent */
  39.  
  40.  
  41.  
  42. #define RIGHTGAMEPORT 1 /* The "Joystick" port. */
  43. #define LEFTGAMEPORT  0 /* The "Mouse" port.    */
  44.  
  45.  
  46.  
  47. /* Pointer to the Graphics library: */
  48. struct GfxBase *GfxBase;
  49.  
  50.  
  51.  
  52. struct IOStdReq *game_io_msg;    /* Pointer to our IOStdReq str.    */
  53. struct MsgPort   *game_msg_port; /* Pointer to our message port.    */
  54. BOOL deviceerror;                /* Have we opened the device, OK?  */
  55.  
  56.  
  57.  
  58. /* Declare some external functions:        */
  59. /* (We must otherwise do so much casting.) */
  60. extern struct MsgPort *CreatePort();
  61. extern struct IOStdReq *CreateStdIO();    
  62.  
  63.  
  64.  
  65. /* Declare all functions in this module: */
  66. void main();
  67. void PrintJoystickData();    /* Print some information about the joystick. */
  68. BYTE SetControllerType();    /* Set type of controller.                    */
  69. BYTE SetControllerTrigger(); /* Set trigger.                               */
  70. void SetControllerRead();    /* Prepare it so we can read.                 */
  71. BYTE GetControllerType();    /* Get type of controller already connected.  */
  72. void clean_up();             /* Cleanup, and quit.                         */
  73.  
  74.  
  75.  
  76. void main()
  77. {
  78.   /* Put all data in this structure: */
  79.   struct InputEvent gamedata;
  80.  
  81.   BYTE type;
  82.   int counter;
  83.  
  84.  
  85.  
  86.   /* Open the Graphics library: */
  87.   GfxBase = (struct GfxBase *)
  88.     OpenLibrary( "graphics.library", 0 );
  89.   if( !GfxBase )
  90.     clean_up( "ERROR! Could not open the Graphics library!" );
  91.  
  92.  
  93.   /* 1. Create a message port so the system can communicate with us: */
  94.   game_msg_port = CreatePort( 0, 0 );
  95.   if( !game_msg_port )
  96.     clean_up( "ERROR! Could not create message port!" );
  97.  
  98.  
  99.   /* 2. Allocate and initialize a new I/O request block.  */
  100.   /* It should use our new message port as reply port:    */
  101.   game_io_msg = CreateStdIO( game_msg_port );     
  102.   if( !game_io_msg )
  103.     clean_up( "ERROR! Could not allocate new I/O request block!" );
  104.  
  105.  
  106.   /* 3. Open the Game Port Device, use the right port: */
  107.   deviceerror = OpenDevice( "gameport.device", RIGHTGAMEPORT, game_io_msg, 0xFFFF );
  108.   if( deviceerror )
  109.     clean_up( "ERROR! Could not open the Game Port Device!" );
  110.  
  111.  
  112.   /* 4. Check if some other task is already using the gameport: */
  113.   if( type = GetControllerType() )
  114.   {
  115.     switch( type )
  116.     {
  117.       case GPCT_MOUSE:
  118.         printf( "A mouse is connected to the port!\n" );
  119.         break;
  120.  
  121.       case GPCT_RELJOYSTICK:
  122.         printf( "A proportional joystick is connected to the port!\n" );
  123.         break;
  124.  
  125.       case GPCT_ABSJOYSTICK:
  126.         printf( "A normal joystick is connected to the port!\n" );
  127.         break;
  128.     }
  129.     
  130.     /* Do not close the device! If we do it, the other task will */
  131.     /* then not be able to use the gameport device either!       */
  132.     deviceerror = TRUE;
  133.     clean_up( "ERROR! Some other task is already using the Gameport!" );
  134.   }
  135.  
  136.  
  137.   /* 5. Set device type (absolute joystick): */
  138.   if( SetControllerType( GPCT_ABSJOYSTICK ) )
  139.     clean_up( "ERROR! Could not set device type!" );
  140.  
  141.  
  142.   /* 6. Set device trigger: */
  143.   if( SetControllerTrigger
  144.       (                            /* Report following events:        */
  145.         GPTF_DOWNKEYS|GPTF_UPKEYS, /* firebutton pressed and release, */ 
  146.         0,                         /* no timeout messages,            */
  147.         1,                         /* every X movements, and          */
  148.         1                          /* every Y movements.              */
  149.       )
  150.     )
  151.     clean_up( "ERROR! Could not set device trigger!" );
  152.  
  153.  
  154.   /* 7. Prepare the device to read: */
  155.   SetControllerRead( &gamedata );
  156.  
  157.  
  158.  
  159.   printf( "Gameport Device - Joystick Example - Waiting\n" );
  160.   /* Collect 20 messages, and the leave: */
  161.   counter = 0;
  162.   while( counter < 20 )
  163.   {
  164.     printf("\nWaiting... ");
  165.  
  166.     /* Do our request, and return without delay: */
  167.     SendIO( game_io_msg );
  168.  
  169.  
  170.     /* Wait for a message to arrive at our message port. */
  171.     /* While we are waiting our task is put to sleep,    */
  172.     /* which means that we will not waste any computer   */ 
  173.     /* time. Zzz Zzz Zzz...                              */
  174.     WaitPort( game_msg_port );
  175.  
  176.  
  177.     /* Try to collect a message: */
  178.     if( GetMsg( game_msg_port ) )
  179.     {
  180.       /* Print some information abut the joystick: */
  181.       PrintJoystickData( &gamedata );
  182.       counter++;
  183.     }
  184.   }
  185.  
  186.  
  187.   /* 8. Clean up nicely, and quit: */
  188.   clean_up( "The End!" );
  189. }    
  190.  
  191.  
  192.  
  193. /****************************************************************/
  194. /*                                                              */
  195. /* PrintJoystickData() prints current stick and button position */
  196. /* of a Joystick.                                               */
  197. /*                                                              */
  198. /* Synopsis: PrintJoystickData( data );                         */
  199. /*                                                              */
  200. /* data:     (struct InputEvent *) Pointer to an InputEvent     */
  201. /*           structure which has previously been initialized.   */
  202. /*                                                              */
  203. /****************************************************************/
  204.  
  205. void PrintJoystickData( data )
  206. struct InputEvent *data;
  207. {
  208.   WORD xdirection;
  209.   WORD ydirection;
  210.   UWORD code;
  211.  
  212.   /* Collect data: */
  213.   xdirection = data->ie_X;
  214.   ydirection = data->ie_Y;
  215.   code = data->ie_Code;
  216.  
  217.  
  218.   /* Was the button pressed or released? */
  219.   if( code == IECODE_LBUTTON ) 
  220.     printf("Button pressed. ");
  221.  
  222.   if( code == IECODE_LBUTTON + IECODE_UP_PREFIX )
  223.     printf("Button released. ");
  224.  
  225.  
  226.   /* What is the position of the stick: */
  227.   printf( "Stick position:" );
  228.   switch(ydirection) 
  229.   {
  230.     case -1:
  231.       printf( " Forward" );
  232.       break;
  233.  
  234.     case 1:
  235.       printf( " Back" );
  236.       break;
  237.   }
  238.  
  239.   switch(xdirection) 
  240.   {
  241.     case -1:
  242.       printf( " Left" );
  243.       break;
  244.  
  245.     case 1:
  246.       printf( " Right" );
  247.       break;
  248.   }
  249. }
  250.  
  251.  
  252.  
  253. /*************************************************************/
  254. /*                                                           */
  255. /* SetControllerType() tells the System what type of device  */
  256. /* (absolute joystick, proportional joystick or a mouse) you */
  257. /* want to handle.                                           */
  258. /*                                                           */
  259. /* Synopsis: error = SetControllerType( type, data );        */
  260. /*                                                           */
  261. /* error:    (BYTE) If th