home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / rec / games / programm / 4725 < prev    next >
Encoding:
Text File  |  1992-11-17  |  1.7 KB  |  77 lines

  1. Newsgroups: rec.games.programmer
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!agate!spool.mu.edu!sdd.hp.com!cs.utexas.edu!torn!nott!cunews!revcan!micor!goss
  3. From: goss@micor.ocunix.on.ca (Timothy D. Goss)
  4. Subject: code to read joystick values
  5. Organization: LogiSoft Computing Services 
  6. Date: Wed, 18 Nov 1992 04:53:23 GMT
  7. Message-ID: <1992Nov18.045323.8500@micor.ocunix.on.ca>
  8. Keywords: joystick
  9. Lines: 66
  10.  
  11. Well here's my $.02 worth.  The routine to read the joystick values is written
  12. in a mix of C and inline assembler.  The assembler is only used to actually
  13. generate the INT (cause I hate int86 calls, why? I'll never know).  Anyhow, the
  14. stuff is pretty easy to read and works.  If anyone has any trouble with this
  15. please let me know and I'll try to find out what went wrong.  When in doubt
  16. blame evil spirits, drool and sway from side to side.  (This doesn't really 
  17. solve anything but people will stop bugging you if you do it).
  18.  
  19. Tim
  20.  
  21. -----[ cut here ]-----
  22.  
  23. #include <dos.h>
  24.  
  25. #define ERROR   0
  26. #define SUCCESS 1
  27.  
  28. typedef struct {
  29.    int x1, y1, x2, y2;
  30.    int sw0, sw1, sw2, sw3;
  31. } JOYSTICK;
  32.  
  33. JOYSTICK stick;
  34.  
  35. int ReadJoyStick(void)
  36. {
  37.  
  38.    int buttons;
  39.  
  40.    asm mov ah,084h
  41.    asm mov dx,0
  42.    asm int 15h
  43.    asm cmp cx,-1
  44.    asm je error
  45.  
  46.    buttons = _AL>>4;
  47.    stick.sw0 = !(buttons & 0x01);
  48.    stick.sw1 = !(buttons>>1 & 0x01);
  49.    stick.sw2 = !(buttons>>2 & 0x01);
  50.    stick.sw3 = !(buttons>>3 & 0x01);
  51.  
  52.    asm mov ah,084h
  53.    asm mov dx,1
  54.    asm int 15h
  55.    asm cmp cx,0ffffh
  56.    asm je error
  57.  
  58.    stick.x1 = _AX;
  59.    stick.y1 = _BX;
  60.    stick.x2 = _CX;
  61.    stick.y2 = _DX;
  62.  
  63.    ok   : return(SUCCESS);
  64.    error: return(ERROR);
  65.  
  66. }
  67.  
  68. void main(void)
  69. {
  70.  
  71.    while (!kbhit())
  72.       if (ReadJoyStick())
  73.          printf("%d %d %d %d\n", stick.sw0, stick.sw1, stick.x1, stick.y1);
  74.  
  75. }
  76.  
  77.