Adapted easings as single header and inline functions

This commit is contained in:
raysan5 2016-01-20 18:20:36 +01:00
parent efa1c96d19
commit 634f8f0d0f

View File

@ -1,6 +1,12 @@
/*******************************************************************************************
*
* raylib Easings - Useful easing functions for values animation
* raylib easings (header only file)
*
* Useful easing functions for values animation
*
* This header uses:
* #define EASINGS_STATIC_INLINE // Inlines all functions code, so it runs faster.
* // This requires lots of memory on system.
*
* A port of Robert Penner's easing equations to C (http://robertpenner.com/easing/)
*
@ -56,6 +62,14 @@
#ifndef EASINGS_H
#define EASINGS_H
#define EASINGS_STATIC_INLINE // NOTE: By default, compile functions as static inline
#if defined(EASINGS_STATIC_INLINE)
#define EASEDEF static inline
#else
#define EASEDEF extern
#endif
#include <math.h>
#ifdef __cplusplus
@ -63,47 +77,47 @@ extern "C" { // Prevents name mangling of functions
#endif
// Linear Easing functions
float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); }
float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); }
float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); }
float EaseLinearInOut(float t,float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearNone(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearIn(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearOut(float t, float b, float c, float d) { return (c*t/d + b); }
EASEDEF float EaseLinearInOut(float t,float b, float c, float d) { return (c*t/d + b); }
// Sine Easing functions
float EaseSineIn(float t, float b, float c, float d) { return (-c*cos(t/d*(PI/2)) + c + b); }
float EaseSineOut(float t, float b, float c, float d) { return (c*sin(t/d*(PI/2)) + b); }
float EaseSineInOut(float t, float b, float c, float d) { return (-c/2*(cos(PI*t/d) - 1) + b); }
EASEDEF float EaseSineIn(float t, float b, float c, float d) { return (-c*cos(t/d*(PI/2)) + c + b); }
EASEDEF float EaseSineOut(float t, float b, float c, float d) { return (c*sin(t/d*(PI/2)) + b); }
EASEDEF float EaseSineInOut(float t, float b, float c, float d) { return (-c/2*(cos(PI*t/d) - 1) + b); }
// Circular Easing functions
float EaseCircIn(float t, float b, float c, float d) { return (-c*(sqrt(1 - (t/=d)*t) - 1) + b); }
float EaseCircOut(float t, float b, float c, float d) { return (c*sqrt(1 - (t=t/d-1)*t) + b); }
float EaseCircInOut(float t, float b, float c, float d)
EASEDEF float EaseCircIn(float t, float b, float c, float d) { return (-c*(sqrt(1 - (t/=d)*t) - 1) + b); }
EASEDEF float EaseCircOut(float t, float b, float c, float d) { return (c*sqrt(1 - (t=t/d-1)*t) + b); }
EASEDEF float EaseCircInOut(float t, float b, float c, float d)
{
if ((t/=d/2) < 1) return (-c/2*(sqrt(1 - t*t) - 1) + b);
return (c/2*(sqrt(1 - t*(t-=2)) + 1) + b);
}
// Cubic Easing functions
float EaseCubicIn(float t, float b, float c, float d) { return (c*(t/=d)*t*t + b); }
float EaseCubicOut(float t, float b, float c, float d) { return (c*((t=t/d-1)*t*t + 1) + b); }
float EaseCubicInOut(float t, float b, float c, float d)
EASEDEF float EaseCubicIn(float t, float b, float c, float d) { return (c*(t/=d)*t*t + b); }
EASEDEF float EaseCubicOut(float t, float b, float c, float d) { return (c*((t=t/d-1)*t*t + 1) + b); }
EASEDEF float EaseCubicInOut(float t, float b, float c, float d)
{
if ((t/=d/2) < 1) return (c/2*t*t*t + b);
return (c/2*((t-=2)*t*t + 2) + b);
}
// Quadratic Easing functions
float EaseQuadIn(float t, float b, float c, float d) { return (c*(t/=d)*t + b); }
float EaseQuadOut(float t, float b, float c, float d) { return (-c*(t/=d)*(t-2) + b); }
float EaseQuadInOut(float t, float b, float c, float d)
EASEDEF float EaseQuadIn(float t, float b, float c, float d) { return (c*(t/=d)*t + b); }
EASEDEF float EaseQuadOut(float t, float b, float c, float d) { return (-c*(t/=d)*(t-2) + b); }
EASEDEF float EaseQuadInOut(float t, float b, float c, float d)
{
if ((t/=d/2) < 1) return (((c/2)*(t*t)) + b);
return (-c/2*(((t-2)*(--t)) - 1) + b);
}
// Exponential Easing functions
float EaseExpoIn(float t, float b, float c, float d) { return (t == 0) ? b : (c*pow(2, 10*(t/d - 1)) + b); }
float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-pow(2, -10*t/d) + 1) + b); }
float EaseExpoInOut(float t, float b, float c, float d)
EASEDEF float EaseExpoIn(float t, float b, float c, float d) { return (t == 0) ? b : (c*pow(2, 10*(t/d - 1)) + b); }
EASEDEF float EaseExpoOut(float t, float b, float c, float d) { return (t == d) ? (b + c) : (c*(-pow(2, -10*t/d) + 1) + b); }
EASEDEF float EaseExpoInOut(float t, float b, float c, float d)
{
if (t == 0) return b;
if (t == d) return (b + c);
@ -113,20 +127,20 @@ float EaseExpoInOut(float t, float b, float c, float d)
}
// Back Easing functions
float EaseBackIn(float t, float b, float c, float d)
EASEDEF float EaseBackIn(float t, float b, float c, float d)
{
float s = 1.70158f;
float postFix = t/=d;
return (c*(postFix)*t*((s + 1)*t - s) + b);
}
float EaseBackOut(float t, float b, float c, float d)
EASEDEF float EaseBackOut(float t, float b, float c, float d)
{
float s = 1.70158f;
return (c*((t=t/d-1)*t*((s + 1)*t + s) + 1) + b);
}
float EaseBackInOut(float t, float b, float c, float d)
EASEDEF float EaseBackInOut(float t, float b, float c, float d)
{
float s = 1.70158f;
if ((t/=d/2) < 1) return (c/2*(t*t*(((s*=(1.525f)) + 1)*t - s)) + b);
@ -136,8 +150,7 @@ float EaseBackInOut(float t, float b, float c, float d)
}
// Bounce Easing functions
float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d-t, 0, c, d) + b); }
float EaseBounceOut(float t, float b, float c, float d)
EASEDEF float EaseBounceOut(float t, float b, float c, float d)
{
if ((t/=d) < (1/2.75f))
{
@ -160,14 +173,15 @@ float EaseBounceOut(float t, float b, float c, float d)
}
}
float EaseBounceInOut(float t, float b, float c, float d)
EASEDEF float EaseBounceIn(float t, float b, float c, float d) { return (c - EaseBounceOut(d-t, 0, c, d) + b); }
EASEDEF float EaseBounceInOut(float t, float b, float c, float d)
{
if (t < d/2) return (EaseBounceIn(t*2, 0, c, d)*0.5f + b);
else return (EaseBounceOut(t*2-d, 0, c, d)*0.5f + c*0.5f + b);
}
// Elastic Easing functions
float EaseElasticIn(float t, float b, float c, float d)
EASEDEF float EaseElasticIn(float t, float b, float c, float d)
{
if (t == 0) return b;
if ((t/=d) == 1) return (b + c);
@ -180,7 +194,7 @@ float EaseElasticIn(float t, float b, float c, float d)
return (-(postFix*sin((t*d-s)*(2*PI)/p )) + b);
}
float EaseElasticOut(float t, float b, float c, float d)
EASEDEF float EaseElasticOut(float t, float b, float c, float d)
{
if (t == 0) return b;
if ((t/=d) == 1) return (b + c);
@ -192,7 +206,7 @@ float EaseElasticOut(float t, float b, float c, float d)
return (a*pow(2,-10*t)*sin((t*d-s)*(2*PI)/p) + c + b);
}
float EaseElasticInOut(float t, float b, float c, float d)
EASEDEF float EaseElasticInOut(float t, float b, float c, float d)
{
if (t == 0) return b;
if ((t/=d/2) == 2) return (b + c);