您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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