shapes_starfield_effect.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - starfield effect
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.5, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by JP Mortiboys (@themushroompirates) 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 JP Mortiboys (@themushroompirates)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h" // Required for: Lerp()
  19. #define STAR_COUNT 420
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - starfield effect");
  30. Color bgColor = ColorLerp(DARKBLUE, BLACK, 0.69f);
  31. // Speed at which we fly forward
  32. float speed = 10.0f/9.0f;
  33. // We're either drawing lines or circles
  34. bool drawLines = true;
  35. Vector3 stars[STAR_COUNT] = { 0 };
  36. Vector2 starsScreenPos[STAR_COUNT] = { 0 };
  37. // Setup the stars with a random position
  38. for (int i = 0; i < STAR_COUNT; i++)
  39. {
  40. stars[i].x = GetRandomValue(-screenWidth*0.5f, screenWidth*0.5f);
  41. stars[i].y = GetRandomValue(-screenHeight*0.5f, screenHeight*0.5f);
  42. stars[i].z = 1.0f;
  43. }
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. // Change speed based on mouse
  52. float mouseMove = GetMouseWheelMove();
  53. if ((int)mouseMove != 0) speed += 2.0f*mouseMove/9.0f;
  54. if (speed < 0.0f) speed = 0.1f;
  55. else if (speed > 2.0f) speed = 2.0f;
  56. // Toggle lines / points with space bar
  57. if (IsKeyPressed(KEY_SPACE)) drawLines = !drawLines;
  58. float dt = GetFrameTime();
  59. for (int i = 0; i < STAR_COUNT; i++)
  60. {
  61. // Update star's timer
  62. stars[i].z -= dt*speed;
  63. // Calculate the screen position
  64. starsScreenPos[i] = (Vector2){
  65. screenWidth*0.5f + stars[i].x/stars[i].z,
  66. screenHeight*0.5f + stars[i].y/stars[i].z,
  67. };
  68. // If the star is too old, or offscreen, it dies and we make a new random one
  69. if ((stars[i].z < 0.0f) || (starsScreenPos[i].x < 0) || (starsScreenPos[i].y < 0.0f) ||
  70. (starsScreenPos[i].x > screenWidth) || (starsScreenPos[i].y > screenHeight))
  71. {
  72. stars[i].x = GetRandomValue(-screenWidth*0.5f, screenWidth*0.5f);
  73. stars[i].y = GetRandomValue(-screenHeight*0.5f, screenHeight*0.5f);
  74. stars[i].z = 1.0f;
  75. }
  76. }
  77. //----------------------------------------------------------------------------------
  78. // Draw
  79. //----------------------------------------------------------------------------------
  80. BeginDrawing();
  81. ClearBackground(bgColor);
  82. for (int i = 0; i < STAR_COUNT; i++)
  83. {
  84. if (drawLines)
  85. {
  86. // Get the time a little while ago for this star, but clamp it
  87. float t = Clamp(stars[i].z + 1.0f/32.0f, 0.0f, 1.0f);
  88. // If it's different enough from the current time, we proceed
  89. if ((t - stars[i].z) > 1e-3)
  90. {
  91. // Calculate the screen position of the old point
  92. Vector2 startPos = (Vector2){
  93. screenWidth*0.5f + stars[i].x/t,
  94. screenHeight*0.5f + stars[i].y/t,
  95. };
  96. // Draw a line connecting the old point to the current point
  97. DrawLineV(startPos, starsScreenPos[i], RAYWHITE);
  98. }
  99. }
  100. else
  101. {
  102. // Make the radius grow as the star ages
  103. float radius = Lerp(stars[i].z, 1.0f, 5.0f);
  104. // Draw the circle
  105. DrawCircleV(starsScreenPos[i], radius, RAYWHITE);
  106. }
  107. }
  108. DrawText(TextFormat("[MOUSE WHEEL] Current Speed: %.0f", 9.0f*speed/2.0f), 10, 40, 20, RAYWHITE);
  109. DrawText(TextFormat("[SPACE] Current draw mode: %s", drawLines ? "Lines" : "Circles"), 10, 70, 20, RAYWHITE);
  110. DrawFPS(10, 10);
  111. EndDrawing();
  112. //----------------------------------------------------------------------------------
  113. }
  114. // De-Initialization
  115. //--------------------------------------------------------------------------------------
  116. CloseWindow(); // Close window and OpenGL context
  117. //--------------------------------------------------------------------------------------
  118. return 0;
  119. }