Friday, 5 January 2007

MDX Custom Action Mapping (Part II)

[Warning: This article is obsolote. I´d suggest you to read the Refreshed version here.]

Taking the issue where we left it yesterday...

The next step is to make an important choice: Inmediate or Buffered mode? Take into account that DX Action Mapping works in Buffered Mode only. Why? To answer that, let´s describe both modes a little bit:

Inmediate Mode

Take a look at how data is reported under the Inmediate Mode: all you get is a struct of the type JoystickState, MouseState or KeyboardState, which has all the data you need under some default fields, defined by DirectX (like AxisX, AxixY, etc). This fields are always the same, no matter which device you are accesing to. It´s device´s builder (i.e. Logitech) who decides what physical objects are mapped to what DX default fields. An example:

- For a joystick, it´s quite trivial to map it´s objects to fields, because JoystickState was originally designed for that: joysticks (as it´s name states). So, the AxisX field will almost always be mapped to the X-Axis of the joystick.

- What happens for a driving wheel? Ah! that´s different. That´s something DirectInput was not originally designed for, and when this kind of devices came out, instead of adapting DInput for them, DX guys decided to use existing structs to handle new devices. So, there´s no default field in the JoystickState structure for the WheelAxis object. In this way, some device builders will map wheel axis to AxisX, while others will do to the Rx Axis, and so on...

Buffered Mode

Are things different in Buffered Mode? Quite a bit.

In buffered mode, you don´t get access to the whole structure of data. Instead of that, you call the GetBufferedData() method, which retrieves a collection of BufferedData objects, one for each changing object in the device. That means, if the device is absolutely stall, no data will be returned.

One tip: To set the buffered mode, you have to manually change the property:

Device.Properties.BufferSize = 16.

Making the relationship

What we need is a way to save and recover from a file something like this:

Action="Steering Action" PhysicalDevice="Logitech G25" PhysicalObject="Wheel Axis"

What info do we put there? how do we recover it in order to read data fron the device ? Let´s see:

1.- The first attribute is easy, just gameAction.ToString() to save,

and Enum.Parse(typeof(eGameActions), attributeInnerText); to recover from the file.

2.- The second attribute is not hard either. Instead of saving device´s name, we will save device´s Guid:

Write the guid as DeviceGuid.ToString()
and recover it as: DeviceGuid = new Guid( attributeGuidInnerText );

3.- The third attribute.... aaaahhh. This is a little bit more tricky.

more on this later...

1 comment:

d4rkstar said...

Great Man,
expecially the way of Device.Properties.Buffer ... i could have die before to find that s..t of setting.... Thanks again for your hints!