gamepadutils.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. void RenderGamepadDisplay(GamepadDisplay *ctx, SDL_Gamepad *gamepad)
  527. {
  528. float x, y;
  529. int i;
  530. char text[128];
  531. const float margin = 8.0f;
  532. const float center = ctx->area.w / 2.0f;
  533. const float arrow_extent = 48.0f;
  534. SDL_FRect dst, rect;
  535. Uint8 r, g, b, a;
  536. SDL_bool has_accel;
  537. SDL_bool has_gyro;
  538. if (!ctx || !gamepad) {
  539. return;
  540. }
  541. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  542. x = ctx->area.x + margin;
  543. y = ctx->area.y + margin;
  544. for (i = 0; i < SDL_GAMEPAD_BUTTON_MAX; ++i) {
  545. if (SDL_GamepadHasButton(gamepad, (SDL_GamepadButton)i)) {
  546. SDL_snprintf(text, sizeof(text), "%s:", gamepad_button_names[i]);
  547. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  548. if (SDL_GetGamepadButton(gamepad, (SDL_GamepadButton)i)) {
  549. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  550. } else {
  551. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  552. }
  553. dst.x = x + center + 2.0f;
  554. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  555. dst.w = (float)ctx->button_width;
  556. dst.h = (float)ctx->button_height;
  557. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  558. y += ctx->button_height + 2.0f;
  559. }
  560. }
  561. for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
  562. if (SDL_GamepadHasAxis(gamepad, (SDL_GamepadAxis)i)) {
  563. SDL_bool has_negative = (i != SDL_GAMEPAD_AXIS_LEFT_TRIGGER && i != SDL_GAMEPAD_AXIS_RIGHT_TRIGGER);
  564. Sint16 value = SDL_GetGamepadAxis(gamepad, (SDL_GamepadAxis)i);
  565. SDL_snprintf(text, sizeof(text), "%s:", gamepad_axis_names[i]);
  566. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  567. dst.x = x + center + 2.0f;
  568. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  569. dst.w = (float)ctx->arrow_width;
  570. dst.h = (float)ctx->arrow_height;
  571. if (has_negative) {
  572. if (value == SDL_MIN_SINT16) {
  573. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  574. } else {
  575. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  576. }
  577. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  578. }
  579. dst.x += (float)ctx->arrow_width;
  580. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  581. rect.x = dst.x + arrow_extent - 2.0f;
  582. rect.y = dst.y;
  583. rect.w = 4.0f;
  584. rect.h = (float)ctx->arrow_height;
  585. SDL_RenderFillRect(ctx->renderer, &rect);
  586. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  587. if (value < 0) {
  588. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  589. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  590. rect.x = dst.x + arrow_extent - rect.w;
  591. rect.y = dst.y + ctx->arrow_height * 0.25f;
  592. rect.h = ctx->arrow_height / 2.0f;
  593. SDL_RenderFillRect(ctx->renderer, &rect);
  594. }
  595. dst.x += arrow_extent;
  596. if (value > 0) {
  597. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  598. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  599. rect.x = dst.x;
  600. rect.y = dst.y + ctx->arrow_height * 0.25f;
  601. rect.h = ctx->arrow_height / 2.0f;
  602. SDL_RenderFillRect(ctx->renderer, &rect);
  603. }
  604. dst.x += arrow_extent;
  605. if (value == SDL_MAX_SINT16) {
  606. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  607. } else {
  608. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  609. }
  610. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  611. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  612. y += ctx->button_height + 2;
  613. }
  614. }
  615. if (SDL_GetNumGamepadTouchpads(gamepad) > 0) {
  616. int num_fingers = SDL_GetNumGamepadTouchpadFingers(gamepad, 0);
  617. for (i = 0; i < num_fingers; ++i) {
  618. Uint8 state;
  619. float finger_x, finger_y, finger_pressure;
  620. if (SDL_GetGamepadTouchpadFinger(gamepad, 0, i, &state, &finger_x, &finger_y, &finger_pressure) < 0) {
  621. continue;
  622. }
  623. SDL_snprintf(text, sizeof(text), "Touch finger %d:", i);
  624. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  625. if (state) {
  626. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  627. } else {
  628. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  629. }
  630. dst.x = x + center + 2.0f;
  631. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  632. dst.w = (float)ctx->button_width;
  633. dst.h = (float)ctx->button_height;
  634. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  635. if (state) {
  636. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f)", finger_x, finger_y);
  637. SDLTest_DrawString(ctx->renderer, x + center + ctx->button_width + 4.0f, y, text);
  638. }
  639. y += ctx->button_height + 2.0f;
  640. }
  641. }
  642. has_accel = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_ACCEL);
  643. has_gyro = SDL_GamepadHasSensor(gamepad, SDL_SENSOR_GYRO);
  644. if (has_accel || has_gyro) {
  645. const int SENSOR_UPDATE_INTERVAL_MS = 100;
  646. Uint64 now = SDL_GetTicks();
  647. if (now >= ctx->last_sensor_update + SENSOR_UPDATE_INTERVAL_MS) {
  648. if (has_accel) {
  649. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_ACCEL, ctx->accel_data, SDL_arraysize(ctx->accel_data));
  650. }
  651. if (has_gyro) {
  652. SDL_GetGamepadSensorData(gamepad, SDL_SENSOR_GYRO, ctx->gyro_data, SDL_arraysize(ctx->gyro_data));
  653. }
  654. ctx->last_sensor_update = now;
  655. }
  656. if (has_accel) {
  657. SDL_strlcpy(text, "Accelerometer:", sizeof(text));
  658. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  659. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->accel_data[0], ctx->accel_data[1], ctx->accel_data[2]);
  660. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  661. y += ctx->button_height + 2.0f;
  662. }
  663. if (has_gyro) {
  664. SDL_strlcpy(text, "Gyro:", sizeof(text));
  665. SDLTest_DrawString(ctx->renderer, x + center - SDL_strlen(text) * FONT_CHARACTER_SIZE, y, text);
  666. SDL_snprintf(text, sizeof(text), "(%.2f,%.2f,%.2f)", ctx->gyro_data[0], ctx->gyro_data[1], ctx->gyro_data[2]);
  667. SDLTest_DrawString(ctx->renderer, x + center + 2.0f, y, text);
  668. y += ctx->button_height + 2.0f;
  669. }
  670. }
  671. }
  672. void DestroyGamepadDisplay(GamepadDisplay *ctx)
  673. {
  674. if (!ctx) {
  675. return;
  676. }
  677. SDL_DestroyTexture(ctx->button_texture);
  678. SDL_DestroyTexture(ctx->arrow_texture);
  679. SDL_free(ctx);
  680. }
  681. struct JoystickDisplay
  682. {
  683. SDL_Renderer *renderer;
  684. SDL_Texture *button_texture;
  685. SDL_Texture *arrow_texture;
  686. int button_width;
  687. int button_height;
  688. int arrow_width;
  689. int arrow_height;
  690. SDL_Rect area;
  691. };
  692. JoystickDisplay *CreateJoystickDisplay(SDL_Renderer *renderer)
  693. {
  694. JoystickDisplay *ctx = SDL_calloc(1, sizeof(*ctx));
  695. if (ctx) {
  696. ctx->renderer = renderer;
  697. ctx->button_texture = CreateTexture(renderer, gamepad_button_small_bmp, gamepad_button_small_bmp_len);
  698. SDL_QueryTexture(ctx->button_texture, NULL, NULL, &ctx->button_width, &ctx->button_height);
  699. ctx->arrow_texture = CreateTexture(renderer, gamepad_axis_arrow_bmp, gamepad_axis_arrow_bmp_len);
  700. SDL_QueryTexture(ctx->arrow_texture, NULL, NULL, &ctx->arrow_width, &ctx->arrow_height);
  701. }
  702. return ctx;
  703. }
  704. void SetJoystickDisplayArea(JoystickDisplay *ctx, int x, int y, int w, int h)
  705. {
  706. if (!ctx) {
  707. return;
  708. }
  709. ctx->area.x = x;
  710. ctx->area.y = y;
  711. ctx->area.w = w;
  712. ctx->area.h = h;
  713. }
  714. void RenderJoystickDisplay(JoystickDisplay *ctx, SDL_Joystick *joystick)
  715. {
  716. float x, y;
  717. int i;
  718. int nbuttons = SDL_GetNumJoystickButtons(joystick);
  719. int naxes = SDL_GetNumJoystickAxes(joystick);
  720. int nhats = SDL_GetNumJoystickHats(joystick);
  721. char text[32];
  722. const float margin = 8.0f;
  723. const float center = 80.0f;
  724. const float arrow_extent = 48.0f;
  725. SDL_FRect dst, rect;
  726. Uint8 r, g, b, a;
  727. if (!ctx) {
  728. return;
  729. }
  730. SDL_GetRenderDrawColor(ctx->renderer, &r, &g, &b, &a);
  731. x = (float)ctx->area.x + margin;
  732. y = (float)ctx->area.y + margin;
  733. if (nbuttons > 0) {
  734. SDLTest_DrawString(ctx->renderer, x, y, "BUTTONS");
  735. y += FONT_LINE_HEIGHT + 2;
  736. for (i = 0; i < nbuttons; ++i) {
  737. SDL_snprintf(text, sizeof(text), "%2.d:", i);
  738. SDLTest_DrawString(ctx->renderer, x, y, text);
  739. if (SDL_GetJoystickButton(joystick, (Uint8)i)) {
  740. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  741. } else {
  742. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  743. }
  744. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  745. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  746. dst.w = (float)ctx->button_width;
  747. dst.h = (float)ctx->button_height;
  748. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  749. y += ctx->button_height + 2;
  750. }
  751. }
  752. x = (float)ctx->area.x + margin + center + margin;
  753. y = (float)ctx->area.y + margin;
  754. if (naxes > 0) {
  755. SDLTest_DrawString(ctx->renderer, x, y, "AXES");
  756. y += FONT_LINE_HEIGHT + 2;
  757. for (i = 0; i < naxes; ++i) {
  758. Sint16 value = SDL_GetJoystickAxis(joystick, i);
  759. SDL_snprintf(text, sizeof(text), "%d:", i);
  760. SDLTest_DrawString(ctx->renderer, x, y, text);
  761. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2.0f;
  762. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->arrow_height / 2;
  763. dst.w = (float)ctx->arrow_width;
  764. dst.h = (float)ctx->arrow_height;
  765. if (value == SDL_MIN_SINT16) {
  766. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  767. } else {
  768. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  769. }
  770. SDL_RenderTextureRotated(ctx->renderer, ctx->arrow_texture, NULL, &dst, 0.0f, NULL, SDL_FLIP_HORIZONTAL);
  771. dst.x += (float)ctx->arrow_width;
  772. SDL_SetRenderDrawColor(ctx->renderer, 200, 200, 200, SDL_ALPHA_OPAQUE);
  773. rect.x = dst.x + arrow_extent - 2.0f;
  774. rect.y = dst.y;
  775. rect.w = 4.0f;
  776. rect.h = (float)ctx->arrow_height;
  777. SDL_RenderFillRect(ctx->renderer, &rect);
  778. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  779. if (value < 0) {
  780. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  781. rect.w = ((float)value / SDL_MIN_SINT16) * arrow_extent;
  782. rect.x = dst.x + arrow_extent - rect.w;
  783. rect.y = dst.y + ctx->arrow_height * 0.25f;
  784. rect.h = ctx->arrow_height / 2.0f;
  785. SDL_RenderFillRect(ctx->renderer, &rect);
  786. }
  787. dst.x += arrow_extent;
  788. if (value > 0) {
  789. SDL_SetRenderDrawColor(ctx->renderer, 8, 200, 16, SDL_ALPHA_OPAQUE);
  790. rect.w = ((float)value / SDL_MAX_SINT16) * arrow_extent;
  791. rect.x = dst.x;
  792. rect.y = dst.y + ctx->arrow_height * 0.25f;
  793. rect.h = ctx->arrow_height / 2.0f;
  794. SDL_RenderFillRect(ctx->renderer, &rect);
  795. }
  796. dst.x += arrow_extent;
  797. if (value == SDL_MAX_SINT16) {
  798. SDL_SetTextureColorMod(ctx->arrow_texture, 10, 255, 21);
  799. } else {
  800. SDL_SetTextureColorMod(ctx->arrow_texture, 255, 255, 255);
  801. }
  802. SDL_RenderTexture(ctx->renderer, ctx->arrow_texture, NULL, &dst);
  803. SDL_SetRenderDrawColor(ctx->renderer, r, g, b, a);
  804. y += ctx->button_height + 2;
  805. }
  806. }
  807. y += FONT_LINE_HEIGHT + 2;
  808. if (nhats > 0) {
  809. SDLTest_DrawString(ctx->renderer, x, y, "HATS");
  810. y += FONT_LINE_HEIGHT + 2 + 1.5f * ctx->button_height - FONT_CHARACTER_SIZE / 2;
  811. for (i = 0; i < nhats; ++i) {
  812. Uint8 value = SDL_GetJoystickHat(joystick, i);
  813. SDL_snprintf(text, sizeof(text), "%d:", i);
  814. SDLTest_DrawString(ctx->renderer, x, y, text);
  815. if (value & SDL_HAT_LEFT) {
  816. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  817. } else {
  818. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  819. }
  820. dst.x = x + FONT_CHARACTER_SIZE * SDL_strlen(text) + 2;
  821. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  822. dst.w = (float)ctx->button_width;
  823. dst.h = (float)ctx->button_height;
  824. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  825. if (value & SDL_HAT_UP) {
  826. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  827. } else {
  828. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  829. }
  830. dst.x += (float)ctx->button_width;
  831. dst.y -= (float)ctx->button_height;
  832. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  833. if (value & SDL_HAT_DOWN) {
  834. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  835. } else {
  836. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  837. }
  838. dst.y += (float)ctx->button_height * 2;
  839. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  840. if (value & SDL_HAT_RIGHT) {
  841. SDL_SetTextureColorMod(ctx->button_texture, 10, 255, 21);
  842. } else {
  843. SDL_SetTextureColorMod(ctx->button_texture, 255, 255, 255);
  844. }
  845. dst.x += (float)ctx->button_width;
  846. dst.y = y + FONT_CHARACTER_SIZE / 2 - ctx->button_height / 2;
  847. SDL_RenderTexture(ctx->renderer, ctx->button_texture, NULL, &dst);
  848. y += 3 * ctx->button_height + 2;
  849. }
  850. }
  851. }
  852. void DestroyJoystickDisplay(JoystickDisplay *ctx)
  853. {
  854. if (!ctx) {
  855. return;
  856. }
  857. SDL_DestroyTexture(ctx->button_texture);
  858. SDL_DestroyTexture(ctx->arrow_texture);
  859. SDL_free(ctx);
  860. }
  861. struct GamepadButton
  862. {
  863. SDL_Renderer *renderer;
  864. SDL_Texture *background;
  865. int background_width;
  866. int background_height;
  867. SDL_FRect area;
  868. char *label;
  869. int label_width;
  870. int label_height;
  871. SDL_bool highlight;
  872. };
  873. GamepadButton *CreateGamepadButton(SDL_Renderer *renderer, const char *label)
  874. {
  875. GamepadButton *ctx = SDL_calloc(1, sizeof(*ctx));
  876. if (ctx) {
  877. ctx->renderer = renderer;
  878. ctx->background = CreateTexture(renderer, gamepad_button_background_bmp, gamepad_button_background_bmp_len);
  879. SDL_QueryTexture(ctx->background, NULL, NULL, &ctx->background_width, &ctx->background_height);
  880. ctx->label = SDL_strdup(label);
  881. ctx->label_width = (int)(FONT_CHARACTER_SIZE * SDL_strlen(label));
  882. ctx->label_height = FONT_CHARACTER_SIZE;
  883. }
  884. return ctx;
  885. }
  886. void SetGamepadButtonArea(GamepadButton *ctx, int x, int y, int w, int h)
  887. {
  888. if (!ctx) {
  889. return;
  890. }
  891. ctx->area.x = (float)x;
  892. ctx->area.y = (float)y;
  893. ctx->area.w = (float)w;
  894. ctx->area.h = (float)h;
  895. }
  896. void SetGamepadButtonHighlight(GamepadButton *ctx, SDL_bool highlight)
  897. {
  898. if (!ctx) {
  899. return;
  900. }
  901. ctx->highlight = highlight;
  902. }
  903. int GetGamepadButtonLabelWidth(GamepadButton *ctx)
  904. {
  905. if (!ctx) {
  906. return 0;
  907. }
  908. return ctx->label_width;
  909. }
  910. int GetGamepadButtonLabelHeight(GamepadButton *ctx)
  911. {
  912. if (!ctx) {
  913. return 0;
  914. }
  915. return ctx->label_height;
  916. }
  917. SDL_bool GamepadButtonContains(GamepadButton *ctx, float x, float y)
  918. {
  919. SDL_FPoint point;
  920. if (!ctx) {
  921. return SDL_FALSE;
  922. }
  923. point.x = x;
  924. point.y = y;
  925. return SDL_PointInRectFloat(&point, &ctx->area);
  926. }
  927. void RenderGamepadButton(GamepadButton *ctx)
  928. {
  929. SDL_FRect src, dst;
  930. float one_third_src_width;
  931. float one_third_src_height;
  932. if (!ctx) {
  933. return;
  934. }
  935. one_third_src_width = (float)ctx->background_width / 3;
  936. one_third_src_height = (float)ctx->background_height / 3;
  937. if (ctx->highlight) {
  938. SDL_SetTextureColorMod(ctx->background, 10, 255, 21);
  939. } else {
  940. SDL_SetTextureColorMod(ctx->background, 255, 255, 255);
  941. }
  942. /* Top left */
  943. src.x = 0.0f;
  944. src.y = 0.0f;
  945. src.w = one_third_src_width;
  946. src.h = one_third_src_height;
  947. dst.x = ctx->area.x;
  948. dst.y = ctx->area.y;
  949. dst.w = src.w;
  950. dst.h = src.h;
  951. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  952. /* Bottom left */
  953. src.y = (float)ctx->background_height - src.h;
  954. dst.y = ctx->area.y + ctx->area.h - dst.h;
  955. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  956. /* Bottom right */
  957. src.x = (float)ctx->background_width - src.w;
  958. dst.x = ctx->area.x + ctx->area.w - dst.w;
  959. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  960. /* Top right */
  961. src.y = 0.0f;
  962. dst.y = ctx->area.y;
  963. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  964. /* Left */
  965. src.x = 0.0f;
  966. src.y = one_third_src_height;
  967. dst.x = ctx->area.x;
  968. dst.y = ctx->area.y + one_third_src_height;
  969. dst.w = one_third_src_width;
  970. dst.h = (float)ctx->area.h - 2 * one_third_src_height;
  971. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  972. /* Right */
  973. src.x = (float)ctx->background_width - one_third_src_width;
  974. dst.x = ctx->area.x + ctx->area.w - one_third_src_width;
  975. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  976. /* Top */
  977. src.x = one_third_src_width;
  978. src.y = 0.0f;
  979. dst.x = ctx->area.x + one_third_src_width;
  980. dst.y = ctx->area.y;
  981. dst.w = ctx->area.w - 2 * one_third_src_width;
  982. dst.h = one_third_src_height;
  983. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  984. /* Bottom */
  985. src.y = (float)ctx->background_height - src.h;
  986. dst.y = ctx->area.y + ctx->area.h - one_third_src_height;
  987. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  988. /* Center */
  989. src.x = one_third_src_width;
  990. src.y = one_third_src_height;
  991. dst.x = ctx->area.x + one_third_src_width;
  992. dst.y = ctx->area.y + one_third_src_height;
  993. dst.w = ctx->area.w - 2 * one_third_src_width;
  994. dst.h = (float)ctx->area.h - 2 * one_third_src_height;
  995. SDL_RenderTexture(ctx->renderer, ctx->background, &src, &dst);
  996. /* Label */
  997. dst.x = ctx->area.x + ctx->area.w / 2 - ctx->label_width / 2;
  998. dst.y = ctx->area.y + ctx->area.h / 2 - ctx->label_height / 2;
  999. SDLTest_DrawString(ctx->renderer, dst.x, dst.y, ctx->label);
  1000. }
  1001. void DestroyGamepadButton(GamepadButton *ctx)
  1002. {
  1003. if (!ctx) {
  1004. return;
  1005. }
  1006. SDL_DestroyTexture(ctx->background);
  1007. SDL_free(ctx->label);
  1008. SDL_free(ctx);
  1009. }