diff --git a/include/bx/easing.h b/include/bx/easing.h new file mode 100644 index 0000000..776e637 --- /dev/null +++ b/include/bx/easing.h @@ -0,0 +1,265 @@ +/* + * Copyright 2011-2016 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bx#license-bsd-2-clause + */ + +#ifndef BX_EASING_H_HEADER_GUARD +#define BX_EASING_H_HEADER_GUARD + +#include "fpumath.h" + +// Reference: +// http://easings.net/ +// http://robertpenner.com/easing/ + +namespace bx +{ + typedef float (*EaseFn)(float _t); + + template + float easeOut(float _t) + { + return 1.0f - ease(1.0f - _t); + } + + template + float easeMix(float _t) + { + return _t < 0.5f + ? easeFrom0toH(2.0f*_t)*0.5f + : easeFromHto1(2.0f*_t - 1.0f)*0.5f + 0.5f + ; + } + + inline float easeLinear(float _t) + { + return _t; + } + + inline float easeInQuad(float _t) + { + return fsq(_t); + } + + inline float easeOutQuad(float _t) + { + return easeOut(_t); + } + + inline float easeInOutQuad(float _t) + { + return easeMix(_t); + } + + inline float easeOutInQuad(float _t) + { + return easeMix(_t); + } + + inline float easeInCubic(float _t) + { + return _t*_t*_t; + } + + inline float easeOutCubic(float _t) + { + return easeOut(_t); + } + + inline float easeInOutCubic(float _t) + { + return easeMix(_t); + } + + inline float easeOutInCubic(float _t) + { + return easeMix(_t); + } + + inline float easeInQuart(float _t) + { + return _t*_t*_t*_t; + } + + inline float easeOutQuart(float _t) + { + return easeOut(_t); + } + + inline float easeInOutQuart(float _t) + { + return easeMix(_t); + } + + inline float easeOutInQuart(float _t) + { + return easeMix(_t); + } + + inline float easeInQuint(float _t) + { + return _t*_t*_t*_t*_t; + } + + inline float easeOutQuint(float _t) + { + return easeOut(_t); + } + + inline float easeInOutQuint(float _t) + { + return easeMix(_t); + } + + inline float easeOutInQuint(float _t) + { + return easeMix(_t); + } + + inline float easeInSine(float _t) + { + return 1.0f - fcos(_t*piHalf); + } + + inline float easeOutSine(float _t) + { + return easeOut(_t); + } + + inline float easeInOutSine(float _t) + { + return easeMix(_t); + } + + inline float easeOutInSine(float _t) + { + return easeMix(_t); + } + + inline float easeInExpo(float _t) + { + return fpow(2.0f, 10.0f * (_t - 1.0f) ) - 0.001f; + } + + inline float easeOutExpo(float _t) + { + return easeOut(_t); + } + + inline float easeInOutExpo(float _t) + { + return easeMix(_t); + } + + inline float easeOutInExpo(float _t) + { + return easeMix(_t); + } + + inline float easeInCirc(float _t) + { + return -(fsqrt(1.0f - _t*_t) - 1.0f); + } + + inline float easeOutCirc(float _t) + { + return easeOut(_t); + } + + inline float easeInOutCirc(float _t) + { + return easeMix(_t); + } + + inline float easeOutInCirc(float _t) + { + return easeMix(_t); + } + + inline float easeOutElastic(float _t) + { + return fpow(2.0f, -10.0f*_t)*fsin( (_t-0.3f/4.0f)*(2.0f*pi)/0.3f) + 1.0f; + } + + inline float easeInElastic(float _t) + { + return easeOut(_t); + } + + inline float easeInOutElastic(float _t) + { + return easeMix(_t); + } + + inline float easeOutInElastic(float _t) + { + return easeMix(_t); + } + + inline float easeInBack(float _t) + { + return easeInCubic(_t) - _t*fsin(_t*pi); + } + + inline float easeOutBack(float _t) + { + return easeOut(_t); + } + + inline float easeInOutBack(float _t) + { + return easeMix(_t); + } + + inline float easeOutInBack(float _t) + { + return easeMix(_t); + } + + inline float easeOutBounce(float _t) + { + if (4.0f/11.0f > _t) + { + return 121.0f/16.0f*_t*_t; + } + + if (8.0f/11.0f > _t) + { + return 363.0f/40.0f*_t*_t + - 99.0f/10.0f*_t + + 17.0f/ 5.0f + ; + } + + if (9.0f/10.0f > _t) + { + return 4356.0f/ 361.0f*_t*_t + - 35442.0f/1805.0f*_t + + 16061.0f/1805.0f + ; + } + + return 54.0f/ 5.0f*_t*_t + - 513.0f/25.0f*_t + + 268.0f/25.0f + ; + } + + inline float easeInBounce(float _t) + { + return easeOut(_t); + } + + inline float easeInOutBounce(float _t) + { + return easeMix(_t); + } + + inline float easeOutInBounce(float _t) + { + return easeMix(_t); + } + +} // namespace bx + +#endif // BX_EASING_H_HEADER_GUARD diff --git a/include/bx/fpumath.h b/include/bx/fpumath.h index bbc2dfa..7820d1a 100644 --- a/include/bx/fpumath.h +++ b/include/bx/fpumath.h @@ -102,16 +102,6 @@ namespace bx return _a < 0.0f ? -1.0f : 1.0f; } - inline float fstep(float _edge, float _a) - { - return _a < _edge ? 0.0f : 1.0f; - } - - inline float fpulse(float _a, float _start, float _end) - { - return fstep(_a, _start) - fstep(_a, _end); - } - inline float fabsolute(float _a) { return fabsf(_a); @@ -127,11 +117,21 @@ namespace bx return sinf(_a); } + inline float fasin(float _a) + { + return asinf(_a); + } + inline float fcos(float _a) { return cosf(_a); } + inline float facos(float _a) + { + return acosf(_a); + } + inline float fpow(float _a, float _b) { return powf(_a, _b); @@ -197,6 +197,21 @@ namespace bx return result; } + inline float fstep(float _edge, float _a) + { + return _a < _edge ? 0.0f : 1.0f; + } + + inline float fpulse(float _a, float _start, float _end) + { + return fstep(_a, _start) - fstep(_a, _end); + } + + inline float fsmoothstep(float _a) + { + return fsq(_a)*(3.0f - 2.0f*_a); + } + // References: // - Bias And Gain Are Your Friend // http://blog.demofox.org/2012/09/24/bias-and-gain-are-your-friend/