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.

193 lines
11 KiB

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