gamepadutils.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <SDL3/SDL.h>
  11. #include <SDL3/SDL_test_font.h>
  12. #include "gamepadutils.h"
  13. #include "gamepad_front.h"
  14. #include "gamepad_back.h"
  15. #include "gamepad_battery_unknown.h"
  16. #include "gamepad_battery_empty.h"
  17. #include "gamepad_battery_low.h"
  18. #include "gamepad_battery_medium.h"
  19. #include "gamepad_battery_full.h"
  20. #include "gamepad_battery_wired.h"
  21. #include "gamepad_touchpad.h"
  22. #include "gamepad_button.h"
  23. #include "gamepad_button_small.h"
  24. #include "gamepad_axis.h"
  25. #include "gamepad_axis_arrow.h"
  26. /* This is indexed by SDL_GamepadButton. */
  27. static const struct
  28. {
  29. int x;
  30. int y;
  31. } button_positions[] = {
  32. { 412, 192 }, /* SDL_GAMEPAD_BUTTON_A */
  33. { 456, 157 }, /* SDL_GAMEPAD_BUTTON_B */
  34. { 367, 157 }, /* SDL_GAMEPAD_BUTTON_X */
  35. { 414, 126 }, /* SDL_GAMEPAD_BUTTON_Y */
  36. { 199, 157 }, /* SDL_GAMEPAD_BUTTON_BACK */
  37. { 257, 153 }, /* SDL_GAMEPAD_BUTTON_GUIDE */
  38. { 314, 157 }, /* SDL_GAMEPAD_BUTTON_START */
  39. { 100, 179 }, /* SDL_GAMEPAD_BUTTON_LEFT_STICK */
  40. { 330, 255 }, /* SDL_GAMEPAD_BUTTON_RIGHT_STICK */
  41. { 102, 65 }, /* SDL_GAMEPAD_BUTTON_LEFT_SHOULDER */
  42. { 421, 61 }, /* SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER */
  43. { 179, 213 }, /* SDL_GAMEPAD_BUTTON_DPAD_UP */
  44. { 179, 274 }, /* SDL_GAMEPAD_BUTTON_DPAD_DOWN */
  45. { 141, 242 }, /* SDL_GAMEPAD_BUTTON_DPAD_LEFT */
  46. { 211, 242 }, /* SDL_GAMEPAD_BUTTON_DPAD_RIGHT */
  47. { 257, 199 }, /* SDL_GAMEPAD_BUTTON_MISC1 */
  48. { 157, 160 }, /* SDL_GAMEPAD_BUTTON_PADDLE1 */
  49. { 355, 160 }, /* SDL_GAMEPAD_BUTTON_PADDLE2 */
  50. { 157, 200 }, /* SDL_GAMEPAD_BUTTON_PADDLE3 */
  51. { 355, 200 }, /* SDL_GAMEPAD_BUTTON_PADDLE4 */
  52. };
  53. /* This is indexed by SDL_GamepadAxis. */
  54. static const struct
  55. {
  56. int x;
  57. int y;
  58. double angle;
  59. } axis_positions[] = {
  60. { 99, 178, 270.0 }, /* LEFTX */
  61. { 99, 178, 0.0 }, /* LEFTY */
  62. { 331, 256, 270.0 }, /* RIGHTX */
  63. { 331, 256, 0.0 }, /* RIGHTY */
  64. { 116, 5, 0.0 }, /* TRIGGERLEFT */
  65. { 400, 5, 0.0 }, /* TRIGGERRIGHT */
  66. };
  67. static SDL_Rect touchpad_area = {
  68. 148, 20, 216, 118
  69. };
  70. typedef struct
  71. {
  72. Uint8 state;
  73. float x;
  74. float y;
  75. float pressure;
  76. } GamepadTouchpadFinger;
  77. struct GamepadImage
  78. {
  79. SDL_Renderer *renderer;
  80. SDL_Texture *front_texture;
  81. SDL_Texture *back_texture;
  82. SDL_Texture *battery_texture[SDL_JOYSTICK_POWER_MAX];
  83. SDL_Texture *touchpad_texture;
  84. SDL_Texture *button_texture;
  85. SDL_Texture *axis_texture;
  86. int gamepad_width;
  87. int gamepad_height;
  88. int battery_width;
  89. int battery_height;
  90. int touchpad_width;
  91. int touchpad_height;
  92. int button_width;
  93. int button_height;
  94. int axis_width;
  95. int axis_height;
  96. int x;
  97. int y;
  98. SDL_bool showing_front;
  99. SDL_bool showing_battery;
  100. SDL_bool showing_touchpad;
  101. SDL_bool buttons[SDL_GAMEPAD_BUTTON_MAX];
  102. int axes[SDL_GAMEPAD_AXIS_MAX];
  103. SDL_JoystickPowerLevel battery_level;
  104. int num_fingers;
  105. GamepadTouchpadFinger *fingers;
  106. };
  107. static SDL_Texture *CreateTexture(SDL_Renderer *renderer, unsigned char *data, unsigned int len)
  108. {
  109. SDL_Texture *texture = NULL;
  110. SDL_Surface *surface;
  111. SDL_RWops *src = SDL_RWFromConstMem(data, len);
  112. if (src) {
  113. surface = SDL_LoadBMP_RW(src, SDL_TRUE);
  114. if (surface) {
  115. texture = SDL_CreateTextureFromSurface(renderer, surface);
  116. SDL_DestroySurface(surface);
  117. }
  118. }
  119. return texture;
  120. }
  121. GamepadImage *CreateGamepadImage(SDL_Renderer *renderer)
  122. {
  123. GamepadImage *ctx = SDL_calloc(1, sizeof(*ctx));
  124. if (ctx) {
  125. ctx->renderer = renderer;
  126. ctx->front_texture = CreateTexture(renderer, gamepad_front_bmp, gamepad_front_bmp_len);
  127. ctx->back_texture = CreateTexture(renderer, gamepad_back_bmp, gamepad_back_bmp_len);
  128. SDL_QueryTexture(ctx->front_texture, NULL, NULL, &ctx->gamepad_width, &ctx->gamepad_height);
  129. ctx->battery_texture[SDL_JOYSTICK_POWER_UNKNOWN] = CreateTexture(renderer, gamepad_battery_unknown_bmp, gamepad_battery_unknown_bmp_len);
  130. ctx->battery_texture[SDL_JOYSTICK_POWER_EMPTY] = CreateTexture(renderer, gamepad_battery_empty_bmp, gamepad_battery_empty_bmp_len);
  131. ctx->battery_texture[SDL_JOYSTICK_POWER_LOW] = CreateTexture(renderer, gamepad_battery_low_bmp, gamepad_battery_low_bmp_len);
  132. ctx->battery_texture[SDL_JOYSTICK_POWER_MEDIUM] = CreateTexture(renderer, gamepad_battery_medium_bmp, gamepad_battery_medium_bmp_len);
  133. ctx->battery_texture[SDL_JOYSTICK_POWER_FULL] = CreateTexture(renderer, gamepad_battery_full_bmp, gamepad_battery_full_bmp_len);
  134. ctx->battery_texture[SDL_JOYSTICK_POWER_WIRED] = CreateTexture(renderer, gamepad_battery_wired_bmp, gamepad_battery_wired_bmp_len);
  135. SDL_QueryTexture(ctx->battery_texture[SDL_JOYSTICK_POWER_UNKNOWN], NULL, NULL, &ctx->battery_width, &ctx->battery_height);
  136. ctx->touchpad_texture = CreateTexture(renderer, gamepad_touchpad_bmp, gamepad_touchpad_bmp_len);
  137. SDL_QueryTexture(ctx->touchpad_texture, NULL, NULL, &ctx->touchpad_width, &ctx->touchpad_height);
  138. ctx->button_texture = CreateTexture(renderer, gamepad_button_bmp, gamepad_button_bmp_len);
  139. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  140. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  141. ctx->axis_texture = CreateTexture(renderer, gamepad_axis_bmp, gamepad_axis_bmp_len);
  142. SDL_QueryTexture(ctx->axis_texture, NULL, NULL, &ctx->axis_width, &ctx->axis_height);
  143. SDL_SetTextureColorMod(ctx->axis_texture, 10, 255, 21);
  144. ctx->showing_front = SDL_TRUE;
  145. }
  146. return ctx;
  147. }
  148. void SetGamepadImagePosition(GamepadImage *ctx, int x, int y)
  149. {
  150. if (!ctx) {
  151. return;
  152. }
  153. ctx->x = x;
  154. ctx->y = y;
  155. }
  156. void SetGamepadImageShowingFront(GamepadImage *ctx, SDL_bool showing_front)
  157. {
  158. if (!ctx) {
  159. return;
  160. }
  161. ctx->showing_front = showing_front;
  162. }
  163. void SetGamepadImageShowingBattery(GamepadImage *ctx, SDL_bool showing_battery)
  164. {
  165. if (!ctx) {
  166. return;
  167. }
  168. ctx->showing_battery = showing_battery;
  169. }
  170. void SetGamepadImageShowingTouchpad(GamepadImage *ctx, SDL_bool showing_touchpad)
  171. {
  172. if (!ctx) {
  173. return;
  174. }
  175. ctx->showing_touchpad = showing_touchpad;
  176. }
  177. void GetGamepadImageArea(GamepadImage *ctx, int *x, int *y, int *width, int *height)
  178. {
  179. if (!ctx) {
  180. if (x) {
  181. *x = 0;
  182. }
  183. if (y) {
  184. *y = 0;
  185. }
  186. if (width) {
  187. *width = 0;
  188. }
  189. if (height) {
  190. *height = 0;
  191. }
  192. return;
  193. }
  194. if (x) {
  195. *x = ctx->x;
  196. }
  197. if (y) {
  198. *y = ctx->y;
  199. }
  200. if (width) {
  201. *width = ctx->gamepad_width;
  202. }
  203. if (height) {
  204. *height = ctx->gamepad_height;
  205. if (ctx->showing_touchpad) {
  206. *height += ctx->touchpad_height;
  207. }
  208. }
  209. }
  210. int GetGamepadImageButtonWidth(GamepadImage *ctx)
  211. {
  212. if (!ctx) {
  213. return 0;
  214. }
  215. return ctx->button_width;
  216. }
  217. int GetGamepadImageButtonHeight(GamepadImage *ctx)
  218. {
  219. if (!ctx) {
  220. return 0;
  221. }
  222. return ctx->button_height;
  223. }
  224. int GetGamepadImageAxisWidth(GamepadImage *ctx)
  225. {
  226. if (!ctx) {
  227. return 0;
  228. }
  229. return ctx->axis_width;
  230. }
  231. int GetGamepadImageAxisHeight(GamepadImage *ctx)
  232. {
  233. if (!ctx) {
  234. return 0;
  235. }
  236. return ctx->axis_height;
  237. }
  238. SDL_GamepadButton GetGamepadImageButtonAt(GamepadImage *ctx, float x, float y)
  239. {
  240. SDL_FPoint point;
  241. int i;
  242. if (!ctx) {
  243. return SDL_GAMEPAD_BUTTON_INVALID;
  244. }
  245. point.x = x;
  246. point.y = y;
  247. for (i = 0; i < SDL_arraysize(button_positions); ++i) {
  248. SDL_bool on_front = SDL_TRUE;
  249. if (i >= SDL_GAMEPAD_BUTTON_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_PADDLE4) {
  250. on_front = SDL_FALSE;
  251. }
  252. if (on_front == ctx->showing_front) {
  253. SDL_FRect rect;
  254. rect.x = (float)ctx->x + button_positions[i].x - ctx->button_width / 2;
  255. rect.y = (float)ctx->y + button_positions[i].y - ctx->button_height / 2;
  256. rect.w = (float)ctx->button_width;
  257. rect.h = (float)ctx->button_height;
  258. if (SDL_PointInRectFloat(&point, &rect)) {
  259. return (SDL_GamepadButton)i;
  260. }
  261. }
  262. }
  263. return SDL_GAMEPAD_BUTTON_INVALID;
  264. }
  265. SDL_GamepadAxis GetGamepadImageAxisAt(GamepadImage *ctx, float x, float y)
  266. {
  267. SDL_FPoint point;
  268. int i;
  269. if (!ctx) {
  270. return SDL_GAMEPAD_AXIS_INVALID;
  271. }
  272. point.x = x;
  273. point.y = y;
  274. if (ctx->showing_front) {
  275. for (i = 0; i < SDL_arraysize(axis_positions); ++i) {
  276. SDL_FRect rect;
  277. rect.x = (float)ctx->x + axis_positions[i].x - ctx->axis_width / 2;
  278. rect.y = (float)ctx->y + axis_positions[i].y - ctx->axis_height / 2;
  279. rect.w = (float)ctx->axis_width;
  280. rect.h = (float)ctx->axis_height;
  281. if (SDL_PointInRectFloat(&point, &rect)) {
  282. return (SDL_GamepadAxis)i;
  283. }
  284. }
  285. }
  286. return SDL_GAMEPAD_AXIS_INVALID;
  287. }
  288. void ClearGamepadImage(GamepadImage *ctx)
  289. {
  290. if (!ctx) {
  291. return;
  292. }
  293. SDL_zeroa(ctx->buttons);
  294. SDL_zeroa(ctx->axes);
  295. }
  296. void SetGamepadImageButton(GamepadImage *ctx, SDL_GamepadButton button, SDL_bool active)
  297. {
  298. if (!ctx) {
  299. return;
  300. }
  301. ctx->buttons[button] = active;
  302. }
  303. void SetGamepadImageAxis(GamepadImage *ctx, SDL_GamepadAxis axis, int direction)
  304. {
  305. if (!ctx) {
  306. return;
  307. }
  308. ctx->axes[axis] = direction;
  309. }
  310. void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad)
  311. {
  312. int i;
  313. if (!ctx) {
  314. return;
  315. }
  316. for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
  317. const SDL_GamepadButton button = (SDL_GamepadButton)i;
  318. if (SDL_GetGamepadButton(gamepad, button) == SDL_PRESSED) {
  319. SetGamepadImageButton(ctx, button, SDL_TRUE);
  320. } else {
  321. SetGamepadImageButton(ctx, button, SDL_FALSE);
  322. }
  323. }
  324. for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
  325. const SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
  326. const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
  327. const Sint16 value = SDL_GetGamepadAxis(gamepad, axis);
  328. if (value < -deadzone) {
  329. SetGamepadImageAxis(ctx, axis, -1);
  330. } else if (value > deadzone) {
  331. SetGamepadImageAxis(ctx, axis, 1);
  332. } else {
  333. SetGamepadImageAxis(ctx, axis, 0);
  334. }
  335. }
  336. ctx->battery_level = SDL_GetJoystickPowerLevel(SDL_GetGamepadJoystick(gamepad));
  337. if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
  338. int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
  339. if (num_fingers != ctx->num_fingers) {
  340. GamepadTouchpadFinger *fingers = (GamepadTouchpadFinger *)SDL_realloc(ctx->fingers, num_fingers * sizeof(*fingers));
  341. if (fingers) {
  342. ctx->fingers = fingers;
  343. ctx->num_fingers = num_fingers;
  344. } else {
  345. num_fingers = SDL_min(ctx->num_fingers, num_fingers);
  346. }
  347. }
  348. for (i = 0; i < num_fingers; ++i) {
  349. GamepadTouchpadFinger *finger = &ctx->fingers[i];
  350. SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &finger->state, &finger->x, &finger->y, &finger->pressure);
  351. }
  352. }
  353. }
  354. void RenderGamepadImage(GamepadImage *ctx)
  355. {
  356. SDL_FRect dst;
  357. int i;
  358. if (!ctx) {
  359. return;
  360. }
  361. dst.x = (float)ctx->x;
  362. dst.y = (float)ctx->y;
  363. dst.w = (float)ctx->gamepad_width;
  364. dst.h = (float)ctx->gamepad_height;
  365. if (ctx->showing_front) {
  366. SDL_RenderTexture(ctx->renderer, ctx->front_texture, NULL, &dst);
  367. } else {
  368. SDL_RenderTexture(ctx->renderer, ctx->back_texture, NULL, &dst);
  369. }
  370. for (i = 0; i < SDL_arraysize(button_positions); ++i) {
  371. if (ctx->buttons[i]) {
  372. SDL_bool on_front = SDL_TRUE;
  373. if (i >= SDL_GAMEPAD_BUTTON_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_PADDLE4) {
  374. on_front = SDL_FALSE;
  375. }
  376. if (on_front == ctx->showing_front) {
  377. dst.x = (float)ctx->x + button_positions[i].x - ctx->button_width / 2;
  378. dst.y = (float)ctx->y + button_positions[i].y - ctx->button_height / 2;
  379. dst.w = (float)ctx->button_width;
  380. dst.h = (float)ctx->button_height;
  381. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  382. }
  383. }
  384. }
  385. if (ctx->showing_front) {
  386. for (i = 0; i < SDL_arraysize(axis_positions); ++i) {
  387. if (ctx->axes[i] < 0) {
  388. const double angle = axis_positions[i].angle;
  389. dst.x = (float)ctx->x + axis_positions[i].x - ctx->axis_width / 2;
  390. dst.y = (float)ctx->y + axis_positions[i].y - ctx->axis_height / 2;
  391. dst.w = (float)ctx->axis_width;
  392. dst.h = (float)ctx->axis_height;
  393. SDL_RenderTextureRotated(ctx->renderer, ctx->axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  394. } else if (ctx->axes[i] > 0) {
  395. const double angle = axis_positions[i].angle + 180.0;
  396. dst.x = (float)ctx->x + axis_positions[i].x - ctx->axis_width / 2;
  397. dst.y = (float)ctx->y + axis_positions[i].y - ctx->axis_height / 2;
  398. dst.w = (float)ctx->axis_width;
  399. dst.h = (float)ctx->axis_height;
  400. SDL_RenderTextureRotated(ctx->renderer, ctx->axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  401. }
  402. }
  403. }
  404. if (ctx->showing_battery) {
  405. dst.x = (float)ctx->x + ctx->gamepad_width - ctx->battery_width;
  406. dst.y = (float)ctx->y;
  407. dst.w = (float)ctx->battery_width;
  408. dst.h = (float)ctx->battery_height;
  409. SDL_RenderTexture(ctx->renderer, ctx->battery_texture[ctx->battery_level], NULL, &dst);
  410. }
  411. if (ctx->showing_touchpad) {
  412. dst.x = (float)ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
  413. dst.y = (float)ctx->y + ctx->gamepad_height;
  414. dst.w = (float)ctx->touchpad_width;
  415. dst.h = (float)ctx->touchpad_height;
  416. SDL_RenderTexture(ctx->renderer, ctx->touchpad_texture, NULL, &dst);
  417. for (i = 0; i < ctx->num_fingers; ++i) {
  418. GamepadTouchpadFinger *finger = &ctx->fingers[i];
  419. if (finger->state) {
  420. dst.x = (float)ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
  421. dst.x += touchpad_area.x + finger->x * touchpad_area.w;
  422. dst.x -= ctx->button_width / 2;
  423. dst.y = (float)ctx->y + ctx->gamepad_height;
  424. dst.y += touchpad_area.y + finger->y * touchpad_area.h;
  425. dst.y -= ctx->button_height / 2;
  426. dst.w = (float)ctx->button_width;
  427. dst.h = (float)ctx->button_height;
  428. SDL_SetTextureAlphaMod(ctx->button_texture, (Uint8)(finger->pressure * SDL_ALPHA_OPAQUE));
  429. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  430. SDL_SetTextureAlphaMod(ctx->button_texture, SDL_ALPHA_OPAQUE);
  431. }
  432. }
  433. }
  434. }
  435. void DestroyGamepadImage(GamepadImage *ctx)
  436. {
  437. if (ctx) {
  438. int i;
  439. SDL_DestroyTexture(ctx->front_texture);
  440. SDL_DestroyTexture(ctx->back_texture);
  441. for (i = 0; i < SDL_arraysize(ctx->battery_texture); ++i) {
  442. SDL_DestroyTexture(ctx->battery_texture[i]);
  443. }
  444. SDL_DestroyTexture(ctx->touchpad_texture);
  445. SDL_DestroyTexture(ctx->button_texture);
  446. SDL_DestroyTexture(ctx->axis_texture);
  447. SDL_free(ctx);
  448. }
  449. }
  450. static const char *gamepad_button_names[] = {
  451. "A",
  452. "B",
  453. "X",
  454. "Y",
  455. "Back",
  456. "Guide",
  457. "Start",
  458. "Left Stick",
  459. "Right Stick",
  460. "Left Shoulder",
  461. "Right Shoulder",
  462. "DPAD Up",
  463. "DPAD Down",
  464. "DPAD Left",
  465. "DPAD Right",
  466. "Misc1",
  467. "Paddle1",
  468. "Paddle2",
  469. "Paddle3",
  470. "Paddle4",
  471. "Touchpad",
  472. };
  473. SDL_COMPILE_TIME_ASSERT(gamepad_button_names, SDL_arraysize(gamepad_button_names) == SDL_GAMEPAD_BUTTON_MAX);
  474. static const char *gamepad_axis_names[] = {
  475. "LeftX",
  476. "RightX",
  477. "RightX",
  478. "RightY",
  479. "Left Trigger",
  480. "Right Trigger",
  481. };
  482. SDL_COMPILE_TIME_ASSERT(gamepad_axis_names, SDL_arraysize(gamepad_axis_names) == SDL_GAMEPAD_AXIS_MAX);
  483. struct GamepadDisplay
  484. {
  485. SDL_Renderer *renderer;
  486. SDL_Texture *button_texture;
  487. SDL_Texture *arrow_texture;
  488. int button_width;
  489. int button_height;
  490. int arrow_width;
  491. int arrow_height;
  492. float accel_data[3];
  493. float gyro_data[3];
  494. Uint64 last_sensor_update;
  495. SDL_Rect area;
  496. };
  497. GamepadDisplay *CreateGamepadDisplay(SDL_Renderer *renderer)
  498. {
  499. GamepadDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
  500. if (ctx) {
  501. ctx->renderer = renderer;
  502. ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
  503. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  504. ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
  505. SDL_QueryTexture(ctx->arrow_texture, NULL, NULL, &ctx->arrow_width, &ctx->arrow_height);
  506. }
  507. return ctx;
  508. }
  509. void SetGamepadDisplayArea(GamepadDisplay *ctx, int x, int y, int w, int h)
  510. {
  511. if (!ctx) {
  512. return;
  513. }
  514. ctx->area.x = x;
  515. ctx->area.y = y;
  516. ctx->area.w = w;
  517. ctx->area.h = h;
  518. }
  519. void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
  520. {
  521. float x, y;
  522. int i;
  523. char text[128];
  524. const float margin = 8.0f;
  525. const float center = ctx->area.w / 2.0f;
  526. const float arrow_extent = 48.0f;
  527. SDL_FRect dst, rect;
  528. Uint8 r, g, b, a;
  529. SDL_bool has_accel;
  530. SDL_bool has_gyro;
  531. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  532. x = ctx->area.x + margin;
  533. y = ctx->area.y + margin;
  534. for (i = 0; i < SDL_GAMEPAD_BUTTON_MAX; ++i) {
  535. if (SDL_GamepadHasButton(gamepad, (SDL_GamepadButton)i)) {
  536. SDL_snprintf(text, sizeof(text), "%s:", gamepad_button_names[i]);
  537. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  538. if (SDL_GetGamepadButton(gamepad, (SDL_GamepadButton)i)) {
  539. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  540. } else {
  541. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  542. }
  543. dst.x = x + center + 2.0f;
  544. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  545. dst.w = (float)ctx->button_width;
  546. dst.h = (float)ctx->button_height;
  547. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  548. y += ctx->button_height + 2.0f;
  549. }
  550. }
  551. for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
  552. if (SDL_GamepadHasAxis(gamepad, (SDL_GamepadAxis)i)) {
  553. SDL_bool has_negative = (i != SDL_GAMEPAD_AXIS_LEFT_TRIGGER && i != SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
  554. Sint16 value = SDL_GetGamepadAxis(gamepad, (SDL_GamepadAxis)i);
  555. SDL_snprintf(text, sizeof(text), "%s:", gamepad_axis_names[i]);
  556. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  557. dst.x = x + center + 2.0f;
  558. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  559. dst.w = (float)ctx->arrow_width;
  560. dst.h = (float)ctx->arrow_height;
  561. if (has_negative) {
  562. if (value == SDL_MIN_SINT16) {
  563. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  564. } else {
  565. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  566. }
  567. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  568. }
  569. dst.x += (float)ctx->arrow_width;
  570. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  571. rect.x = dst.x + arrow_extent - 2.0f;
  572. rect.y = dst.y;
  573. rect.w = 4.0f;
  574. rect.h = (float)ctx->arrow_height;
  575. SDL_RenderFillRect(ctx->renderer, &rect);
  576. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  577. if (value < 0) {
  578. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  579. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  580. rect.x = dst.x + arrow_extent - rect.w;
  581. rect.y = dst.y + ctx->arrow_height * 0.25f;
  582. rect.h = ctx->arrow_height / 2.0f;
  583. SDL_RenderFillRect(ctx->renderer, &rect);
  584. }
  585. dst.x += arrow_extent;
  586. if (value > 0) {
  587. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  588. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  589. rect.x = dst.x;
  590. rect.y = dst.y + ctx->arrow_height * 0.25f;
  591. rect.h = ctx->arrow_height / 2.0f;
  592. SDL_RenderFillRect(ctx->renderer, &rect);
  593. }
  594. dst.x += arrow_extent;
  595. if (value == SDL_MAX_SINT16) {
  596. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  597. } else {
  598. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  599. }
  600. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  601. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  602. y += ctx->button_height + 2;
  603. }
  604. }
  605. if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
  606. int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
  607. for (i = 0; i < num_fingers; ++i) {
  608. Uint8 state;
  609. float finger_x, finger_y, finger_pressure;
  610. if (SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &state, &finger_x, &finger_y, &finger_pressure) < 0) {
  611. continue;
  612. }
  613. SDL_snprintf(text, sizeof(text), "Touch finger %d:", i);
  614. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  615. if (state) {
  616. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  617. } else {
  618. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  619. }
  620. dst.x = x + center + 2.0f;
  621. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  622. dst.w = (float)ctx->button_width;
  623. dst.h = (float)ctx->button_height;
  624. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  625. if (state) {
  626. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f)", finger_x, finger_y);
  627. SDLTest_DrawString(ctx->renderer, x + center + ctx->button_width + 4.0f, y, text);
  628. }
  629. y += ctx->button_height + 2.0f;
  630. }
  631. }
  632. has_accel = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_ACCEL);
  633. has_gyro = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_GYRO);
  634. if (has_accel || has_gyro) {
  635. const int SENSOR_UPDATE_INTERVAL_MS = 100;
  636. Uint64 now = SDL_GetTicks();
  637. if (now >= ctx->last_sensor_update + SENSOR_UPDATE_INTERVAL_MS) {
  638. if (has_accel) {
  639. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_ACCEL, ctx->accel_data, SDL_arraysize(ctx->accel_data));
  640. }
  641. if (has_gyro) {
  642. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_GYRO, ctx->gyro_data, SDL_arraysize(ctx->gyro_data));
  643. }
  644. ctx->last_sensor_update = now;
  645. }
  646. if (has_accel) {
  647. SDL_strlcpy(text, "Accelerometer:", sizeof(text));
  648. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  649. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->accel_data[0], ctx->accel_data[1], ctx->accel_data[2]);
  650. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  651. y += ctx->button_height + 2.0f;
  652. }
  653. if (has_gyro) {
  654. SDL_strlcpy(text, "Gyro:", sizeof(text));
  655. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  656. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->gyro_data[0], ctx->gyro_data[1], ctx->gyro_data[2]);
  657. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  658. y += ctx->button_height + 2.0f;
  659. }
  660. }
  661. }
  662. void DestroyGamepadDisplay(GamepadDisplay *ctx)
  663. {
  664. SDL_free(ctx);
  665. }
  666. struct JoystickDisplay
  667. {
  668. SDL_Renderer *renderer;
  669. SDL_Texture *button_texture;
  670. SDL_Texture *arrow_texture;
  671. int button_width;
  672. int button_height;
  673. int arrow_width;
  674. int arrow_height;
  675. SDL_Rect area;
  676. };
  677. JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer)
  678. {
  679. JoystickDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
  680. if (ctx) {
  681. ctx->renderer = renderer;
  682. ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
  683. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  684. ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
  685. SDL_QueryTexture(ctx->arrow_texture, NULL, NULL, &ctx->arrow_width, &ctx->arrow_height);
  686. }
  687. return ctx;
  688. }
  689. void SetJoystickDisplayArea(JoystickDisplay *ctx, int x, int y, int w, int h)
  690. {
  691. if (!ctx) {
  692. return;
  693. }
  694. ctx->area.x = x;
  695. ctx->area.y = y;
  696. ctx->area.w = w;
  697. ctx->area.h = h;
  698. }
  699. void RenderJoystickDisplay(JoystickDisplay *ctx, SDL_Joystick *joystick)
  700. {
  701. float x, y;
  702. int i;
  703. int nbuttons = SDL_GetNumJoystickButtons(joystick);
  704. int naxes = SDL_GetNumJoystickAxes(joystick);
  705. int nhats = SDL_GetNumJoystickHats(joystick);
  706. char text[32];
  707. const float margin = 8.0f;
  708. const float center = 80.0f;
  709. const float arrow_extent = 48.0f;
  710. SDL_FRect dst, rect;
  711. Uint8 r, g, b, a;
  712. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  713. x = (float)ctx->area.x + margin;
  714. y = (float)ctx->area.y + margin;
  715. if (nbuttons > 0) {
  716. SDLTest_DrawString(ctx->renderer, x, y, "BUTTONS");
  717. y += FONT_LINE_HEIGHT + 2;
  718. for (i = 0; i < nbuttons; ++i) {
  719. SDL_snprintf(text, sizeof(text), "%2.d:", i);
  720. SDLTest_DrawString(ctx->renderer, x, y, text);
  721. if (SDL_GetJoystickButton(joystick, (Uint8)i)) {
  722. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  723. } else {
  724. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  725. }
  726. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  727. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  728. dst.w = (float)ctx->button_width;
  729. dst.h = (float)ctx->button_height;
  730. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  731. y += ctx->button_height + 2;
  732. }
  733. }
  734. x = (float)ctx->area.x + margin + center + margin;
  735. y = (float)ctx->area.y + margin;
  736. if (naxes > 0) {
  737. SDLTest_DrawString(ctx->renderer, x, y, "AXES");
  738. y += FONT_LINE_HEIGHT + 2;
  739. for (i = 0; i < naxes; ++i) {
  740. Sint16 value = SDL_GetJoystickAxis(joystick, i);
  741. SDL_snprintf(text, sizeof(text), "%d:", i);
  742. SDLTest_DrawString(ctx->renderer, x, y, text);
  743. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2.0f;
  744. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  745. dst.w = (float)ctx->arrow_width;
  746. dst.h = (float)ctx->arrow_height;
  747. if (value == SDL_MIN_SINT16) {
  748. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  749. } else {
  750. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  751. }
  752. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  753. dst.x += (float)ctx->arrow_width;
  754. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  755. rect.x = dst.x + arrow_extent - 2.0f;
  756. rect.y = dst.y;
  757. rect.w = 4.0f;
  758. rect.h = (float)ctx->arrow_height;
  759. SDL_RenderFillRect(ctx->renderer, &rect);
  760. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  761. if (value < 0) {
  762. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  763. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  764. rect.x = dst.x + arrow_extent - rect.w;
  765. rect.y = dst.y + ctx->arrow_height * 0.25f;
  766. rect.h = ctx->arrow_height / 2.0f;
  767. SDL_RenderFillRect(ctx->renderer, &rect);
  768. }
  769. dst.x += arrow_extent;
  770. if (value > 0) {
  771. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  772. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  773. rect.x = dst.x;
  774. rect.y = dst.y + ctx->arrow_height * 0.25f;
  775. rect.h = ctx->arrow_height / 2.0f;
  776. SDL_RenderFillRect(ctx->renderer, &rect);
  777. }
  778. dst.x += arrow_extent;
  779. if (value == SDL_MAX_SINT16) {
  780. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  781. } else {
  782. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  783. }
  784. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  785. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  786. y += ctx->button_height + 2;
  787. }
  788. }
  789. y += FONT_LINE_HEIGHT + 2;
  790. if (nhats > 0) {
  791. SDLTest_DrawString(ctx->renderer, x, y, "HATS");
  792. y += FONT_LINE_HEIGHT + 2 + 1.5f * ctx->button_height - FONT_CHARACTER_SIZE / 2;
  793. for (i = 0; i < nhats; ++i) {
  794. Uint8 value = SDL_GetJoystickHat(joystick, i);
  795. SDL_snprintf(text, sizeof(text), "%d:", i);
  796. SDLTest_DrawString(ctx->renderer, x, y, text);
  797. if (value & SDL_HAT_LEFT) {
  798. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  799. } else {
  800. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  801. }
  802. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  803. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  804. dst.w = (float)ctx->button_width;
  805. dst.h = (float)ctx->button_height;
  806. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  807. if (value & SDL_HAT_UP) {
  808. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  809. } else {
  810. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  811. }
  812. dst.x += (float)ctx->button_width;
  813. dst.y -= (float)ctx->button_height;
  814. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  815. if (value & SDL_HAT_DOWN) {
  816. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  817. } else {
  818. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  819. }
  820. dst.y += (float)ctx->button_height * 2;
  821. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  822. if (value & SDL_HAT_RIGHT) {
  823. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  824. } else {
  825. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  826. }
  827. dst.x += (float)ctx->button_width;
  828. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  829. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  830. y += 3 * ctx->button_height + 2;
  831. }
  832. }
  833. }
  834. void DestroyJoystickDisplay(JoystickDisplay *ctx)
  835. {
  836. SDL_free(ctx);
  837. }