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.

135 lines
4.6 KiB

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