I'm posting this here as well because it works with DBClassic.
Link to original post on DBPro board:
https://forum.thegamecreators.com/thread/229529
Link to attachment there:
https://forum.thegamecreators.com/attachment/89667
Source Code:
RemStart
########################################################################################
|
¤Title¤ | Xbox Controller Tester - for DarkBasic Classic and Pro
¤AUTHOR¤ | David Yakobi
¤DATE¤ | Feb 26, 2024
¤CONTACT¤ | Email: dyakobi@hotmail.com
|
------------------------------------------------------------
%Revisions%
Feb 26, 2024 Initial version
########################################################################################
RemEnd
SET WINDOW ON
SET WINDOW TITLE "Xbox Controller Tester"
SET DISPLAY MODE 1024 , 768 , 32
SYNC RATE 60
SYNC ON
HIDE MOUSE
_check_controller:
rem ********************************************
rem ********************************************
rem
rem ** Setup xbox controller
rem
rem ********************************************
rem ********************************************
DIM ffEnabled(0) : ffEnabled(0) = 0
DIM rawButtonsPressed$(0) : rawButtonsPressed$(0) = ""
DIM mappedButtonsPressed$(0) : mappedButtonsPressed$(0) = ""
DIM namedButtonsPressed$(0) : namedButtonsPressed$(0) = ""
DIM rumbleIntensity(0) : rumbleIntensity(0) = 0
rem Performs a checklist for all contol devices
PERFORM CHECKLIST FOR CONTROL DEVICES
rem Check for Force Feedback flag
rem you will need to install special drivers to enable xbox rumble feature
rem look in the folder: /force_feedback
FOR t=1 TO CHECKLIST QUANTITY()
IF CHECKLIST VALUE A(t)=1 THEN ffEnabled(0) = t
NEXT t
rem Assume the first one is a force feedback device
IF ffEnabled(0)>0
SET CONTROL DEVICE CHECKLIST STRING$(ffEnabled(0))
ENDIF
_load_images:
rem ********************************************
rem ********************************************
rem
rem ** Load images onto planes
rem
rem ********************************************
rem ********************************************
objNum = 0
READ filename$
WHILE filename$ <> ""
INC objNum
MAKE OBJECT PLAIN objNum , 1200 , 806
rem alternately comment one of these out based on where you are running this
` GOSUB _load_for_DBClassic
GOSUB _load_for_DBPro
TEXTURE OBJECT objNum , objNum
LOCK OBJECT ON objNum
POSITION OBJECT objNum , 0 , 0 , 2000
SCALE OBJECT objNum , 200 , 200 , 1
set_object_idle( objNum )
READ filename$
ENDWHILE
totalImages = objNum
_DATA_file_names:
DATA "A" : `1
DATA "B" : `2
DATA "X" : `3
DATA "Y" : `4
DATA "LB" : `5
DATA "RB" : `6
DATA "LT" : `7
DATA "RT" : `8
DATA "LAnalog" : `9
DATA "RAnalog" : `10
DATA "DPad" : `11
DATA "Fwd" : `12
DATA "Back" : `13
DATA "Xbox" : `14
DATA "_Base" : `15
rem randomize backdrop colours
RANDOMIZE TIMER()
red = 90 + RND(60) : rInc = 1
green = 90 + RND(60) : gInc = 1
blue = 90 + RND(60) : bInc = 1
rem create backdrop
`INC objNum
`bgObj = objNum
`MAKE OBJECT PLAIN bgObj , 1200 , 806
`LOCK OBJECT ON bgObj
`POSITION OBJECT bgObj , 0 , 0 , 2500
`SET OBJECT objNum , 1 , 1 , 0 , 1 , 1 , 0 , 1
`SCALE OBJECT bgObj , 800 , 800 , 1
COLOR BACKDROP RGB( red , green , blue )
_setup:
rem Joystick Type Check
rem test right stick for range, it can be
rem normal: -1000 to 1000
rem or unsigned integer: 0 to 65535
jTwistX = JOYSTICK TWIST X()
jTwistY = JOYSTICK TWIST Y()
IF jTwistX > 1001 OR jTwistY > 1001 THEN rightStickUInt = 1
rem set initial values
displayScale# = 0
displayStep# = 0.3
_ML_main_loop:
rem ********************************************
rem ********************************************
rem
rem ** Main Loop
rem
rem ********************************************
rem ********************************************
DO
rawButtonsPressed$(0) = ""
mappedButtonsPressed$(0) = ""
namedButtonsPressed$(0) = ""
rumbleIntensity(0) = 0
_ML_input:
rem test for controller button presses
FOR b=1 TO 14
IF xbox_controller_fire( b )=1
set_object_highlight( b , 0.0 , -8.0 , 0 )
ELSE
set_object_idle( b )
ENDIF
NEXT b
rem left analog stick
jX = JOYSTICK X()
jY = JOYSTICK Y()
IF jX<>0 OR jY<>0
set_object_highlight( 9 , 0.09 * jX , -0.07 * jY , -5 )
ENDIF
rem test for trigger sliders
jZ = JOYSTICK Z()
IF jZ>0
set_object_highlight( 7 , 0.0 , -0.05 * jZ , 0 )
DEC tRumble
IF tRumble<0 THEN tRumble = 0
set_rumble( tRumble )
ELSE
set_object_idle( 7 )
IF jZ<0
set_object_highlight( 8 , 0.0 , 0.05 * jZ , 0 )
INC tRumble
IF tRumble>100 THEN tRumble = 100
set_rumble( tRumble )
ELSE
set_object_idle( 8 )
ENDIF
ENDIF
rem right analog stick
jTwistX = JOYSTICK TWIST X()
jTwistY = JOYSTICK TWIST Y()
IF rightStickUInt=1
rem apply conversion to normal -1000 to 1000 range
jTwistX = (jTwistX - 32767) / 32.727
jTwistY = (jTwistY - 32767) / 32.727
ENDIF
IF jTwistX<>0 OR jTwistY<>0
set_object_highlight( 10 , 0.07 * jTwistX , -0.06 * jTwistY , -5 )
ENDIF
rem D-pad - 8 directions possible
dPad = JOYSTICK HAT ANGLE(0)
IF dPad>-1
SELECT dPad
CASE 0 : set_object_highlight( 11 , 0.0 , 80.0 , -5 ) : ENDCASE
CASE 4500 : set_object_highlight( 11 , 50.0 , 50.0 , -5 ) : ENDCASE
CASE 9000 : set_object_highlight( 11 , 80.0 , 0.0 , -5 ) : ENDCASE
CASE 13500 : set_object_highlight( 11 , 50.0 , -50.0 , -5 ) : ENDCASE
CASE 18000 : set_object_highlight( 11 , 0.0 , -80.0 , -5 ) : ENDCASE
CASE 22500 : set_object_highlight( 11 , -50.0 , -50.0 , -5 ) : ENDCASE
CASE 27000 : set_object_highlight( 11 , -80.0 , 0.0 , -5 ) : ENDCASE
CASE 31500 : set_object_highlight( 11 , -50.0 , 50.0 , -5 ) : ENDCASE
ENDSELECT
ENDIF
rem perform joystick rumble
IF ffEnabled(0)>0
IF rumbleIntensity(0)=-1
FORCE NO EFFECT
ELSE
IF rumbleIntensity(0) > 0
rem calculate rumble duration for this cycle in milliseconds based on fps
FORCE SHOOT rumbleIntensity(0) , 1000 / fps
ENDIF
ENDIF
ENDIF
_ML_effects:
rem "bob" all images for cool effect!
IF displayScale#>2 THEN displayStep# = -0.05
IF displayScale#<-2 THEN displayStep# = 0.05
IF rawButtonsPressed$(0)=""
displayScale# = displayScale# + displayStep#
ELSE
displayScale# = displayScale# + (displayStep# * 2.5)
ENDIF
FOR img=1 TO totalImages
SCALE OBJECT img , 200 + displayScale# , 200 + displayScale# , 1
NEXT img
rem change backdrop slightly for effect!
red = red + ( rInc * RND( 1 ) )
IF red > 170 THEN rInc = -1
IF red < 90 THEN rInc = 1
green = green + ( gInc * RND( 1 ) )
IF green > 170 THEN gInc = -1
IF green < 90 THEN gInc = 1
blue = blue + ( bInc * RND( 1 ) )
IF blue > 170 THEN bInc = -1
IF blue < 90 THEN bInc = 1
COLOR BACKDROP RGB( red , green , blue )
_ML_logging:
SET CURSOR 0,5
INK RGB(0,0,0), 0
` PRINT " ", fps
IF jX<>0 OR jY<>0
PRINT " Up Right Down Left: ",str$( JOYSTICK UP() )," ",str$( JOYSTICK RIGHT() )," ",str$( JOYSTICK DOWN() )," ",str$( JOYSTICK LEFT() )
PRINT " Dir X Y: ",str$( JOYSTICK X() )," ",str$( JOYSTICK Y() )," ",str$( JOYSTICK Z() )
ENDIF
IF jZ<>0
PRINT " Dir Z(triggers): ",str$( JOYSTICK Z() )
ENDIF
IF jTwistX<>0 OR jTwistY<>0
PRINT " Twist X Y Z: ",str$( JOYSTICK TWIST X() )," ",str$( JOYSTICK TWIST Y() )," ",str$( JOYSTICK TWIST Z() )
ENDIF
IF dPad>-1
PRINT " Hat Angle: ",str$( JOYSTICK HAT ANGLE(0) )
` PRINT " ",str$( JOYSTICK SLIDER A() )," ",str$( JOYSTICK SLIDER B() )," ",str$( JOYSTICK SLIDER C() )," ",str$( JOYSTICK SLIDER D() )
ENDIF
IF rawButtonsPressed$(0)<>""
PRINT " Raw Buttons: ", rawButtonsPressed$(0)
PRINT " Mapped Buttons: ", mappedButtonsPressed$(0)
PRINT " Named Buttons: ", namedButtonsPressed$(0)
ENDIF
IF rumbleIntensity(0) > 0
IF ffEnabled(0)>0
PRINT " Rumble Intensity: ", rumbleIntensity(0)
ELSE
PRINT " Force-Feedback not Enabled"
ENDIF
ENDIF
fps = SCREEN FPS() + 1 : ` add 1 to avoid divide by 0 errors
SYNC
LOOP
__subroutines:
_load_for_DBClassic:
IF DBversion=2 THEN RETURN
LOAD IMAGE "imgpcx/" + filename$ + ".pcx" , objNum
SET OBJECT objNum , 1 , 0 , 0 , 1 , 1 , 0 , 1
DBversion = 1
RETURN
_load_for_DBPro:
IF DBversion=1 THEN RETURN
LOAD IMAGE "imgpng/" + filename$ + ".png" , objNum
SET OBJECT objNum , 1 , 1 , 0 , 1 , 1 , 0 , 1
DBversion = 2
RETURN
__functions:
rem Object behaviour functions
FUNCTION set_object_idle( objNum )
GHOST OBJECT OFF objNum
POSITION OBJECT objNum , 0 , 0 , 2000
ENDFUNCTION
FUNCTION set_object_highlight( objNum , offSetX# , offSetY#, offSetZ )
GHOST OBJECT ON objNum
POSITION OBJECT objNum , offSetX# , offSetY# , 2000 + offSetZ
ENDFUNCTION
rem Joystick functions
FUNCTION xbox_controller_fire( key )
rem X-box controller
rem remap joystick buttons key here if necessary
rem test input parameter "key" against the actual key integer value
rem assigned on the supported joystick, returns 1 if that key is pressed
retVal = 0
remap = -1
SELECT key
CASE 1 : remap = 0 : jAction$ = "A" : ENDCASE
CASE 2 : remap = 1 : jAction$ = "B" : ENDCASE
CASE 3 : remap = 2 : jAction$ = "X" : ENDCASE
CASE 4 : remap = 3 : jAction$ = "Y" : ENDCASE
CASE 5 : remap = 4 : jAction$ = "LB" : ENDCASE
CASE 6 : remap = 5 : jAction$ = "RB" : ENDCASE
CASE 13 : remap = 6 : jAction$ = "Back" : ENDCASE
CASE 12 : remap = 7 : jAction$ = "Start" : ENDCASE
CASE 9 : remap = 8 : jAction$ = "LS" : ENDCASE
CASE 10 : remap = 9 : jAction$ = "RS" : ENDCASE
ENDSELECT
IF remap<>-1
rem returns a 1 if remapped button is pressed, 0 otherwise
retVal = JOYSTICK FIRE X( remap )
IF retVal=1
rem set rumble intensity based on button pressed
set_rumble( (remap+1) * 10 )
rem log buttons pressed
IF rawButtonsPressed$(0)=""
rawButtonsPressed$(0) = STR$( remap )
mappedButtonsPressed$(0) = STR$( key )
namedButtonsPressed$(0) = jAction$
ELSE
rawButtonsPressed$(0) = rawButtonsPressed$(0) + "," + STR$( remap )
mappedButtonsPressed$(0) = mappedButtonsPressed$(0) + "," + STR$( key )
namedButtonsPressed$(0) = namedButtonsPressed$(0) + "," + jAction$
ENDIF
ENDIF
ENDIF
ENDFUNCTION retVal
FUNCTION set_rumble( intensity )
rem set the Maximum rumble inensity for this cycle
IF intensity < 0
rumbleIntensity(0) = -1
ELSE
IF rumbleIntensity(0) < intensity AND intensity < 101
rumbleIntensity(0) = intensity
ENDIF
ENDIF
ENDFUNCTION
Enjoy!