gpt4 book ai didi

C# DirectInput - 确定游戏 handle Controller 类型

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:05 29 4
gpt4 key购买 nike

我正在尝试使用 DirectInput API 在我的应用程序中使用各种游戏 handle Controller 。据我所知,轴在设备之间的映射不同。

除了使用 DeviceInformation.ProductName/InstanceName 来区分 Controller 以允许不同的轴控制分配之外,是否有确定 Controller 类型的通用实践方法?

最佳答案

如果您只需要知道 Controller 类型(FirstPerson、Joystick、Gamepad...),请使用 DeviceInformation.DeviceType。

要知道连接了哪个特定 Controller ,请使用 ProductName 或更好的 ProductGuid。对于两个不同的 Controller ,ProductName 可能相同。另一方面,即使对于相同生产费用的两个 Controller ,ProductGuid 也可能不同。

只有使用 ProductGuid,您才能毫无疑问地识别连接的设备实例。

人们可能会想通过 dev.GetObjects(DeviceObjectTypeFlags.Axis) 进行 foreach 并检查轴的名称(和偏移量),但不能保证名为“X 轴”的轴确实映射到 someDevice.CurrentJoystickState.X

您必须事先为要连接的任何 Controller 完成所有映射(只有您自己开发应用程序时才可以接受)并将该信息存储在应用程序的配置文件中,否则您将需要提供某种形式,用户可以自己映射新设备。我目前正在开发这个,但这个答案会超出这个问题的范围......

另请考虑,为了获取此基本信息(如 ProductGuid),您不必创 build 备。这些属性已经在 DeviceInstance 中可用(在 Microsoft.DirectX.DirectInput.Manager.GetDevices() 的 DeviceList 中)...


更新:因为我自己的回答一直困扰着我,所以我更深入地探讨了这个话题。
所以这是一种确定哪些轴映射到哪些值的方法:

// loop through the axes of an acquired device:
foreach (DeviceObjectInstance doi in
_currentDevice.GetObjects(DeviceObjectTypeFlags.Axis))
{
doi.Name; // the name of the axis, e.g. 'X-Achse' (that's a german device...)
doi.Offset / 4; // the enumeration 'index' of the axis
doi.UsagePage; // the UsagePage determines the context of the axis value
// vvvv
doi.Usage; // the Usage tells you which JoystickState field to map to.
// ^^^^
}

到目前为止,我发现了这些使用值(value)表示:

(with JoystickState s = _currentDevice.CurrentJoystickState)
Usage Axis value
48 s.X
49 s.Y
50 s.Z
51 s.Rx
52 s.Ry
53 s.Rz
54 s.GetSlider()[0]

所以使用switch(doi.Usage)你可以自动获取相应的轴值。

关于C# DirectInput - 确定游戏 handle Controller 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3246163/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com