core_input_gamepad.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 Xbox 360 gamepad, check raylib.h for buttons configuration
  7. *
  8. * This example has been created using raylib 1.0 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. int main()
  16. {
  17. // Initialization
  18. //--------------------------------------------------------------------------------------
  19. int screenWidth = 800;
  20. int screenHeight = 450;
  21. InitWindow(screenWidth, screenHeight, "raylib [core] example - gamepad input");
  22. Vector2 ballPosition = { screenWidth/2, screenHeight/2 };
  23. Vector2 gamepadMovement = { 0, 0 };
  24. SetTargetFPS(60); // Set target frames-per-second
  25. //--------------------------------------------------------------------------------------
  26. // Main game loop
  27. while (!WindowShouldClose()) // Detect window close button or ESC key
  28. {
  29. // Update
  30. //----------------------------------------------------------------------------------
  31. if (IsGamepadAvailable(GAMEPAD_PLAYER1))
  32. {
  33. gamepadMovement = GetGamepadMovement(GAMEPAD_PLAYER1);
  34. ballPosition.x += gamepadMovement.x;
  35. ballPosition.y -= gamepadMovement.y;
  36. if (IsGamepadButtonPressed(GAMEPAD_PLAYER1, GAMEPAD_BUTTON_A))
  37. {
  38. ballPosition.x = screenWidth/2;
  39. ballPosition.y = screenHeight/2;
  40. }
  41. }
  42. //----------------------------------------------------------------------------------
  43. // Draw
  44. //----------------------------------------------------------------------------------
  45. BeginDrawing();
  46. ClearBackground(RAYWHITE);
  47. DrawText("move the ball with gamepad", 10, 10, 20, DARKGRAY);
  48. DrawCircleV(ballPosition, 50, MAROON);
  49. EndDrawing();
  50. //----------------------------------------------------------------------------------
  51. }
  52. // De-Initialization
  53. //--------------------------------------------------------------------------------------
  54. CloseWindow(); // Close window and OpenGL context
  55. //--------------------------------------------------------------------------------------
  56. return 0;
  57. }