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.

229 lines
9.1 KiB

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