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.

140 lines
4.9 KiB

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