🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
12 KiB

  1. /*
  2. Copyright (C) 1997-2020 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. /* Simple program to test the SDL game controller routines */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "SDL.h"
  15. #ifdef __EMSCRIPTEN__
  16. #include <emscripten/emscripten.h>
  17. #endif
  18. #ifndef SDL_JOYSTICK_DISABLED
  19. #ifdef __IPHONEOS__
  20. #define SCREEN_WIDTH 480
  21. #define SCREEN_HEIGHT 320
  22. #else
  23. #define SCREEN_WIDTH 512
  24. #define SCREEN_HEIGHT 320
  25. #endif
  26. /* This is indexed by SDL_GameControllerButton. */
  27. static const struct { int x; int y; } button_positions[] = {
  28. {387, 167}, /* A */
  29. {431, 132}, /* B */
  30. {342, 132}, /* X */
  31. {389, 101}, /* Y */
  32. {174, 132}, /* BACK */
  33. {233, 132}, /* GUIDE */
  34. {289, 132}, /* START */
  35. {75, 154}, /* LEFTSTICK */
  36. {305, 230}, /* RIGHTSTICK */
  37. {77, 40}, /* LEFTSHOULDER */
  38. {396, 36}, /* RIGHTSHOULDER */
  39. {154, 188}, /* DPAD_UP */
  40. {154, 249}, /* DPAD_DOWN */
  41. {116, 217}, /* DPAD_LEFT */
  42. {186, 217}, /* DPAD_RIGHT */
  43. };
  44. /* This is indexed by SDL_GameControllerAxis. */
  45. static const struct { int x; int y; double angle; } axis_positions[] = {
  46. {74, 153, 270.0}, /* LEFTX */
  47. {74, 153, 0.0}, /* LEFTY */
  48. {306, 231, 270.0}, /* RIGHTX */
  49. {306, 231, 0.0}, /* RIGHTY */
  50. {91, -20, 0.0}, /* TRIGGERLEFT */
  51. {375, -20, 0.0}, /* TRIGGERRIGHT */
  52. };
  53. SDL_Window *window = NULL;
  54. SDL_Renderer *screen = NULL;
  55. SDL_bool retval = SDL_FALSE;
  56. SDL_bool done = SDL_FALSE;
  57. SDL_Texture *background, *button, *axis;
  58. SDL_GameController *gamecontroller;
  59. static SDL_Texture *
  60. LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
  61. {
  62. SDL_Surface *temp = NULL;
  63. SDL_Texture *texture = NULL;
  64. temp = SDL_LoadBMP(file);
  65. if (temp == NULL) {
  66. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  67. } else {
  68. /* Set transparent pixel as the pixel at (0,0) */
  69. if (transparent) {
  70. if (temp->format->BytesPerPixel == 1) {
  71. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
  72. }
  73. }
  74. texture = SDL_CreateTextureFromSurface(renderer, temp);
  75. if (!texture) {
  76. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  77. }
  78. }
  79. if (temp) {
  80. SDL_FreeSurface(temp);
  81. }
  82. return texture;
  83. }
  84. static void
  85. UpdateWindowTitle()
  86. {
  87. const char *name = SDL_GameControllerName(gamecontroller);
  88. const char *basetitle = "Game Controller Test: ";
  89. const size_t titlelen = SDL_strlen(basetitle) + SDL_strlen(name) + 1;
  90. char *title = (char *)SDL_malloc(titlelen);
  91. retval = SDL_FALSE;
  92. done = SDL_FALSE;
  93. if (title) {
  94. SDL_snprintf(title, titlelen, "%s%s", basetitle, name);
  95. SDL_SetWindowTitle(window, title);
  96. SDL_free(title);
  97. }
  98. }
  99. void
  100. loop(void *arg)
  101. {
  102. SDL_Event event;
  103. int i;
  104. /* blank screen, set up for drawing this frame. */
  105. SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE);
  106. SDL_RenderClear(screen);
  107. SDL_RenderCopy(screen, background, NULL, NULL);
  108. while (SDL_PollEvent(&event)) {
  109. switch (event.type) {
  110. case SDL_CONTROLLERDEVICEADDED:
  111. SDL_Log("Game controller device %d added.\n", (int) event.cdevice.which);
  112. if (!gamecontroller) {
  113. gamecontroller = SDL_GameControllerOpen(event.cdevice.which);
  114. if (gamecontroller) {
  115. UpdateWindowTitle();
  116. } else {
  117. SDL_Log("Couldn't open controller: %s\n", SDL_GetError());
  118. }
  119. }
  120. break;
  121. case SDL_CONTROLLERDEVICEREMOVED:
  122. SDL_Log("Game controller device %d removed.\n", (int) event.cdevice.which);
  123. if (gamecontroller && event.cdevice.which == SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamecontroller))) {
  124. SDL_GameControllerClose(gamecontroller);
  125. gamecontroller = SDL_GameControllerOpen(0);
  126. if (gamecontroller) {
  127. UpdateWindowTitle();
  128. }
  129. }
  130. break;
  131. case SDL_CONTROLLERAXISMOTION:
  132. SDL_Log("Controller axis %s changed to %d\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value);
  133. break;
  134. case SDL_CONTROLLERBUTTONDOWN:
  135. case SDL_CONTROLLERBUTTONUP:
  136. SDL_Log("Controller button %s %s\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released");
  137. break;
  138. case SDL_KEYDOWN:
  139. if (event.key.keysym.sym != SDLK_ESCAPE) {
  140. break;
  141. }
  142. /* Fall through to signal quit */
  143. case SDL_QUIT:
  144. done = SDL_TRUE;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. if (gamecontroller) {
  151. /* Update visual controller state */
  152. for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) {
  153. if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) {
  154. const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 };
  155. SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, SDL_FLIP_NONE);
  156. }
  157. }
  158. for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) {
  159. const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */
  160. const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i));
  161. if (value < -deadzone) {
  162. const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
  163. const double angle = axis_positions[i].angle;
  164. SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  165. } else if (value > deadzone) {
  166. const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 };
  167. const double angle = axis_positions[i].angle + 180.0;
  168. SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE);
  169. }
  170. }
  171. /* Update rumble based on trigger state */
  172. {
  173. Uint16 low_frequency_rumble = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERLEFT) * 2;
  174. Uint16 high_frequency_rumble = SDL_GameControllerGetAxis(gamecontroller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) * 2;
  175. SDL_GameControllerRumble(gamecontroller, low_frequency_rumble, high_frequency_rumble, 250);
  176. }
  177. }
  178. SDL_RenderPresent(screen);
  179. #ifdef __EMSCRIPTEN__
  180. if (done) {
  181. emscripten_cancel_main_loop();
  182. }
  183. #endif
  184. }
  185. int
  186. main(int argc, char *argv[])
  187. {
  188. int i;
  189. int nController = 0;
  190. char guid[64];
  191. /* Enable standard application logging */
  192. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  193. /* Initialize SDL (Note: video is required to start event loop) */
  194. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER ) < 0) {
  195. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  196. return 1;
  197. }
  198. SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt");
  199. /* Print information about the mappings */
  200. if (argv[1] && SDL_strcmp(argv[1], "--mappings") == 0) {
  201. SDL_Log("Supported mappings:\n");
  202. for (i = 0; i < SDL_GameControllerNumMappings(); ++i) {
  203. char *mapping = SDL_GameControllerMappingForIndex(i);
  204. if (mapping) {
  205. SDL_Log("\t%s\n", mapping);
  206. SDL_free(mapping);
  207. }
  208. }
  209. SDL_Log("\n");
  210. }
  211. /* Print information about the controller */
  212. for (i = 0; i < SDL_NumJoysticks(); ++i) {
  213. const char *name;
  214. const char *description;
  215. SDL_JoystickGetGUIDString(SDL_JoystickGetDeviceGUID(i),
  216. guid, sizeof (guid));
  217. if ( SDL_IsGameController(i) ) {
  218. nController++;
  219. name = SDL_GameControllerNameForIndex(i);
  220. switch (SDL_GameControllerTypeForIndex(i)) {
  221. case SDL_CONTROLLER_TYPE_XBOX360:
  222. description = "XBox 360 Controller";
  223. break;
  224. case SDL_CONTROLLER_TYPE_XBOXONE:
  225. description = "XBox One Controller";
  226. break;
  227. case SDL_CONTROLLER_TYPE_PS3:
  228. description = "PS3 Controller";
  229. break;
  230. case SDL_CONTROLLER_TYPE_PS4:
  231. description = "PS4 Controller";
  232. break;
  233. case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO:
  234. description = "Nintendo Switch Pro Controller";
  235. break;
  236. case SDL_CONTROLLER_TYPE_VIRTUAL:
  237. description = "Virtual Game Controller";
  238. break;
  239. default:
  240. description = "Game Controller";
  241. break;
  242. }
  243. } else {
  244. name = SDL_JoystickNameForIndex(i);
  245. description = "Joystick";
  246. }
  247. SDL_Log("%s %d: %s (guid %s, VID 0x%.4x, PID 0x%.4x, player index = %d)\n",
  248. description, i, name ? name : "Unknown", guid,
  249. SDL_JoystickGetDeviceVendor(i), SDL_JoystickGetDeviceProduct(i), SDL_JoystickGetDevicePlayerIndex(i));
  250. }
  251. SDL_Log("There are %d game controller(s) attached (%d joystick(s))\n", nController, SDL_NumJoysticks());
  252. /* Create a window to display controller state */
  253. window = SDL_CreateWindow("Game Controller Test", SDL_WINDOWPOS_CENTERED,
  254. SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
  255. SCREEN_HEIGHT, 0);
  256. if (window == NULL) {
  257. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
  258. return 2;
  259. }
  260. screen = SDL_CreateRenderer(window, -1, 0);
  261. if (screen == NULL) {
  262. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
  263. SDL_DestroyWindow(window);
  264. return 2;
  265. }
  266. SDL_SetRenderDrawColor(screen, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  267. SDL_RenderClear(screen);
  268. SDL_RenderPresent(screen);
  269. /* scale for platforms that don't give you the window size you asked for. */
  270. SDL_RenderSetLogicalSize(screen, SCREEN_WIDTH, SCREEN_HEIGHT);
  271. background = LoadTexture(screen, "controllermap.bmp", SDL_FALSE);
  272. button = LoadTexture(screen, "button.bmp", SDL_TRUE);
  273. axis = LoadTexture(screen, "axis.bmp", SDL_TRUE);
  274. if (!background || !button || !axis) {
  275. SDL_DestroyRenderer(screen);
  276. SDL_DestroyWindow(window);
  277. return 2;
  278. }
  279. SDL_SetTextureColorMod(button, 10, 255, 21);
  280. SDL_SetTextureColorMod(axis, 10, 255, 21);
  281. /* !!! FIXME: */
  282. /*SDL_RenderSetLogicalSize(screen, background->w, background->h);*/
  283. /* Loop, getting controller events! */
  284. #ifdef __EMSCRIPTEN__
  285. emscripten_set_main_loop_arg(loop, NULL, 0, 1);
  286. #else
  287. while (!done) {
  288. loop(NULL);
  289. }
  290. #endif
  291. SDL_DestroyRenderer(screen);
  292. screen = NULL;
  293. background = NULL;
  294. button = NULL;
  295. axis = NULL;
  296. SDL_DestroyWindow(window);
  297. SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER);
  298. return 0;
  299. }
  300. #else
  301. int
  302. main(int argc, char *argv[])
  303. {
  304. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
  305. return 1;
  306. }
  307. #endif
  308. /* vi: set ts=4 sw=4 expandtab: */