Saturday, 24 August 2013

Joystick acquisition with SharpDX

Joystick acquisition with SharpDX

I'm new about C# and Sharpdx. I have this code problem from a couple of
days and I don't understand way doesn't work!
This is a simple task This is about acquiring a value of one axis of a
Joystick and show it on a text box in a Form.
I did a new project on Visual Studio 2010 express and I did a Form with a
button and a textBox for show the value of the joystick axis (X axis). The
code is down here
The first part of code down here is the example on the sharpdx
documentation, the second part is a little bit different.
The problem is that the value doesn't change every time I push the button
I wrong something but I don't know what
Many Thanks.
//
private void button3_Click(object sender, EventArgs e) {
// Initialize DirectInput
var directInput = new DirectInput();
// Find a Joystick Guid
var joystickGuid = Guid.Empty;
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Gamepad not found, look for a Joystick
if (joystickGuid == Guid.Empty)
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;
// If Joystick not found, throws an error
if (joystickGuid == Guid.Empty)
{
Console.WriteLine("No joystick/Gamepad found.");
Console.ReadKey();
Environment.Exit(1);
}
// Instantiate the joystick e stato
Joystick joystick = new Joystick(directInput, joystickGuid);
JoystickState stato = new JoystickState();
// specifico se relativo o assoluto
joystick.Properties.AxisMode = DeviceAxisMode.Absolute;
// effettuo un collegamento con il joystick
joystick.Acquire();
// qui faccio una acquisizione dello stato che memorizzo
joystick.Poll();
// effettuo una lettura dello stato
joystick.GetCurrentState(ref stato);
// stampo il valore dell'ordinata
textBox1.Text = stato.X.ToString();
}

No comments:

Post a Comment