gamepadutils.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  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. }
  354. }
  355. void RenderGamepadImage(GamepadImage *ctx)
  356. {
  357. SDL_FRect dst;
  358. int i;
  359. if (!ctx) {
  360. return;
  361. }
  362. dst.x = (float)ctx->x;
  363. dst.y = (float)ctx->y;
  364. dst.w = (float)ctx->gamepad_width;
  365. dst.h = (float)ctx->gamepad_height;
  366. if (ctx->showing_front) {
  367. SDL_RenderTexture(ctx->renderer, ctx->front_texture, NULL, &dst);
  368. } else {
  369. SDL_RenderTexture(ctx->renderer, ctx->back_texture, NULL, &dst);
  370. }
  371. for (i = 0; i < SDL_arraysize(button_positions); ++i) {
  372. if (ctx->buttons[i]) {
  373. SDL_bool on_front = SDL_TRUE;
  374. if (i >= SDL_GAMEPAD_BUTTON_PADDLE1 && i <= SDL_GAMEPAD_BUTTON_PADDLE4) {
  375. on_front = SDL_FALSE;
  376. }
  377. if (on_front == ctx->showing_front) {
  378. dst.x = (float)ctx->x + button_positions[i].x - ctx->button_width / 2;
  379. dst.y = (float)ctx->y + button_positions[i].y - ctx->button_height / 2;
  380. dst.w = (float)ctx->button_width;
  381. dst.h = (float)ctx->button_height;
  382. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  383. }
  384. }
  385. }
  386. if (ctx->showing_front) {
  387. for (i = 0; i < SDL_arraysize(axis_positions); ++i) {
  388. if (ctx->axes[i] < 0) {
  389. const double angle = axis_positions[i].angle;
  390. dst.x = (float)ctx->x + axis_positions[i].x - ctx->axis_width / 2;
  391. dst.y = (float)ctx->y + axis_positions[i].y - ctx->axis_height / 2;
  392. dst.w = (float)ctx->axis_width;
  393. dst.h = (float)ctx->axis_height;
  394. SDL_RenderTextureRotated(ctx->renderer, ctx->axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  395. } else if (ctx->axes[i] > 0) {
  396. const double angle = axis_positions[i].angle + 180.0;
  397. dst.x = (float)ctx->x + axis_positions[i].x - ctx->axis_width / 2;
  398. dst.y = (float)ctx->y + axis_positions[i].y - ctx->axis_height / 2;
  399. dst.w = (float)ctx->axis_width;
  400. dst.h = (float)ctx->axis_height;
  401. SDL_RenderTextureRotated(ctx->renderer, ctx->axis_texture, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  402. }
  403. }
  404. }
  405. if (ctx->showing_battery) {
  406. dst.x = (float)ctx->x + ctx->gamepad_width - ctx->battery_width;
  407. dst.y = (float)ctx->y;
  408. dst.w = (float)ctx->battery_width;
  409. dst.h = (float)ctx->battery_height;
  410. SDL_RenderTexture(ctx->renderer, ctx->battery_texture[1 + ctx->battery_level], NULL, &dst);
  411. }
  412. if (ctx->showing_touchpad) {
  413. dst.x = (float)ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
  414. dst.y = (float)ctx->y + ctx->gamepad_height;
  415. dst.w = (float)ctx->touchpad_width;
  416. dst.h = (float)ctx->touchpad_height;
  417. SDL_RenderTexture(ctx->renderer, ctx->touchpad_texture, NULL, &dst);
  418. for (i = 0; i < ctx->num_fingers; ++i) {
  419. GamepadTouchpadFinger *finger = &ctx->fingers[i];
  420. if (finger->state) {
  421. dst.x = (float)ctx->x + (ctx->gamepad_width - ctx->touchpad_width) / 2;
  422. dst.x += touchpad_area.x + finger->x * touchpad_area.w;
  423. dst.x -= ctx->button_width / 2;
  424. dst.y = (float)ctx->y + ctx->gamepad_height;
  425. dst.y += touchpad_area.y + finger->y * touchpad_area.h;
  426. dst.y -= ctx->button_height / 2;
  427. dst.w = (float)ctx->button_width;
  428. dst.h = (float)ctx->button_height;
  429. SDL_SetTextureAlphaMod(ctx->button_texture, (Uint8)(finger->pressure * SDL_ALPHA_OPAQUE));
  430. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  431. SDL_SetTextureAlphaMod(ctx->button_texture, SDL_ALPHA_OPAQUE);
  432. }
  433. }
  434. }
  435. }
  436. void DestroyGamepadImage(GamepadImage *ctx)
  437. {
  438. if (ctx) {
  439. int i;
  440. SDL_DestroyTexture(ctx->front_texture);
  441. SDL_DestroyTexture(ctx->back_texture);
  442. for (i = 0; i < SDL_arraysize(ctx->battery_texture); ++i) {
  443. SDL_DestroyTexture(ctx->battery_texture[i]);
  444. }
  445. SDL_DestroyTexture(ctx->touchpad_texture);
  446. SDL_DestroyTexture(ctx->button_texture);
  447. SDL_DestroyTexture(ctx->axis_texture);
  448. SDL_free(ctx);
  449. }
  450. }
  451. static const char *gamepad_button_names[] = {
  452. "A",
  453. "B",
  454. "X",
  455. "Y",
  456. "Back",
  457. "Guide",
  458. "Start",
  459. "Left Stick",
  460. "Right Stick",
  461. "Left Shoulder",
  462. "Right Shoulder",
  463. "DPAD Up",
  464. "DPAD Down",
  465. "DPAD Left",
  466. "DPAD Right",
  467. "Misc1",
  468. "Paddle1",
  469. "Paddle2",
  470. "Paddle3",
  471. "Paddle4",
  472. "Touchpad",
  473. };
  474. SDL_COMPILE_TIME_ASSERT(gamepad_button_names, SDL_arraysize(gamepad_button_names) == SDL_GAMEPAD_BUTTON_MAX);
  475. static const char *gamepad_axis_names[] = {
  476. "LeftX",
  477. "RightX",
  478. "RightX",
  479. "RightY",
  480. "Left Trigger",
  481. "Right Trigger",
  482. };
  483. SDL_COMPILE_TIME_ASSERT(gamepad_axis_names, SDL_arraysize(gamepad_axis_names) == SDL_GAMEPAD_AXIS_MAX);
  484. struct GamepadDisplay
  485. {
  486. SDL_Renderer *renderer;
  487. SDL_Texture *button_texture;
  488. SDL_Texture *arrow_texture;
  489. int button_width;
  490. int button_height;
  491. int arrow_width;
  492. int arrow_height;
  493. float accel_data[3];
  494. float gyro_data[3];
  495. Uint64 last_sensor_update;
  496. SDL_Rect area;
  497. };
  498. GamepadDisplay *CreateGamepadDisplay(SDL_Renderer *renderer)
  499. {
  500. GamepadDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
  501. if (ctx) {
  502. ctx->renderer = renderer;
  503. ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
  504. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  505. ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
  506. SDL_QueryTexture(ctx->arrow_texture, NULL, NULL, &ctx->arrow_width, &ctx->arrow_height);
  507. }
  508. return ctx;
  509. }
  510. void SetGamepadDisplayArea(GamepadDisplay *ctx, int x, int y, int w, int h)
  511. {
  512. if (!ctx) {
  513. return;
  514. }
  515. ctx->area.x = x;
  516. ctx->area.y = y;
  517. ctx->area.w = w;
  518. ctx->area.h = h;
  519. }
  520. void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
  521. {
  522. float x, y;
  523. int i;
  524. char text[128];
  525. const float margin = 8.0f;
  526. const float center = ctx->area.w / 2.0f;
  527. const float arrow_extent = 48.0f;
  528. SDL_FRect dst, rect;
  529. Uint8 r, g, b, a;
  530. SDL_bool has_accel;
  531. SDL_bool has_gyro;
  532. if (!ctx) {
  533. return;
  534. }
  535. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  536. x = ctx->area.x + margin;
  537. y = ctx->area.y + margin;
  538. for (i = 0; i < SDL_GAMEPAD_BUTTON_MAX; ++i) {
  539. if (SDL_GamepadHasButton(gamepad, (SDL_GamepadButton)i)) {
  540. SDL_snprintf(text, sizeof(text), "%s:", gamepad_button_names[i]);
  541. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  542. if (SDL_GetGamepadButton(gamepad, (SDL_GamepadButton)i)) {
  543. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  544. } else {
  545. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  546. }
  547. dst.x = x + center + 2.0f;
  548. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  549. dst.w = (float)ctx->button_width;
  550. dst.h = (float)ctx->button_height;
  551. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  552. y += ctx->button_height + 2.0f;
  553. }
  554. }
  555. for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
  556. if (SDL_GamepadHasAxis(gamepad, (SDL_GamepadAxis)i)) {
  557. SDL_bool has_negative = (i != SDL_GAMEPAD_AXIS_LEFT_TRIGGER && i != SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
  558. Sint16 value = SDL_GetGamepadAxis(gamepad, (SDL_GamepadAxis)i);
  559. SDL_snprintf(text, sizeof(text), "%s:", gamepad_axis_names[i]);
  560. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  561. dst.x = x + center + 2.0f;
  562. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  563. dst.w = (float)ctx->arrow_width;
  564. dst.h = (float)ctx->arrow_height;
  565. if (has_negative) {
  566. if (value == SDL_MIN_SINT16) {
  567. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  568. } else {
  569. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  570. }
  571. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  572. }
  573. dst.x += (float)ctx->arrow_width;
  574. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  575. rect.x = dst.x + arrow_extent - 2.0f;
  576. rect.y = dst.y;
  577. rect.w = 4.0f;
  578. rect.h = (float)ctx->arrow_height;
  579. SDL_RenderFillRect(ctx->renderer, &rect);
  580. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  581. if (value < 0) {
  582. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  583. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  584. rect.x = dst.x + arrow_extent - rect.w;
  585. rect.y = dst.y + ctx->arrow_height * 0.25f;
  586. rect.h = ctx->arrow_height / 2.0f;
  587. SDL_RenderFillRect(ctx->renderer, &rect);
  588. }
  589. dst.x += arrow_extent;
  590. if (value > 0) {
  591. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  592. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  593. rect.x = dst.x;
  594. rect.y = dst.y + ctx->arrow_height * 0.25f;
  595. rect.h = ctx->arrow_height / 2.0f;
  596. SDL_RenderFillRect(ctx->renderer, &rect);
  597. }
  598. dst.x += arrow_extent;
  599. if (value == SDL_MAX_SINT16) {
  600. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  601. } else {
  602. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  603. }
  604. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  605. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  606. y += ctx->button_height + 2;
  607. }
  608. }
  609. if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
  610. int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
  611. for (i = 0; i < num_fingers; ++i) {
  612. Uint8 state;
  613. float finger_x, finger_y, finger_pressure;
  614. if (SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &state, &finger_x, &finger_y, &finger_pressure) < 0) {
  615. continue;
  616. }
  617. SDL_snprintf(text, sizeof(text), "Touch finger %d:", i);
  618. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  619. if (state) {
  620. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  621. } else {
  622. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  623. }
  624. dst.x = x + center + 2.0f;
  625. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  626. dst.w = (float)ctx->button_width;
  627. dst.h = (float)ctx->button_height;
  628. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  629. if (state) {
  630. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f)", finger_x, finger_y);
  631. SDLTest_DrawString(ctx->renderer, x + center + ctx->button_width + 4.0f, y, text);
  632. }
  633. y += ctx->button_height + 2.0f;
  634. }
  635. }
  636. has_accel = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_ACCEL);
  637. has_gyro = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_GYRO);
  638. if (has_accel || has_gyro) {
  639. const int SENSOR_UPDATE_INTERVAL_MS = 100;
  640. Uint64 now = SDL_GetTicks();
  641. if (now >= ctx->last_sensor_update + SENSOR_UPDATE_INTERVAL_MS) {
  642. if (has_accel) {
  643. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_ACCEL, ctx->accel_data, SDL_arraysize(ctx->accel_data));
  644. }
  645. if (has_gyro) {
  646. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_GYRO, ctx->gyro_data, SDL_arraysize(ctx->gyro_data));
  647. }
  648. ctx->last_sensor_update = now;
  649. }
  650. if (has_accel) {
  651. SDL_strlcpy(text, "Accelerometer:", sizeof(text));
  652. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  653. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->accel_data[0], ctx->accel_data[1], ctx->accel_data[2]);
  654. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  655. y += ctx->button_height + 2.0f;
  656. }
  657. if (has_gyro) {
  658. SDL_strlcpy(text, "Gyro:", sizeof(text));
  659. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  660. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->gyro_data[0], ctx->gyro_data[1], ctx->gyro_data[2]);
  661. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  662. y += ctx->button_height + 2.0f;
  663. }
  664. }
  665. }
  666. void DestroyGamepadDisplay(GamepadDisplay *ctx)
  667. {
  668. if (!ctx) {
  669. return;
  670. }
  671. SDL_DestroyTexture(ctx->button_texture);
  672. SDL_DestroyTexture(ctx->arrow_texture);
  673. SDL_free(ctx);
  674. }
  675. struct JoystickDisplay
  676. {
  677. SDL_Renderer *renderer;
  678. SDL_Texture *button_texture;
  679. SDL_Texture *arrow_texture;
  680. int button_width;
  681. int button_height;
  682. int arrow_width;
  683. int arrow_height;
  684. SDL_Rect area;
  685. };
  686. JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer)
  687. {
  688. JoystickDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
  689. if (ctx) {
  690. ctx->renderer = renderer;
  691. ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
  692. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  693. ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
  694. SDL_QueryTexture(ctx->arrow_texture, NULL, NULL, &ctx->arrow_width, &ctx->arrow_height);
  695. }
  696. return ctx;
  697. }
  698. void SetJoystickDisplayArea(JoystickDisplay *ctx, int x, int y, int w, int h)
  699. {
  700. if (!ctx) {
  701. return;
  702. }
  703. ctx->area.x = x;
  704. ctx->area.y = y;
  705. ctx->area.w = w;
  706. ctx->area.h = h;
  707. }
  708. void RenderJoystickDisplay(JoystickDisplay *ctx, SDL_Joystick *joystick)
  709. {
  710. float x, y;
  711. int i;
  712. int nbuttons = SDL_GetNumJoystickButtons(joystick);
  713. int naxes = SDL_GetNumJoystickAxes(joystick);
  714. int nhats = SDL_GetNumJoystickHats(joystick);
  715. char text[32];
  716. const float margin = 8.0f;
  717. const float center = 80.0f;
  718. const float arrow_extent = 48.0f;
  719. SDL_FRect dst, rect;
  720. Uint8 r, g, b, a;
  721. if (!ctx) {
  722. return;
  723. }
  724. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  725. x = (float)ctx->area.x + margin;
  726. y = (float)ctx->area.y + margin;
  727. if (nbuttons > 0) {
  728. SDLTest_DrawString(ctx->renderer, x, y, "BUTTONS");
  729. y += FONT_LINE_HEIGHT + 2;
  730. for (i = 0; i < nbuttons; ++i) {
  731. SDL_snprintf(text, sizeof(text), "%2.d:", i);
  732. SDLTest_DrawString(ctx->renderer, x, y, text);
  733. if (SDL_GetJoystickButton(joystick, (Uint8)i)) {
  734. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  735. } else {
  736. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  737. }
  738. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  739. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  740. dst.w = (float)ctx->button_width;
  741. dst.h = (float)ctx->button_height;
  742. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  743. y += ctx->button_height + 2;
  744. }
  745. }
  746. x = (float)ctx->area.x + margin + center + margin;
  747. y = (float)ctx->area.y + margin;
  748. if (naxes > 0) {
  749. SDLTest_DrawString(ctx->renderer, x, y, "AXES");
  750. y += FONT_LINE_HEIGHT + 2;
  751. for (i = 0; i < naxes; ++i) {
  752. Sint16 value = SDL_GetJoystickAxis(joystick, i);
  753. SDL_snprintf(text, sizeof(text), "%d:", i);
  754. SDLTest_DrawString(ctx->renderer, x, y, text);
  755. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2.0f;
  756. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  757. dst.w = (float)ctx->arrow_width;
  758. dst.h = (float)ctx->arrow_height;
  759. if (value == SDL_MIN_SINT16) {
  760. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  761. } else {
  762. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  763. }
  764. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  765. dst.x += (float)ctx->arrow_width;
  766. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  767. rect.x = dst.x + arrow_extent - 2.0f;
  768. rect.y = dst.y;
  769. rect.w = 4.0f;
  770. rect.h = (float)ctx->arrow_height;
  771. SDL_RenderFillRect(ctx->renderer, &rect);
  772. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  773. if (value < 0) {
  774. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  775. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  776. rect.x = dst.x + arrow_extent - rect.w;
  777. rect.y = dst.y + ctx->arrow_height * 0.25f;
  778. rect.h = ctx->arrow_height / 2.0f;
  779. SDL_RenderFillRect(ctx->renderer, &rect);
  780. }
  781. dst.x += arrow_extent;
  782. if (value > 0) {
  783. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  784. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  785. rect.x = dst.x;
  786. rect.y = dst.y + ctx->arrow_height * 0.25f;
  787. rect.h = ctx->arrow_height / 2.0f;
  788. SDL_RenderFillRect(ctx->renderer, &rect);
  789. }
  790. dst.x += arrow_extent;
  791. if (value == SDL_MAX_SINT16) {
  792. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  793. } else {
  794. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  795. }
  796. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  797. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  798. y += ctx->button_height + 2;
  799. }
  800. }
  801. y += FONT_LINE_HEIGHT + 2;
  802. if (nhats > 0) {
  803. SDLTest_DrawString(ctx->renderer, x, y, "HATS");
  804. y += FONT_LINE_HEIGHT + 2 + 1.5f * ctx->button_height - FONT_CHARACTER_SIZE / 2;
  805. for (i = 0; i < nhats; ++i) {
  806. Uint8 value = SDL_GetJoystickHat(joystick, i);
  807. SDL_snprintf(text, sizeof(text), "%d:", i);
  808. SDLTest_DrawString(ctx->renderer, x, y, text);
  809. if (value & SDL_HAT_LEFT) {
  810. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  811. } else {
  812. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  813. }
  814. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  815. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  816. dst.w = (float)ctx->button_width;
  817. dst.h = (float)ctx->button_height;
  818. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  819. if (value & SDL_HAT_UP) {
  820. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  821. } else {
  822. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  823. }
  824. dst.x += (float)ctx->button_width;
  825. dst.y -= (float)ctx->button_height;
  826. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  827. if (value & SDL_HAT_DOWN) {
  828. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  829. } else {
  830. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  831. }
  832. dst.y += (float)ctx->button_height * 2;
  833. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  834. if (value & SDL_HAT_RIGHT) {
  835. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  836. } else {
  837. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  838. }
  839. dst.x += (float)ctx->button_width;
  840. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  841. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  842. y += 3 * ctx->button_height + 2;
  843. }
  844. }
  845. }
  846. void DestroyJoystickDisplay(JoystickDisplay *ctx)
  847. {
  848. if (!ctx) {
  849. return;
  850. }
  851. SDL_DestroyTexture(ctx->button_texture);
  852. SDL_DestroyTexture(ctx->arrow_texture);
  853. SDL_free(ctx);
  854. }
  855. struct GamepadButton
  856. {
  857. SDL_Renderer *renderer;
  858. SDL_Texture *background;
  859. int background_width;
  860. int background_height;
  861. SDL_FRect area;
  862. char *label;
  863. int label_width;
  864. int label_height;
  865. SDL_bool highlight;
  866. };
  867. GamepadButton *CreateGamepadButton(SDL_Renderer *renderer, const char *label)
  868. {
  869. GamepadButton *ctx = SDL_calloc(1, sizeof(*ctx));
  870. if (ctx) {
  871. ctx->renderer = renderer;
  872. ctx->background = CreateTexture(renderer, gamepad_button_background_bmp, gamepad_button_background_bmp_len);
  873. SDL_QueryTexture(ctx->background, NULL, NULL, &ctx->background_width, &ctx->background_height);
  874. ctx->label = SDL_strdup(label);
  875. ctx->label_width = (int)(FONT_CHARACTER_SIZE * SDL_strlen(label));
  876. ctx->label_height = FONT_CHARACTER_SIZE;
  877. }
  878. return ctx;
  879. }
  880. void SetGamepadButtonArea(GamepadButton *ctx, int x, int y, int w, int h)
  881. {
  882. if (!ctx) {
  883. return;
  884. }
  885. ctx->area.x = (float)x;
  886. ctx->area.y = (float)y;
  887. ctx->area.w = (float)w;
  888. ctx->area.h = (float)h;
  889. }
  890. void SetGamepadButtonHighlight(GamepadButton *ctx, SDL_bool highlight)
  891. {
  892. if (!ctx) {
  893. return;
  894. }
  895. ctx->highlight = highlight;
  896. }
  897. int GetGamepadButtonLabelWidth(GamepadButton *ctx)
  898. {
  899. if (!ctx) {
  900. return 0;
  901. }
  902. return ctx->label_width;
  903. }
  904. int GetGamepadButtonLabelHeight(GamepadButton *ctx)
  905. {
  906. if (!ctx) {
  907. return 0;
  908. }
  909. return ctx->label_height;
  910. }
  911. SDL_bool GamepadButtonContains(GamepadButton *ctx, float x, float y)
  912. {
  913. SDL_FPoint point;
  914. if (!ctx) {
  915. return SDL_FALSE;
  916. }
  917. point.x = x;
  918. point.y = y;
  919. return SDL_PointInRectFloat(&point, &ctx->area);
  920. }
  921. void RenderGamepadButton(GamepadButton *ctx)
  922. {
  923. SDL_FRect src, dst;
  924. float one_third_src_width;
  925. float one_third_src_height;
  926. if (!ctx) {
  927. return;
  928. }
  929. one_third_src_width = (float)ctx->background_width / 3;
  930. one_third_src_height = (float)ctx->background_height / 3;
  931. if (ctx->highlight) {
  932. SDL_SetTextureColorMod(ctx->background, 10, 255, 21);
  933. } else {
  934. SDL_SetTextureColorMod(ctx->background, 255, 255, 255);
  935. }
  936. /* Top left */
  937. src.x = 0.0f;
  938. src.y = 0.0f;
  939. src.w = one_third_src_width;
  940. src.h = one_third_src_height;
  941. dst.x = ctx->area.x;
  942. dst.y = ctx->area.y;
  943. dst.w = src.w;
  944. dst.h = src.h;
  945. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  946. /* Bottom left */
  947. src.y = (float)ctx->background_height - src.h;
  948. dst.y = ctx->area.y + ctx->area.h - dst.h;
  949. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  950. /* Bottom right */
  951. src.x = (float)ctx->background_width - src.w;
  952. dst.x = ctx->area.x + ctx->area.w - dst.w;
  953. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  954. /* Top right */
  955. src.y = 0.0f;
  956. dst.y = ctx->area.y;
  957. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  958. /* Left */
  959. src.x = 0.0f;
  960. src.y = one_third_src_height;
  961. dst.x = ctx->area.x;
  962. dst.y = ctx->area.y + one_third_src_height;
  963. dst.w = one_third_src_width;
  964. dst.h = (float)ctx->area.h - 2 * one_third_src_height;
  965. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  966. /* Right */
  967. src.x = (float)ctx->background_width - one_third_src_width;
  968. dst.x = ctx->area.x + ctx->area.w - one_third_src_width;
  969. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  970. /* Top */
  971. src.x = one_third_src_width;
  972. src.y = 0.0f;
  973. dst.x = ctx->area.x + one_third_src_width;
  974. dst.y = ctx->area.y;
  975. dst.w = ctx->area.w - 2 * one_third_src_width;
  976. dst.h = one_third_src_height;
  977. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  978. /* Bottom */
  979. src.y = (float)ctx->background_height - src.h;
  980. dst.y = ctx->area.y + ctx->area.h - one_third_src_height;
  981. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  982. /* Center */
  983. src.x = one_third_src_width;
  984. src.y = one_third_src_height;
  985. dst.x = ctx->area.x + one_third_src_width;
  986. dst.y = ctx->area.y + one_third_src_height;
  987. dst.w = ctx->area.w - 2 * one_third_src_width;
  988. dst.h = (float)ctx->area.h - 2 * one_third_src_height;
  989. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  990. /* Label */
  991. dst.x = ctx->area.x + ctx->area.w / 2 - ctx->label_width / 2;
  992. dst.y = ctx->area.y + ctx->area.h / 2 - ctx->label_height / 2;
  993. SDLTest_DrawString(ctx->renderer, dst.x, dst.y, ctx->label);
  994. }
  995. void DestroyGamepadButton(GamepadButton *ctx)
  996. {
  997. if (!ctx) {
  998. return;
  999. }
  1000. SDL_DestroyTexture(ctx->background);
  1001. SDL_free(ctx->label);
  1002. SDL_free(ctx);
  1003. }