Skip to content

Commit

Permalink
Use explicit template instantiations whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Sep 20, 2016
1 parent 0b84685 commit 62ce9b8
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 104 deletions.
1 change: 1 addition & 0 deletions build/experimental.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
'<@(pomdog_expr_dir)/TexturePacker/TextureAtlasLoader.cpp',
'<@(pomdog_expr_dir)/TexturePacker/TextureAtlasLoader.hpp',

'<@(pomdog_expr_dir)/Tween/EasingHelper.cpp',
'<@(pomdog_expr_dir)/Tween/EasingHelper.hpp',

'<@(pomdog_expr_dir)/Rendering/RenderCommand.hpp',
Expand Down
143 changes: 143 additions & 0 deletions experimental/Pomdog.Experimental/Tween/EasingHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (c) 2013-2016 mogemimi. Distributed under the MIT license.

#include "Pomdog.Experimental/Tween/EasingHelper.hpp"

namespace Pomdog {
namespace Detail {
namespace Easings {

template <typename T>
T Quadratic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Quadratic easing
return time * time;
}

template <typename T>
T Cubic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Cubic easing
return time * time * time;
}

template <typename T>
T Quartic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Quartic easing
return time * time * time * time;
}

template <typename T>
T Quintic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Quintic easing
return time * time * time * time * time;
}

template <typename T>
T Sine(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Sinusoidal easing
return 1 - std::cos(time * MathConstants<T>::PiOver2());
}

template <typename T>
T Exponential(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Exponential easing
return std::pow(2, 10 * (time - 1));
}

template <typename T>
T Circle(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Circular easing
return 1 - std::sqrt(1 - time * time);
}

template <typename T>
T Elastic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Elastic easing
constexpr auto period = T(0.3);
constexpr auto s = period / 4;
const auto postFix = std::pow(2, 10 * (time - 1));
return (time <= 0 || time >= 1) ? time
: - (postFix * std::sin(((time - 1) - s) * MathConstants<T>::TwoPi() / period));
}

template <typename T>
T Bounce(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Bounce easing
time = 1 - time;

if (time < 1/2.75) {
return 1 - (7.5625 * time * time);
}
else if (time < 2/2.75) {
auto t = time - 1.5/2.75;
return 1 - (7.5625 * t * t + 0.75);
}
else if (time < 2.5/2.75) {
auto t = time - 2.25/2.75;
return 1 - (7.5625 * t * t + 0.9375);
}

auto postFix = time -= 2.625/2.75;
return 1 - (7.5625 * postFix * time + 0.984375);
}

template <typename T>
T Back(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Back easing
constexpr auto s = T(1.70158);
return time * time * ((s + 1) * time - s);
}

// NOTE: explicit instantiations
template float Quadratic<float>(float time);
template float Cubic(float time);
template float Quartic(float time);
template float Quintic(float time);
template float Sine(float time);
template float Exponential(float time);
template float Circle(float time);
template float Elastic(float time);
template float Bounce(float time);
template float Back(float time);

} // namespace Easings
} // namespace Detail
} // namespace Pomdog
119 changes: 15 additions & 104 deletions experimental/Pomdog.Experimental/Tween/EasingHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,136 +24,47 @@ class Ease {

static T Out(T normalizedTime)
{
return 1 - Function(1 - normalizedTime);
return static_cast<T>(1) - Function(static_cast<T>(1) - normalizedTime);
}

static T InOut(T normalizedTime)
{
constexpr auto half = T(1) / 2;
return (time < half) ? half * In(normalizedTime * 2)
: half + half * Out(normalizedTime * 2 - 1);
constexpr auto half = static_cast<T>(1) / static_cast<T>(2);
return (normalizedTime < half)
? half * In(normalizedTime * static_cast<T>(2))
: half + half * Out(normalizedTime * static_cast<T>(2) - static_cast<T>(1));
}
};

template <typename T>
constexpr T Quadratic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Quadratic easing
return time * time;
}
T Quadratic(T time);

template <typename T>
constexpr T Cubic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Cubic easing
return time * time * time;
}
T Cubic(T time);

template <typename T>
constexpr T Quartic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Quartic easing
return time * time * time * time;
}
T Quartic(T time);

template <typename T>
constexpr T Quintic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Quintic easing
return time * time * time * time * time;
}
T Quintic(T time);

template <typename T>
T Sine(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Sinusoidal easing
return 1 - std::cos(time * MathConstants<T>::PiOver2());
}
T Sine(T time);

template <typename T>
T Exponential(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Exponential easing
return std::pow(2, 10 * (time - 1));
}
T Exponential(T time);

template <typename T>
T Circle(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Circular easing
return 1 - std::sqrt(1 - time * time);
}
T Circle(T time);

template <typename T>
T Elastic(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Elastic easing
constexpr auto period = T(0.3);
constexpr auto s = period / 4;
const auto postFix = std::pow(2, 10 * (time - 1));
return (time <= 0 || time >= 1) ? time
: - (postFix * std::sin(((time - 1) - s) * MathConstants<T>::TwoPi() / period));
}
T Elastic(T time);

template <typename T>
T Bounce(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Bounce easing
time = 1 - time;

if (time < 1/2.75) {
return 1 - (7.5625 * time * time);
}
else if (time < 2/2.75) {
auto t = time - 1.5/2.75;
return 1 - (7.5625 * t * t + 0.75);
}
else if (time < 2.5/2.75) {
auto t = time - 2.25/2.75;
return 1 - (7.5625 * t * t + 0.9375);
}

auto postFix = time -= 2.625/2.75;
return 1 - (7.5625 * postFix * time + 0.984375);
}
T Bounce(T time);

template <typename T>
T Back(T time)
{
static_assert(std::is_floating_point<T>::value,
"You can only use floating-point types");

// Back easing
constexpr auto s = T(1.70158);
return time * time * ((s + 1) * time - s);
}
T Back(T time);

template <typename T>
using EaseBack = Ease<T, Back<T>>;
Expand Down

0 comments on commit 62ce9b8

Please sign in to comment.