home *** CD-ROM | disk | FTP | other *** search
/ Frostbyte's 1980s DOS Shareware Collection / floppyshareware.zip / floppyshareware / BOUT / SNIP9-91.ZIP / JOYSTICK.C < prev    next >
C/C++ Source or Header  |  1991-08-10  |  2KB  |  75 lines

  1. /*
  2. **  JOYSTICK.H
  3. **
  4. **  Joystick support functions
  5. **
  6. **  Public domain demo by Bob Stout
  7. */
  8.  
  9. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  10.  
  11. #define BOOL(x) (!(!(x)))
  12. #define SUCCESS 0
  13.  
  14. struct joystick {
  15.         LOGICAL switch_0;
  16.         LOGICAL switch_1;
  17.         LOGICAL switch_2;
  18.         LOGICAL switch_3;
  19.  
  20.         int     pos_Ax;
  21.         int     pos_Ay;
  22.         int     pos_Bx;
  23.         int     pos_By;
  24. };
  25.  
  26. LOGICAL read_joystick(void);
  27.  
  28. /*
  29. **  JOYSTICK.C
  30. **
  31. **  Joystick support functions
  32. **
  33. **  Public domain demo by Bob Stout
  34. */
  35.  
  36. #include <dos.h>
  37. #include "joystick.h"
  38.  
  39. struct joystick JoyStick;
  40.  
  41. /*
  42. **  read_joystick()
  43. **
  44. **  returns SUCCESS or ERROR
  45. **
  46. **  fills in global JoyStick structure
  47. */
  48.  
  49. LOGICAL read_joystick(void)
  50. {
  51.         union REGS regs;
  52.  
  53.         regs.h.ah = 0x84;                       /* Read the switches    */
  54.         regs.x.dx = 0;
  55.         int86(0x15, ®s, ®s);
  56.         if (regs.x.cflag)
  57.                 return ERROR;
  58.         JoyStick.switch_0 = BOOL(regs.h.al & 0x10);
  59.         JoyStick.switch_1 = BOOL(regs.h.al & 0x20);
  60.         JoyStick.switch_2 = BOOL(regs.h.al & 0x40);
  61.         JoyStick.switch_3 = BOOL(regs.h.al & 0x80);
  62.  
  63.         regs.h.ah = 0x84;                       /* Read positions       */
  64.         regs.x.dx = 1;
  65.         int86(0x15, ®s, ®s);
  66.         if (regs.x.cflag)
  67.                 return ERROR;
  68.         JoyStick.pos_Ax = regs.x.ax;
  69.         JoyStick.pos_Ay = regs.x.bx;
  70.         JoyStick.pos_Bx = regs.x.cx;
  71.         JoyStick.pos_By = regs.x.dx;
  72.  
  73.         return SUCCESS;
  74. }
  75.