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.

109 lines
3.7 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - easings ball 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 ball anim");
  20. // Ball variable value to be animated with easings
  21. int ballPositionX = -100;
  22. int ballRadius = 20;
  23. float ballAlpha = 0.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. if (state == 0) // Move ball position X with easing
  34. {
  35. framesCounter++;
  36. ballPositionX = EaseElasticOut(framesCounter, -100, screenWidth/2 + 100, 120);
  37. if (framesCounter >= 120)
  38. {
  39. framesCounter = 0;
  40. state = 1;
  41. }
  42. }
  43. else if (state == 1) // Increase ball radius with easing
  44. {
  45. framesCounter++;
  46. ballRadius = EaseElasticIn(framesCounter, 20, 500, 200);
  47. if (framesCounter >= 200)
  48. {
  49. framesCounter = 0;
  50. state = 2;
  51. }
  52. }
  53. else if (state == 2) // Change ball alpha with easing (background color blending)
  54. {
  55. framesCounter++;
  56. ballAlpha = EaseCubicOut(framesCounter, 0.0f, 1.0f, 200);
  57. if (framesCounter >= 200)
  58. {
  59. framesCounter = 0;
  60. state = 3;
  61. }
  62. }
  63. else if (state == 3) // Reset state to play again
  64. {
  65. if (IsKeyPressed(KEY_ENTER))
  66. {
  67. // Reset required variables to play again
  68. ballPositionX = -100;
  69. ballRadius = 20;
  70. ballAlpha = 0.0f;
  71. state = 0;
  72. }
  73. }
  74. if (IsKeyPressed(KEY_R)) framesCounter = 0;
  75. //----------------------------------------------------------------------------------
  76. // Draw
  77. //----------------------------------------------------------------------------------
  78. BeginDrawing();
  79. ClearBackground(RAYWHITE);
  80. if (state >= 2) DrawRectangle(0, 0, screenWidth, screenHeight, GREEN);
  81. DrawCircle(ballPositionX, 200, ballRadius, Fade(RED, 1.0f - ballAlpha));
  82. if (state == 3) DrawText("PRESS [ENTER] TO PLAY AGAIN!", 240, 200, 20, BLACK);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }