2
0

ex03c_input_gamepad.c 2.5 KB

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