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.

122 lines
4.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - easings rectangle array
  4. *
  5. * NOTE: This example requires 'easings.h' library, provided on raylib/src. Just copy
  6. * the library to same directory as example or make sure it's available on include path.
  7. *
  8. * Example originally created with raylib 2.0, last time updated with raylib 2.5
  9. *
  10. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  11. * BSD-like license that allows static linking with closed source software
  12. *
  13. * Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #include "reasings.h" // Required for easing functions
  18. #define RECS_WIDTH 50
  19. #define RECS_HEIGHT 50
  20. #define MAX_RECS_X 800/RECS_WIDTH
  21. #define MAX_RECS_Y 450/RECS_HEIGHT
  22. #define PLAY_TIME_IN_FRAMES 240 // At 60 fps = 4 seconds
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings rectangle array");
  33. Rectangle recs[MAX_RECS_X*MAX_RECS_Y] = { 0 };
  34. for (int y = 0; y < MAX_RECS_Y; y++)
  35. {
  36. for (int x = 0; x < MAX_RECS_X; x++)
  37. {
  38. recs[y*MAX_RECS_X + x].x = RECS_WIDTH/2.0f + RECS_WIDTH*x;
  39. recs[y*MAX_RECS_X + x].y = RECS_HEIGHT/2.0f + RECS_HEIGHT*y;
  40. recs[y*MAX_RECS_X + x].width = RECS_WIDTH;
  41. recs[y*MAX_RECS_X + x].height = RECS_HEIGHT;
  42. }
  43. }
  44. float rotation = 0.0f;
  45. int framesCounter = 0;
  46. int state = 0; // Rectangles animation state: 0-Playing, 1-Finished
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. if (state == 0)
  55. {
  56. framesCounter++;
  57. for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++)
  58. {
  59. recs[i].height = EaseCircOut((float)framesCounter, RECS_HEIGHT, -RECS_HEIGHT, PLAY_TIME_IN_FRAMES);
  60. recs[i].width = EaseCircOut((float)framesCounter, RECS_WIDTH, -RECS_WIDTH, PLAY_TIME_IN_FRAMES);
  61. if (recs[i].height < 0) recs[i].height = 0;
  62. if (recs[i].width < 0) recs[i].width = 0;
  63. if ((recs[i].height == 0) && (recs[i].width == 0)) state = 1; // Finish playing
  64. rotation = EaseLinearIn((float)framesCounter, 0.0f, 360.0f, PLAY_TIME_IN_FRAMES);
  65. }
  66. }
  67. else if ((state == 1) && IsKeyPressed(KEY_SPACE))
  68. {
  69. // When animation has finished, press space to restart
  70. framesCounter = 0;
  71. for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++)
  72. {
  73. recs[i].height = RECS_HEIGHT;
  74. recs[i].width = RECS_WIDTH;
  75. }
  76. state = 0;
  77. }
  78. //----------------------------------------------------------------------------------
  79. // Draw
  80. //----------------------------------------------------------------------------------
  81. BeginDrawing();
  82. ClearBackground(RAYWHITE);
  83. if (state == 0)
  84. {
  85. for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++)
  86. {
  87. DrawRectanglePro(recs[i], (Vector2){ recs[i].width/2, recs[i].height/2 }, rotation, RED);
  88. }
  89. }
  90. else if (state == 1) DrawText("PRESS [SPACE] TO PLAY AGAIN!", 240, 200, 20, GRAY);
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. CloseWindow(); // Close window and OpenGL context
  97. //--------------------------------------------------------------------------------------
  98. return 0;
  99. }