shapes_easings_box.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - easings box
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2014-2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "reasings.h" // Required for easing functions
  17. //------------------------------------------------------------------------------------
  18. // Program main entry point
  19. //------------------------------------------------------------------------------------
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. const int screenWidth = 800;
  25. const int screenHeight = 450;
  26. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - easings box");
  27. // Box variables to be animated with easings
  28. Rectangle rec = { GetScreenWidth()/2.0f, -100, 100, 100 };
  29. float rotation = 0.0f;
  30. float alpha = 1.0f;
  31. int state = 0;
  32. int framesCounter = 0;
  33. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  34. //--------------------------------------------------------------------------------------
  35. // Main game loop
  36. while (!WindowShouldClose()) // Detect window close button or ESC key
  37. {
  38. // Update
  39. //----------------------------------------------------------------------------------
  40. switch (state)
  41. {
  42. case 0: // Move box down to center of screen
  43. {
  44. framesCounter++;
  45. // NOTE: Remember that 3rd parameter of easing function refers to
  46. // desired value variation, do not confuse it with expected final value!
  47. rec.y = EaseElasticOut((float)framesCounter, -100, GetScreenHeight()/2.0f + 100, 120);
  48. if (framesCounter >= 120)
  49. {
  50. framesCounter = 0;
  51. state = 1;
  52. }
  53. } break;
  54. case 1: // Scale box to an horizontal bar
  55. {
  56. framesCounter++;
  57. rec.height = EaseBounceOut((float)framesCounter, 100, -90, 120);
  58. rec.width = EaseBounceOut((float)framesCounter, 100, (float)GetScreenWidth(), 120);
  59. if (framesCounter >= 120)
  60. {
  61. framesCounter = 0;
  62. state = 2;
  63. }
  64. } break;
  65. case 2: // Rotate horizontal bar rectangle
  66. {
  67. framesCounter++;
  68. rotation = EaseQuadOut((float)framesCounter, 0.0f, 270.0f, 240);
  69. if (framesCounter >= 240)
  70. {
  71. framesCounter = 0;
  72. state = 3;
  73. }
  74. } break;
  75. case 3: // Increase bar size to fill all screen
  76. {
  77. framesCounter++;
  78. rec.height = EaseCircOut((float)framesCounter, 10, (float)GetScreenWidth(), 120);
  79. if (framesCounter >= 120)
  80. {
  81. framesCounter = 0;
  82. state = 4;
  83. }
  84. } break;
  85. case 4: // Fade out animation
  86. {
  87. framesCounter++;
  88. alpha = EaseSineOut((float)framesCounter, 1.0f, -1.0f, 160);
  89. if (framesCounter >= 160)
  90. {
  91. framesCounter = 0;
  92. state = 5;
  93. }
  94. } break;
  95. default: break;
  96. }
  97. // Reset animation at any moment
  98. if (IsKeyPressed(KEY_SPACE))
  99. {
  100. rec = (Rectangle){ GetScreenWidth()/2.0f, -100, 100, 100 };
  101. rotation = 0.0f;
  102. alpha = 1.0f;
  103. state = 0;
  104. framesCounter = 0;
  105. }
  106. //----------------------------------------------------------------------------------
  107. // Draw
  108. //----------------------------------------------------------------------------------
  109. BeginDrawing();
  110. ClearBackground(RAYWHITE);
  111. DrawRectanglePro(rec, (Vector2){ rec.width/2, rec.height/2 }, rotation, Fade(BLACK, alpha));
  112. DrawText("PRESS [SPACE] TO RESET BOX ANIMATION!", 10, GetScreenHeight() - 25, 20, LIGHTGRAY);
  113. EndDrawing();
  114. //----------------------------------------------------------------------------------
  115. }
  116. // De-Initialization
  117. //--------------------------------------------------------------------------------------
  118. CloseWindow(); // Close window and OpenGL context
  119. //--------------------------------------------------------------------------------------
  120. return 0;
  121. }