home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / dev / c / cmanual / devices / gameportdevice / example2.c < prev    next >
C/C++ Source or Header  |  1993-10-12  |  17KB  |  484 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:    Example2.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. /* This example does not wait for something to */
  29. /* happen, but is instead constantly trying to */
  30. /* collect messages and can therefore continue */
  31. /* do do something while the user is not using */
  32. /* the Joystick.                               */
  33. /*                                             */
  34. /* Note how this program terminates!           */
  35.  
  36.  
  37.  
  38. #include <exec/types.h>         /* UWORD, STRPTR */
  39. #include <devices/gameport.h>   /* GPCT_ABSJOYSTICK  */
  40. #include <devices/inputevent.h> /* struct InputEvent */
  41.  
  42.  
  43.  
  44. #define RIGHTGAMEPORT 1 /* The "Joystick" port. */
  45. #define LEFTGAMEPORT  0 /* The "Mouse" port.    */
  46.  
  47.  
  48.  
  49. /* Pointer to the Graphics library: */
  50. struct GfxBase *GfxBase;
  51.  
  52.  
  53.  
  54. struct IOStdReq *game_io_msg;    /* Pointer to our IOStdReq str.    */
  55. struct MsgPort   *game_msg_port; /* Pointer to our message port.    */
  56. BOOL deviceerror;                /* Have we opened the device, OK?  */
  57.  
  58.  
  59.  
  60. /* Declare some external functions:        */
  61. /* (We must otherwise do so much casting.) */
  62. extern struct MsgPort *CreatePort();
  63. extern struct IOStdReq *CreateStdIO();    
  64.  
  65.  
  66.  
  67. /* Declare all functions in this module: */
  68. void main();
  69. void PrintJoystickData();    /* Print some information about the joystick. */
  70. BYTE SetControllerType();    /* Set type of controller.                    */
  71. BYTE SetControllerTrigger(); /* Set trigger.                               */
  72. void SetControllerRead();    /* Prepare it so we can read.                 */
  73. BYTE GetControllerType();    /* Get type of controller already connected.  */
  74. void clean_up();             /* Cleanup, and quit.                         */
  75.  
  76.  
  77.  
  78. void main()
  79. {
  80.   /* Put all data in this structure: */
  81.   struct InputEvent gamedata;
  82.  
  83.   BYTE type;
  84.   int counter;
  85.  
  86.  
  87.  
  88.   /* Open the Graphics library: */
  89.   GfxBase = (struct GfxBase *)
  90.     OpenLibrary( "graphics.library", 0 );
  91.   if( !GfxBase )
  92.     clean_up( "ERROR! Could not open the Graphics library!" );
  93.  
  94.  
  95.   /* 1. Create a message port so the system can communicate with us: */
  96.   game_msg_port = CreatePort( 0, 0 );
  97.   if( !game_msg_port )
  98.     clean_up( "ERROR! Could not create message port!" );
  99.  
  100.  
  101.   /* 2. Allocate and initialize a new I/O request block.  */
  102.   /* It should use our new message port as reply port:    */
  103.   game_io_msg = CreateStdIO( game_msg_port );     
  104.   if( !game_io_msg )
  105.     clean_up( "ERROR! Could not allocate new I/O request block!" );
  106.  
  107.  
  108.   /* 3. Open the Game Port Device, use the right port: */
  109.   deviceerror = OpenDevice( "gameport.device", RIGHTGAMEPORT, game_io_msg, 0xFFFF );
  110.   if( deviceerror )
  111.     clean_up( "ERROR! Could not open the Game Port Device!" );
  112.  
  113.  
  114.   /* 4. Check if some other task is already using the gameport: */
  115.   if( type = GetControllerType() )
  116.   {
  117.     switch( type )
  118.     {
  119.       case GPCT_MOUSE:
  120.         printf( "A mouse is connected to the port!\n" );
  121.         break;
  122.  
  123.       case GPCT_RELJOYSTICK:
  124.         printf( "A proportional joystick is connected to the port!\n" );
  125.         break;
  126.  
  127.       case GPCT_ABSJOYSTICK:
  128.         printf( "A normal joystick is connected to the port!\n" );
  129.         break;
  130.     }
  131.     
  132.     /* Do not close the device! If we do it, the other task will */
  133.     /* then not be able to use the gameport device either!       */
  134.     deviceerror = TRUE;
  135.     clean_up( "ERROR! Some other task is already using the Gameport!" );
  136.   }
  137.  
  138.  
  139.   /* 5. Set device type (absolute joystick): */
  140.   if( SetControllerType( GPCT_ABSJOYSTICK ) )
  141.     clean_up( "ERROR! Could not set device type!" );
  142.  
  143.  
  144.   /* 6. Set device trigger: */
  145.   if( SetControllerTrigger
  146.       (                            /* Report following events:        */
  147.         GPTF_DOWNKEYS|GPTF_UPKEYS, /* firebutton pressed and release, */ 
  148.         0,                         /* no timeout messages,            */
  149.         1,                         /* every X movements, and          */
  150.         1                          /* every Y movements.              */
  151.       )
  152.     )
  153.     clean_up( "ERROR! Could not set device trigger!" );
  154.  
  155.  
  156.   /* 7. Prepare the device to read: */
  157.   SetControllerRead( &gamedata );
  158.  
  159.  
  160.   /* 8. Do our request, and return without delay: */
  161.   SendIO( game_io_msg );
  162.  
  163.  
  164.   printf( "Gameport Device - Joystick Example - Action\n" );
  165.   /* Collect 20 messages, and the leave: */
  166.   counter = 0;
  167.   while( counter < 20 )
  168.   {
  169.     printf("Working...\n");
  170.  
  171.  
  172.     /* Try to collect a message: */
  173.     if( GetMsg( game_msg_port ) )
  174.     {
  175.       /* Print some information abut the joystick: */
  176.       PrintJoystickData( &gamedata );
  177.       counter++;
  178.  
  179.       /* We have now collected a joystick event, so prepare the */
  180.       /* gameport device again:                                 */
  181.       SendIO( game_io_msg );
  182.     }
  183.   }
  184.   
  185.   
  186.   /* 9. NOTE! In this example we will have done one more SendIO()  */
  187.   /*    than GetMsg(), so before we quit we must try to cancel our */
  188.   /*    last command:                                              */
  189.   AbortIO( game_io_msg );
  190.  
  191.   /*    We must also make sure that there are no messages waiting at */
  192.   /*    our message port, so we try to collect as many messages as   */
  193.   /*    possible, until we can not collect any more:                 */
  194.   while( GetMsg( game_msg_port ) )
  195.     printf( "Collecting remaining messages...\n" );
  196.  
  197.  
  198.  
  199.   /* 10. Clean up nicely, and quit: */
  200.   clean_up( "The End!" );
  201. }    
  202.  
  203.  
  204.  
  205. /****************************************************************/
  206. /*                                                              */
  207. /* PrintJoystickData() prints current stick and button position */
  208. /* of a Joystick.                                               */
  209. /*                                                              */
  210. /* Synopsis: PrintJoystickData( data );                         */
  211. /*                                                              */
  212. /* data:     (struct InputEvent *) Pointer to an InputEvent     */
  213. /*           structure which has previously been initialized.   */
  214. /*                                                              */
  215. /****************************************************************/
  216.  
  217. void PrintJoystickData( data )
  218. struct InputEvent *data;
  219. {
  220.   WORD xdirection;
  221.   WORD ydirection;
  222.   UWORD code;
  223.  
  224.   /* Collect data: */
  225.   xdirection = data->ie_X;
  226.   ydirection = data->ie_Y;
  227.   code = data->ie_Code;
  228.  
  229.  
  230.   /* Was the button pressed or released? */
  231.   if( code == IECODE_LBUTTON ) 
  232.     printf("Button pressed. ");
  233.  
  234.   if( code == IECODE_LBUTTON + IECODE_UP_PREFIX )
  235.     printf("Button released. ");
  236.  
  237.  
  238.   /* What is the position of the stick: */
  239.   printf( "Stick position:" );
  240.   switch(ydirection) 
  241.   {
  242.     case -1:
  243.       printf( " Forward" );
  244.       break;
  245.  
  246.     case 1:
  247.       printf( " Back" );
  248.       break;
  249.   }
  250.  
  251.   switch(xdirection) 
  252.   {
  253.     case -1:
  254.       printf( " Left" );
  255.       break;
  256.  
  257.     case 1:
  258.       printf( " Right" );
  259.       break;
  260.   }
  261.  
  262.   printf( "\n" );