-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamepad.h
323 lines (286 loc) · 10.1 KB
/
gamepad.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Gamepad Input Library
* Sean Middleditch <[email protected]>
* Copyright (C) 2010,2011 Sean Middleditch
* LICENSE: MIT/X
*/
#if !defined(GAMEPAD_H)
#define GAMEPAD_H 1
#if defined(__cplusplus)
extern "C" {
#endif
#if defined(GAMEPAD_STATIC_LIB)
# define GAMEPAD_API
#else
# if defined(_WIN32)
# if defined(GAMEPAD_EXPORT)
# define GAMEPAD_API __declspec(dllexport)
# else
# define GAMEPAD_API __declspec(dllimport)
# endif
# elif defined(__GNUC__) && defined(GAMEPAD_EXPORT)
# define GAMEPAD_API __attribute__((visibility("default")))
# else
# define GAMEPAD_API extern
# endif
#endif
/**
* Enumeration of the possible devices.
*
* Only four devices are supported as this is the limit of Windows.
*/
enum GAMEPAD_DEVICE {
GAMEPAD_0 = 0, /**< First gamepad */
GAMEPAD_1 = 1, /**< Second gamepad */
GAMEPAD_2 = 2, /**< Third gamepad */
GAMEPAD_3 = 3, /**< Fourth gamepad */
GAMEPAD_COUNT /**< Maximum number of supported gamepads */
};
/**
* Enumeration of the possible buttons.
*/
enum GAMEPAD_BUTTON {
BUTTON_DPAD_UP = 0, /**< UP on the direction pad */
BUTTON_DPAD_DOWN = 1, /**< DOWN on the direction pad */
BUTTON_DPAD_LEFT = 2, /**< LEFT on the direction pad */
BUTTON_DPAD_RIGHT = 3, /**< RIGHT on the direction pad */
BUTTON_START = 4, /**< START button */
BUTTON_BACK = 5, /**< BACK button */
BUTTON_LEFT_THUMB = 6, /**< Left analog stick button */
BUTTON_RIGHT_THUMB = 7, /**< Right analog stick button */
BUTTON_LEFT_SHOULDER = 8, /**< Left bumper button */
BUTTON_RIGHT_SHOULDER = 9, /**< Right bumper button */
BUTTON_A = 12, /**< A button */
BUTTON_B = 13, /**< B button */
BUTTON_X = 14, /**< X button */
BUTTON_Y = 15, /**< Y button */
BUTTON_COUNT /**< Maximum number of supported buttons */
};
/**
* Enumeration of the possible pressure/trigger buttons.
*/
enum GAMEPAD_TRIGGER {
TRIGGER_LEFT = 0, /**< Left trigger */
TRIGGER_RIGHT = 1, /**< Right trigger */
TRIGGER_COUNT /**< Number of triggers */
};
/**
* Enumeration of the analog sticks.
*/
enum GAMEPAD_STICK {
STICK_LEFT = 0, /**< Left stick */
STICK_RIGHT = 1, /**< Right stick */
STICK_COUNT /**< Number of analog sticks */
};
/**
* Enumeration of main stick directions.
*
* This is used for some of the convenience routines in the library.
*/
enum GAMEPAD_STICKDIR {
STICKDIR_CENTER = 0, /**< CENTER, no direction */
STICKDIR_UP = 1, /**< UP direction */
STICKDIR_DOWN = 2, /**< DOWN direction */
STICKDIR_LEFT = 3, /**< LEFT direction */
STICKDIR_RIGHT = 4, /**< RIGHT direction */
STICKDIR_COUNT
};
/**
* Enumeration for true/false values
*/
enum GAMEPAD_BOOL {
GAMEPAD_FALSE = 0, /**< FALSE value for boolean parameters */
GAMEPAD_TRUE = 1 /**< TRUE value for boolean parameters */
};
typedef enum GAMEPAD_DEVICE GAMEPAD_DEVICE;
typedef enum GAMEPAD_BUTTON GAMEPAD_BUTTON;
typedef enum GAMEPAD_TRIGGER GAMEPAD_TRIGGER;
typedef enum GAMEPAD_STICK GAMEPAD_STICK;
typedef enum GAMEPAD_STICKDIR GAMEPAD_STICKDIR;
typedef enum GAMEPAD_BOOL GAMEPAD_BOOL;
#define GAMEPAD_DEADZONE_LEFT_STICK 7849 /**< Suggested deadzone magnitude for left analog stick */
#define GAMEPAD_DEADZONE_RIGHT_STICK 8689 /**< Suggested deadzone magnitude for right analog stick */
#define GAMEPAD_DEADZONE_TRIGGER 30 /**< Suggested deadzone for triggers */
/**
* Initialize the library.
*
* This is critical on non-Windows platforms.
*/
GAMEPAD_API void GamepadInit(void);
/**
* Shutdown the library.
*
* This will release resources allocated by the library internally.
*
* This should be called after forking as well.
*/
GAMEPAD_API void GamepadShutdown(void);
/**
* Updates the state of the gamepads.
*
* This must be called (at least) once per game loop.
*/
GAMEPAD_API void GamepadUpdate(void);
/**
* Test if a particular gamepad is connected.
*
* \param device The device to check.
* \returns GAMEPAD_TRUE if the device is connected, GAMEPAD_FALSE if it is not.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadIsConnected(GAMEPAD_DEVICE device);
/**
* Test if a particular button is being pressed.
*
* \param device The device to check.
* \param button The button to check.
* \returns GAMEPAD_TRUE if the button is down, GAMEPAD_FALSE if it is not.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadButtonDown(GAMEPAD_DEVICE device, GAMEPAD_BUTTON button);
/**
* Test if a particular button has been depressed since the previous call to GamepadUpdate.
*
* \param device The device to check.
* \param button The button to check.
* \returns GAMEPAD_TRUE if the button has been pressed, GAMEPAD_FALSE if it is not or if it was depressed the previous frame.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadButtonTriggered(GAMEPAD_DEVICE device, GAMEPAD_BUTTON button);
/**
* Test if a particular button has been released since the previous call to GamepadUpdate.
*
* \param device The device to check.
* \param button The button to check.
* \returns GAMEPAD_TRUE if the button has been released, GAMEPAD_FALSE if it is down or if it was not down the previous frame.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadButtonReleased(GAMEPAD_DEVICE device, GAMEPAD_BUTTON button);
/**
* Get the trigger value (depression magnitude) in its raw form.
*
* \param device The device to check.
* \param trigger The trigger to check.
* \returns Trigger depression magnitude (0 to 32767).
*/
GAMEPAD_API int GamepadTriggerValue(GAMEPAD_DEVICE device, GAMEPAD_TRIGGER trigger);
/**
* Get the trigger value (depression magnitude) in normalized form.
*
* \param device The device to check.
* \param trigger The trigger to check.
* \returns Trigger depression magnitude (0 to 1).
*/
GAMEPAD_API float GamepadTriggerLength(GAMEPAD_DEVICE device, GAMEPAD_TRIGGER trigger);
/**
* Test if a trigger is depressed
*
* \param device The device to check.
* \param trigger The trigger to check.
* \returns GAMEPAD_TRUE if down, GAMEPAD_FALSE otherwise.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadTriggerDown(GAMEPAD_DEVICE device, GAMEPAD_TRIGGER trigger);
/**
* Test if a trigger is depressed
*
* \param device The device to check.
* \param trigger The trigger to check.
* \returns GAMEPAD_TRUE if triggered, GAMEPAD_FALSE otherwise.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadTriggerTriggered(GAMEPAD_DEVICE device, GAMEPAD_TRIGGER trigger);
/**
* Test if a trigger is depressed
*
* \param device The device to check.
* \param trigger The trigger to check.
* \returns GAMEPAD_TRUE if released, GAMEPAD_FALSE otherwise.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadTriggerReleased(GAMEPAD_DEVICE device, GAMEPAD_TRIGGER trigger);
/**
* Set the rumble motors on/off.
*
* To turn off the rumble effect, set values to 0 for both motors.
*
* The left motor is the low-frequency/strong motor, and the right motor is the high-frequency/weak motor.
*
* \param device The device to update.
* \param left Left motor strengh (0 to 1).
* \param right Right motor strengh (0 to 1).
*/
GAMEPAD_API void GamepadSetRumble(GAMEPAD_DEVICE device, float left, float right);
/**
* Query the position of an analog stick as raw values.
*
* The values retrieved by this function represent the magnitude of the analog
* stick in each direction. Note that it shouldn't be possible to get full
* magnitude in one direction unless the other direction has a magnitude of
* zero, as the stick has a circular movement range.
*
* \param device The device to check.
* \param stick The stick to check.
* \param outX Pointer to integer to store the X magnitude in (-32767 to 32767).
* \param outX Pointer to integer to store the Y magnitude in (-32767 to 32767).
*/
GAMEPAD_API void GamepadStickXY(GAMEPAD_DEVICE device, GAMEPAD_STICK stick, int* outX, int* outY);
/**
* Query the position of an analog stick as normalized values.
*
* The values retrieved by this function represent the magnitude of the analog
* stick in each direction. Note that it shouldn't be possible to get full
* magnitude in one direction unless the other direction has a magnitude of
* zero, as the stick has a circular movement range.
*
* \param device The device to check.
* \param stick The stick to check.
* \param outX Pointer to float to store the X magnitude in (-1 to 1).
* \param outX Pointer to float to store the Y magnitude in (-1 to 1).
*/
GAMEPAD_API void GamepadStickNormXY(GAMEPAD_DEVICE device, GAMEPAD_STICK stick, float* outX, float* outY);
/**
* Query the magnitude of an analog stick.
*
* This returns the normalized value of the magnitude of the stick. That is,
* if the stick is pushed all the way in any direction, it returns 1.0.
*
* \param device The device to check.
* \param stick The stick to check.
* \returns The magnitude of the stick (0 to 1).
*/
GAMEPAD_API float GamepadStickLength(GAMEPAD_DEVICE device, GAMEPAD_STICK stick);
/**
* Query the direction of a stick (in radians).
*
* This returns the direction of the stick. This value is in radians, not
* degrees. Zero is to the right, and the angle increases in a
* counter-clockwise direction.
*
* \param device The device to check.
* \param stick The stick to check.
* \returns The angle of the stick (0 to 2*PI).
*/
GAMEPAD_API float GamepadStickAngle(GAMEPAD_DEVICE device, GAMEPAD_STICK stick);
/**
* Get the direction the stick is pushed in (if any).
*
* This is a useful utility function for when the stick should be treated as a simple
* directional pad, such as for menu UIs.
*
* \param device The device to check.
* \param stick The trigger to check.
* \returns The stick's current direction.
*/
GAMEPAD_API GAMEPAD_STICKDIR GamepadStickDir(GAMEPAD_DEVICE device, GAMEPAD_STICK stick);
/**
* Test whether a stick has been pressed in a particular direction since the last update.
*
* This only returns true if the stick was centered last frame.
*
* This is a useful utility function for when the stick should be treated as a simple
* directional pad, such as for menu UIs.
*
* \param device The device to check.
* \param stick The trigger to check.
* \param stickdir The direction to check for.
* \returns GAMEPAD_TRUE if the stick is pressed in the specified direction, GAMEPAD_FALSE otherwise.
*/
GAMEPAD_API GAMEPAD_BOOL GamepadStickDirTriggered(GAMEPAD_DEVICE device, GAMEPAD_STICK stick, GAMEPAD_STICKDIR dir);
#if defined(__cplusplus)
} /* extern "C" */
#endif
#endif