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.

114 lines
4.0 KiB

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