gamepadutils.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. #include "gamepad_button_background.h"
  27. /* This is indexed by SDL_GamepadButton. */
  28. static const struct
  29. {
  30. int x;
  31. int y;
  32. } button_positions[] = {
  33. { 412, 192 }, /* SDL_GAMEPAD_BUTTON_A */
  34. { 456, 157 }, /* SDL_GAMEPAD_BUTTON_B */
  35. { 367, 157 }, /* SDL_GAMEPAD_BUTTON_X */
  36. { 414, 126 }, /* SDL_GAMEPAD_BUTTON_Y */
  37. { 199, 157 }, /* SDL_GAMEPAD_BUTTON_BACK */
  38. { 257, 153 }, /* SDL_GAMEPAD_BUTTON_GUIDE */
  39. { 314, 157 }, /* SDL_GAMEPAD_BUTTON_START */
  40. { 100, 179 }, /* SDL_GAMEPAD_BUTTON_LEFT_STICK */
  41. { 330, 255 }, /* SDL_GAMEPAD_BUTTON_RIGHT_STICK */
  42. { 102, 65 }, /* SDL_GAMEPAD_BUTTON_LEFT_SHOULDER */
  43. { 421, 61 }, /* SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER */
  44. { 179, 213 }, /* SDL_GAMEPAD_BUTTON_DPAD_UP */
  45. { 179, 274 }, /* SDL_GAMEPAD_BUTTON_DPAD_DOWN */
  46. { 141, 242 }, /* SDL_GAMEPAD_BUTTON_DPAD_LEFT */
  47. { 211, 242 }, /* SDL_GAMEPAD_BUTTON_DPAD_RIGHT */
  48. { 257, 199 }, /* SDL_GAMEPAD_BUTTON_MISC1 */
  49. { 157, 160 }, /* SDL_GAMEPAD_BUTTON_PADDLE1 */
  50. { 355, 160 }, /* SDL_GAMEPAD_BUTTON_PADDLE2 */
  51. { 157, 200 }, /* SDL_GAMEPAD_BUTTON_PADDLE3 */
  52. { 355, 200 }, /* SDL_GAMEPAD_BUTTON_PADDLE4 */
  53. };
  54. /* This is indexed by SDL_GamepadAxis. */
  55. static const struct
  56. {
  57. int x;
  58. int y;
  59. double angle;
  60. } axis_positions[] = {
  61. { 99, 178, 270.0 }, /* LEFTX */
  62. { 99, 178, 0.0 }, /* LEFTY */
  63. { 331, 256, 270.0 }, /* RIGHTX */
  64. { 331, 256, 0.0 }, /* RIGHTY */
  65. { 116, 5, 0.0 }, /* TRIGGERLEFT */
  66. { 400, 5, 0.0 }, /* TRIGGERRIGHT */
  67. };
  68. static SDL_Rect touchpad_area = {
  69. 148, 20, 216, 118
  70. };
  71. typedef struct
  72. {
  73. Uint8 state;
  74. float x;
  75. float y;
  76. float pressure;
  77. } GamepadTouchpadFinger;
  78. struct GamepadImage
  79. {
  80. SDL_Renderer *renderer;
  81. SDL_Texture *front_texture;
  82. SDL_Texture *back_texture;
  83. SDL_Texture *battery_texture[1 + SDL_JOYSTICK_POWER_MAX];
  84. SDL_Texture *touchpad_texture;
  85. SDL_Texture *button_texture;
  86. SDL_Texture *axis_texture;
  87. int gamepad_width;
  88. int gamepad_height;
  89. int battery_width;
  90. int battery_height;
  91. int touchpad_width;
  92. int touchpad_height;
  93. int button_width;
  94. int button_height;
  95. int axis_width;
  96. int axis_height;
  97. int x;
  98. int y;
  99. SDL_bool showing_front;
  100. SDL_bool showing_battery;
  101. SDL_bool showing_touchpad;
  102. SDL_bool buttons[SDL_GAMEPAD_BUTTON_MAX];
  103. int axes[SDL_GAMEPAD_AXIS_MAX];
  104. SDL_JoystickPowerLevel battery_level;
  105. int num_fingers;
  106. GamepadTouchpadFinger *fingers;
  107. };
  108. static SDL_Texture *CreateTexture(SDL_Renderer *renderer, unsigned char *data, unsigned int len)
  109. {
  110. SDL_Texture *texture = NULL;
  111. SDL_Surface *surface;
  112. SDL_RWops *src = SDL_RWFromConstMem(data, len);
  113. if (src) {
  114. surface = SDL_LoadBMP_RW(src, SDL_TRUE);
  115. if (surface) {
  116. texture = SDL_CreateTextureFromSurface(renderer, surface);
  117. SDL_DestroySurface(surface);
  118. }
  119. }
  120. return texture;
  121. }
  122. GamepadImage *CreateGamepadImage(SDL_Renderer *renderer)
  123. {
  124. GamepadImage *ctx = SDL_calloc(1, sizeof(*ctx));
  125. if (ctx) {
  126. ctx->renderer = renderer;
  127. ctx->front_texture = CreateTexture(renderer, gamepad_front_bmp, gamepad_front_bmp_len);
  128. ctx->back_texture = CreateTexture(renderer, gamepad_back_bmp, gamepad_back_bmp_len);
  129. SDL_QueryTexture(ctx->front_texture, NULL, NULL, &ctx->gamepad_width, &ctx->gamepad_height);
  130. ctx->battery_texture[1 + SDL_JOYSTICK_POWER_UNKNOWN] = CreateTexture(renderer, gamepad_battery_unknown_bmp, gamepad_battery_unknown_bmp_len);
  131. ctx->battery_texture[1 + SDL_JOYSTICK_POWER_EMPTY] = CreateTexture(renderer, gamepad_battery_empty_bmp, gamepad_battery_empty_bmp_len);
  132. ctx->battery_texture[1 + SDL_JOYSTICK_POWER_LOW] = CreateTexture(renderer, gamepad_battery_low_bmp, gamepad_battery_low_bmp_len);
  133. ctx->battery_texture[1 + SDL_JOYSTICK_POWER_MEDIUM] = CreateTexture(renderer, gamepad_battery_medium_bmp, gamepad_battery_medium_bmp_len);
  134. ctx->battery_texture[1 + SDL_JOYSTICK_POWER_FULL] = CreateTexture(renderer, gamepad_battery_full_bmp, gamepad_battery_full_bmp_len);
  135. ctx->battery_texture[1 + SDL_JOYSTICK_POWER_WIRED] = CreateTexture(renderer, gamepad_battery_wired_bmp, gamepad_battery_wired_bmp_len);
  136. SDL_QueryTexture(ctx->battery_texture[1 + SDL_JOYSTICK_POWER_UNKNOWN], NULL, NULL, &ctx->battery_width, &ctx->battery_height);
  137. ctx->touchpad_texture = CreateTexture(renderer, gamepad_touchpad_bmp, gamepad_touchpad_bmp_len);
  138. SDL_QueryTexture(ctx->touchpad_texture, NULL, NULL, &ctx->touchpad_width, &ctx->touchpad_height);
  139. ctx->button_texture = CreateTexture(renderer, gamepad_button_bmp, gamepad_button_bmp_len);
  140. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  141. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  142. ctx->axis_texture = CreateTexture(renderer, gamepad_axis_bmp, gamepad_axis_bmp_len);
  143. SDL_QueryTexture(ctx->axis_texture, NULL, NULL, &ctx->axis_width, &ctx->axis_height);
  144. SDL_SetTextureColorMod(ctx->axis_texture, 10, 255, 21);
  145. ctx->showing_front = SDL_TRUE;
  146. }
  147. return ctx;
  148. }
  149. void SetGamepadImagePosition(GamepadImage *ctx, int x, int y)
  150. {
  151. if (!ctx) {
  152. return;
  153. }
  154. ctx->x = x;
  155. ctx->y = y;
  156. }
  157. void SetGamepadImageShowingFront(GamepadImage *ctx, SDL_bool showing_front)
  158. {
  159. if (!ctx) {
  160. return;
  161. }
  162. ctx->showing_front = showing_front;
  163. }
  164. void SetGamepadImageShowingBattery(GamepadImage *ctx, SDL_bool showing_battery)
  165. {
  166. if (!ctx) {
  167. return;
  168. }
  169. ctx->showing_battery = showing_battery;
  170. }
  171. void SetGamepadImageShowingTouchpad(GamepadImage *ctx, SDL_bool showing_touchpad)
  172. {
  173. if (!ctx) {
  174. return;
  175. }
  176. ctx->showing_touchpad = showing_touchpad;
  177. }
  178. void GetGamepadImageArea(GamepadImage *ctx, int *x, int *y, int *width, int *height)
  179. {
  180. if (!ctx) {
  181. if (x) {
  182. *x = 0;
  183. }
  184. if (y) {
  185. *y = 0;
  186. }
  187. if (width) {
  188. *width = 0;
  189. }
  190. if (height) {
  191. *height = 0;
  192. }
  193. return;
  194. }
  195. if (x) {
  196. *x = ctx->x;
  197. }
  198. if (y) {
  199. *y = ctx->y;
  200. }
  201. if (width) {
  202. *width = ctx->gamepad_width;
  203. }
  204. if (height) {
  205. *height = ctx->gamepad_height;
  206. if (ctx->showing_touchpad) {
  207. *height += ctx->touchpad_height;
  208. }
  209. }
  210. }
  211. int GetGamepadImageButtonWidth(GamepadImage *ctx)
  212. {
  213. if (!ctx) {
  214. return 0;
  215. }
  216. return ctx->button_width;
  217. }
  218. int GetGamepadImageButtonHeight(GamepadImage *ctx)
  219. {
  220. if (!ctx) {
  221. return 0;
  222. }
  223. return ctx->button_height;
  224. }
  225. int GetGamepadImageAxisWidth(GamepadImage *ctx)
  226. {
  227. if (!ctx) {
  228. return 0;
  229. }
  230. return ctx->axis_width;
  231. }
  232. int GetGamepadImageAxisHeight(GamepadImage *ctx)
  233. {
  234. if (!ctx) {
  235. return 0;
  236. }
  237. return ctx->axis_height;
  238. }
  239. SDL_GamepadButton GetGamepadImageButtonAt(GamepadImage *ctx, float x, float y)
  240. {
  241. SDL_FPoint point;
  242. int i;
  243. if (!ctx) {
  244. return SDL_GAMEPAD_BUTTON_INVALID;
  245. }
  246. point.x = x;
  247. point.y = y;
  248. for (i = 0; i < SDL_arraysize(button_positions); ++i) {
  249. SDL_bool on_front = SDL_TRUE;
  250. if (i >= SDL_GAMEPAD_BUTTON_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_PADDLE4) {
  251. on_front = SDL_FALSE;
  252. }
  253. if (on_front == ctx->showing_front) {
  254. SDL_FRect rect;
  255. rect.x = (float)ctx->x + button_positions[i].x - ctx->button_width / 2;
  256. rect.y = (float)ctx->y + button_positions[i].y - ctx->button_height / 2;
  257. rect.w = (float)ctx->button_width;
  258. rect.h = (float)ctx->button_height;
  259. if (SDL_PointInRectFloat(&point, &rect)) {
  260. return (SDL_GamepadButton)i;
  261. }
  262. }
  263. }
  264. return SDL_GAMEPAD_BUTTON_INVALID;
  265. }
  266. SDL_GamepadAxis GetGamepadImageAxisAt(GamepadImage *ctx, float x, float y)
  267. {
  268. SDL_FPoint point;
  269. int i;
  270. if (!ctx) {
  271. return SDL_GAMEPAD_AXIS_INVALID;
  272. }
  273. point.x = x;
  274. point.y = y;
  275. if (ctx->showing_front) {
  276. for (i = 0; i < SDL_arraysize(axis_positions); ++i) {
  277. SDL_FRect rect;
  278. rect.x = (float)ctx->x + axis_positions[i].x - ctx->axis_width / 2;
  279. rect.y = (float)ctx->y + axis_positions[i].y - ctx->axis_height / 2;
  280. rect.w = (float)ctx->axis_width;
  281. rect.h = (float)ctx->axis_height;
  282. if (SDL_PointInRectFloat(&point, &rect)) {
  283. return (SDL_GamepadAxis)i;
  284. }
  285. }
  286. }
  287. return SDL_GAMEPAD_AXIS_INVALID;
  288. }
  289. void ClearGamepadImage(GamepadImage *ctx)
  290. {
  291. if (!ctx) {
  292. return;
  293. }
  294. SDL_zeroa(ctx->buttons);
  295. SDL_zeroa(ctx->axes);
  296. }
  297. void SetGamepadImageButton(GamepadImage *ctx, SDL_GamepadButton button, SDL_bool active)
  298. {
  299. if (!ctx) {
  300. return;
  301. }
  302. ctx->buttons[button] = active;
  303. }
  304. void SetGamepadImageAxis(GamepadImage *ctx, SDL_GamepadAxis axis, int direction)
  305. {
  306. if (!ctx) {
  307. return;
  308. }
  309. ctx->axes[axis] = direction;
  310. }
  311. void UpdateGamepadImageFromGamepad(GamepadImage *ctx, SDL_Gamepad *gamepad)
  312. {
  313. int i;
  314. if (!ctx) {
  315. return;
  316. }
  317. for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
  318. const SDL_GamepadButton button = (SDL_GamepadButton)i;
  319. if (SDL_GetGamepadButton(gamepad, button) == SDL_PRESSED) {
  320. SetGamepadImageButton(ctx, button, SDL_TRUE);
  321. } else {
  322. SetGamepadImageButton(ctx, button, SDL_FALSE);
  323. }
  324. }
  325. for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
  326. const SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
  327. const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
  328. const Sint16 value = SDL_GetGamepadAxis(gamepad, axis);
  329. if (value < -deadzone) {
  330. SetGamepadImageAxis(ctx, axis, -1);
  331. } else if (value > deadzone) {
  332. SetGamepadImageAxis(ctx, axis, 1);
  333. } else {
  334. SetGamepadImageAxis(ctx, axis, 0);
  335. }
  336. }
  337. ctx->battery_level = SDL_GetGamepadPowerLevel(gamepad);
  338. if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
  339. int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
  340. if (num_fingers != ctx->num_fingers) {
  341. GamepadTouchpadFinger *fingers = (GamepadTouchpadFinger *)SDL_realloc(ctx->fingers, num_fingers * sizeof(*fingers));
  342. if (fingers) {
  343. ctx->fingers = fingers;
  344. ctx->num_fingers = num_fingers;
  345. } else {
  346. num_fingers = SDL_min(ctx->num_fingers, num_fingers);
  347. }
  348. }
  349. for (i = 0; i < num_fingers; ++i) {
  350. GamepadTouchpadFinger *finger = &ctx->fingers[i];
  351. SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &finger->state, &finger->x, &finger->y, &finger->pressure);
  352. }
  353. } else {
  354. if (ctx->fingers) {
  355. SDL_free(ctx->fingers);
  356. ctx->fingers = NULL;
  357. ctx->num_fingers = 0;
  358. }
  359. }
  360. }
  361. void RenderGamepadImage(GamepadImage *ctx)
  362. {
  363. SDL_FRect dst;
  364. int i;
  365. if (!ctx) {
  366. return;
  367. }
  368. dst.x = (float)ctx->x;
  369. dst.y = (float)ctx->y;
  370. dst.w = (float)ctx->gamepad_width;
  371. dst.h = (float)ctx->gamepad_height;
  372. if (ctx->showing_front) {
  373. SDL_RenderTexture(ctx->renderer, ctx->front_texture, NULL, &dst);
  374. } else {
  375. SDL_RenderTexture(ctx->renderer, ctx->back_texture, NULL, &dst);
  376. }
  377. for (i = 0; i < SDL_arraysize(button_positions); ++i) {
  378. if (ctx->buttons[i]) {
  379. SDL_bool on_front = SDL_TRUE;
  380. if (i >= SDL_GAMEPAD_BUTTON_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_PADDLE4) {
  381. on_front = SDL_FALSE;
  382. }
  383. if (on_front == ctx->showing_front) {
  384. dst.x = (float)ctx->x + button_positions[i].x - ctx->button_width / 2;
  385. dst.y = (float)ctx->y + button_positions[i].y - ctx->button_height / 2;
  386. dst.w = (float)ctx->button_width;
  387. dst.h = (float)ctx->button_height;
  388. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  389. }
  390. }
  391. }
  392. if (ctx->showing_front) {
  393. for (i = 0; i < SDL_arraysize(axis_positions); ++i) {
  394. if (ctx->axes[i] < 0) {
  395. const double angle = axis_positions[i].angle;
  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. } else if (ctx->axes[i] > 0) {
  402. const double angle = axis_positions[i].angle + 180.0;
  403. dst.x = (float)ctx->x + axis_positions[i].x - ctx->axis_width / 2;
  404. dst.y = (float)ctx->y + axis_positions[i].y - ctx->axis_height / 2;
  405. dst.w = (float)ctx->axis_width;
  406. dst.h = (float)ctx->axis_height;
  407. SDL_RenderTextureRotated(ctx->renderer, ctx->axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  408. }
  409. }
  410. }
  411. if (ctx->showing_battery) {
  412. dst.x = (float)ctx->x + ctx->gamepad_width - ctx->battery_width;
  413. dst.y = (float)ctx->y;
  414. dst.w = (float)ctx->battery_width;
  415. dst.h = (float)ctx->battery_height;
  416. SDL_RenderTexture(ctx->renderer, ctx->battery_texture[1 + ctx->battery_level], NULL, &dst);
  417. }
  418. if (ctx->showing_touchpad) {
  419. dst.x = (float)ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
  420. dst.y = (float)ctx->y + ctx->gamepad_height;
  421. dst.w = (float)ctx->touchpad_width;
  422. dst.h = (float)ctx->touchpad_height;
  423. SDL_RenderTexture(ctx->renderer, ctx->touchpad_texture, NULL, &dst);
  424. for (i = 0; i < ctx->num_fingers; ++i) {
  425. GamepadTouchpadFinger *finger = &ctx->fingers[i];
  426. if (finger->state) {
  427. dst.x = (float)ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
  428. dst.x += touchpad_area.x + finger->x * touchpad_area.w;
  429. dst.x -= ctx->button_width / 2;
  430. dst.y = (float)ctx->y + ctx->gamepad_height;
  431. dst.y += touchpad_area.y + finger->y * touchpad_area.h;
  432. dst.y -= ctx->button_height / 2;
  433. dst.w = (float)ctx->button_width;
  434. dst.h = (float)ctx->button_height;
  435. SDL_SetTextureAlphaMod(ctx->button_texture, (Uint8)(finger->pressure * SDL_ALPHA_OPAQUE));
  436. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  437. SDL_SetTextureAlphaMod(ctx->button_texture, SDL_ALPHA_OPAQUE);
  438. }
  439. }
  440. }
  441. }
  442. void DestroyGamepadImage(GamepadImage *ctx)
  443. {
  444. if (ctx) {
  445. int i;
  446. SDL_DestroyTexture(ctx->front_texture);
  447. SDL_DestroyTexture(ctx->back_texture);
  448. for (i = 0; i < SDL_arraysize(ctx->battery_texture); ++i) {
  449. SDL_DestroyTexture(ctx->battery_texture[i]);
  450. }
  451. SDL_DestroyTexture(ctx->touchpad_texture);
  452. SDL_DestroyTexture(ctx->button_texture);
  453. SDL_DestroyTexture(ctx->axis_texture);
  454. SDL_free(ctx);
  455. }
  456. }
  457. static const char *gamepad_button_names[] = {
  458. "A",
  459. "B",
  460. "X",
  461. "Y",
  462. "Back",
  463. "Guide",
  464. "Start",
  465. "Left Stick",
  466. "Right Stick",
  467. "Left Shoulder",
  468. "Right Shoulder",
  469. "DPAD Up",
  470. "DPAD Down",
  471. "DPAD Left",
  472. "DPAD Right",
  473. "Misc1",
  474. "Paddle1",
  475. "Paddle2",
  476. "Paddle3",
  477. "Paddle4",
  478. "Touchpad",
  479. };
  480. SDL_COMPILE_TIME_ASSERT(gamepad_button_names, SDL_arraysize(gamepad_button_names) == SDL_GAMEPAD_BUTTON_MAX);
  481. static const char *gamepad_axis_names[] = {
  482. "LeftX",
  483. "RightX",
  484. "RightX",
  485. "RightY",
  486. "Left Trigger",
  487. "Right Trigger",
  488. };
  489. SDL_COMPILE_TIME_ASSERT(gamepad_axis_names, SDL_arraysize(gamepad_axis_names) == SDL_GAMEPAD_AXIS_MAX);
  490. struct GamepadDisplay
  491. {
  492. SDL_Renderer *renderer;
  493. SDL_Texture *button_texture;
  494. SDL_Texture *arrow_texture;
  495. int button_width;
  496. int button_height;
  497. int arrow_width;
  498. int arrow_height;
  499. float accel_data[3];
  500. float gyro_data[3];
  501. Uint64 last_sensor_update;
  502. SDL_Rect area;
  503. };
  504. GamepadDisplay *CreateGamepadDisplay(SDL_Renderer *renderer)
  505. {
  506. GamepadDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
  507. if (ctx) {
  508. ctx->renderer = renderer;
  509. ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
  510. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  511. ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
  512. SDL_QueryTexture(ctx->arrow_texture, NULL, NULL, &ctx->arrow_width, &ctx->arrow_height);
  513. }
  514. return ctx;
  515. }
  516. void SetGamepadDisplayArea(GamepadDisplay *ctx, int x, int y, int w, int h)
  517. {
  518. if (!ctx) {
  519. return;
  520. }
  521. ctx->area.x = x;
  522. ctx->area.y = y;
  523. ctx->area.w = w;
  524. ctx->area.h = h;
  525. }
  526. static SDL_bool GetBindingString(const char *label, char *mapping, char *text, size_t size)
  527. {
  528. char *key;
  529. char *value, *end;
  530. size_t length;
  531. *text = '\0';
  532. if (!mapping) {
  533. return SDL_FALSE;
  534. }
  535. key = SDL_strstr(mapping, label);
  536. if (key) {
  537. value = key + SDL_strlen(label);
  538. end = SDL_strchr(value, ',');
  539. if (end) {
  540. length = (end - value);
  541. } else {
  542. length = SDL_strlen(value);
  543. }
  544. if (length >= size) {
  545. length = size - 1;
  546. }
  547. SDL_memcpy(text, value, length);
  548. text[length] = '\0';
  549. }
  550. return *text ? SDL_TRUE : SDL_FALSE;
  551. }
  552. static SDL_bool GetButtonBindingString(SDL_GamepadButton button, char *mapping, char *text, size_t size)
  553. {
  554. char label[32];
  555. SDL_snprintf(label, sizeof(label), "%s:", SDL_GetGamepadStringForButton(button));
  556. return GetBindingString(label, mapping, text, size);
  557. }
  558. static SDL_bool GetAxisBindingString(SDL_GamepadAxis axis, int direction, char *mapping, char *text, size_t size)
  559. {
  560. char label[32];
  561. /* Check for explicit half-axis */
  562. if (direction < 0) {
  563. SDL_snprintf(label, sizeof(label), "-%s:", SDL_GetGamepadStringForAxis(axis));
  564. } else {
  565. SDL_snprintf(label, sizeof(label), "+%s:", SDL_GetGamepadStringForAxis(axis));
  566. }
  567. if (GetBindingString(label, mapping, text, size)) {
  568. return SDL_TRUE;
  569. }
  570. /* Get the binding for the whole axis and split it if necessary */
  571. SDL_snprintf(label, sizeof(label), "%s:", SDL_GetGamepadStringForAxis(axis));
  572. if (!GetBindingString(label, mapping, text, size)) {
  573. return SDL_FALSE;
  574. }
  575. if (axis != SDL_GAMEPAD_AXIS_LEFT_TRIGGER && axis != SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
  576. if (*text == 'a') {
  577. /* Split the axis */
  578. size_t length = SDL_strlen(text) + 1;
  579. if ((length + 1) <= size) {
  580. SDL_memmove(text + 1, text, length);
  581. if (direction > 0) {
  582. *text = '+';
  583. } else {
  584. *text = '-';
  585. }
  586. }
  587. } else if (*text == '~') {
  588. /* Invert directions and split the axis */
  589. if (direction > 0) {
  590. *text = '-';
  591. } else {
  592. *text = '+';
  593. }
  594. }
  595. }
  596. return SDL_TRUE;
  597. }
  598. void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
  599. {
  600. float x, y;
  601. int i;
  602. char text[128], binding[32];
  603. const float margin = 8.0f;
  604. const float center = ctx->area.w / 2.0f;
  605. const float arrow_extent = 48.0f;
  606. SDL_FRect dst, rect;
  607. Uint8 r, g, b, a;
  608. char *mapping;
  609. SDL_bool has_accel;
  610. SDL_bool has_gyro;
  611. if (!ctx) {
  612. return;
  613. }
  614. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  615. mapping = SDL_GetGamepadMapping(gamepad);
  616. x = ctx->area.x + margin;
  617. y = ctx->area.y + margin;
  618. for (i = 0; i < SDL_GAMEPAD_BUTTON_MAX; ++i) {
  619. SDL_GamepadButton button = (SDL_GamepadButton)i;
  620. SDL_snprintf(text, sizeof(text), "%s:", gamepad_button_names[i]);
  621. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  622. if (SDL_GetGamepadButton(gamepad, button)) {
  623. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  624. } else {
  625. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  626. }
  627. dst.x = x + center + 2.0f;
  628. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  629. dst.w = (float)ctx->button_width;
  630. dst.h = (float)ctx->button_height;
  631. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  632. if (GetButtonBindingString(button, mapping, binding, sizeof(binding))) {
  633. dst.x += dst.w + 2 * margin;
  634. SDLTest_DrawString(ctx->renderer, dst.x, y, binding);
  635. }
  636. y += ctx->button_height + 2.0f;
  637. }
  638. for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
  639. SDL_GamepadAxis axis = (SDL_GamepadAxis)i;
  640. SDL_bool has_negative = (axis != SDL_GAMEPAD_AXIS_LEFT_TRIGGER && axis != SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
  641. Sint16 value = SDL_GetGamepadAxis(gamepad, axis);
  642. SDL_snprintf(text, sizeof(text), "%s:", gamepad_axis_names[i]);
  643. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  644. dst.x = x + center + 2.0f;
  645. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  646. dst.w = (float)ctx->arrow_width;
  647. dst.h = (float)ctx->arrow_height;
  648. if (has_negative) {
  649. if (value == SDL_MIN_SINT16) {
  650. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  651. } else {
  652. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  653. }
  654. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  655. }
  656. dst.x += (float)ctx->arrow_width;
  657. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  658. rect.x = dst.x + arrow_extent - 2.0f;
  659. rect.y = dst.y;
  660. rect.w = 4.0f;
  661. rect.h = (float)ctx->arrow_height;
  662. SDL_RenderFillRect(ctx->renderer, &rect);
  663. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  664. if (value < 0) {
  665. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  666. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  667. rect.x = dst.x + arrow_extent - rect.w;
  668. rect.y = dst.y + ctx->arrow_height * 0.25f;
  669. rect.h = ctx->arrow_height / 2.0f;
  670. SDL_RenderFillRect(ctx->renderer, &rect);
  671. }
  672. if (has_negative && GetAxisBindingString(axis, -1, mapping, binding, sizeof(binding))) {
  673. float text_x;
  674. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  675. text_x = dst.x + arrow_extent / 2 - ((float)FONT_CHARACTER_SIZE * SDL_strlen(binding)) / 2;
  676. SDLTest_DrawString(ctx->renderer, text_x, y, binding);
  677. }
  678. dst.x += arrow_extent;
  679. if (value > 0) {
  680. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  681. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  682. rect.x = dst.x;
  683. rect.y = dst.y + ctx->arrow_height * 0.25f;
  684. rect.h = ctx->arrow_height / 2.0f;
  685. SDL_RenderFillRect(ctx->renderer, &rect);
  686. }
  687. if (GetAxisBindingString(axis, 1, mapping, binding, sizeof(binding))) {
  688. float text_x;
  689. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  690. text_x = dst.x + arrow_extent / 2 - ((float)FONT_CHARACTER_SIZE * SDL_strlen(binding)) / 2;
  691. SDLTest_DrawString(ctx->renderer, text_x, y, binding);
  692. }
  693. dst.x += arrow_extent;
  694. if (value == SDL_MAX_SINT16) {
  695. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  696. } else {
  697. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  698. }
  699. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  700. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  701. y += ctx->button_height + 2;
  702. }
  703. if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
  704. int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
  705. for (i = 0; i < num_fingers; ++i) {
  706. Uint8 state;
  707. float finger_x, finger_y, finger_pressure;
  708. if (SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &state, &finger_x, &finger_y, &finger_pressure) < 0) {
  709. continue;
  710. }
  711. SDL_snprintf(text, sizeof(text), "Touch finger %d:", i);
  712. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  713. if (state) {
  714. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  715. } else {
  716. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  717. }
  718. dst.x = x + center + 2.0f;
  719. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  720. dst.w = (float)ctx->button_width;
  721. dst.h = (float)ctx->button_height;
  722. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  723. if (state) {
  724. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f)", finger_x, finger_y);
  725. SDLTest_DrawString(ctx->renderer, x + center + ctx->button_width + 4.0f, y, text);
  726. }
  727. y += ctx->button_height + 2.0f;
  728. }
  729. }
  730. has_accel = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_ACCEL);
  731. has_gyro = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_GYRO);
  732. if (has_accel || has_gyro) {
  733. const int SENSOR_UPDATE_INTERVAL_MS = 100;
  734. Uint64 now = SDL_GetTicks();
  735. if (now >= ctx->last_sensor_update + SENSOR_UPDATE_INTERVAL_MS) {
  736. if (has_accel) {
  737. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_ACCEL, ctx->accel_data, SDL_arraysize(ctx->accel_data));
  738. }
  739. if (has_gyro) {
  740. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_GYRO, ctx->gyro_data, SDL_arraysize(ctx->gyro_data));
  741. }
  742. ctx->last_sensor_update = now;
  743. }
  744. if (has_accel) {
  745. SDL_strlcpy(text, "Accelerometer:", sizeof(text));
  746. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  747. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->accel_data[0], ctx->accel_data[1], ctx->accel_data[2]);
  748. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  749. y += ctx->button_height + 2.0f;
  750. }
  751. if (has_gyro) {
  752. SDL_strlcpy(text, "Gyro:", sizeof(text));
  753. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  754. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->gyro_data[0], ctx->gyro_data[1], ctx->gyro_data[2]);
  755. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  756. y += ctx->button_height + 2.0f;
  757. }
  758. }
  759. SDL_free(mapping);
  760. }
  761. void DestroyGamepadDisplay(GamepadDisplay *ctx)
  762. {
  763. if (!ctx) {
  764. return;
  765. }
  766. SDL_DestroyTexture(ctx->button_texture);
  767. SDL_DestroyTexture(ctx->arrow_texture);
  768. SDL_free(ctx);
  769. }
  770. struct JoystickDisplay
  771. {
  772. SDL_Renderer *renderer;
  773. SDL_Texture *button_texture;
  774. SDL_Texture *arrow_texture;
  775. int button_width;
  776. int button_height;
  777. int arrow_width;
  778. int arrow_height;
  779. SDL_Rect area;
  780. };
  781. JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer)
  782. {
  783. JoystickDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
  784. if (ctx) {
  785. ctx->renderer = renderer;
  786. ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
  787. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  788. ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
  789. SDL_QueryTexture(ctx->arrow_texture, NULL, NULL, &ctx->arrow_width, &ctx->arrow_height);
  790. }
  791. return ctx;
  792. }
  793. void SetJoystickDisplayArea(JoystickDisplay *ctx, int x, int y, int w, int h)
  794. {
  795. if (!ctx) {
  796. return;
  797. }
  798. ctx->area.x = x;
  799. ctx->area.y = y;
  800. ctx->area.w = w;
  801. ctx->area.h = h;
  802. }
  803. void RenderJoystickDisplay(JoystickDisplay *ctx, SDL_Joystick *joystick)
  804. {
  805. float x, y;
  806. int i;
  807. int nbuttons = SDL_GetNumJoystickButtons(joystick);
  808. int naxes = SDL_GetNumJoystickAxes(joystick);
  809. int nhats = SDL_GetNumJoystickHats(joystick);
  810. char text[32];
  811. const float margin = 8.0f;
  812. const float center = 80.0f;
  813. const float arrow_extent = 48.0f;
  814. SDL_FRect dst, rect;
  815. Uint8 r, g, b, a;
  816. if (!ctx) {
  817. return;
  818. }
  819. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  820. x = (float)ctx->area.x + margin;
  821. y = (float)ctx->area.y + margin;
  822. if (nbuttons > 0) {
  823. SDLTest_DrawString(ctx->renderer, x, y, "BUTTONS");
  824. y += FONT_LINE_HEIGHT + 2;
  825. for (i = 0; i < nbuttons; ++i) {
  826. SDL_snprintf(text, sizeof(text), "%2.d:", i);
  827. SDLTest_DrawString(ctx->renderer, x, y, text);
  828. if (SDL_GetJoystickButton(joystick, (Uint8)i)) {
  829. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  830. } else {
  831. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  832. }
  833. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  834. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  835. dst.w = (float)ctx->button_width;
  836. dst.h = (float)ctx->button_height;
  837. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  838. y += ctx->button_height + 2;
  839. }
  840. }
  841. x = (float)ctx->area.x + margin + center + margin;
  842. y = (float)ctx->area.y + margin;
  843. if (naxes > 0) {
  844. SDLTest_DrawString(ctx->renderer, x, y, "AXES");
  845. y += FONT_LINE_HEIGHT + 2;
  846. for (i = 0; i < naxes; ++i) {
  847. Sint16 value = SDL_GetJoystickAxis(joystick, i);
  848. SDL_snprintf(text, sizeof(text), "%d:", i);
  849. SDLTest_DrawString(ctx->renderer, x, y, text);
  850. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2.0f;
  851. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  852. dst.w = (float)ctx->arrow_width;
  853. dst.h = (float)ctx->arrow_height;
  854. if (value == SDL_MIN_SINT16) {
  855. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  856. } else {
  857. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  858. }
  859. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  860. dst.x += (float)ctx->arrow_width;
  861. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  862. rect.x = dst.x + arrow_extent - 2.0f;
  863. rect.y = dst.y;
  864. rect.w = 4.0f;
  865. rect.h = (float)ctx->arrow_height;
  866. SDL_RenderFillRect(ctx->renderer, &rect);
  867. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  868. if (value < 0) {
  869. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  870. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  871. rect.x = dst.x + arrow_extent - rect.w;
  872. rect.y = dst.y + ctx->arrow_height * 0.25f;
  873. rect.h = ctx->arrow_height / 2.0f;
  874. SDL_RenderFillRect(ctx->renderer, &rect);
  875. }
  876. dst.x += arrow_extent;
  877. if (value > 0) {
  878. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  879. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  880. rect.x = dst.x;
  881. rect.y = dst.y + ctx->arrow_height * 0.25f;
  882. rect.h = ctx->arrow_height / 2.0f;
  883. SDL_RenderFillRect(ctx->renderer, &rect);
  884. }
  885. dst.x += arrow_extent;
  886. if (value == SDL_MAX_SINT16) {
  887. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  888. } else {
  889. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  890. }
  891. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  892. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  893. y += ctx->button_height + 2;
  894. }
  895. }
  896. y += FONT_LINE_HEIGHT + 2;
  897. if (nhats > 0) {
  898. SDLTest_DrawString(ctx->renderer, x, y, "HATS");
  899. y += FONT_LINE_HEIGHT + 2 + 1.5f * ctx->button_height - FONT_CHARACTER_SIZE / 2;
  900. for (i = 0; i < nhats; ++i) {
  901. Uint8 value = SDL_GetJoystickHat(joystick, i);
  902. SDL_snprintf(text, sizeof(text), "%d:", i);
  903. SDLTest_DrawString(ctx->renderer, x, y, text);
  904. if (value & SDL_HAT_LEFT) {
  905. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  906. } else {
  907. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  908. }
  909. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  910. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  911. dst.w = (float)ctx->button_width;
  912. dst.h = (float)ctx->button_height;
  913. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  914. if (value & SDL_HAT_UP) {
  915. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  916. } else {
  917. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  918. }
  919. dst.x += (float)ctx->button_width;
  920. dst.y -= (float)ctx->button_height;
  921. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  922. if (value & SDL_HAT_DOWN) {
  923. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  924. } else {
  925. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  926. }
  927. dst.y += (float)ctx->button_height * 2;
  928. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  929. if (value & SDL_HAT_RIGHT) {
  930. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  931. } else {
  932. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  933. }
  934. dst.x += (float)ctx->button_width;
  935. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  936. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  937. y += 3 * ctx->button_height + 2;
  938. }
  939. }
  940. }
  941. void DestroyJoystickDisplay(JoystickDisplay *ctx)
  942. {
  943. if (!ctx) {
  944. return;
  945. }
  946. SDL_DestroyTexture(ctx->button_texture);
  947. SDL_DestroyTexture(ctx->arrow_texture);
  948. SDL_free(ctx);
  949. }
  950. struct GamepadButton
  951. {
  952. SDL_Renderer *renderer;
  953. SDL_Texture *background;
  954. int background_width;
  955. int background_height;
  956. SDL_FRect area;
  957. char *label;
  958. int label_width;
  959. int label_height;
  960. SDL_bool highlight;
  961. };
  962. GamepadButton *CreateGamepadButton(SDL_Renderer *renderer, const char *label)
  963. {
  964. GamepadButton *ctx = SDL_calloc(1, sizeof(*ctx));
  965. if (ctx) {
  966. ctx->renderer = renderer;
  967. ctx->background = CreateTexture(renderer, gamepad_button_background_bmp, gamepad_button_background_bmp_len);
  968. SDL_QueryTexture(ctx->background, NULL, NULL, &ctx->background_width, &ctx->background_height);
  969. ctx->label = SDL_strdup(label);
  970. ctx->label_width = (int)(FONT_CHARACTER_SIZE * SDL_strlen(label));
  971. ctx->label_height = FONT_CHARACTER_SIZE;
  972. }
  973. return ctx;
  974. }
  975. void SetGamepadButtonArea(GamepadButton *ctx, int x, int y, int w, int h)
  976. {
  977. if (!ctx) {
  978. return;
  979. }
  980. ctx->area.x = (float)x;
  981. ctx->area.y = (float)y;
  982. ctx->area.w = (float)w;
  983. ctx->area.h = (float)h;
  984. }
  985. void SetGamepadButtonHighlight(GamepadButton *ctx, SDL_bool highlight)
  986. {
  987. if (!ctx) {
  988. return;
  989. }
  990. ctx->highlight = highlight;
  991. }
  992. int GetGamepadButtonLabelWidth(GamepadButton *ctx)
  993. {
  994. if (!ctx) {
  995. return 0;
  996. }
  997. return ctx->label_width;
  998. }
  999. int GetGamepadButtonLabelHeight(GamepadButton *ctx)
  1000. {
  1001. if (!ctx) {
  1002. return 0;
  1003. }
  1004. return ctx->label_height;
  1005. }
  1006. SDL_bool GamepadButtonContains(GamepadButton *ctx, float x, float y)
  1007. {
  1008. SDL_FPoint point;
  1009. if (!ctx) {
  1010. return SDL_FALSE;
  1011. }
  1012. point.x = x;
  1013. point.y = y;
  1014. return SDL_PointInRectFloat(&point, &ctx->area);
  1015. }
  1016. void RenderGamepadButton(GamepadButton *ctx)
  1017. {
  1018. SDL_FRect src, dst;
  1019. float one_third_src_width;
  1020. float one_third_src_height;
  1021. if (!ctx) {
  1022. return;
  1023. }
  1024. one_third_src_width = (float)ctx->background_width / 3;
  1025. one_third_src_height = (float)ctx->background_height / 3;
  1026. if (ctx->highlight) {
  1027. SDL_SetTextureColorMod(ctx->background, 10, 255, 21);
  1028. } else {
  1029. SDL_SetTextureColorMod(ctx->background, 255, 255, 255);
  1030. }
  1031. /* Top left */
  1032. src.x = 0.0f;
  1033. src.y = 0.0f;
  1034. src.w = one_third_src_width;
  1035. src.h = one_third_src_height;
  1036. dst.x = ctx->area.x;
  1037. dst.y = ctx->area.y;
  1038. dst.w = src.w;
  1039. dst.h = src.h;
  1040. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1041. /* Bottom left */
  1042. src.y = (float)ctx->background_height - src.h;
  1043. dst.y = ctx->area.y + ctx->area.h - dst.h;
  1044. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1045. /* Bottom right */
  1046. src.x = (float)ctx->background_width - src.w;
  1047. dst.x = ctx->area.x + ctx->area.w - dst.w;
  1048. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1049. /* Top right */
  1050. src.y = 0.0f;
  1051. dst.y = ctx->area.y;
  1052. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1053. /* Left */
  1054. src.x = 0.0f;
  1055. src.y = one_third_src_height;
  1056. dst.x = ctx->area.x;
  1057. dst.y = ctx->area.y + one_third_src_height;
  1058. dst.w = one_third_src_width;
  1059. dst.h = (float)ctx->area.h - 2 * one_third_src_height;
  1060. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1061. /* Right */
  1062. src.x = (float)ctx->background_width - one_third_src_width;
  1063. dst.x = ctx->area.x + ctx->area.w - one_third_src_width;
  1064. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1065. /* Top */
  1066. src.x = one_third_src_width;
  1067. src.y = 0.0f;
  1068. dst.x = ctx->area.x + one_third_src_width;
  1069. dst.y = ctx->area.y;
  1070. dst.w = ctx->area.w - 2 * one_third_src_width;
  1071. dst.h = one_third_src_height;
  1072. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1073. /* Bottom */
  1074. src.y = (float)ctx->background_height - src.h;
  1075. dst.y = ctx->area.y + ctx->area.h - one_third_src_height;
  1076. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1077. /* Center */
  1078. src.x = one_third_src_width;
  1079. src.y = one_third_src_height;
  1080. dst.x = ctx->area.x + one_third_src_width;
  1081. dst.y = ctx->area.y + one_third_src_height;
  1082. dst.w = ctx->area.w - 2 * one_third_src_width;
  1083. dst.h = (float)ctx->area.h - 2 * one_third_src_height;
  1084. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  1085. /* Label */
  1086. dst.x = ctx->area.x + ctx->area.w / 2 - ctx->label_width / 2;
  1087. dst.y = ctx->area.y + ctx->area.h / 2 - ctx->label_height / 2;
  1088. SDLTest_DrawString(ctx->renderer, dst.x, dst.y, ctx->label);
  1089. }
  1090. void DestroyGamepadButton(GamepadButton *ctx)
  1091. {
  1092. if (!ctx) {
  1093. return;
  1094. }
  1095. SDL_DestroyTexture(ctx->background);
  1096. SDL_free(ctx->label);
  1097. SDL_free(ctx);
  1098. }