gamepadutils.c 43 KB

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