shapes_mouse_trail.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - Draw a mouse trail (position history)
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 5.6
  8. *
  9. * Example contributed by Balamurugan R (@Bala050814]) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Balamurugan R (@Bala050814)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h"
  19. // Define the maximum number of positions to store in the trail
  20. #define MAX_TRAIL_LENGTH 30
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - mouse trail");
  31. // Array to store the history of mouse positions (our fixed-size queue)
  32. Vector2 trailPositions[MAX_TRAIL_LENGTH] = { 0 };
  33. SetTargetFPS(60);
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. Vector2 mousePosition = GetMousePosition();
  41. // Shift all existing positions backward by one slot in the array
  42. // The last element (the oldest position) is dropped
  43. for (int i = MAX_TRAIL_LENGTH - 1; i > 0; i--)
  44. {
  45. trailPositions[i] = trailPositions[i - 1];
  46. }
  47. // Store the new, current mouse position at the start of the array (Index 0)
  48. trailPositions[0] = mousePosition;
  49. //----------------------------------------------------------------------------------
  50. // Draw
  51. //----------------------------------------------------------------------------------
  52. BeginDrawing();
  53. ClearBackground(BLACK);
  54. // Draw the trail by looping through the history array
  55. for (int i = 0; i < MAX_TRAIL_LENGTH; i++)
  56. {
  57. // Ensure we skip drawing if the array hasn't been fully filled on startup
  58. if ((trailPositions[i].x != 0.0f) || (trailPositions[i].y != 0.0f))
  59. {
  60. // Calculate relative trail strength (ratio is near 1.0 for new, near 0.0 for old)
  61. float ratio = (float)(MAX_TRAIL_LENGTH - i)/MAX_TRAIL_LENGTH;
  62. // Fade effect: oldest positions are more transparent
  63. // Fade (color, alpha) - alpha is 0.5 to 1.0 based on ratio
  64. Color trailColor = Fade(SKYBLUE, ratio*0.5f + 0.5f);
  65. // Size effect: oldest positions are smaller
  66. float trailRadius = 15.0f*ratio;
  67. DrawCircleV(trailPositions[i], trailRadius, trailColor);
  68. }
  69. }
  70. // Draw a distinct white circle for the current mouse position (Index 0)
  71. DrawCircleV(mousePosition, 15.0f, WHITE);
  72. DrawText("Move the mouse to see the trail effect!", 10, screenHeight - 30, 20, LIGHTGRAY);
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }