📜 ⬆️ ⬇️

Xbox One gamepad for PC games

Many fans of computer games are familiar with the Xbox 360 controller. This is a well-deserved "veteran" who has a significant army of fans. It can be connected to a computer using a cord or wireless adapter, and many players prefer to play PC games with it. The updated version for Xbox One can also be connected to the PC via a micro USB connector, and also has a number of improvements that will appeal to players. For this controller, drivers for Windows 8 and Windows 7 have already been released and are compatible with the XInput API. Applications that are designed to work with gamepads will immediately be able to work with the new controller.


The Xbox 360 controller has established itself as a good and high-quality product, which has been serving for a long time, without any fatal flaws. There is a steady increase in sales of these controllers, as well as the number of games that can be played using this device.


What's new?



The updated version for Xbox One has absorbed all the best that was in the Xbox 360 controller, it has become more convenient. The form has changed a bit, now the controller is better “sitting” in the hands. Many will also note the lack of a battery compartment. Now the back of the controller does not have this protrusion.

However, this controller uses ordinary AA batteries or batteries of the same size to power it. Also noteworthy is the absence of holes for screws, improved sensitivity of DPAD and joysticks, new vibration motors in the cocks. There are also “invisible” changes that concern the wireless work protocol. The Xbox 360 controller worked at a speed of about 1.6 megabits per second. In the new version, the bandwidth has increased more than 20 times, which allows you to expand the capabilities of the connected accessories . One of such devices can be a stereo headset:

API


The main method of working with a gamepad in Windows is the use of C ++ API XInput . It should be noted that there are no initialization functions, you simply query the state of the controller:
')
XINPUT_STATE state; DWORD result=XInputGetState(0, &state); if (result == ERROR_SUCCESS) { if (state.Gamepad.wButtons & XINPUT_GAMEPAD_A) { //  A } } 


The XInputGetState function takes as parameters the controller's index (several of them can be connected) as well as the structure with the state where the button values ​​are returned:
 typedef struct _XINPUT_STATE { DWORD dwPacketNumber; //   XINPUT_GAMEPAD Gamepad; } XINPUT_STATE, *PXINPUT_STATE; typedef struct _XINPUT_GAMEPAD { WORD wButtons; BYTE bLeftTrigger; BYTE bRightTrigger; SHORT sThumbLX; SHORT sThumbLY; SHORT sThumbRX; SHORT sThumbRY; } XINPUT_GAMEPAD, *PXINPUT_GAMEPAD; 


Buttons are coded by bits:

 #define XINPUT_GAMEPAD_DPAD_UP 0x0001 #define XINPUT_GAMEPAD_DPAD_DOWN 0x0002 #define XINPUT_GAMEPAD_DPAD_LEFT 0x0004 #define XINPUT_GAMEPAD_DPAD_RIGHT 0x0008 #define XINPUT_GAMEPAD_START 0x0010 #define XINPUT_GAMEPAD_BACK 0x0020 #define XINPUT_GAMEPAD_LEFT_THUMB 0x0040 #define XINPUT_GAMEPAD_RIGHT_THUMB 0x0080 #define XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100 #define XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200 #define XINPUT_GAMEPAD_A 0x1000 #define XINPUT_GAMEPAD_B 0x2000 #define XINPUT_GAMEPAD_X 0x4000 #define XINPUT_GAMEPAD_Y 0x8000 


A little more complicated is the case with the values ​​of the triggers and the two joysticks. The values ​​of the axes X, Y are within SHRT_MIN-SHRT_MAX (-32768 +32767), and for the trigger _UI8_MAX (255). Usually in games these values ​​are normalized to -1.0 +1.0. Also for joysticks, the so-called dead zone should be considered. The return values ​​of the axes at the neutral position may differ from zero, and in order not to take them into account, the standard values ​​of the “dead zone” should be used, which should be calculated according to the following algorithm:

 float magnitude = sqrt(state.Gamepad.sThumbRX*state.Gamepad.sThumbRX + state.Gamepad.sThumbRY*state.Gamepad.sThumbRY); if (magnitude > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE) { //     } 


The standard values ​​of these thresholds are as follows:
 #define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE 7849 #define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689 #define XINPUT_GAMEPAD_TRIGGER_THRESHOLD 30 


You can look at more detailed examples of working with a gamepad on the site code.msdn.com and also use the wrapper that is included in the DirectX Toolkit .
In addition to the functions directly related to polling controller states, XInput also includes functions for controlling vibration motors and plug-in accessories, for example, to record voice from the headset or play sound on the headset.

Joystick support is also available for managed code in the XNA and Monogame libraries . The Internet Explorer Developer Channel included an experimental implementation of the W3C Gamepad API and you can use your controller to create HTML / Javasctipt games for both the web and Windows 8 applications:

 <!DOCTYPE html> <html> <head> <title>Gamepad API Sample</title> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <script> function gameLoop() { var gamepads = navigator.getGamepads(); for (var playerIndex = 0; playerIndex < gamepads.length; playerIndex++) { var gamepad = gamepads[playerIndex]; if (gamepad) { if (gamepad.buttons[6].pressed || gamepad.buttons[7].pressed) { // A trigger is pressed, fire weapon. fireWeapon(playerIndex); } } } window.requestAnimationFrame(gameLoop); } gameLoop(); </script> </head> <body> 


If you want to embed gamepad support into the HTML5 game for Windows 8 now, without waiting for the next version of Internet Explorer to be released, then you can use a wrapper over XInput for Javasctipt .

Gamepad support is also available in Unity3d. The Input class contains all the necessary methods for working with controller states:

 using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { void Start () { } void Update () { var axisX = Input.GetAxis("Horisontal"); if (Input.GetButtonDown("A Btn")) { //  A } } } 

Just do not forget to configure the correct names for the buttons and joysticks ( Edit / Project Settings / Input ):


From the above examples it is obvious that working with a gamepad is very simple and does not require supercomplicated efforts. If you are developing a game for Windows 8 using C ++, C #, Unity, or HTML - be sure to include support for gamepads, this feature will appeal to many players.

useful links


Source: https://habr.com/ru/post/241742/


All Articles