' ################################################################################################################################################################
' Multi gamepad test
' BASED ON CODE BY SMcNeill FROM:
' Simple Joystick Detection and Interaction (Read 316 times)
' https://www.qb64.org/forum/index.php?topic=2160.msg129051#msg129051
' ################################################################################################################################################################
' =============================================================================
' GLOBAL DECLARATIONS a$=string, i%=integer, L&=long, s!=single, d#=double
' boolean constants
' =============================================================================
' UDTs
' UDT TO HOLD THE INFO FOR A PLAYER
c
AS INTEGER ' character to display on screen
' =============================================================================
' GLOBAL VARIABLES
' =============================================================================
' INITIALIZE
'DIM a$ ' string
'DIM i% ' integer
'DIM L& ' long integer
'DIM s! ' single precision
'DIM d# ' double precision
'DIM bValue% ' boolean is an integer
' =============================================================================
' TRY THE MOUSE
main ProgramName$
' =============================================================================
' FINISH
SYSTEM ' return control to the operating system PRINT ProgramName$
+ " finished."
' /////////////////////////////////////////////////////////////////////////////
iDeviceCount
= _DEVICES ' Find the number of devices on someone's system
' 1 is the keyboard
' 2 is the mouse
' 3 is the joystick
' unless someone has a strange setup with multiple mice/keyboards/ect...
' In that case, you can use _DEVICE$(i) to look for "KEYBOARD", "MOUSE", "JOYSTICK", if necessary.
' I've never actually found it necessary, but I figure it's worth mentioning, just in case...
TestJoysticks
PRINT "No joysticks found."
INPUT "PRESS <ENTER> TO CONTINUE", in$
' /////////////////////////////////////////////////////////////////////////////
DIM RoutineName
AS STRING :: RoutineName
= "TestJoysticks"
' WE'RE PREPARED TO SUPPORT UPTO 8 JOYSTICKS, WITH UPTO 2 BUTTONS AND 2 AXES EACH
' (THIS IS FOR ATARI 2600 JOYSTICKS)
DIM arrButton
(8, 2) ' number of buttons on the joystick DIM arrAxis
(8, 3) ' number of axis on the joystick 'DIM Button(_LASTBUTTON(3) ) ' number of buttons on the joystick
'DIM Axis(_LASTAXIS(3) ) ' number of axis on the joystick
DIM arrPlayer
(8) AS PlayerType
' holds info for each player
' COUNT # OF JOYSTICKS
iDeviceCount
= _DEVICES ' Find the number of devices on someone's system PRINT "NO JOYSTICKS FOUND, EXITING..." INPUT "PRESS <ENTER>", in$
' BASE # OF PLAYERS ON HOW MANY CONTROLLERS FOUND
iNumPlayers = iDeviceCount - 2 ' TODO: find out the right way to count joysticks
IF iNumPlayers
> cMaxPlayers
THEN iNumPlayers = cMaxPlayers
' INITIALIZE PLAYER COORDINATES AND SCREEN CHARACTERS
iNextY = 1
iNextX = -3
iNextC = 64
FOR iPlayerLoop
= 1 to iNumPlayers
iNextX = iNextX + 4
iNextX = 1
iNextY = iNextY + 4
iNextC = iNextC + 1
arrPlayer(iPlayerLoop).x = iNextX
arrPlayer(iPlayerLoop).y = iNextY
arrPlayer(iPlayerLoop).c = iNextC
arrPlayer(iPlayerLoop).xOld = iNextX
arrPlayer(iPlayerLoop).yOld = iNextY
arrPlayer(iPlayerLoop).buttonCount = cMaxButtons
arrPlayer(iPlayerLoop).axisCount = cMaxAxis
' CLEAR THE SCREEN
FOR iPlayerLoop
= 1 to iNumPlayers
iDevice = iPlayerLoop + 2
'IF _DEVICEINPUT = 3 THEN ' this says we only care about joystick input values
' check all the buttons
arrPlayer(iPlayerLoop).buttonCount = iLoop
' update button array to indicate if a button is up or down currently.
' _BUTTON(number) returns -1 when a button is pressed and 0 when released.
'arrButton(iLoop) = NOT arrButton(iLoop)
arrButton
(iPlayerLoop
, iLoop
) = _BUTTON(iLoop
)
arrPlayer(iPlayerLoop).axisCount = iLoop
' I like to give a little "jiggle" resistance to my controls, as I have an old joystick
' which is prone to always give minute values and never really center on true 0.
' A value of 1 means my axis is pushed fully in one direction.
' A value greater than 0.1 means it's been partially pushed in a direction (such as at a 45 degree diagional angle).
' A value of less than 0.1 means we count it as being centered. (As if it was 0.)
arrAxis
(iPlayerLoop
, iLoop
) = _AXIS(iLoop
) arrAxis(iPlayerLoop, iLoop) = 0
WEND ' clear and update the device buffer
' And below here is just the simple display routine which displays our values.
' If this was for a game, I'd choose something like arrAxis(1) = -1 for a left arrow style input,
' arrAxis(1) = 1 for a right arrow style input, rather than just using _KEYHIT or INKEY$.
FOR iPlayerLoop
= 1 to iNumPlayers
FOR iLoop
= 1 TO arrPlayer
(iPlayerLoop
).axisCount
' A loop for each axis ' display their status to the screen
PRINT "Player " + cstr$
(iPlayerLoop
) + ", Axis " + cstr$
(iLoop
) + " = " + cstr$
(arrAxis
(iPlayerLoop
, iLoop
) ) FOR iLoop
= 1 TO arrPlayer
(iPlayerLoop
).buttonCount
' A loop for each button ' display their status to the screen
PRINT "Player " + cstr$
(iPlayerLoop
) + ", Button " + cstr$
(iLoop
) + " = " + cstr$
(arrButton
(iPlayerLoop
, iLoop
) ) PRINT "PRESS <ESC> TO EXIT"
' ################################################################################################################################################################
' BEGIN GENERAL ROUTINES
' ################################################################################################################################################################
' /////////////////////////////////////////////////////////////////////////////
'cstr$ = LTRIM$(RTRIM$(STR$(myValue)))
' ################################################################################################################################################################
' END GENERAL ROUTINES
' ################################################################################################################################################################