Nov 232013
 

One annoying fact around XNA is that it supports natively only xbox gamepads.

Well Nuclex fixes that for us : )
See here : http://nuclexframework.codeplex.com/ .
Download, unzip and add a reference in your project to nuclex.input.dll.

The source code : XNA_DEMO_21 .

Now lets see the code.

All the magic is in the object inputmanager.
Then, just like we would check the state of keyboard keys, we will do the same with the gamepad buttons.

If you want to check the state of the first DirectInput game pad in your system instead, just write:
If (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.X = ButtonState.Pressed) Then ...
Notice that DirectInput Gamepads (as opposed to xbox gamepads) starts at index 5. However, if you have an xbox pad plugged, index 5 will also work.


Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input
Imports Nuclex.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
Dim graphics As GraphicsDeviceManager
Dim SpriteBatch As SpriteBatch
Dim input As InputManager
Dim myfont As SpriteFont
Dim msg As String = "."
Dim dummyTexture As Texture2D
Dim rc As Rectangle

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
input = New InputManager(Services, Window.Handle)
Components.Add(input)
Window.Title = "Test"
End Sub

Protected Overrides Sub Initialize()
MyBase.Initialize()
End Sub
Protected Overrides Sub LoadContent()
SpriteBatch = New SpriteBatch(GraphicsDevice)
myfont = Content.Load(Of SpriteFont)("xnb\myfont")
dummyTexture = New Texture2D(GraphicsDevice, 1, 1)
'
Dim c(0) As Color
c(0) = Color.White
dummyTexture.SetData(Of Color)(c)
'
rc = New Rectangle(100, 100, 10, 10)
'
MyBase.LoadContent()

End Sub
Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'right
If (Keyboard.GetState.IsKeyDown(Keys.Right)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.X = ButtonState.Pressed) Then
msg = "right"
rc.X += 1
End If
'left
If (Keyboard.GetState.IsKeyDown(Keys.Left)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.A = ButtonState.Pressed) Then
msg = "left"
rc.X -= 1
End If
'up
If (Keyboard.GetState.IsKeyDown(Keys.Up)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.RightShoulder = ButtonState.Pressed) Then
msg = "up"
rc.Y -= 1
End If
'down
If (Keyboard.GetState.IsKeyDown(Keys.Down)) Or (input.GetGamePad(ExtendedPlayerIndex.Five).GetState().Buttons.LeftShoulder = ButtonState.Pressed) Then
msg = "down"
rc.Y += 1
End If
MyBase.Update(gameTime)
End Sub

Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
GraphicsDevice.Clear(Color.CornflowerBlue)
SpriteBatch.Begin()
SpriteBatch.DrawString(myfont, msg, New Vector2(10, 10), Color.White)
SpriteBatch.Draw(dummyTexture, rc, Color.White)
SpriteBatch.End()
MyBase.Draw(gameTime)
End Sub

End Class

 Posted by at 13 h 18 min

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.