'===========================================================================
' Subject: FAST JOYSTICK ROUTINES             Date: 10-08-97 (03:47)       
'  Author: Phillip Jay Cohen                  Code: QB, QBasic, PDS        
'  Origin: cohennet@erols.com               Packet: MISC.ABC
'===========================================================================
' ***************************************************************************
' ****************************** FastJoy ************************************
' ****              Coded 10/08/97 by Phillip Jay Cohen                  ****
' ****                                                                   ****
' ****   I was working on my game of Pac-Man which will probably never   ****
' **** be finished, and I got very frustrated when coding the joystick   ****
' **** routines.  You know, when you've been programming for long enough ****
' **** a millisecond gets to be just too darn long!  Anyway, I made no   ****
' **** real progress in solving the problem until I found the following  ****
' **** info in Wim Osterholt's list of port addresses:                   ****
' ****                                                                   ****
' **** 0201 r  read joystick position and status                         ****
' ****          bit 7  status B joystick button 2 / D paddle button      ****
' ****          bit 6  status B joystick button 1 / C paddle button      ****
' ****          bit 5  status A joystick button 2 / B paddle button      ****
' ****          bit 4  status A joystick button 1 / A paddle button      ****
' ****          bit 3  B joystick Y coordinate    / D paddle coordinate  ****
' ****          bit 2  B joystick X coordinate    / C paddle coordinate  ****
' ****          bit 1  A joystick Y coordinate    / B paddle coordinate  ****
' ****          bit 0  A joystick X coordinate    / A paddle coordinate  ****
' ****                                                                   ****
' **** So, I asked the folks at comp.lang.basic.misc and alt.lang.basic  ****
' **** if any of them knew how to use this, and all I got back was that  ****
' **** I should just use the STICK and STRIG functions. Helpful - NOT!   ****
' **** I searched for further info on the WWW, and came across a pretty  ****
' **** informative page of C++ which allowed me to write this code. It's ****
' **** faster than STICK and I hope some of you guys can use it. If you  ****
' **** do, please give me some credit, and if you REALLY like it, e-mail ****
' **** me at cohennet@erols.com, and tell me.  Also, if you're an expert ****
' **** coder and manage to speed it up, please let me know. -- P. Cohen  ****
' ***************************************************************************
' ***************************************************************************

DECLARE SUB FastJoy (JoyX%, JoyY%, JoyNum%)
' Pretty badly programmed demo, don't you think?
SCREEN 13
COLOR 22
PRINT "FastJoy: joystick routine"
PRINT
FOR V = 0 TO 7
FOR H = 0 TO 55
IF POINT(H, V) THEN
LINE (H * 4, V * 4 + 130)-(H * 4 + 3, V * 4 + 130), 15
LINE (H * 4 + 1, V * 4 + 131)-(H * 4 + 4, V * 4 + 131), 40
LINE (H * 4 + 2, V * 4 + 132)-(H * 4 + 5, V * 4 + 132), 4
END IF
NEXT
NEXT
COLOR 1
LOCATE 5, 4
PRINT "X Coord"
PRINT
PRINT SPACE$(12); "Y Coord:"
COLOR 144
PRINT : PRINT : PRINT : PRINT SPACE$(18); "Coded by:"
COLOR 2
PRINT : PRINT SPACE$(23); "Phillip Jay Cohen"
LOCATE 23: COLOR 15
PRINT "WAY faster than QB's STICK function!"
IF STICK(1) < 2 THEN
COLOR 4
LOCATE 3: PRINT "Joystick not found!"
END
END IF
COLOR 9: LOCATE 3: PRINT "Using Joystick 1"
COLOR 44
DO
FastJoy X%, Y%, 1
LOCATE 7, 5
PRINT X%
LOCATE 9, 15
PRINT Y%
LOOP WHILE INKEY$ = ""
END

SUB FastJoy (JoyX%, JoyY%, JoyNum%)
' Fast joystick routines by Phillip Jay Cohen

' JoyX% returns vertical postion of specified joystick
' JoyY% returns horizontal postion of specified joystick
' JoyNum% specifies whether joystick number one or two is to be read

SELECT CASE JoyNum% ' Choose appropriate bitmasks
CASE 1
XMask% = 1
YMask% = 2
CASE 2
XMask% = 4
YMask% = 8
CASE ELSE
EXIT SUB
END SELECT
JoyX% = 1: JoyY% = 1
OUT &H201, 0 ' Reset joystick port
DO
  Joy% = INP(&H201) ' Poll joystick port
  IF Joy% AND XMask% THEN JoyX% = JoyX% + 1
  IF Joy% AND YMask% THEN JoyY% = JoyY% + 1
LOOP UNTIL (Joy% AND XMask%) = 0 AND (Joy% AND YMask%) = 0
END SUB
