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.

287 lines
16 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Gamepad input
  4. *
  5. * NOTE: This example requires a Gamepad connected to the system
  6. * raylib is configured to work with the following gamepads:
  7. * - Xbox 360 Controller (Xbox 360, Xbox One)
  8. * - PLAYSTATION(R)3 Controller
  9. * Check raylib.h for buttons configuration
  10. *
  11. * Example originally created with raylib 1.1, last time updated with raylib 4.2
  12. *
  13. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  14. * BSD-like license that allows static linking with closed source software
  15. *
  16. * Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. // NOTE: Gamepad name ID depends on drivers and OS
  21. #define XBOX_ALIAS_1 "xbox"
  22. #define XBOX_ALIAS_2 "x-box"
  23. #define PS_ALIAS "playstation"
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. SetConfigFlags(FLAG_MSAA_4X_HINT); // Set MSAA 4X hint before windows creation
  34. InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input");
  35. Texture2D texPs3Pad = LoadTexture("resources/ps3.png");
  36. Texture2D texXboxPad = LoadTexture("resources/xbox.png");
  37. // Set axis deadzones
  38. const float leftStickDeadzoneX = 0.1f;
  39. const float leftStickDeadzoneY = 0.1f;
  40. const float rightStickDeadzoneX = 0.1f;
  41. const float rightStickDeadzoneY = 0.1f;
  42. const float leftTriggerDeadzone = -0.9f;
  43. const float rightTriggerDeadzone = -0.9f;
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. int gamepad = 0; // which gamepad to display
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. // ...
  53. //----------------------------------------------------------------------------------
  54. // Draw
  55. //----------------------------------------------------------------------------------
  56. BeginDrawing();
  57. ClearBackground(RAYWHITE);
  58. if (IsKeyPressed(KEY_LEFT) && gamepad > 0) gamepad--;
  59. if (IsKeyPressed(KEY_RIGHT)) gamepad++;
  60. if (IsGamepadAvailable(gamepad))
  61. {
  62. DrawText(TextFormat("GP%d: %s", gamepad, GetGamepadName(gamepad)), 10, 10, 10, BLACK);
  63. // Get axis values
  64. float leftStickX = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_X);
  65. float leftStickY = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_Y);
  66. float rightStickX = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_X);
  67. float rightStickY = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_Y);
  68. float leftTrigger = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_TRIGGER);
  69. float rightTrigger = GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_TRIGGER);
  70. // Calculate deadzones
  71. if (leftStickX > -leftStickDeadzoneX && leftStickX < leftStickDeadzoneX) leftStickX = 0.0f;
  72. if (leftStickY > -leftStickDeadzoneY && leftStickY < leftStickDeadzoneY) leftStickY = 0.0f;
  73. if (rightStickX > -rightStickDeadzoneX && rightStickX < rightStickDeadzoneX) rightStickX = 0.0f;
  74. if (rightStickY > -rightStickDeadzoneY && rightStickY < rightStickDeadzoneY) rightStickY = 0.0f;
  75. if (leftTrigger < leftTriggerDeadzone) leftTrigger = -1.0f;
  76. if (rightTrigger < rightTriggerDeadzone) rightTrigger = -1.0f;
  77. if (TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_1) > -1 || TextFindIndex(TextToLower(GetGamepadName(gamepad)), XBOX_ALIAS_2) > -1)
  78. {
  79. DrawTexture(texXboxPad, 0, 0, DARKGRAY);
  80. // Draw buttons: xbox home
  81. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE)) DrawCircle(394, 89, 19, RED);
  82. // Draw buttons: basic
  83. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawCircle(436, 150, 9, RED);
  84. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawCircle(352, 150, 9, RED);
  85. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(501, 151, 15, BLUE);
  86. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(536, 187, 15, LIME);
  87. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(572, 151, 15, MAROON);
  88. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(536, 115, 15, GOLD);
  89. // Draw buttons: d-pad
  90. DrawRectangle(317, 202, 19, 71, BLACK);
  91. DrawRectangle(293, 228, 69, 19, BLACK);
  92. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(317, 202, 19, 26, RED);
  93. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(317, 202 + 45, 19, 26, RED);
  94. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(292, 228, 25, 19, RED);
  95. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(292 + 44, 228, 26, 19, RED);
  96. // Draw buttons: left-right back
  97. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawCircle(259, 61, 20, RED);
  98. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawCircle(536, 61, 20, RED);
  99. // Draw axis: left joystick
  100. Color leftGamepadColor = BLACK;
  101. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
  102. DrawCircle(259, 152, 39, BLACK);
  103. DrawCircle(259, 152, 34, LIGHTGRAY);
  104. DrawCircle(259 + (int)(leftStickX*20),
  105. 152 + (int)(leftStickY*20), 25, leftGamepadColor);
  106. // Draw axis: right joystick
  107. Color rightGamepadColor = BLACK;
  108. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
  109. DrawCircle(461, 237, 38, BLACK);
  110. DrawCircle(461, 237, 33, LIGHTGRAY);
  111. DrawCircle(461 + (int)(rightStickX*20),
  112. 237 + (int)(rightStickY*20), 25, rightGamepadColor);
  113. // Draw axis: left-right triggers
  114. DrawRectangle(170, 30, 15, 70, GRAY);
  115. DrawRectangle(604, 30, 15, 70, GRAY);
  116. DrawRectangle(170, 30, 15, (int)(((1 + leftTrigger)/2)*70), RED);
  117. DrawRectangle(604, 30, 15, (int)(((1 + rightTrigger)/2)*70), RED);
  118. //DrawText(TextFormat("Xbox axis LT: %02.02f", GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_LEFT_TRIGGER)), 10, 40, 10, BLACK);
  119. //DrawText(TextFormat("Xbox axis RT: %02.02f", GetGamepadAxisMovement(gamepad, GAMEPAD_AXIS_RIGHT_TRIGGER)), 10, 60, 10, BLACK);
  120. }
  121. else if (TextFindIndex(TextToLower(GetGamepadName(gamepad)), PS_ALIAS) > -1)
  122. {
  123. DrawTexture(texPs3Pad, 0, 0, DARKGRAY);
  124. // Draw buttons: ps
  125. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE)) DrawCircle(396, 222, 13, RED);
  126. // Draw buttons: basic
  127. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawRectangle(328, 170, 32, 13, RED);
  128. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawTriangle((Vector2){ 436, 168 }, (Vector2){ 436, 185 }, (Vector2){ 464, 177 }, RED);
  129. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(557, 144, 13, LIME);
  130. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(586, 173, 13, RED);
  131. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(557, 203, 13, VIOLET);
  132. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(527, 173, 13, PINK);
  133. // Draw buttons: d-pad
  134. DrawRectangle(225, 132, 24, 84, BLACK);
  135. DrawRectangle(195, 161, 84, 25, BLACK);
  136. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(225, 132, 24, 29, RED);
  137. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(225, 132 + 54, 24, 30, RED);
  138. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(195, 161, 30, 25, RED);
  139. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(195 + 54, 161, 30, 25, RED);
  140. // Draw buttons: left-right back buttons
  141. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawCircle(239, 82, 20, RED);
  142. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawCircle(557, 82, 20, RED);
  143. // Draw axis: left joystick
  144. Color leftGamepadColor = BLACK;
  145. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
  146. DrawCircle(319, 255, 35, BLACK);
  147. DrawCircle(319, 255, 31, LIGHTGRAY);
  148. DrawCircle(319 + (int)(leftStickX*20),
  149. 255 + (int)(leftStickY*20), 25, leftGamepadColor);
  150. // Draw axis: right joystick
  151. Color rightGamepadColor = BLACK;
  152. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
  153. DrawCircle(475, 255, 35, BLACK);
  154. DrawCircle(475, 255, 31, LIGHTGRAY);
  155. DrawCircle(475 + (int)(rightStickX*20),
  156. 255 + (int)(rightStickY*20), 25, rightGamepadColor);
  157. // Draw axis: left-right triggers
  158. DrawRectangle(169, 48, 15, 70, GRAY);
  159. DrawRectangle(611, 48, 15, 70, GRAY);
  160. DrawRectangle(169, 48, 15, (int)(((1 + leftTrigger)/2)*70), RED);
  161. DrawRectangle(611, 48, 15, (int)(((1 + rightTrigger)/2)*70), RED);
  162. }
  163. else
  164. {
  165. // Draw background: generic
  166. DrawRectangleRounded((Rectangle){ 175, 110, 460, 220}, 0.3f, 16, DARKGRAY);
  167. // Draw buttons: basic
  168. DrawCircle(365, 170, 12, RAYWHITE);
  169. DrawCircle(405, 170, 12, RAYWHITE);
  170. DrawCircle(445, 170, 12, RAYWHITE);
  171. DrawCircle(516, 191, 17, RAYWHITE);
  172. DrawCircle(551, 227, 17, RAYWHITE);
  173. DrawCircle(587, 191, 17, RAYWHITE);
  174. DrawCircle(551, 155, 17, RAYWHITE);
  175. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawCircle(365, 170, 10, RED);
  176. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE)) DrawCircle(405, 170, 10, GREEN);
  177. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawCircle(445, 170, 10, BLUE);
  178. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(516, 191, 15, GOLD);
  179. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(551, 227, 15, BLUE);
  180. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(587, 191, 15, GREEN);
  181. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(551, 155, 15, RED);
  182. // Draw buttons: d-pad
  183. DrawRectangle(245, 145, 28, 88, RAYWHITE);
  184. DrawRectangle(215, 174, 88, 29, RAYWHITE);
  185. DrawRectangle(247, 147, 24, 84, BLACK);
  186. DrawRectangle(217, 176, 84, 25, BLACK);
  187. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(247, 147, 24, 29, RED);
  188. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(247, 147 + 54, 24, 30, RED);
  189. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(217, 176, 30, 25, RED);
  190. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(217 + 54, 176, 30, 25, RED);
  191. // Draw buttons: left-right back
  192. DrawRectangleRounded((Rectangle){ 215, 98, 100, 10}, 0.5f, 16, DARKGRAY);
  193. DrawRectangleRounded((Rectangle){ 495, 98, 100, 10}, 0.5f, 16, DARKGRAY);
  194. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawRectangleRounded((Rectangle){ 215, 98, 100, 10}, 0.5f, 16, RED);
  195. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawRectangleRounded((Rectangle){ 495, 98, 100, 10}, 0.5f, 16, RED);
  196. // Draw axis: left joystick
  197. Color leftGamepadColor = BLACK;
  198. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_LEFT_THUMB)) leftGamepadColor = RED;
  199. DrawCircle(345, 260, 40, BLACK);
  200. DrawCircle(345, 260, 35, LIGHTGRAY);
  201. DrawCircle(345 + (int)(leftStickX*20),
  202. 260 + (int)(leftStickY*20), 25, leftGamepadColor);
  203. // Draw axis: right joystick
  204. Color rightGamepadColor = BLACK;
  205. if (IsGamepadButtonDown(gamepad, GAMEPAD_BUTTON_RIGHT_THUMB)) rightGamepadColor = RED;
  206. DrawCircle(465, 260, 40, BLACK);
  207. DrawCircle(465, 260, 35, LIGHTGRAY);
  208. DrawCircle(465 + (int)(rightStickX*20),
  209. 260 + (int)(rightStickY*20), 25, rightGamepadColor);
  210. // Draw axis: left-right triggers
  211. DrawRectangle(151, 110, 15, 70, GRAY);
  212. DrawRectangle(644, 110, 15, 70, GRAY);
  213. DrawRectangle(151, 110, 15, (int)(((1 + leftTrigger)/2)*70), RED);
  214. DrawRectangle(644, 110, 15, (int)(((1 + rightTrigger)/2)*70), RED);
  215. }
  216. DrawText(TextFormat("DETECTED AXIS [%i]:", GetGamepadAxisCount(0)), 10, 50, 10, MAROON);
  217. for (int i = 0; i < GetGamepadAxisCount(0); i++)
  218. {
  219. DrawText(TextFormat("AXIS %i: %.02f", i, GetGamepadAxisMovement(0, i)), 20, 70 + 20*i, 10, DARKGRAY);
  220. }
  221. if (GetGamepadButtonPressed() != GAMEPAD_BUTTON_UNKNOWN) DrawText(TextFormat("DETECTED BUTTON: %i", GetGamepadButtonPressed()), 10, 430, 10, RED);
  222. else DrawText("DETECTED BUTTON: NONE", 10, 430, 10, GRAY);
  223. }
  224. else
  225. {
  226. DrawText(TextFormat("GP%d: NOT DETECTED", gamepad), 10, 10, 10, GRAY);
  227. DrawTexture(texXboxPad, 0, 0, LIGHTGRAY);
  228. }
  229. EndDrawing();
  230. //----------------------------------------------------------------------------------
  231. }
  232. // De-Initialization
  233. //--------------------------------------------------------------------------------------
  234. UnloadTexture(texPs3Pad);
  235. UnloadTexture(texXboxPad);
  236. CloseWindow(); // Close window and OpenGL context
  237. //--------------------------------------------------------------------------------------
  238. return 0;
  239. }