Just like how HID devices are described by a usage page and usage, each input on an HID device is also described by a usage page and usage. This can be implemented as bitflags or an integer representing a direction, such as 1 to the north, 2 to north-east, 3 to east, etc. Analog sticks have 2 axes and triggers have one. Values for analog sticks and shoulder trigger axes, delivered as signed or unsigned integers.Buttons, delivered as bitflags, booleans, or a list of which are pressed.If you press both at the same time, it's the same as pressing neither. Pressing RT makes the axis go in the negative direction, and LT makes it go in the positive direction. The HID version comes with a big, annoying downside: the XInput driver combines both shoulder tiggers into one axis when converted to HID. Effectively there are two copies of each XBox controller on your system, the XInput version and the HID version. The XInput driver creates a virtual HID device and forwards inputs to it. XBox controllers also work on older games that only implemented support for HID. The downside is alienating players who only have a PS4 controller, or some other controller that uses HID. This is nice for indie developers as XBox controllers are a small, consistent subset of the wide world of USB game controllers, and doing XBox support well is significantly easier than supporting all possible controllers. The XInput API is one of the simplest APIs Microsoft has ever made and is extremely easy to use, but it only works with XBox controllers. It supports up to 4 controllers at a time, with a light on the controller indicating the player index. We'll refer to the driver, the API, and the protocol as XInput. These use a custom communication protocol, and Microsoft introduced a new API to work with them. The only controllers you're likely to find that don't use HID are Microsoft's own XBox controllers. I'll be redundantly referring to game controllers as HID devices. Games interface with the virtual device while a driver handles communicating those bytes to the physical one. HID refers to the standard protocol, physical devices (the gamepad in your hands), and virtual devices. You can read the official usage table from usb.org, or this more concise reference. Modern game controllers use HID usage page 1 (generic desktop) with usage 0x04 (Joystick) or 0x05 (Gamepad), and Windows has several APIs for working with them. An HID usage page is a number describing the purpose of the device, and usages are further subcategories. Standing for Human Interface Device, HID is a standardized communication protocol for USB devices a user directly interacts with, covering anything from keyboards to treadmills. Special thanks to Handmade Network for fostering a community that values exploring details, as well as Martins for sharing his limitless knowledge. You can build the Linux examples by running make. The combined.cpp example uses RawInput and XInput to demonstrate a more complete Windows implementation. You can build the Windows examples in Visual Studio by opening Joystick Input Examples.sln in the vs folder, or by running build.bat with a Visual Studio developer command line. The src folder contains small example programs to illustrate implementation details. It's a well designed library that offloads tons of headaches. TLDR: if you're a solo indie developer who just wants good basic joystick support, use SDL Game Controller. Prerequisites: familiarity with Windows programming and C-style C++. This guide aims to provide everything you need to know about implementing joystick input in PC games.
0 Comments
Leave a Reply. |