; Input Dll V1.2 - (c)2002 Exotecha Enterprises 
; - Joystick & Mouse Input Functions
; By:Todd Riggins (Blitz Forums aka. Pepsi)
; 07-22-02
; Exotecha Enterprises
; http://www.exodev.com

; New in version 1.2
; - Fixed multi effects - Thanks Simon S for spotting that
; - Fixed Effects Release - Thanks Simon S for spotting that

; New in version 1.1
; - Force Feed Back functionality
; - INPUT_INIT now requires the application title name to be passed as a parameter

; Brief "How to use" steps: 
;
; 1) Please make sure you windows' console->gaming options see's your joystick 
;    before running this demo!!! The DirectX's DirectInput component looks for installed
;    drivers that windows can see through the "gaming options" application in your
;    console directory. Thankyou.
;
; 2) Include this file in your main file with the include command at the top of your code.
;    You must call INPUT_INIT( *Your application name here* ) first before any other functions work.
;
; 3) UPDATE_INPUT() needs to be placed in your main game loop or any other place such as
;    gui loops to poll the joystick and mouse Data. This is an important DirectX DirectInput
;    functionality for immediate input data gathering. Note that I am not using buffered input.
;
; 4) Use the rest of the functions offered to do whatever you need them to do for you.
;
; 5) It's important to call INPUT_RELEASE() at the end of your program to shutdown and release
;    memory used by the input initialization.
;
; 6) Add Force Feed Back Functionality to your games! Currently, you can only make
;    FFB effects with the MS DirectX utility that is included. Next thing I will do for
;    the next version is to add where you can program your own effects and even allow
;    you to save those effects to file (you could even make your own super duper effects utility)!
;    --- Brief usage ---
;    a) Load the effect you want to use with FFB_LOAD_EFFECT.
;    b) Set the loaded effect to any joystick available with FFB_SET_EFFECT.
;    c) Then you can play your effect with FFB_PLAY_EFFECT.
;
; 7) Examine the Input_Demo.bb file to learn how to use the functions better. Also, below for 
;    each function, I give more information about each function. And you can always ask me
;    how to use a function or anything else about this library.
;
; 8) Last but not least, Have Fun! :)

;
; !!!!!!!!!!!! known issues: !!!!!!!!!!!!
;
; 1) Currently, I'm having trouble with "Programmable Effects" and looks like it will
;    be a while until I figure out any kind of solution if none.
;
; 2) In the function FFB_PLAY_EFFECT, there is a LOOP parameter. It does not allow looping... or isn't
;    programmed correctly for looping properly. Hopely I'll have a perminate solution in the
;    next update implemented. This involves programming effects as in issue 1.
; 
; 3) This may be a crappy way to document my functions, but it's going to have to do until
;    I finish this library. Then I will gather all info from here and "try" to make a nice
;    html documentation for it.
;
; 4) If you have trouble and dont understand something, see a bug or get errors, please ask me! :)



; todo: 
; ?
;  * Below is in no paticular order:
;  - Include a buffered input mode for joystick and mouse input.
;  - Include keyboard support( with immediate and buffered modes for it too ).
;  - fix any unforseen bugs...
;  - Im not to sure about adding DirectX Action Mapping part of DirectInput8 yet. It is
;    yet another beast of its own to battle against and I have yet to see it in 
;    commercial PC games, thus leaving me wondering if its useful or not. I dont need it.

Global xBank_In, xBank_Out, result, size
Global thisdll$

thisdll$="Exo_Inputv1_2"

; INPUT_INIT(MyTitle$)
; Intializes input functions to be used
;
; Params:
;  MyTitle$ - it's important to let the input dll know what application it is being used
;             for. As in the provided input demo examples, use the Blitz function AppTitle
;			  to set the application and then pass that same name through the INPUT_INIT
;             parameter to tell the Input Dll to work with your application.
; Errorcodes(Return Value of Dll function):
; 0  = Unable to call DLL function
; 1  = Successful
; >1 = Error in Initialization
Function INPUT_INIT(MyTitle$)
	Local initbank
	
	xBank_In = CreateBank(4)
	xBank_Out = CreateBank(4)

	size=Len(MyTitle$)+1
	initbank = CreateBank(size)
	POKESTRING(initbank,MyTitle$)
	
	result=CallDLL(thisdll$,"INPUT_INIT",initbank)
	FreeBank initbank
	
	Return result
End Function

; INPUT_RELEASE()
; Shutdown and releases memory used by the input initialization
; Intializes input functions to be used
; Errorcodes(Return Value of Dll function):
; 0 = Unable to call DLL function
; 1 = Successful
Function INPUT_RELEASE()
	FreeBank xBank_Out
	FreeBank xBank_In

	Return CallDLL(thisdll$,"INPUT_RELEASE")
End Function

; NUMBER_OF_JOYSTICKS()
; Returns number of joysticks available provided by the Windows Game Console
Function NUMBER_OF_JOYSTICKS()
	Return CallDLL(thisdll$,"NUMBER_OF_JOYSTICKS")
End Function

; JOYSTICK_NAME$(NUM)
; Returns the name of specified Joystick
; Params:
; NUM = Joystick number (0 - 255)
Function JOYSTICK_NAME$(NUM)
	Local xBankOutString
	Local name$, jnum

	PokeInt xBank_In,0,NUM

	xBankOutString = CreateBank(255)
	result = CallDLL(thisdll$,"JOYSTICK_NAME",xBank_In,xBankOutString)
	
	name$ = PEEKSTRING$(xBankOutString)
	FreeBank xBankOutString
	Return name$
End Function

; NUMBER_OF_MOUSE()
; Returns number of mouse(mices, whatever) available
Function NUMBER_OF_MOUSE()
	Return CallDLL(thisdll$,"NUMBER_OF_MOUSE")
End Function

; MOUSE_NAME$(NUM)
; Returns the name of specified Mouse
; Params:
; NUM = Mouse number (0 - 255)
Function MOUSE_NAME$(NUM)
	Local xBankOutString
	Local name$, jnum

	PokeInt xBank_In,0,NUM

	xBankOutString = CreateBank(255)
	result = CallDLL(thisdll$,"MOUSE_NAME",xBank_In,xBankOutString)
	
	name$ = PEEKSTRING$(xBankOutString)
	FreeBank xBankOutString
	Return name$
End Function

; UPDATE_INPUT()
; Needs to be placed in your main game loop to poll the joystick and mouse data
; Errorcodes(Return Value of Dll function):
; 0 = Unable to call DLL function
; 1 = Successful
; >1 = Error in polling (should fix itself unless you unplugged your joystick
;                         or mouse during program execution)
Function UPDATE_INPUT()
	Return CallDLL(thisdll$,"UPDATE_INPUT")
End Function

; FFB_EXIST(NUM)
; Returns 0 if FFB is not available or 1 if FFB is available for
;  the specified joystick.
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Not available
; 1 = Available 
Function FFB_EXIST(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"FFB_EXIST",xBank_In)
	Return result
End Function

; FFB_AUTOCENTER(NUM,TOGGLE)
; Toggle a force feedback joystick auto centering on or off.
; An example: For a SideWinder Force FeedBack Pro, when you
; 	grab the joystick and you feel it automatically centering
;   itself, that means the autocentering is on. This function
;   will allow FFB joysticks with this capability to turn it
;   on or off.
; Params:
; NUM = Joystick number (0 - 255)
; TOGGLE : 0 = OFF 1 = ON
; Return Values:
; 0 = Not available
; 1 = Successful
Function FFB_AUTOCENTER(NUM,TOGGLE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,TOGGLE
	result = CallDLL(thisdll$,"FFB_AUTOCENTER",xBank8_In)
	FreeBank xBank8_In
	Return result
End Function

;DllExport int _cdecl FFB_AUTOCENTER_STATUS( const void *in,int in_size,void *out,int out_sz );
; FFB_AUTOCENTER_STATUS(NUM)
;  Returns a value if autocentering is on or off. Look below for valid return values.
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = autocentering is off
; 1 = autocentering is on
;
; 2 = ffb not available
Function FFB_AUTOCENTER_STATUS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"FFB_AUTOCENTER_STATUS",xBank_In)
	Return result
End Function

; FFB_LOAD_EFFECT(EFFECT_FILENAME$)
;  Loads a force feed back effect to be used by a specific FFB capable joystick
; Params:
; EFFECT_FILENAME$ = Filename ( please include the .ffe extension ) of .ffe file to load
; Return Values:
; 0 = Unable to call DLL function
; 1 = Invalid Effect Filename ( Not found )
; 2 = To many Effects stored ( I only allow 256 effects to be stored currently )
; 1000 - 1255 = FFB Effect Entity
Function FFB_LOAD_EFFECT(EFFECT_FILENAME$)
	Local xBank_Effect
	
	size=Len(EFFECT_FILENAME$)+1
	xBank_Effect = CreateBank(size)
	POKESTRING(xBank_Effect,EFFECT_FILENAME$)
	
	result = CallDLL(thisdll$,"FFB_LOAD_EFFECT",xBank_Effect)
	FreeBank xBank_Effect

	Return result
End Function

; FFB_SET_EFFECT(NUM,EFFECT)
;  sets a force feed back effect for a specific FFB capable joystick
; Params:
; NUM = Joystick number (0 - 255)
; EFFECT = Effect entity abtained by FFB_LOAD_EFFECT
; Return Values:
; 0 = Unable to call DLL function
; 1 = Successful
;
; 2 = Invalid Joystick
; 3 = Invalid Effect Entity
; 4 = Set Effect failed
; 5 = ffb not available
Function FFB_SET_EFFECT(NUM,EFFECT)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,EFFECT
	
	result = CallDLL(thisdll$,"FFB_SET_EFFECT",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; FFB_STOP_ALL_EFFECTS(NUM)
;  Stop any effects that are currently playing on a specified FFB capable joystick
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Unable to call DLL function
; 1 = Successful
;
; 2 = Invalid Joystick
; 3 = Stop All Effects Error
; 4 = ffb not available
Function FFB_STOP_ALL_EFFECTS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"FFB_STOP_ALL_EFFECTS",xBank_In)
	Return result
End Function

; FFB_STOP_EFFECT(NUM,EFFECT)
;  stops a current specified FFB effect from playing from a specified FFB capable joystick
; Params:
; NUM = Joystick number (0 - 255)
; EFFECT = Effect entity previously loaded or created to be played
; Return Values:
; 0 = Unable to call DLL function
; 1 = Successful
;
; 2 = Invalid Joystick Number
; 3 = Invalid Effect
; 4 = Specified Effect hasn't been previosly set
; 5 = Stop Effect Error
; 6 = ffb not available
Function FFB_STOP_EFFECT(NUM,EFFECT)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,EFFECT

	result = CallDLL(thisdll$,"FFB_STOP_EFFECT",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; FFB_PLAY_EFFECT(NUM,EFFECT,LOOP,SOLO)
;  plays the effect previous loaded or created for the specified FFB capable joystick
; Params:
; NUM = Joystick number (0 - 255)
; EFFECT = Effect entity previously loaded or created to be played
; LOOP   = set to 0 for a one shot effect, 
;          set to 1 for continuous looping effect ( this is like playing with sounds )
; SOLO   = set to 0 for effect to play with other currently playing effects,
;          set to 1 to stop all other effects and play this effect only
; Return Values:
; 0 = Unable to call DLL function
; 1 = Successful
;
; 2 = Invalid Joystick Number
; 3 = Invalid Effect
; 4 = Specified Effect hasn't been previosly set
; 5 = Play Effect error
; 6 = ffb not available
Function FFB_PLAY_EFFECT(NUM,EFFECT,LOOP=0,SOLO=0)
	Local xBank16_In
	xBank16_In = CreateBank(16)
	PokeInt xBank16_In,0,NUM
	PokeInt xBank16_In,4,EFFECT
	PokeInt xBank16_In,8,LOOP
	PokeInt xBank16_In,12,SOLO

	result = CallDLL(thisdll$,"FFB_PLAY_EFFECT",xBank16_In)
	FreeBank xBank16_In

	Return result
End Function

; FFB_PLAY_STATUS(NUM,EFFECT)
;  returns a value on the status of the specified FFB capable joystick if it is
;  currently playing or not
; Params:
; NUM = Joystick number (0 - 255) 
; EFFECT = Effect entity to see if playing or not
; Return Values:
; 0 = Not playing
; 1 = Is currently playing
;
; 2 = Invalid Joystick Number
; 3 = Invalid Effect
; 4 = Specified Effect hasn't been previosly set
; 5 = Effect Lost Error
; 6 = Play Status Error
; 7 = ffb not available
Function FFB_PLAY_STATUS(NUM,EFFECT)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,EFFECT

	result = CallDLL(thisdll$,"FFB_PLAY_STATUS",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; FFB_DELETE_EFFECTS()
;  deletes all force feed back effects loaded or created
Function FFB_DELETE_EFFECTS()
	result = CallDLL(thisdll$,"FFB_DELETE_EFFECTS")
	Return result
End Function

; AXIS_X_EXIST(NUM)
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Not available
; 1 = Available 
Function AXIS_X_EXIST(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"AXIS_X_EXIST",xBank_In)
	Return result
End Function

; AXIS_Y_EXIST()
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Not available
; 1 = Available 
Function AXIS_Y_EXIST(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"AXIS_Y_EXIST",xBank_In)
	Return result
End Function

; AXIS_Z_EXIST()
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Not available
; 1 = Available 
Function AXIS_Z_EXIST(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"AXIS_Z_EXIST",xBank_In)
	Return result
End Function

; AXIS_RX_EXIST()
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Not available
; 1 = Available 
Function AXIS_RX_EXIST(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"AXIS_RX_EXIST",xBank_In)
	Return result
End Function

; AXIS_RY_EXIST()
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Not available
; 1 = Available 
Function AXIS_RY_EXIST(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"AXIS_RY_EXIST",xBank_In)
	Return result
End Function

; AXIS_RZ_EXIST()
; Params:
; NUM = Joystick number (0 - 255)
; Return Values:
; 0 = Not available
; 1 = Available 
Function AXIS_RZ_EXIST(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"AXIS_RZ_EXIST",xBank_In)
	Return result
End Function

; SLIDER_EXIST(NUM,SNUM)
; Params:
; NUM   = Joystick number (0 - 255)
; SNUM  = Slider number (0 - 1)
; Return Values:
; 0 = Not available
; 1 = Available 
Function SLIDER_EXIST(NUM,SNUM)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,SNUM

	result = CallDLL(thisdll$,"SLIDER_EXIST",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; POV_EXIST(NUM,PNUM)
; Params:
; NUM  = Joystick number (0 - 255)
; PNUM = POV 0 - 3
; Return Values:
; 0 = Not available
; 1 = Available 
Function POV_EXIST(NUM,PNUM)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,PNUM

	result = CallDLL(thisdll$,"POV_EXIST",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_X_AXIS(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; Return Value = X axis position value (usaully left and right movment)
Function JOY_X_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_X_AXIS",xBank_In)
	Return result
End Function

; JOY_Y_AXIS(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; Return Value = Y axis position value (usaully forward and backwards movment)
Function JOY_Y_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_Y_AXIS",xBank_In)
	Return result
End Function

; JOY_Z_AXIS(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; Return Value = Z axis position value (often the throttle control)
; Note: should return 0 if joystick does not have this
Function JOY_Z_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_Z_AXIS",xBank_In)
	Return result
End Function

; JOY_RX_AXIS(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; Return Value = X axis rotation
; Note: should return 0 if joystick does not have this
Function JOY_RX_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_RX_AXIS",xBank_In)
	Return result
End Function

; JOY_RY_AXIS(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; Return Value = Y axis rotation
; Note: should return 0 if joystick does not have this
Function JOY_RY_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_RY_AXIS",xBank_In)
	Return result
End Function

; JOY_RZ_AXIS(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; Return Value = Z axis rotation (often the rudder)
; Note: should return 0 if joystick does not have this
Function JOY_RZ_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_RZ_AXIS",xBank_In)
	Return result
End Function

;[New DX 8 Stuff] Below, I get no results... ?
; There are slider equivalents, but I can't even get this stuff
; to work with Blitz, so whats the point? Might as well delete
; the next 12 functions.

;JOY_VEL_X_AXIS
Function JOY_VEL_X_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_VEL_X_AXIS",xBank_In)
	Return result
End Function
;JOY_VEL_Y_AXIS
Function JOY_VEL_Y_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_VEL_Y_AXIS",xBank_In)
	Return result
End Function
;JOY_VEL_Z_AXIS
Function JOY_VEL_Z_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_VEL_Z_AXIS",xBank_In)
	Return result
End Function
;JOY_ANG_VEL_RX_AXIS
Function JOY_ANG_VEL_RX_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_ANG_VEL_RX_AXIS",xBank_In)
	Return result
End Function
;JOY_ANG_VEL_RY_AXIS
Function JOY_ANG_VEL_RY_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_ANG_VEL_RY_AXIS",xBank_In)
	Return result
End Function
;JOY_ANG_VEL_RZ_AXIS
Function JOY_ANG_VEL_RZ_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_ANG_VEL_RZ_AXIS",xBank_In)
	Return result
End Function
;JOY_ACC_X_AXIS
Function JOY_ACC_X_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_ACC_X_AXIS",xBank_In)
	Return result
End Function
;JOY_ACC_Y_AXIS
Function JOY_ACC_Y_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_ACC_Y_AXIS",xBank_In)
	Return result
End Function
;JOY_ACC_Z_AXIS
Function JOY_ACC_Z_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_ACC_Z_AXIS",xBank_In)
	Return result
End Function
;JOY_FORCE_X_AXIS
Function JOY_FORCE_X_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_FORCE_X_AXIS",xBank_In)
	Return result
End Function
;JOY_FORCE_Y_AXIS
Function JOY_FORCE_Y_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_FORCE_Y_AXIS",xBank_In)
	Return result
End Function
;JOY_FORCE_Z_AXIS
Function JOY_FORCE_Z_AXIS(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"JOY_FORCE_Z_AXIS",xBank_In)
	Return result
End Function
;[/New DX 8 Stuff] Above, I get no results... ?

; JOY_BUTTON(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; BNUM = Button to check if pressed or not
; Return Value:
; 0 = not pressed
; 1 = is pressed
Function JOY_BUTTON(NUM,BNUM)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,BNUM

	result = CallDLL(thisdll$,"JOY_BUTTON",xBank8_In)
	FreeBank xBank8_In
	
	Return result
End Function

; JOY_SLIDER(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; SNUM = Slider Number 0-1
; Return Value = Slider position value
Function JOY_SLIDER(NUM,SNUM)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,SNUM

	result = CallDLL(thisdll$,"JOY_SLIDER",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_POV(NUM)
; Params:
; NUM  = Joystick number (0 - 255)
; PNUM = POV Number 0-3
; Return Value:
; Centered = -1
; Up       = 0
; Right    = 90
; Down     = 180
; Left     = 270
Function JOY_POV(NUM,PNUM)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,PNUM

	result = CallDLL(thisdll$,"JOY_POV",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_X_RANGE(NUM,MINR,MAXR)
; Params:
; NUM  = Joystick number (0 - 255)
; MINR = Minimum Range Value 
; MAXR = Maximum Range Value 
; EX:
; If MINR is set to -1000 and MAXR is set to 1000 then
; X axis range of full left position  = -1000
; X axis range of full right position = 1000
; will make center X axis = 0
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Range value(s) not accepted
Function JOY_X_RANGE(NUM,MINR,MAXR)
	Local xBank12_In
	xBank12_In = CreateBank(12)
	PokeInt xBank12_In,0,NUM
	PokeInt xBank12_In,4,MINR
	PokeInt xBank12_In,8,MAXR
	
	result = CallDLL(thisdll$,"JOY_X_RANGE",xBank12_In)
	FreeBank xBank12_In

	Return result
End Function

; JOY_Y_RANGE(NUM,MINR,MAXR)
; Params:
; NUM  = Joystick number (0 - 255)
; MINR = Minimum Range Value 
; MAXR = Maximum Range Value 
; EX:
; See 'JOY_X_RANGE' for example return values
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Range value(s) not accepted
Function JOY_Y_RANGE(NUM,MINR,MAXR)
	Local xBank12_In
	xBank12_In = CreateBank(12)
	PokeInt xBank12_In,0,NUM
	PokeInt xBank12_In,4,MINR
	PokeInt xBank12_In,8,MAXR

	result = CallDLL(thisdll$,"JOY_Y_RANGE",xBank12_In)
	FreeBank xBank12_In

	Return result
End Function

; JOY_Z_RANGE(NUM,MINR,MAXR)
; Params:
; NUM  = Joystick number (0 - 255)
; MINR = Minimum Range Value 
; MAXR = Maximum Range Value 
; EX:
; See 'JOY_X_RANGE' for example return values
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Range value(s) not accepted
Function JOY_Z_RANGE(NUM,MINR,MAXR)
	Local xBank12_In
	xBank12_In = CreateBank(12)
	PokeInt xBank12_In,0,NUM
	PokeInt xBank12_In,4,MINR
	PokeInt xBank12_In,8,MAXR

	result = CallDLL(thisdll$,"JOY_Z_RANGE",xBank12_In)
	FreeBank xBank12_In

	Return result
End Function

; JOY_RX_RANGE(NUM,MINR,MAXR)
; Params:
; NUM  = Joystick number (0 - 255)
; MINR = Minimum Range Value 
; MAXR = Maximum Range Value 
; EX:
; See 'JOY_X_RANGE' for example return values
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Range value(s) not accepted
Function JOY_RX_RANGE(NUM,MINR,MAXR)
	Local xBank12_In
	xBank12_In = CreateBank(12)
	PokeInt xBank12_In,0,NUM
	PokeInt xBank12_In,4,MINR
	PokeInt xBank12_In,8,MAXR

	result = CallDLL(thisdll$,"JOY_RX_RANGE",xBank12_In)
	FreeBank xBank12_In

	Return result
End Function

; JOY_RY_RANGE(NUM,MINR,MAXR)
; Params:
; NUM  = Joystick number (0 - 255)
; MINR = Minimum Range Value 
; MAXR = Maximum Range Value 
; EX:
; See 'JOY_X_RANGE' for example return values
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Range value(s) not accepted
Function JOY_RY_RANGE(NUM,MINR,MAXR)
	Local xBank12_In
	xBank12_In = CreateBank(12)
	PokeInt xBank12_In,0,NUM
	PokeInt xBank12_In,4,MINR
	PokeInt xBank12_In,8,MAXR

	result = CallDLL(thisdll$,"JOY_RY_RANGE",xBank12_In)
	FreeBank xBank12_In

	Return result
End Function

; JOY_RZ_RANGE(NUM,MINR,MAXR)
; Params:
; NUM  = Joystick number (0 - 255)
; MINR = Minimum Range Value 
; MAXR = Maximum Range Value 
; EX:
; See 'JOY_X_RANGE' for example return values
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Range value(s) not accepted
Function JOY_RZ_RANGE(NUM,MINR,MAXR)
	Local xBank12_In
	xBank12_In = CreateBank(12)
	PokeInt xBank12_In,0,NUM
	PokeInt xBank12_In,4,MINR
	PokeInt xBank12_In,8,MAXR

	result = CallDLL(thisdll$,"JOY_RZ_RANGE",xBank12_In)
	FreeBank xBank12_In

	Return result
End Function

; JOY_X_DEADZONE(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = The value for the dead zone of the joystick (0-10000)
; EX: 
; 0     = no dead zone
; 5000  = dead zone extended from the center outwards 50 percent
; 10000 = entire range of axis is dead
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Dead zone value(s) not accepted
Function JOY_X_DEADZONE(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_X_DEADZONE",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_Y_DEADZONE(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = The value for the dead zone of the joystick (0-10000)
; EX: 
; 0     = no dead zone
; 5000  = dead zone extended from the center outwards 50 percent
; 10000 = entire range of axis is dead
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Dead zone value(s) not accepted
Function JOY_Y_DEADZONE(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_Y_DEADZONE",xBank8_In)
	FreeBank xBank8_In
	
	Return result
End Function

; JOY_Z_DEADZONE(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = The value for the dead zone of the joystick (0-10000)
; EX: 
; 0     = no dead zone
; 5000  = dead zone extended from the center outwards 50 percent
; 10000 = entire range of axis is dead
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Dead zone value(s) not accepted
Function JOY_Z_DEADZONE(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_Z_DEADZONE",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_RX_DEADZONE(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = The value for the dead zone of the joystick (0-10000)
; EX: 
; 0     = no dead zone
; 5000  = dead zone extended from the center outwards 50 percent
; 10000 = entire range of axis is dead
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Dead zone value(s) not accepted
Function JOY_RX_DEADZONE(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_RX_DEADZONE",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_RY_DEADZONE(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = The value for the dead zone of the joystick (0-10000)
; EX: 
; 0     = no dead zone
; 5000  = dead zone extended from the center outwards 50 percent
; 10000 = entire range of axis is dead
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Dead zone value(s) not accepted
Function JOY_RY_DEADZONE(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_RY_DEADZONE",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_RZ_DEADZONE(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = The value for the dead zone of the joystick (0-10000)
; EX: 
; 0     = no dead zone
; 5000  = dead zone extended from the center outwards 50 percent
; 10000 = entire range of axis is dead
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: Dead zone value(s) not accepted
Function JOY_RZ_DEADZONE(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_RZ_DEADZONE",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_X_SATURATION(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = Saturation level value of this axis (0-10000)
; EX: 
; The saturation level is the point at which the axis
; is considered to be the most extreme position.
; If the saturation level is set to 9500, the axis reaches
; it's extreme of it's range when it has moved 95 percent of
; the physical distance from it's center position ( Or from the dead zone ).
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: saturation zone value(s) not accepted
Function JOY_X_SATURATION(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_X_SATURATION",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_Y_SATURATION(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = Saturation level value of this axis (0-10000)
; EX: 
; The saturation level is the point at which the axis
; is considered to be the most extreme position.
; If the saturation level is set to 9500, the axis reaches
; it's extreme of it's range when it has moved 95 percent of
; the physical distance from it's center position ( Or from the dead zone ).
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: saturation zone value(s) not accepted
Function JOY_Y_SATURATION(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_Y_SATURATION",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_Z_SATURATION(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = Saturation level value of this axis (0-10000)
; EX: 
; The saturation level is the point at which the axis
; is considered to be the most extreme position.
; If the saturation level is set to 9500, the axis reaches
; it's extreme of it's range when it has moved 95 percent of
; the physical distance from it's center position ( Or from the dead zone ).
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: saturation zone value(s) not accepted
Function JOY_Z_SATURATION(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_Z_SATURATION",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_RX_SATURATION(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = Saturation level value of this axis (0-10000)
; EX: 
; The saturation level is the point at which the axis
; is considered to be the most extreme position.
; If the saturation level is set to 9500, the axis reaches
; it's extreme of it's range when it has moved 95 percent of
; the physical distance from it's center position ( Or from the dead zone ).
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: saturation zone value(s) not accepted
Function JOY_RX_SATURATION(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_RX_SATURATION",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_RY_SATURATION(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = Saturation level value of this axis (0-10000)
; EX: 
; The saturation level is the point at which the axis
; is considered to be the most extreme position.
; If the saturation level is set to 9500, the axis reaches
; it's extreme of it's range when it has moved 95 percent of
; the physical distance from it's center position ( Or from the dead zone ).
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: saturation zone value(s) not accepted
Function JOY_RY_SATURATION(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_RY_SATURATION",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

; JOY_RZ_SATURATION(NUM,ZONE)
; Params:
; NUM  = Joystick number (0 - 255)
; ZONE = Saturation level value of this axis (0-10000)
; EX: 
; The saturation level is the point at which the axis
; is considered to be the most extreme position.
; If the saturation level is set to 9500, the axis reaches
; it's extreme of it's range when it has moved 95 percent of
; the physical distance from it's center position ( Or from the dead zone ).
; Errorcodes:
; 0 = Unable to call DLL function
; 1 = Successful
; 2 = Error: saturation zone value(s) not accepted
Function JOY_RZ_SATURATION(NUM,ZONE)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,ZONE

	result = CallDLL(thisdll$,"JOY_RZ_SATURATION",xBank8_In)
	FreeBank xBank8_In

	Return result
End Function

;=============
; mouse stuff
;=============

; MOUSE_X(NUM)
; Params:
; NUM  = Mouse number (0 - 255)
; Return Value = X axis position value (usaully left and right movment)
Function MOUSE_X(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"MOUSE_X",xBank_In)
	Return result
End Function

; MOUSE_Y(NUM)
; Params:
; NUM  = Mouse number (0 - 255)
; Return Value = Y axis position value (usaully up and down movment)
Function MOUSE_Y(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"MOUSE_Y",xBank_In)
	Return result
End Function

; MOUSE_Z(NUM)
; Params:
; NUM  = Mouse number (0 - 255)
; Return Value = Z axis position value (usaully mouse wheel)
Function MOUSE_Z(NUM)
	PokeInt xBank_In,0,NUM
	result = CallDLL(thisdll$,"MOUSE_Z",xBank_In)
	Return result
End Function

; MOUSE_BUTTON(NUM)
; Params:
; NUM  = Mouse number (0 - 255)
; BNUM = Button to check if pressed or not (0 - 7)
; Return Value:
; 0 = not pressed
; 1 = is pressed
Function MOUSE_BUTTON(NUM,BNUM)
	Local xBank8_In
	xBank8_In = CreateBank(8)
	PokeInt xBank8_In,0,NUM
	PokeInt xBank8_In,4,BNUM

	result = CallDLL(thisdll$,"MOUSE_BUTTON",xBank8_In)
	FreeBank xBank8_In
	
	Return result
End Function

; Finally got these two string functions to work from cool pd code by Rob Hutchinson
Function POKESTRING$(XBank,XString$)
	Local XOffset=0
	
	For X=1 To Len(XString$)
		PokeByte XBank,XOffset,Asc(Mid$(XString$,X,1))
		XOffset=XOffset+1
	Next
	
	PokeByte XBank,XOffset,0

End Function


Function PEEKSTRING$(XBank)
	Local XString$=""
	Local XChar
	Local XOffset=0
	DebugLog BankSize(XBank)

	For X=0 To BankSize(XBank)-1
		XChar = PeekByte(XBank,XOffset)
		

		If XChar<>0 
			XString$ = XString$ + Chr(XChar) 
			
		Else
			Exit
		EndIf
		XOffset=XOffset+1
	Next

	Return XString$
End Function