core_input_gamepad.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * This example has been created using raylib 1.7 (www.raylib.com)
  12. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  13. *
  14. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #if defined(PLATFORM_WEB)
  19. #include <emscripten/emscripten.h>
  20. #endif
  21. // NOTE: Gamepad name ID depends on drivers and OS
  22. #if defined(PLATFORM_RPI)
  23. #define XBOX360_NAME_ID "Microsoft X-Box 360 pad"
  24. #define PS3_NAME_ID "PLAYSTATION(R)3 Controller"
  25. #else
  26. #define XBOX360_NAME_ID "Xbox 360 Controller"
  27. #define PS3_NAME_ID "PLAYSTATION(R)3 Controller"
  28. #endif
  29. //----------------------------------------------------------------------------------
  30. // Global Variables Definition
  31. //----------------------------------------------------------------------------------
  32. int screenWidth = 800;
  33. int screenHeight = 450;
  34. Texture2D texPs3Pad;
  35. Texture2D texXboxPad;
  36. //----------------------------------------------------------------------------------
  37. // Module Functions Declaration
  38. //----------------------------------------------------------------------------------
  39. void UpdateDrawFrame(void); // Update and Draw one frame
  40. //----------------------------------------------------------------------------------
  41. // Main Enry Point
  42. //----------------------------------------------------------------------------------
  43. int main()
  44. {
  45. // Initialization
  46. //--------------------------------------------------------------------------------------
  47. InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input");
  48. texPs3Pad = LoadTexture("resources/ps3.png");
  49. texXboxPad = LoadTexture("resources/xbox.png");
  50. #if defined(PLATFORM_WEB)
  51. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  52. #else
  53. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  54. //--------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. UpdateDrawFrame();
  59. }
  60. #endif
  61. // De-Initialization
  62. //--------------------------------------------------------------------------------------
  63. UnloadTexture(texPs3Pad);
  64. UnloadTexture(texXboxPad);
  65. CloseWindow(); // Close window and OpenGL context
  66. //--------------------------------------------------------------------------------------
  67. return 0;
  68. }
  69. //----------------------------------------------------------------------------------
  70. // Module Functions Definition
  71. //----------------------------------------------------------------------------------
  72. void UpdateDrawFrame(void)
  73. {
  74. // Update
  75. //----------------------------------------------------------------------------------
  76. // ...
  77. //----------------------------------------------------------------------------------
  78. // Draw
  79. //----------------------------------------------------------------------------------
  80. BeginDrawing();
  81. ClearBackground(RAYWHITE);
  82. if (IsGamepadAvailable(GAMEPAD_PLAYER1))
  83. {
  84. DrawText(FormatText("GP1: %s", GetGamepadName(GAMEPAD_PLAYER1)), 10, 10, 10, BLACK);
  85. if (IsGamepadName(GAMEPAD_PLAYER1, XBOX360_NAME_ID))
  86. {
  87. DrawTexture(texXboxPad, 0, 0, DARKGRAY);
  88. // Draw buttons: xbox home
  89. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_HOME)) DrawCircle(394, 89, 19, RED);
  90. // Draw buttons: basic
  91. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_START)) DrawCircle(436, 150, 9, RED);
  92. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_SELECT)) DrawCircle(352, 150, 9, RED);
  93. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_X)) DrawCircle(501, 151, 15, BLUE);
  94. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_A)) DrawCircle(536, 187, 15, LIME);
  95. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_B)) DrawCircle(572, 151, 15, MAROON);
  96. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_Y)) DrawCircle(536, 115, 15, GOLD);
  97. // Draw buttons: d-pad
  98. DrawRectangle(317, 202, 19, 71, BLACK);
  99. DrawRectangle(293, 228, 69, 19, BLACK);
  100. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_UP)) DrawRectangle(317, 202, 19, 26, RED);
  101. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_DOWN)) DrawRectangle(317, 202 + 45, 19, 26, RED);
  102. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_LEFT)) DrawRectangle(292, 228, 25, 19, RED);
  103. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_RIGHT)) DrawRectangle(292 + 44, 228, 26, 19, RED);
  104. // Draw buttons: left-right back
  105. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_LB)) DrawCircle(259, 61, 20, RED);
  106. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_XBOX_BUTTON_RB)) DrawCircle(536, 61, 20, RED);
  107. // Draw axis: left joystick
  108. DrawCircle(259, 152, 39, BLACK);
  109. DrawCircle(259, 152, 34, LIGHTGRAY);
  110. DrawCircle(259 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_LEFT_X)*20),
  111. 152 - (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_LEFT_Y)*20), 25, BLACK);
  112. // Draw axis: right joystick
  113. DrawCircle(461, 237, 38, BLACK);
  114. DrawCircle(461, 237, 33, LIGHTGRAY);
  115. DrawCircle(461 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_RIGHT_X)*20),
  116. 237 - (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_RIGHT_Y)*20), 25, BLACK);
  117. // Draw axis: left-right triggers
  118. DrawRectangle(170, 30, 15, 70, GRAY);
  119. DrawRectangle(604, 30, 15, 70, GRAY);
  120. DrawRectangle(170, 30, 15, (((1.0f + GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_LT))/2.0f)*70), RED);
  121. DrawRectangle(604, 30, 15, (((1.0f + GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_RT))/2.0f)*70), RED);
  122. //DrawText(FormatText("Xbox axis LT: %02.02f", GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_LT)), 10, 40, 10, BLACK);
  123. //DrawText(FormatText("Xbox axis RT: %02.02f", GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_XBOX_AXIS_RT)), 10, 60, 10, BLACK);
  124. }
  125. else if (IsGamepadName(GAMEPAD_PLAYER1, PS3_NAME_ID))
  126. {
  127. DrawTexture(texPs3Pad, 0, 0, DARKGRAY);
  128. // Draw buttons: ps
  129. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_PS)) DrawCircle(396, 222, 13, RED);
  130. // Draw buttons: basic
  131. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_SELECT)) DrawRectangle(328, 170, 32, 13, RED);
  132. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_START)) DrawTriangle((Vector2){ 436, 168 }, (Vector2){ 436, 185 }, (Vector2){ 464, 177 }, RED);
  133. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_TRIANGLE)) DrawCircle(557, 144, 13, LIME);
  134. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_CIRCLE)) DrawCircle(586, 173, 13, RED);
  135. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_CROSS)) DrawCircle(557, 203, 13, VIOLET);
  136. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_SQUARE)) DrawCircle(527, 173, 13, PINK);
  137. // Draw buttons: d-pad
  138. DrawRectangle(225, 132, 24, 84, BLACK);
  139. DrawRectangle(195, 161, 84, 25, BLACK);
  140. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_UP)) DrawRectangle(225, 132, 24, 29, RED);
  141. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_DOWN)) DrawRectangle(225, 132 + 54, 24, 30, RED);
  142. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_LEFT)) DrawRectangle(195, 161, 30, 25, RED);
  143. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_RIGHT)) DrawRectangle(195 + 54, 161, 30, 25, RED);
  144. // Draw buttons: left-right back buttons
  145. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_L1)) DrawCircle(239, 82, 20, RED);
  146. if (IsGamepadButtonDown(GAMEPAD_PLAYER1, GAMEPAD_PS3_BUTTON_R1)) DrawCircle(557, 82, 20, RED);
  147. // Draw axis: left joystick
  148. DrawCircle(319, 255, 35, BLACK);
  149. DrawCircle(319, 255, 31, LIGHTGRAY);
  150. DrawCircle(319 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_PS3_AXIS_LEFT_X)*20),
  151. 255 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_PS3_AXIS_LEFT_Y)*20), 25, BLACK);
  152. // Draw axis: right joystick
  153. DrawCircle(475, 255, 35, BLACK);
  154. DrawCircle(475, 255, 31, LIGHTGRAY);
  155. DrawCircle(475 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_PS3_AXIS_RIGHT_X)*20),
  156. 255 + (GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_PS3_AXIS_RIGHT_Y)*20), 25, BLACK);
  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, (((1.0f - GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_PS3_AXIS_L2))/2.0f)*70), RED);
  161. DrawRectangle(611, 48, 15, (((1.0f - GetGamepadAxisMovement(GAMEPAD_PLAYER1, GAMEPAD_PS3_AXIS_R2))/2.0f)*70), RED);
  162. }
  163. else
  164. {
  165. DrawText("- GENERIC GAMEPAD -", 280, 180, 20, GRAY);
  166. // TODO: Draw generic gamepad
  167. }
  168. DrawText(FormatText("DETECTED AXIS [%i]:", GetGamepadAxisCount(GAMEPAD_PLAYER1)), 10, 50, 10, MAROON);
  169. for (int i = 0; i < GetGamepadAxisCount(GAMEPAD_PLAYER1); i++)
  170. {
  171. DrawText(FormatText("AXIS %i: %.02f", i, GetGamepadAxisMovement(GAMEPAD_PLAYER1, i)), 20, 70 + 20*i, 10, DARKGRAY);
  172. }
  173. if (GetGamepadButtonPressed() != -1) DrawText(FormatText("DETECTED BUTTON: %i", GetGamepadButtonPressed()), 10, 430, 10, RED);
  174. else DrawText("DETECTED BUTTON: NONE", 10, 430, 10, GRAY);
  175. }
  176. else
  177. {
  178. DrawText("GP1: NOT DETECTED", 10, 10, 10, GRAY);
  179. DrawTexture(texXboxPad, 0, 0, LIGHTGRAY);
  180. }
  181. EndDrawing();
  182. //----------------------------------------------------------------------------------
  183. }