easings_testbed.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*******************************************************************************************
  2. *
  3. * raylib [others] example - easings testbed
  4. *
  5. * Example originally created with raylib 2.5, last time updated with raylib 2.5
  6. *
  7. * Example complexity rating: [★★★☆] 3/4
  8. *
  9. * Example contributed by Juan Miguel López (@flashback-fx) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2019-2025 Juan Miguel López (@flashback-fx) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "reasings.h" // Required for easing functions
  19. #define FONT_SIZE 20
  20. #define D_STEP 20.0f
  21. #define D_STEP_FINE 2.0f
  22. #define D_MIN 1.0f
  23. #define D_MAX 10000.0f
  24. // Easing types
  25. enum EasingTypes {
  26. EASE_LINEAR_NONE = 0,
  27. EASE_LINEAR_IN,
  28. EASE_LINEAR_OUT,
  29. EASE_LINEAR_IN_OUT,
  30. EASE_SINE_IN,
  31. EASE_SINE_OUT,
  32. EASE_SINE_IN_OUT,
  33. EASE_CIRC_IN,
  34. EASE_CIRC_OUT,
  35. EASE_CIRC_IN_OUT,
  36. EASE_CUBIC_IN,
  37. EASE_CUBIC_OUT,
  38. EASE_CUBIC_IN_OUT,
  39. EASE_QUAD_IN,
  40. EASE_QUAD_OUT,
  41. EASE_QUAD_IN_OUT,
  42. EASE_EXPO_IN,
  43. EASE_EXPO_OUT,
  44. EASE_EXPO_IN_OUT,
  45. EASE_BACK_IN,
  46. EASE_BACK_OUT,
  47. EASE_BACK_IN_OUT,
  48. EASE_BOUNCE_OUT,
  49. EASE_BOUNCE_IN,
  50. EASE_BOUNCE_IN_OUT,
  51. EASE_ELASTIC_IN,
  52. EASE_ELASTIC_OUT,
  53. EASE_ELASTIC_IN_OUT,
  54. NUM_EASING_TYPES,
  55. EASING_NONE = NUM_EASING_TYPES
  56. };
  57. static float NoEase(float t, float b, float c, float d); // NoEase function declaration, function used when "no easing" is selected for any axis
  58. // Easing functions reference data
  59. static const struct {
  60. const char *name;
  61. float (*func)(float, float, float, float);
  62. } Easings[] = {
  63. [EASE_LINEAR_NONE] = { .name = "EaseLinearNone", .func = EaseLinearNone },
  64. [EASE_LINEAR_IN] = { .name = "EaseLinearIn", .func = EaseLinearIn },
  65. [EASE_LINEAR_OUT] = { .name = "EaseLinearOut", .func = EaseLinearOut },
  66. [EASE_LINEAR_IN_OUT] = { .name = "EaseLinearInOut", .func = EaseLinearInOut },
  67. [EASE_SINE_IN] = { .name = "EaseSineIn", .func = EaseSineIn },
  68. [EASE_SINE_OUT] = { .name = "EaseSineOut", .func = EaseSineOut },
  69. [EASE_SINE_IN_OUT] = { .name = "EaseSineInOut", .func = EaseSineInOut },
  70. [EASE_CIRC_IN] = { .name = "EaseCircIn", .func = EaseCircIn },
  71. [EASE_CIRC_OUT] = { .name = "EaseCircOut", .func = EaseCircOut },
  72. [EASE_CIRC_IN_OUT] = { .name = "EaseCircInOut", .func = EaseCircInOut },
  73. [EASE_CUBIC_IN] = { .name = "EaseCubicIn", .func = EaseCubicIn },
  74. [EASE_CUBIC_OUT] = { .name = "EaseCubicOut", .func = EaseCubicOut },
  75. [EASE_CUBIC_IN_OUT] = { .name = "EaseCubicInOut", .func = EaseCubicInOut },
  76. [EASE_QUAD_IN] = { .name = "EaseQuadIn", .func = EaseQuadIn },
  77. [EASE_QUAD_OUT] = { .name = "EaseQuadOut", .func = EaseQuadOut },
  78. [EASE_QUAD_IN_OUT] = { .name = "EaseQuadInOut", .func = EaseQuadInOut },
  79. [EASE_EXPO_IN] = { .name = "EaseExpoIn", .func = EaseExpoIn },
  80. [EASE_EXPO_OUT] = { .name = "EaseExpoOut", .func = EaseExpoOut },
  81. [EASE_EXPO_IN_OUT] = { .name = "EaseExpoInOut", .func = EaseExpoInOut },
  82. [EASE_BACK_IN] = { .name = "EaseBackIn", .func = EaseBackIn },
  83. [EASE_BACK_OUT] = { .name = "EaseBackOut", .func = EaseBackOut },
  84. [EASE_BACK_IN_OUT] = { .name = "EaseBackInOut", .func = EaseBackInOut },
  85. [EASE_BOUNCE_OUT] = { .name = "EaseBounceOut", .func = EaseBounceOut },
  86. [EASE_BOUNCE_IN] = { .name = "EaseBounceIn", .func = EaseBounceIn },
  87. [EASE_BOUNCE_IN_OUT] = { .name = "EaseBounceInOut", .func = EaseBounceInOut },
  88. [EASE_ELASTIC_IN] = { .name = "EaseElasticIn", .func = EaseElasticIn },
  89. [EASE_ELASTIC_OUT] = { .name = "EaseElasticOut", .func = EaseElasticOut },
  90. [EASE_ELASTIC_IN_OUT] = { .name = "EaseElasticInOut", .func = EaseElasticInOut },
  91. [EASING_NONE] = { .name = "None", .func = NoEase },
  92. };
  93. //------------------------------------------------------------------------------------
  94. // Program main entry point
  95. //------------------------------------------------------------------------------------
  96. int main(void)
  97. {
  98. // Initialization
  99. //--------------------------------------------------------------------------------------
  100. const int screenWidth = 800;
  101. const int screenHeight = 450;
  102. InitWindow(screenWidth, screenHeight, "raylib [others] example - easings testbed");
  103. Vector2 ballPosition = { 100.0f, 100.0f };
  104. float t = 0.0f; // Current time (in any unit measure, but same unit as duration)
  105. float d = 300.0f; // Total time it should take to complete (duration)
  106. bool paused = true;
  107. bool boundedT = true; // If true, t will stop when d >= td, otherwise t will keep adding td to its value every loop
  108. int easingX = EASING_NONE; // Easing selected for x axis
  109. int easingY = EASING_NONE; // Easing selected for y axis
  110. SetTargetFPS(60);
  111. //--------------------------------------------------------------------------------------
  112. // Main game loop
  113. while (!WindowShouldClose()) // Detect window close button or ESC key
  114. {
  115. // Update
  116. //----------------------------------------------------------------------------------
  117. if (IsKeyPressed(KEY_T)) boundedT = !boundedT;
  118. // Choose easing for the X axis
  119. if (IsKeyPressed(KEY_RIGHT))
  120. {
  121. easingX++;
  122. if (easingX > EASING_NONE) easingX = 0;
  123. }
  124. else if (IsKeyPressed(KEY_LEFT))
  125. {
  126. if (easingX == 0) easingX = EASING_NONE;
  127. else easingX--;
  128. }
  129. // Choose easing for the Y axis
  130. if (IsKeyPressed(KEY_DOWN))
  131. {
  132. easingY++;
  133. if (easingY > EASING_NONE) easingY = 0;
  134. }
  135. else if (IsKeyPressed(KEY_UP))
  136. {
  137. if (easingY == 0) easingY = EASING_NONE;
  138. else easingY--;
  139. }
  140. // Change d (duration) value
  141. if (IsKeyPressed(KEY_W) && d < D_MAX - D_STEP) d += D_STEP;
  142. else if (IsKeyPressed(KEY_Q) && d > D_MIN + D_STEP) d -= D_STEP;
  143. if (IsKeyDown(KEY_S) && d < D_MAX - D_STEP_FINE) d += D_STEP_FINE;
  144. else if (IsKeyDown(KEY_A) && d > D_MIN + D_STEP_FINE) d -= D_STEP_FINE;
  145. // Play, pause and restart controls
  146. if (IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_T) ||
  147. IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_LEFT) ||
  148. IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_UP) ||
  149. IsKeyPressed(KEY_W) || IsKeyPressed(KEY_Q) ||
  150. IsKeyDown(KEY_S) || IsKeyDown(KEY_A) ||
  151. (IsKeyPressed(KEY_ENTER) && (boundedT == true) && (t >= d)))
  152. {
  153. t = 0.0f;
  154. ballPosition.x = 100.0f;
  155. ballPosition.y = 100.0f;
  156. paused = true;
  157. }
  158. if (IsKeyPressed(KEY_ENTER)) paused = !paused;
  159. // Movement computation
  160. if (!paused && ((boundedT && t < d) || !boundedT))
  161. {
  162. ballPosition.x = Easings[easingX].func(t, 100.0f, 700.0f - 170.0f, d);
  163. ballPosition.y = Easings[easingY].func(t, 100.0f, 400.0f - 170.0f, d);
  164. t += 1.0f;
  165. }
  166. //----------------------------------------------------------------------------------
  167. // Draw
  168. //----------------------------------------------------------------------------------
  169. BeginDrawing();
  170. ClearBackground(RAYWHITE);
  171. // Draw information text
  172. DrawText(TextFormat("Easing x: %s", Easings[easingX].name), 20, FONT_SIZE, FONT_SIZE, LIGHTGRAY);
  173. DrawText(TextFormat("Easing y: %s", Easings[easingY].name), 20, FONT_SIZE*2, FONT_SIZE, LIGHTGRAY);
  174. DrawText(TextFormat("t (%c) = %.2f d = %.2f", (boundedT == true)? 'b' : 'u', t, d), 20, FONT_SIZE*3, FONT_SIZE, LIGHTGRAY);
  175. // Draw instructions text
  176. DrawText("Use ENTER to play or pause movement, use SPACE to restart", 20, GetScreenHeight() - FONT_SIZE*2, FONT_SIZE, LIGHTGRAY);
  177. DrawText("Use Q and W or A and S keys to change duration", 20, GetScreenHeight() - FONT_SIZE*3, FONT_SIZE, LIGHTGRAY);
  178. DrawText("Use LEFT or RIGHT keys to choose easing for the x axis", 20, GetScreenHeight() - FONT_SIZE*4, FONT_SIZE, LIGHTGRAY);
  179. DrawText("Use UP or DOWN keys to choose easing for the y axis", 20, GetScreenHeight() - FONT_SIZE*5, FONT_SIZE, LIGHTGRAY);
  180. // Draw ball
  181. DrawCircleV(ballPosition, 16.0f, MAROON);
  182. EndDrawing();
  183. //----------------------------------------------------------------------------------
  184. }
  185. // De-Initialization
  186. //--------------------------------------------------------------------------------------
  187. CloseWindow();
  188. //--------------------------------------------------------------------------------------
  189. return 0;
  190. }
  191. // NoEase function, used when "no easing" is selected for any axis
  192. // It just ignores all parameters besides b
  193. static float NoEase(float t, float b, float c, float d)
  194. {
  195. // Hack to avoid compiler warning (about unused variables)
  196. float burn = t + b + c + d;
  197. d += burn;
  198. return b;
  199. }