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.

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