Print Page | Close Window

game controllers

Printed From: Mirage Source
Category: Tutorials
Forum Name: Submitted Tutorials
Forum Discription: Tutorial submissions for MSE are posted here, waiting for approval
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=359
Printed Date: 20 December 2006 at 6:05pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: game controllers
Posted By: DarkX
Subject: game controllers
Date Posted: 02 March 2006 at 11:46am

Ok if this tutorial is not any good, you can tell me because this is my first time ever posting a tut on any forum.

by the way I'm not sure if this is compatible with DX7 so you guys might wanna make sure it will work before putting it in your engine, cuz it might cause major problems.

Well in some cases it's best to put DirectInput on the engine, not really sure if it's needed since some of you are way better at this.(this is for directx programs.)

Dim diDev as DirectInputDevice8

Dim diDevEnum as DirectInputEnumDevices8

Dim joyCaps as DIDEVCAPS

 

'enumerate the game controllers

SetdiDevEnum = di.getDIDevices(DI8DEVCLASS_GAMECTRL, _ DIEDFL_ATTACHEDONLY

If diDevEnum.GetCount = 0 then

    MsgBox "No joystick could be found"

End if

 

'create the joystick object

Set diDev = di.CreateDevice(diDevEnum.GetItem(1).GetGuidInstance)

diDev.SetCommonDataFormat DIFORMAT_JOYSTICK

diDev.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND or DISCL_NONEXCLUSIVE

Private sub DirectXEvent8_DXCallback(byval eventid as long)

end sub

 

'create an event handler for the joystick

EventHandler = dx.CreateEvent(Me)

'ask for notification of events

diDev.SetEventNotification EventHandler

 

'retrieve joystick status

dim js as DIHOYSTATE

diDev.GetDeviceStateJoystick js

for n = 0 to joyCaps.lButtons - 1

     if js.Button(n) = 0 then

        Debug.Print "Button " & n & " was released"

   else

         Debug.Print "Button " & n & " was pressed"

   end if

next n

if js.POV(0) = -1 then

 Debug.Print "D-pad is centered"

else

Debug.Print "D-pad =" & js/POV(0)

end if

now go over the source code for the JoystickTest program. this preogram, like uaual is a standard EXE project with a single form and a reference to the "Dircetx8 for visual basic type library". the first part of the program includes the directx events and objects as well as the program variables.

option explicit

option base 0

Implements DirectXEvent8

'Directx objects and structures

dim dx as new directx8

dim di as directinput8

dim didev as directinputdevice8

dim didevenum as directinputenumdevices8

dim joyCaps as DIDEVCAPS

 

'keep track of analog stick motion

dim analog(1 to 2) as D3DVECTOR

 

'program variables

dim eventhandle as long

 

private sub form_load()

on local error resume next

'create the directinput object

Set di = dx.DirectInputCreate()

if Err.Number <> 0 then

MsgBox "Error creating directinput object"

shutdown

end if

'enumerate the game controllers

set diDevEnum =  di.GetDIDevices(DI8DEVCLASS_GAMECTRL, DIEFL_ATTACHEDONLY)

If Err.Number <> 0 then

MsgBox "Error enumerating game controllers"

shutdown

end if

'initialize the joystick

joystick_Init

 

'main polling loop

do while true

didev.poll

doevents

loop

end sub

private sub form_KeyDown(keycode as integer, shift as integer)

 if keycode = 27 then shutdown

end sub

privatesub form_queryunload(cancel as integer, unloadmode as integer)

shutdown

end sub

private sub shutdown()

if eventhandle <> 0 then

dx.destroyevent eventhandle

end if

if not (didev is nothing) then didev.unacquire

set didev = nothing

set di = nothing

set dx = nothing

end

end sub

private sub joystick_init()

on local error resume next

'see if joystick was already acquired

if not didev is nothing then

didev.unacquire

end if

'create joystick onject

set didev = nothing

set didev = di.createdevice(didevenum.getitem(1).Getguidinstance)

didev.setcommondataformat DIFORMAT_JOYSTICK

didev.setcooperativelevel Me.hWnd, DISCL_BACKGROUND or DISCL_NONEXCLUSIVE

'create an event handler for the joystick

eventhandle = dx.createevent(Me)

'ask for notification of events

didev.seteventnotification eventhandle

'set the analog response range

setanalogranges -1000, 1000

'acquire joystick for exclusive use

didev.acquire
'manually poll joystick first time

directxevent8_dxcallback 0

'retrieve joystick information

didev.getcapabilities joycaps

'display information about the joystick

debug.print didevenum.getitem(1).getinstancename

debug.print "Number of axes: " & joycaps.laxes

debug.print "Number of buttons: " & joycaps.lbuttons

debug.print "Device type: " & joycaps.ldevtype

debug.print "Driver version: " & joycaps.lDriversVersion

debug.print "Time resolution: " & joycaps.lffmintimeresolution

debug.print "Firmware revision: " & joycaps.lfirmwarerevision

debug.print "Hardware revision: " & joycaps.lHardwareRevision

debug.print "Number of POVs: " & joycaps.lPOVs

end sub

private sub setanalogranges(byval lmin as long, byval lmax as long)

dim DiProp_Dead as DIPROPLONG

dim DiProp_Range as DIPROPRANGE

dim DiProp_Saturation as DOPROPLONG

on local error resume next

'set range for all axes

with DiProp_Range

.lHow = DIPH_DEVICE

.lMin = lMin

.lMax = lMax

end with

'set deadzone for x and y axes to 5 percent

with DiProp_Dead

.lData = (lMax - lMin) / 5

.lHow = DIPH_BYOFFSET

.lObj = DIJOFS_X

didev.setproperty "DIPROP_DEADZONE", DiProp_Dead

.lObj = DIJOFS_Y

didev.setproperty "DIPROP_DEADZONE", DiProp_Dead

end with

'saturation zone for x and y axes to 95 percent

with DiProp_Saturation

.lData = (lMax - lMin) * 0.95

.lHow = DIPH_BYOFFSET

.lObj = DIJOFS_X

diDev.SetProperty "DIPROP_SATURATION", DiProp_Saturation

.lObj = DIJOFS_Y

diDev.SetProperty "DIPROP_SATURATION", DiProp_Saturation

end with

end sub

private sub joystick_analogmove(byval lNum as long, byref vanalog as d3dvector)

debug.print "Analog stick " & lNum & " = " & vAnalog.x & "," & vAnalog.Y & "," & vAnalog.z

end sub

private sub joystick_slidermove(byval lSlider as long, byval lvalue as long)

debug.print "Slider" & lSlider & " = " & lValue

end sub

private sub directxevent8_Dxxallback(byval eventid as long)

static analog1 as d3dvector

static analog2 as d3dvector

dim n as long

on local error resume next

'retieve joystick status

didev.getdevicestatejoystick js

if err.number - DIERR_NOTAQUIRED or err.number = DIERR_INPUTLOST then

didev.acquire

exit sub

end if

'fire off any joystick analog movement events

for n = 1  to 8

  select case n

case 1

analog1.x = js.x

joystick_analogmove 1, analog1

case 2

analog1.y = js.y

joystick_analog 1, analog1

case 3

analog1.z = js.z

joystick_analogmove 1, analog1

case 4

analog2.x = js.rx

joystick_analogmove 2, analog2

case 5

analog2.y = js.ry

joystick_analogmove 2, analog2

case 6

analog2.z = js.rz

joystick_analogmove 2, analog2

case 7

analog1.slidermove 1, jsslider(0)

case 8

joystick_slidermove 2, js.slider(1)

end select

next n

'fire off any button events

for n = 0 to joycaps.lbuttons - 1

if js.buttons(n) = 0 then

debug.print "Joystick ButtonUp: " & n

else

debug.print "Joystick ButtonDown: " & n

end if

next n

fire off any direction-pad button events

if js.POV(0) = - 1 then

debug.print "DPAD: -1"

else

debug.print "DPAD: " & js.POV(0) / 4500

end if

end sub

Well that's all of the code, if there are any incorrect codes in it at all re post on here for me would you. And if there should be any more please post the rest, Thank You.



-------------
Korrey D.
Visit me at my site http://www.freewebs.com/darkkniteonline



Replies:
Posted By: Misunderstood
Date Posted: 02 March 2006 at 4:00pm
to close a code tag its [ / code ]
fixed it for ya though



Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info