You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib example - particles blending
  4. *
  5. * This example has been created using raylib 1.7 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #define MAX_PARTICLES 200
  13. // Particle structure with basic data
  14. typedef struct {
  15. Vector2 position;
  16. Color color;
  17. float alpha;
  18. float size;
  19. float rotation;
  20. bool active; // NOTE: Use it to activate/deactive particle
  21. } Particle;
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [textures] example - particles blending");
  29. // Particles pool, reuse them!
  30. Particle mouseTail[MAX_PARTICLES] = { 0 };
  31. // Initialize particles
  32. for (int i = 0; i < MAX_PARTICLES; i++)
  33. {
  34. mouseTail[i].position = (Vector2){ 0, 0 };
  35. mouseTail[i].color = (Color){ GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255 };
  36. mouseTail[i].alpha = 1.0f;
  37. mouseTail[i].size = (float)GetRandomValue(1, 30)/20.0f;
  38. mouseTail[i].rotation = (float)GetRandomValue(0, 360);
  39. mouseTail[i].active = false;
  40. }
  41. float gravity = 3.0f;
  42. Texture2D smoke = LoadTexture("resources/smoke.png");
  43. int blending = BLEND_ALPHA;
  44. SetTargetFPS(60);
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. // Activate one particle every frame and Update active particles
  52. // NOTE: Particles initial position should be mouse position when activated
  53. // NOTE: Particles fall down with gravity and rotation... and disappear after 2 seconds (alpha = 0)
  54. // NOTE: When a particle disappears, active = false and it can be reused.
  55. for (int i = 0; i < MAX_PARTICLES; i++)
  56. {
  57. if (!mouseTail[i].active)
  58. {
  59. mouseTail[i].active = true;
  60. mouseTail[i].alpha = 1.0f;
  61. mouseTail[i].position = GetMousePosition();
  62. i = MAX_PARTICLES;
  63. }
  64. }
  65. for (int i = 0; i < MAX_PARTICLES; i++)
  66. {
  67. if (mouseTail[i].active)
  68. {
  69. mouseTail[i].position.y += gravity;
  70. mouseTail[i].alpha -= 0.01f;
  71. if (mouseTail[i].alpha <= 0.0f) mouseTail[i].active = false;
  72. mouseTail[i].rotation += 5.0f;
  73. }
  74. }
  75. if (IsKeyPressed(KEY_SPACE))
  76. {
  77. if (blending == BLEND_ALPHA) blending = BLEND_ADDITIVE;
  78. else blending = BLEND_ALPHA;
  79. }
  80. //----------------------------------------------------------------------------------
  81. // Draw
  82. //----------------------------------------------------------------------------------
  83. BeginDrawing();
  84. ClearBackground(DARKGRAY);
  85. BeginBlendMode(blending);
  86. // Draw active particles
  87. for (int i = 0; i < MAX_PARTICLES; i++)
  88. {
  89. if (mouseTail[i].active) DrawTexturePro(smoke, (Rectangle){ 0.0f, 0.0f, (float)smoke.width, (float)smoke.height },
  90. (Rectangle){ mouseTail[i].position.x, mouseTail[i].position.y, smoke.width*mouseTail[i].size, smoke.height*mouseTail[i].size },
  91. (Vector2){ (float)(smoke.width*mouseTail[i].size/2.0f), (float)(smoke.height*mouseTail[i].size/2.0f) }, mouseTail[i].rotation,
  92. Fade(mouseTail[i].color, mouseTail[i].alpha));
  93. }
  94. EndBlendMode();
  95. DrawText("PRESS SPACE to CHANGE BLENDING MODE", 180, 20, 20, BLACK);
  96. if (blending == BLEND_ALPHA) DrawText("ALPHA BLENDING", 290, screenHeight - 40, 20, BLACK);
  97. else DrawText("ADDITIVE BLENDING", 280, screenHeight - 40, 20, RAYWHITE);
  98. EndDrawing();
  99. //----------------------------------------------------------------------------------
  100. }
  101. // De-Initialization
  102. //--------------------------------------------------------------------------------------
  103. UnloadTexture(smoke);
  104. CloseWindow(); // Close window and OpenGL context
  105. //--------------------------------------------------------------------------------------
  106. return 0;
  107. }