Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

117 рядки
4.2 KiB

5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
5 роки тому
  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. * This example has been created using raylib 2.0 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "easings.h" // Required for easing functions
  16. #define RECS_WIDTH 50
  17. #define RECS_HEIGHT 50
  18. #define MAX_RECS_X 800/RECS_WIDTH
  19. #define MAX_RECS_Y 450/RECS_HEIGHT
  20. #define PLAY_TIME_IN_FRAMES 240 // At 60 fps = 4 seconds
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings rectangle array");
  28. Rectangle recs[MAX_RECS_X*MAX_RECS_Y] = { 0 };
  29. for (int y = 0; y < MAX_RECS_Y; y++)
  30. {
  31. for (int x = 0; x < MAX_RECS_X; x++)
  32. {
  33. recs[y*MAX_RECS_X + x].x = RECS_WIDTH/2 + RECS_WIDTH*x;
  34. recs[y*MAX_RECS_X + x].y = RECS_HEIGHT/2 + RECS_HEIGHT*y;
  35. recs[y*MAX_RECS_X + x].width = RECS_WIDTH;
  36. recs[y*MAX_RECS_X + x].height = RECS_HEIGHT;
  37. }
  38. }
  39. float rotation = 0.0f;
  40. int framesCounter = 0;
  41. int state = 0; // Rectangles animation state: 0-Playing, 1-Finished
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. // Update
  48. //----------------------------------------------------------------------------------
  49. if (state == 0)
  50. {
  51. framesCounter++;
  52. for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++)
  53. {
  54. recs[i].height = EaseCircOut(framesCounter, RECS_HEIGHT, -RECS_HEIGHT, PLAY_TIME_IN_FRAMES);
  55. recs[i].width = EaseCircOut(framesCounter, RECS_WIDTH, -RECS_WIDTH, PLAY_TIME_IN_FRAMES);
  56. if (recs[i].height < 0) recs[i].height = 0;
  57. if (recs[i].width < 0) recs[i].width = 0;
  58. if ((recs[i].height == 0) && (recs[i].width == 0)) state = 1; // Finish playing
  59. rotation = EaseLinearIn(framesCounter, 0.0f, 360.0f, PLAY_TIME_IN_FRAMES);
  60. }
  61. }
  62. else if ((state == 1) && IsKeyPressed(KEY_SPACE))
  63. {
  64. // When animation has finished, press space to restart
  65. framesCounter = 0;
  66. for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++)
  67. {
  68. recs[i].height = RECS_HEIGHT;
  69. recs[i].width = RECS_WIDTH;
  70. }
  71. state = 0;
  72. }
  73. //----------------------------------------------------------------------------------
  74. // Draw
  75. //----------------------------------------------------------------------------------
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. if (state == 0)
  79. {
  80. for (int i = 0; i < MAX_RECS_X*MAX_RECS_Y; i++)
  81. {
  82. DrawRectanglePro(recs[i], (Vector2){ recs[i].width/2, recs[i].height/2 }, rotation, RED);
  83. }
  84. }
  85. else if (state == 1) DrawText("PRESS [SPACE] TO PLAY AGAIN!", 240, 200, 20, GRAY);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }
  89. // De-Initialization
  90. //--------------------------------------------------------------------------------------
  91. CloseWindow(); // Close window and OpenGL context
  92. //--------------------------------------------------------------------------------------
  93. return 0;
  94. }