Article View: comp.os.msdos.programmer
Article #5159Re: Looking for Joystick code
From: prof. n liu
Date: Tue, 12 Mar 1991 23:09
Date: Tue, 12 Mar 1991 23:09
92 lines
3149 bytes
3149 bytes
resnicks@netcom.COM (Steve Resnick) writes:
>I'm looking for some C (preferably Turbo C) code to access the joystick port.
>What I need is a way to read the current X/Y values and the button status.
>This is going to be part of a PD game, so the code should be PD/royalty free
>to avoid copyright infringements as well as an outlay of cash :)
Just whipped something up real quick (MSC 6.00)
I think its short enough to post here.
Note: this uses dos interrupt 15, and does not read the port... reading the port
for this purpose (i think its address 400-407 or something) is a PAIN!
I tried to read the port using C only and it sucks.. only way to do this is
probably with assembly, since timing is vital. Turns out you have to TIME
how long a certain bit stays high... joystick position is then proportional
to that time.
The interupt calls i did are almost identical to what you need for Turbo C,
i think - the Turbo C bible (Waite Group) documents int86, at least.
The interrupt info for the call itself i found in:
iAPX 86 Interrupt Primer, by Ralf Brown (i got it somewhere by ftp, can't
remember where), last updated 4/90.
I highly recommend it for doing any type of interrupt programming.
There's really nothing spectacular here, so i doubt i need to copyright it :)
'nother warning: this is by no means robust enough to just slam into any old
code.. its just kinda a guideline. Use at your own risk.
Nye Liu
nyet@cobalt.caltech.edu
Microsoft C 6.00 code follows:
-----cut me here, press n if you don't want to read this:)-----
#include <stdio.h>
#include <conio.h>
#include <dos.h> /* i think this is msdos.h for TC */
/* nye's quick 'n dirty joystick program */
/* uses dos interrupt 15, ah=84h */
/* pass stick = 0 or 1 depending on which joystick you want to read */
void getjoy(int stick, int *button1, int *button2, int *x, int *y)
{
union REGS in, out; /* pretty sure this is standard */
int byte;
in.h.ah =0x84;
in.x.dx =0x01;
int86(0x15, &in, &out); /* probably have to change this for TC */
if (stick==0)
{
*x=(int)(out.x.ax );
*y=(int)(out.x.bx );
}
if (stick==1)
{
*x=(int)(out.x.cx );
*y=(int)(out.x.dx );
}
in.x.dx =0x00;
int86(0x15, &in, &out); /* probably might have to change this too */
if (stick==0) byte=out.h.al;
if (stick==1) byte=out.h.bl;
*button1=*button2=0;
if ((byte & 16) != 0)
*button1=(-1); /* 0 = button down */
if ((byte & 32) != 0)
*button2=(-1);
}
main()
{
int x,y,b1,b2;
while(!kbhit())
{
getjoy(0,&b1,&b2,&x,&y); /* status of joystick 0 */
printf("%d %d %d %d \r",b1,b2,x,y);
}
}
Technical Details
Message-ID:
<1991Mar12.230926.3458@nntp-server.caltech.edu>Path:
arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jarthur!nntp-server.caltech.edu!nyetReferences:
<27795@netcom.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jarthur!nntp-server.caltech.edu!nyet From: nyet@nntp-server.caltech.edu (prof. n liu) Newsgroups: comp.os.msdos.programmer Subject: Re: Looking for Joystick code Message-ID: <1991Mar12.230926.3458@nntp-server.caltech.edu> Date: 12 Mar 91 23:09:26 GMT References: <27795@netcom.COM> Organization: California Institute of Technology, Pasadena Lines: 92