home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 177 / c / joystick.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-16  |  5.2 KB  |  156 lines

  1. /* JOYST.C - Joystick show positions routine.        v. 123086
  2.    by Kyle Cordes.
  3.    Thanks to Dale Randall for the joystick
  4.    packet format:
  5.       $fe or $ff for which joystick changed,
  6.       data of joystick 0
  7.       data of joystick 1
  8.  
  9.    remember: joystick 0 is the front (left on 1040ST) port and
  10.              joystick 1 is the back (right on 1040ST) port
  11.  
  12.    THIS PROGRAM WORKS WITH EITHER LATTICE C OR ALCYON C.
  13. */
  14.  
  15. /*************************************************************/ 
  16. /* YOUR SOURCE FILE NEEDS THIS:                              */
  17.  
  18. #include "osbind.h"           /* for GEMDOS and XBIOS calls  */
  19.  
  20. long *vecs;                   /* pointer to vector table     */
  21. long oldvec;                  /* old joystick vector         */
  22.  
  23. char joy0,joy1;    /* position of joysticks 0 and 1 */
  24.  
  25. /* THROUGH HERE                                              */
  26. /*************************************************************/
  27.  
  28. #define supexec(a) xbios(38,a)    /* for Alcyon C */
  29.  
  30. main()                        /* all this does is show values on the screen */
  31. {
  32.    char old0,old1;
  33.  
  34.    joysetup();
  35.  
  36.    Cconout(27);               /* clear screen */
  37.    Cconout('E');
  38.  
  39.    Cconws("Joystick show routine.\r\nby Kyle Cordes\r\n");
  40.    Cconws("Press any key to exit.\r\n");
  41.    Cconws("See related article please.\r\n");
  42.  
  43.    old0=255;   /* the joystick and mouse never generate these values, */
  44.    old1=255;        /* so we will be sure the positions will be shown */
  45.                                                        /* immediately */
  46.    while(!Cconis()) {         /* while no key pressed */
  47.       if(joy0!=old0) {
  48.          old0=joy0;
  49.          Cconout(27);         /* position cursor */
  50.          Cconout('Y');
  51.          Cconout(32+6);
  52.          Cconout(32);
  53.          Cconout(27);
  54.          Cconout('l');        /* clear line */
  55.          Cconws("Joystick 0: ");
  56.          if(((int)joy0)&128)
  57.             Cconws("fire ");
  58.          if(joy0&1)
  59.             Cconws("up ");
  60.          if(joy0&2)
  61.             Cconws("down ");
  62.          if(joy0&4)
  63.             Cconws("left ");
  64.          if(joy0&8)
  65.             Cconws("right");
  66.       }
  67.       if(joy1!=old1) {
  68.          old1=joy1;
  69.          Cconout(27);         /* position cursor */
  70.          Cconout('Y');
  71.          Cconout(32+7);
  72.          Cconout(32);
  73.          Cconout(27);
  74.          Cconout('l');        /* clear line */
  75.          Cconws("Joystick 1: ");
  76.          if(((int)joy1)&128)
  77.             Cconws("fire ");
  78.          if(joy1&1)
  79.             Cconws("up ");
  80.          if(joy1&2)
  81.             Cconws("down ");
  82.          if(joy1&4)
  83.             Cconws("left ");
  84.          if(joy1&8)
  85.             Cconws("right");
  86.       }
  87.    }
  88.  
  89.    Cconin();                  /* get the detected key */
  90.  
  91.    joyunset();                /* fix vector, else crash when */
  92. }                             /* BIOS calls destroyed routine */
  93.  
  94. /**********************************************************************/
  95. /* JOYSTICK ROUTINES                                                  */
  96. /* YOUR SOURCE FILE NEEDS THE CODE FROM HERE TO THE END OF MY PROGRAM */
  97. /*                                                                    */
  98. /* Feel free to use this as long as credit is given.                  */
  99. /**********************************************************************/
  100.  
  101. /* The reason that this works is:
  102.    When your routine called, the address of the packet is on the stack
  103.    and the registers have already been saved and will be restored when your
  104.    program exits.
  105.    The documentation doesn't say this; I learned it from the BIOS listing. */
  106.  
  107. joyinter(a)   /* called by the BIOS when the joystick pos changes */
  108. char *a;
  109. {
  110.                     /* first byte is 254 for joystick 0 changed,
  111.                                      255 for joystick 1 changed */
  112.    joy0=(*(a+1));     /* get second byte: position of joystick 0 */
  113.    joy1=(*(a+2));     /* get third byte: position of joystick 1 */
  114. }     /* format:  bits numbered 76543210
  115.          bit 0 - up
  116.          bit 1 - down
  117.          bit 2 - left
  118.          bit 3 - right
  119.          bit 7 - fire button          */
  120.  
  121. joyasetup() /* called in supervsior mode */
  122. {
  123.    oldvec=(*(vecs+6));          /* save old vector */
  124.    *(vecs+6)=(long)joyinter;  /* insert new vector */
  125. }
  126.  
  127. joysetup()
  128. {
  129.    static char onmsg[]={0x12,0x14};   /* send message to IKBD */
  130.                /* message is: mouse off, joystick auto-return mode */
  131.  
  132.    vecs=(long *)Kbdvbase();   /* address of table */
  133.    Ikbdws(1,onmsg);           /* send message: length-1,address of message */
  134.    supexec((long)joyasetup);  /* call XBIOS to call procedure in */
  135. }                             /* supervisor mode */
  136.  
  137. joyaunset()   /* called in supervisor mode */
  138. {
  139.    *(vecs+6)=oldvec;          /* replace old vector */
  140. }
  141.  
  142. joyunset()
  143. {
  144.    static char offmsg[]={0x15,0x8};
  145.                /* message is: joystick auto-return off, mouse on */
  146.    /* you MUST send the mouse off and on commands if you use joystick 0 */
  147.  
  148.    Ikbdws(1,offmsg);    /* transmit */
  149.  
  150.    supexec((long)joyaunset);   /* fix vector */
  151. }
  152.  
  153. /******************************************************/
  154. /* END OF JOYSTICK ROUTINES                           */
  155. /******************************************************/
  156.