/* Demonstration of reading joy stick postition
uses BIOS function for reading the stick;
*/
#include <stdio.h>
#include <dos.h>
#include <bios.h>
#include <conio.h>
#include "cursor.c"
typedef struct {
int sw1,sw2;
int x,y;
int cenx,ceny;
} joy_stick;
joy_stick joy;
#define keypressed (bioskey(1) != 0)
#define kb_clear() while keypressed bioskey(0);
union REGS inregs,outregs;
struct SREGS segregs;
void GotoXY(int x,int y)
{
union REGS r;
/* Set XY position */
r.h.ah = 2;
r.h.bh = 0; /* Assume Video Page 0 */
r.h.dh = (char) y;
r.h.dl = (char) x;
int86(16,&r,&r);
}
disp_stick(int line)
{
GotoXY(0,line);
printf("sw1 %d sw2 %d",joy.sw1,joy.sw2);
GotoXY(0,line+1);
printf("x %6d y %6d",joy.x,joy.y);
}
int read_buttons()
{
inregs.h.ah = 0x84;
inregs.x.dx = 0;
int86x(0x15,&inregs,&outregs,&segregs);
joy.sw1 = ((outregs.h.al >> 4) & 1) == 0;
joy.sw2 = ((outregs.h.al >> 5) & 1) == 0;
return (int)outregs.h.al;
}
read_stick()
{
int jx,jy;
inregs.h.ah = 0x84;
inregs.x.dx = 1;
int86x(0x15,&inregs,&outregs,&segregs);
jx = outregs.x.ax;
jy = outregs.x.bx;
joy.x = jx-joy.cenx;
joy.y = jy-joy.ceny;
}
center_stick()
{
int init_swa,init_swb,swa,swb;
int c,retval;
printf("Joystick testing program BIOS version\n");
printf("--------------------------------------\n\n");
printf("Center joystick and press fire\n");
kb_clear();
c = read_buttons();
init_swa = c & 0x30;
init_swb = c & 0xc0;
do {
c = read_buttons();
swa = c & 0x30;
swb = c & 0xc0;
} while ((swa == init_swa) && (swb == init_swb));
if (swa != init_swa) {
printf("Joystick 1 selected\n");
retval = 1;
} else if (swb != init_swb) {
printf("Joystick 2 selected\n");
retval = 2;
}
joy.cenx = joy.ceny = 0;
read_stick(joy);
joy.cenx = joy.x;
joy.ceny = joy.y;
}
main()
{
int k;
clrscr();
HideCur();
center_stick();
while (!keypressed) {
read_buttons();
read_stick();
disp_stick(6);
}
clrscr();
ShowCur();
}