
Greetings, dear readers!
In one of the projects I needed to work with
game controllers , in particular with a game steering wheel. It was necessary to get the state of the steering wheel - the angle of rotation, pressed buttons. It was not a game that was created, it was an ordinary .net application, I will not go into the details of the project. It will be about how to get information about the state of game controllers in .net.
Subject area
The task is to obtain data from the game steering. First of all, it is necessary to study the subject area. Where are the game wheels used? Obviously in games. Many games are developed using DirectX technology. After reading
Wikipedia , you can find out that DirectX is divided into many interfaces. Of interest to us is the interface DirectInput.
DirectInput is an interface used to process data from the keyboard, mouse, joystick, etc., of game controllers. (c) Wikipedia.org
Used game controllers
To implement the project at my disposal was given a game wheel
Defender Forsage GTR .
I also did not miss the opportunity to experiment with my
Logitech Rumblepad 2 game joystick.
')
Managed directx
As you probably guessed,
Managed DirectX is DirectX support from managed code, i.e. of programs written using .net. MDX is included in the DirectX SDK, starting with the ninth version.
If you are interested in MDX, you can read Tom Miller’s DirectX 9 book with managed code. Programming games and graphics. The network has a lot of information on the topic.
Required Tools
It is logical that first of all it is necessary to install the driver of the manufacturer of the game controller, although, as it turned out, this is not a mandatory item, since during my experiments with the Logitech joystick, I did not install any drivers for it, and it consistently answered my requests (running Windows 7).
We use DirectX, so we need the DirectX SDK, you can download it
here . Everything you need to work with .net is already there. As an IDE, I used Visual Studio 2010.
Beginning of work
So, DirectX SDK is installed, launch Visual Studio, create a new project and connect Managed DirectX to it. To do this, go to
References ->
Add Reference , go to the
Browse tab, go to the
\ Windows \ Microsoft.NET \ DirectX for Managed Code \ 1.0.2902.0 folder and connect to the
Microsoft.DirectX.dll and
Microsoft.DirectX.DirectInput.dll project , respectively, do not forget about:
using Microsoft.DirectX; using Microsoft.DirectX.DirectInput;
Of course, not without pitfalls. In my projects, I mostly use .net framework 4.0, while MDX has problems with this version of the framework, and at the build stage of the project Visual Studio 2010 will just hang. Therefore, we have two ways to solve the problem:
1) use an earlier version of the .net framework.
2) to tweak the project configs a bit, which we will do.
Add a new
app.config file with the following content to the project:
<?xml version="1.0"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/> </startup> </configuration>
Now everything is ready, you can start working with game controllers.
Getting the status of the game wheel
Obviously, the first thing you need to know about all the connected game controllers:
foreach (DeviceInstance instance in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)) {
Next, you need to initialize the gaming device. For clarity, I mean that I have one:
Device device; foreach (DeviceInstance instance in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly)) { device = new Device(instance.ProductGuid);
In the
characteristics of the steering wheel it is said that the angle of rotation of the steering wheel is 180 degrees, therefore in
InputRange I indicated the value of the angle of deviation to the left and to the right of 90 degrees.
Unfortunately, we will not be able to subscribe to any events from the steering wheel or joystick like
KeyDown \ KeyUp , and the state of the game controller will have to be manually polled:
For clarity, you can display the structure of
JoystickState and see which fields change depending on the state of the game steering. All this is also true for the joystick.
findings
It turns out that it is not so difficult to program the game controller to fit your needs, the majority of all kinds of joysticks / steering wheels, etc. obey the DirectInput interface, now you can easily add their support to your projects / games. As for the games, it remains only to implement the functional, giving the user the opportunity to “hang” on the action buttons provided for by the logic of the game.
Links
1.
DirectX Software Development Kit June 2010 \ 572 Mb2.
Tom Miller - Managed DirectX 9 with Managed Code3.
Sources of the demonstration projectThanks for attention.