home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / amiga / programm / 18007 < prev    next >
Encoding:
Text File  |  1992-12-31  |  5.7 KB  |  301 lines

  1. Path: sparky!uunet!olivea!spool.mu.edu!yale.edu!ira.uka.de!fauern!hugis!castle.franken.de!forge.franken.de!Barnard
  2. From: Barnard@forge.franken.de (Henning Schmiedehausen)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Going to the metal
  5. Message-ID: <93Hds*vr0@forge.franken.de>
  6. Date: 31 Dec 92 12:04:09 GMT
  7. References: <BznyFH.J3s@unx.sas.com> <monster.05uo@mbear.apana.org.au> <C01BK6.ApK@unx.sas.com>
  8.  <lk1di9INNrsa@exodus.Eng.Sun.COM>
  9. Organization: Barnard's Software Forge (ECG038)
  10. Lines: 288
  11. X-Newsreader: Arn V1.00
  12.  
  13. In article <lk1di9INNrsa@exodus.Eng.Sun.COM>, Chuck McManis writes:
  14.  
  15. > This is the most powerful motive. Take the most trivial example, finding
  16. > out where an 8 axis joystick is pointed. Do we open the "gameport.device"
  17. > at the begining of the game and look for messages on the input stream
  18. > to tell us where the stick is? Or do we just "mov d1,#port" ?
  19.  
  20. What's wrong with:
  21.  
  22. (This  does  go  to  the bare metal but allocates the controllers is a system
  23. friendly way)
  24.  
  25.  
  26. /**** JOYSTICK MODULE ****/
  27.  
  28. /*
  29.  *    NAME
  30.  *        JoyMove
  31.  *
  32.  *    FUNCTION
  33.  *        keeps data for joystick
  34.  *
  35.  */
  36.  
  37. struct JoyMove
  38.     {
  39. BYTE xOff,            /* x-Position; -1:left, 1:right */
  40.      yOff,            /* y-Position; -1:up,  1: down */
  41.      Button;        /* Knopf: 0: pressed */
  42.     };
  43.  
  44. static struct IOStdReq *joy[2] = { NULL,NULL },
  45.                         *InReq = NULL;
  46.  
  47. static struct MsgPort *jp[2] = { NULL,NULL },
  48.                       *InPort = NULL;
  49.  
  50. static BOOL sctsucc[2] = { FALSE,FALSE };
  51.  
  52. #define    JM_UP        -1
  53. #define    JM_DOWN         1
  54. #define    JM_NO_Y         0
  55.  
  56. #define    JM_LEFT        -1
  57. #define    JM_RIGHT     1
  58. #define    JM_NO_X         0
  59.  
  60. #define    JM_BUTTON     0
  61.  
  62. /*
  63.  *    NAME
  64.  *        static BOOL set_controller_type(req,type)
  65.  *
  66.  *    FUNCTION
  67.  *        allocates a game port
  68.  *
  69.  *    INPUT
  70.  *        req  -> IORequest
  71.  *        type -> wanted type 
  72.  *
  73.  *    OUTPUT
  74.  *        TRUE  -> Controller allocated
  75.  *        FALSE -> error
  76.  *
  77.  */
  78.  
  79.  
  80. static BOOL set_controller_type(req,type)
  81. struct IOStdReq *req;
  82. BYTE type;
  83. {
  84. BOOL success = FALSE;
  85. BYTE controller_type = 0;
  86.  
  87.     Forbid();
  88.     req->io_Command = GPD_ASKCTYPE;
  89.     req->io_Data    = (APTR)&controller_type;
  90.     req->io_Length  = 1;
  91.     req->io_Flags   = IOF_QUICK;
  92.     DoIO(req);
  93.  
  94.     if(controller_type == GPCT_NOCONTROLLER)
  95.     {
  96.         req->io_Command = GPD_SETCTYPE;
  97.         req->io_Flags   = IOF_QUICK;
  98.         req->io_Data    = (APTR)&type;
  99.         req->io_Length  = 1L;
  100.         DoIO(req);
  101.         success = TRUE;
  102.     }
  103.     Permit();
  104.     return(success);
  105. }
  106.  
  107. /*
  108.  *    NAME
  109.  *        void free_gp_unit(req)
  110.  *
  111.  *    FUNCTION
  112.  *        frees a game port
  113.  *
  114.  *    INPUT
  115.  *        req  -> IORequest
  116.  *
  117.  *    OUTPUT
  118.  *        -
  119.  *
  120.  */
  121.  
  122. static void free_gp_unit(req)
  123. struct    IOStdReq    *req;
  124. {
  125. BYTE type    =    GPCT_NOCONTROLLER;
  126.  
  127.     req->io_Command = GPD_SETCTYPE;
  128.     req->io_Flags   = IOF_QUICK;
  129.     req->io_Data    = (APTR)&type;
  130.     req->io_Length  = 1;
  131.     DoIO(req);
  132. }
  133.  
  134. /*
  135.  *    NAME
  136.  *        void JoyOn()
  137.  *
  138.  *    FUNCTION
  139.  *        Allocates the gamesports for joysticks 
  140.  *
  141.  *    INPUT
  142.  *        -
  143.  *
  144.  *    OUTPUT
  145.  *        -
  146.  *
  147.  */
  148.  
  149.  
  150. void JoyOn()
  151. {
  152. UBYTE InputMode = GPCT_NOCONTROLLER;
  153.  
  154. /* remove mouse from input.device */
  155.  
  156.     InPort =    CreatePort("Input_Port",NULL);
  157.     InReq  =    CreateStdIO(InPort);
  158.     OpenDevice("input.device",0,InReq,0);
  159.     InReq->io_Command = (UWORD)IND_SETMTYPE;
  160.     InReq->io_Data = (APTR)&InputMode;
  161.     InReq->io_Length = 1L;
  162.     DoIO(InReq);
  163.  
  164.     Delay(2L*TICKS_PER_SECOND);        /* wait for input.device to free mouse */
  165.  
  166. /* open the Joyports */
  167.  
  168.     jp[0]  = CreatePort("Joystick_1",NULL);
  169.     joy[0] = CreateStdIO(jp[0]);
  170.     OpenDevice("gameport.device",0,joy[0],0);
  171.  
  172.     jp[1]  = CreatePort("Joystick_2",NULL);
  173.     joy[1] = CreateStdIO(jp[1]);
  174.     OpenDevice("gameport.device",1,joy[1],0);
  175.  
  176.     sctsucc[0] = set_controller_type(joy[0],GPCT_ALLOCATED);
  177.     sctsucc[1] = set_controller_type(joy[1],GPCT_ALLOCATED);
  178.  
  179.     if(!(sctsucc[0] && sctsucc[1]))
  180.     {
  181.         printf("Couldn't allocate game ports!\n");
  182.         JoyOff();
  183.         exit(10);
  184.     }
  185. }
  186.  
  187. /*
  188.  *    NAME
  189.  *        void JoyOff()
  190.  *
  191.  *    FUNCTION
  192.  *        frees gameports 
  193.  *
  194.  *    INPUT
  195.  *        -
  196.  *
  197.  *    OUTPUT
  198.  *        -
  199.  *
  200.  */
  201.  
  202. int    JoyOff()
  203. {
  204. UBYTE JoyMode;
  205. UBYTE InputMode;
  206.  
  207.     if(sctsucc[0])
  208.     {
  209.         joy[0]->io_Command = CMD_CLEAR;
  210.         joy[0]->io_Length  = 0L;
  211.         joy[0]->io_Flags   = IOF_QUICK;
  212.         DoIO(joy[0]);
  213.         free_gp_unit(joy[0]);
  214.     }
  215.     CloseDevice(joy[0]);
  216.     DeletePort(joy[0]->io_Message.mn_ReplyPort);
  217.     DeleteStdIO(joy[0]);
  218.  
  219.     if(sctsucc[1])
  220.     {
  221.         joy[1]->io_Command = CMD_CLEAR;
  222.         joy[1]->io_Length  = 0L;
  223.         joy[1]->io_Flags   = IOF_QUICK;
  224.         DoIO(joy[1]);
  225.         free_gp_unit(joy[1]);
  226.     }
  227.     CloseDevice(joy[1]);
  228.     DeletePort(joy[1]->io_Message.mn_ReplyPort);
  229.     DeleteStdIO(joy[1]);
  230.  
  231. /* tell input.device, that there is a mouse */
  232.  
  233.     InputMode = GPCT_MOUSE;
  234.     InReq->io_Command = (UWORD)IND_SETMTYPE;
  235.     InReq->io_Data = (APTR)&InputMode;
  236.     InReq->io_Length = 1L;
  237.     DoIO(InReq);
  238.  
  239.     CloseDevice(InReq);
  240.     DeleteStdIO(InReq);
  241.     DeletePort(InPort);
  242. }
  243.  
  244. /*
  245.  *    NAME
  246.  *        void GetJoyData(port,joy)
  247.  *
  248.  *    FUNCTION
  249.  *        gets joystick-data 
  250.  *
  251.  *    INPUT
  252.  *        port: 0/1 for the game-ports
  253.  *        joy:  pointer to a JoyMove structure
  254.  *
  255.  *    OUTPUT
  256.  *        -
  257.  *
  258.  */
  259.  
  260. void GetJoyData(port,joy)
  261. BYTE port;
  262. struct JoyMove *joy;
  263. {
  264. register UWORD PortData;
  265. register UWORD a,b;
  266.     if(!port)
  267.     {
  268.         joy->Button = (((UBYTE)ciaa.ciapra) >> CIAB_GAMEPORT0) ^ 1;
  269.         PortData = custom.joy0dat;
  270.     }
  271.     else
  272.     {
  273.         joy->Button = (((UBYTE)ciaa.ciapra) >> CIAB_GAMEPORT1) ^ 1;
  274.         PortData = custom.joy1dat;
  275.     }
  276.  
  277.     a = PortData >> 1;            /* Bits 1 und 9 nach 0 und 8 */
  278.     b = PortData ^ a;            /* Bit 0: a ^ b, Bit 8: c ^ d */
  279.     joy->xOff    = (a & 1) ? 1 : ( (a & 0x100) ? -1 : 0);
  280.     joy->yOff    = (b & 1) ? 1 : ( (b & 0x100) ? -1 : 0);
  281. }
  282.  
  283.  
  284. Flame  me,  if  you  want, this is from a VERY OLD project, in fact one of my
  285. first attempts in C, so if you find bugs, tell me.
  286.  
  287. Yes,  there  are  *LOTS*  of  error  checkings ( CreatePort(), DoIO() etc.  )
  288. missing.  I'm a lazy typer, you know ;)
  289.  
  290.  
  291.     Alloooooooha!
  292.         Henning
  293.  
  294.  
  295. -- 
  296.   \\ _ Henning Schmiedehausen - barnard@forge.franken.de _ // Amiga - 
  297.    \X/ --- Home of Barnard's Software Forge - ECG038 --- \X/  Learning to Fly
  298.  
  299. I'm  stepping  out,  I'm  movin' on, I'm gonna see the world.  Like a rolling
  300. stone.  Stepping out. - E.L.O., Out of the blue.
  301.