core_input_gamepad.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 2.5 (www.raylib.com)
  12. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  13. *
  14. * Copyright (c) 2013-2019 Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. // NOTE: Gamepad name ID depends on drivers and OS
  19. #define XBOX360_LEGACY_NAME_ID "Xbox Controller"
  20. #if defined(PLATFORM_RPI)
  21. #define XBOX360_NAME_ID "Microsoft X-Box 360 pad"
  22. #define PS3_NAME_ID "PLAYSTATION(R)3 Controller"
  23. #else
  24. #define XBOX360_NAME_ID "Xbox 360 Controller"
  25. #define PS3_NAME_ID "PLAYSTATION(R)3 Controller"
  26. #endif
  27. //------------------------------------------------------------------------------------
  28. // Program main entry point
  29. //------------------------------------------------------------------------------------
  30. int main(void)
  31. {
  32. // Initialization
  33. //--------------------------------------------------------------------------------------
  34. const int screenWidth = 800;
  35. const int screenHeight = 450;
  36. SetConfigFlags(FLAG_MSAA_4X_HINT); // Set MSAA 4X hint before windows creation
  37. InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input");
  38. Texture2D texPs3Pad = LoadTexture("resources/ps3.png");
  39. Texture2D texXboxPad = LoadTexture("resources/xbox.png");
  40. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  41. //--------------------------------------------------------------------------------------
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. // ...
  48. //----------------------------------------------------------------------------------
  49. // Draw
  50. //----------------------------------------------------------------------------------
  51. BeginDrawing();
  52. ClearBackground(RAYWHITE);
  53. if (IsGamepadAvailable(0))
  54. {
  55. DrawText(TextFormat("GP1: %s", GetGamepadName(0)), 10, 10, 10, BLACK);
  56. if (TextIsEqual(GetGamepadName(0), XBOX360_NAME_ID) || TextIsEqual(GetGamepadName(0), XBOX360_LEGACY_NAME_ID))
  57. {
  58. DrawTexture(texXboxPad, 0, 0, DARKGRAY);
  59. // Draw buttons: xbox home
  60. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE)) DrawCircle(394, 89, 19, RED);
  61. // Draw buttons: basic
  62. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawCircle(436, 150, 9, RED);
  63. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawCircle(352, 150, 9, RED);
  64. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(501, 151, 15, BLUE);
  65. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(536, 187, 15, LIME);
  66. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(572, 151, 15, MAROON);
  67. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(536, 115, 15, GOLD);
  68. // Draw buttons: d-pad
  69. DrawRectangle(317, 202, 19, 71, BLACK);
  70. DrawRectangle(293, 228, 69, 19, BLACK);
  71. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(317, 202, 19, 26, RED);
  72. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(317, 202 + 45, 19, 26, RED);
  73. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(292, 228, 25, 19, RED);
  74. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(292 + 44, 228, 26, 19, RED);
  75. // Draw buttons: left-right back
  76. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawCircle(259, 61, 20, RED);
  77. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawCircle(536, 61, 20, RED);
  78. // Draw axis: left joystick
  79. DrawCircle(259, 152, 39, BLACK);
  80. DrawCircle(259, 152, 34, LIGHTGRAY);
  81. DrawCircle(259 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X)*20),
  82. 152 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y)*20), 25, BLACK);
  83. // Draw axis: right joystick
  84. DrawCircle(461, 237, 38, BLACK);
  85. DrawCircle(461, 237, 33, LIGHTGRAY);
  86. DrawCircle(461 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X)*20),
  87. 237 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y)*20), 25, BLACK);
  88. // Draw axis: left-right triggers
  89. DrawRectangle(170, 30, 15, 70, GRAY);
  90. DrawRectangle(604, 30, 15, 70, GRAY);
  91. DrawRectangle(170, 30, 15, (int)(((1 + GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_TRIGGER))/2)*70), RED);
  92. DrawRectangle(604, 30, 15, (int)(((1 + GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_TRIGGER))/2)*70), RED);
  93. //DrawText(TextFormat("Xbox axis LT: %02.02f", GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_TRIGGER)), 10, 40, 10, BLACK);
  94. //DrawText(TextFormat("Xbox axis RT: %02.02f", GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_TRIGGER)), 10, 60, 10, BLACK);
  95. }
  96. else if (TextIsEqual(GetGamepadName(0), PS3_NAME_ID))
  97. {
  98. DrawTexture(texPs3Pad, 0, 0, DARKGRAY);
  99. // Draw buttons: ps
  100. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE)) DrawCircle(396, 222, 13, RED);
  101. // Draw buttons: basic
  102. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE_LEFT)) DrawRectangle(328, 170, 32, 13, RED);
  103. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_MIDDLE_RIGHT)) DrawTriangle((Vector2){ 436, 168 }, (Vector2){ 436, 185 }, (Vector2){ 464, 177 }, RED);
  104. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_UP)) DrawCircle(557, 144, 13, LIME);
  105. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT)) DrawCircle(586, 173, 13, RED);
  106. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_DOWN)) DrawCircle(557, 203, 13, VIOLET);
  107. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_FACE_LEFT)) DrawCircle(527, 173, 13, PINK);
  108. // Draw buttons: d-pad
  109. DrawRectangle(225, 132, 24, 84, BLACK);
  110. DrawRectangle(195, 161, 84, 25, BLACK);
  111. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_UP)) DrawRectangle(225, 132, 24, 29, RED);
  112. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_DOWN)) DrawRectangle(225, 132 + 54, 24, 30, RED);
  113. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) DrawRectangle(195, 161, 30, 25, RED);
  114. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) DrawRectangle(195 + 54, 161, 30, 25, RED);
  115. // Draw buttons: left-right back buttons
  116. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) DrawCircle(239, 82, 20, RED);
  117. if (IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) DrawCircle(557, 82, 20, RED);
  118. // Draw axis: left joystick
  119. DrawCircle(319, 255, 35, BLACK);
  120. DrawCircle(319, 255, 31, LIGHTGRAY);
  121. DrawCircle(319 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X) * 20),
  122. 255 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_Y) * 20), 25, BLACK);
  123. // Draw axis: right joystick
  124. DrawCircle(475, 255, 35, BLACK);
  125. DrawCircle(475, 255, 31, LIGHTGRAY);
  126. DrawCircle(475 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_X) * 20),
  127. 255 + (int)(GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y) * 20), 25, BLACK);
  128. // Draw axis: left-right triggers
  129. DrawRectangle(169, 48, 15, 70, GRAY);
  130. DrawRectangle(611, 48, 15, 70, GRAY);
  131. DrawRectangle(169, 48, 15, (int)(((1 - GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_TRIGGER)) / 2) * 70), RED);
  132. DrawRectangle(611, 48, 15, (int)(((1 - GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_TRIGGER)) / 2) * 70), RED);
  133. }
  134. else
  135. {
  136. DrawText("- GENERIC GAMEPAD -", 280, 180, 20, GRAY);
  137. // TODO: Draw generic gamepad
  138. }
  139. DrawText(TextFormat("DETECTED AXIS [%i]:", GetGamepadAxisCount(0)), 10, 50, 10, MAROON);
  140. for (int i = 0; i < GetGamepadAxisCount(0); i++)
  141. {
  142. DrawText(TextFormat("AXIS %i: %.02f", i, GetGamepadAxisMovement(0, i)), 20, 70 + 20*i, 10, DARKGRAY);
  143. }
  144. if (GetGamepadButtonPressed() != -1) DrawText(TextFormat("DETECTED BUTTON: %i", GetGamepadButtonPressed()), 10, 430, 10, RED);
  145. else DrawText("DETECTED BUTTON: NONE", 10, 430, 10, GRAY);
  146. }
  147. else
  148. {
  149. DrawText("GP1: NOT DETECTED", 10, 10, 10, GRAY);
  150. DrawTexture(texXboxPad, 0, 0, LIGHTGRAY);
  151. }
  152. EndDrawing();
  153. //----------------------------------------------------------------------------------
  154. }
  155. // De-Initialization
  156. //--------------------------------------------------------------------------------------
  157. UnloadTexture(texPs3Pad);
  158. UnloadTexture(texXboxPad);
  159. CloseWindow(); // Close window and OpenGL context
  160. //--------------------------------------------------------------------------------------
  161. return 0;
  162. }