🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: rec.games.programmer
Article #1575

code to read joystick values

#1575
From: Timothy D. Goss
Date: Wed, 18 Nov 1992 04:53
65 lines
1860 bytes
Well here's my $.02 worth.  The routine to read the joystick values is written
in a mix of C and inline assembler.  The assembler is only used to actually
generate the INT (cause I hate int86 calls, why? I'll never know).  Anyhow, the
stuff is pretty easy to read and works.  If anyone has any trouble with this
please let me know and I'll try to find out what went wrong.  When in doubt
blame evil spirits, drool and sway from side to side.  (This doesn't really 
solve anything but people will stop bugging you if you do it).

Tim

-----[ cut here ]-----

#include <dos.h>

#define ERROR   0
#define SUCCESS 1

typedef struct {
   int x1, y1, x2, y2;
   int sw0, sw1, sw2, sw3;
} JOYSTICK;

JOYSTICK stick;

int ReadJoyStick(void)
{

   int buttons;

   asm mov ah,084h
   asm mov dx,0
   asm int 15h
   asm cmp cx,-1
   asm je error

   buttons = _AL>>4;
   stick.sw0 = !(buttons & 0x01);
   stick.sw1 = !(buttons>>1 & 0x01);
   stick.sw2 = !(buttons>>2 & 0x01);
   stick.sw3 = !(buttons>>3 & 0x01);

   asm mov ah,084h
   asm mov dx,1
   asm int 15h
   asm cmp cx,0ffffh
   asm je error

   stick.x1 = _AX;
   stick.y1 = _BX;
   stick.x2 = _CX;
   stick.y2 = _DX;

   ok   : return(SUCCESS);
   error: return(ERROR);

}

void main(void)
{

   while (!kbhit())
      if (ReadJoyStick())
         printf("%d %d %d %d\n", stick.sw0, stick.sw1, stick.x1, stick.y1);

}
Technical Details
Message-ID: <1992Nov18.045323.8500@micor.ocunix.on.ca>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!ames!agate!spool.mu.edu!sdd.hp.com!cs.utexas.edu!torn!nott!cunews!revcan!micor!goss

Show Full Headers
Complete Article Headers
Newsgroups: rec.games.programmer
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!ames!agate!spool.mu.edu!sdd.hp.com!cs.utexas.edu!torn!nott!cunews!revcan!micor!goss
From: goss@micor.ocunix.on.ca (Timothy D. Goss)
Subject: code to read joystick values
Organization: LogiSoft Computing Services 
Date: Wed, 18 Nov 1992 04:53:23 GMT
Message-ID: <1992Nov18.045323.8500@micor.ocunix.on.ca>
Keywords: joystick
Lines: 66