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.

124 rivejä
4.6 KiB

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