2022-02-12 08:03:27 +08:00
|
|
|
#pragma once
|
2022-10-02 04:29:51 +08:00
|
|
|
#include <blah_common.h>
|
2022-02-12 08:03:27 +08:00
|
|
|
|
|
|
|
namespace Blah
|
|
|
|
{
|
|
|
|
enum class Endian
|
|
|
|
{
|
|
|
|
Little,
|
|
|
|
Big
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace Calc
|
|
|
|
{
|
|
|
|
constexpr f32 PI = 3.141592653f;
|
|
|
|
constexpr f32 TAU = PI * 2.0f;
|
|
|
|
constexpr f32 RIGHT = 0;
|
|
|
|
constexpr f32 LEFT = PI;
|
|
|
|
constexpr f32 UP = PI / -2;
|
|
|
|
constexpr f32 DOWN = PI / 2;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T sign(T x) { return static_cast<T>(x == 0 ? 0 : (x < 0 ? -1 : 1)); }
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T abs(T x) { return x < 0 ? -x : x; }
|
|
|
|
|
|
|
|
template<class T, class TMin, class TMax>
|
|
|
|
T clamp(T value, TMin min, TMax max) { return value < min ? static_cast<T>(min) : (value > max ? static_cast<T>(max) : value); }
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T min(T a, T b) { return (T)(a < b ? a : b); }
|
|
|
|
|
|
|
|
template<class T, typename ... Args>
|
|
|
|
T min(const T& a, const T& b, const Args&... args) { return Calc::min(a, Calc::min(b, args...)); }
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T max(T a, T b) { return (T)(a > b ? a : b); }
|
|
|
|
|
|
|
|
template<class T, typename ... Args>
|
|
|
|
T max(const T& a, const T& b, const Args&... args) { return Calc::max(a, Calc::max(b, args...)); }
|
|
|
|
|
|
|
|
inline f32 round(f32 x) { return ::roundf(x); }
|
|
|
|
inline f32 floor(f32 x) { return ::floorf(x); }
|
|
|
|
inline f32 ceiling(f32 x) { return ::ceilf(x); }
|
|
|
|
inline f32 sin(f32 x) { return ::sinf(x); }
|
|
|
|
inline f32 cos(f32 x) { return ::cosf(x); }
|
|
|
|
inline f32 tan(f32 x) { return ::tanf(x); }
|
|
|
|
inline f32 atan2(f32 y, f32 x) { return ::atan2f(y, x); }
|
|
|
|
inline f32 pow(f32 x, f32 n) { return ::powf(x, n); }
|
|
|
|
inline f32 sqrt(f32 x) { return ::sqrtf(x); }
|
|
|
|
inline f32 mod(f32 x, f32 m) { return x - (int)(x / m) * m; }
|
|
|
|
|
|
|
|
inline f32 approach(f32 t, f32 target, f32 delta) { return t < target ? Calc::min(t + delta, target) : Calc::max(t - delta, target); }
|
|
|
|
inline f32 map(f32 t, f32 old_min, f32 old_max, f32 new_min, f32 new_max) { return new_min + ((t - old_min) / (old_max - old_min)) * (new_max - new_min); }
|
|
|
|
inline f32 clamped_map(f32 t, f32 old_min, f32 old_max, f32 new_min, f32 new_max) { return map(Calc::clamp(t, old_min, old_max), old_min, old_max, new_min, new_max); }
|
|
|
|
inline f32 lerp(f32 a, f32 b, f32 t) { return a + (b - a) * t; }
|
|
|
|
|
|
|
|
inline f32 angle_diff(f32 radians_a, f32 radians_b) { return mod((radians_b - radians_a) + PI, TAU) - PI; }
|
|
|
|
inline f32 angle_lerp(f32 radians_a, f32 radians_b, f32 p)
|
|
|
|
{
|
|
|
|
const auto shortest_angle = mod(mod(radians_b - radians_a, TAU) + (TAU + PI), TAU) - PI;
|
|
|
|
return radians_a + mod(shortest_angle * p, TAU);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline void swap_endian(T* value)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < sizeof(T) / 2; i++)
|
|
|
|
{
|
|
|
|
char* _ptr = (char*)&value;
|
|
|
|
char _temp = *(_ptr + i);
|
|
|
|
*(_ptr + i) = *(_ptr + sizeof(T) - i - 1);
|
|
|
|
*(_ptr + sizeof(T) - i - 1) = _temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 15:15:51 +08:00
|
|
|
inline bool is_big_endian() { return (*((short*)"AB") == 0x4243); }
|
|
|
|
inline bool is_little_endian() { return (*((short*)"AB") != 0x4243); }
|
2022-02-12 08:03:27 +08:00
|
|
|
|
2022-02-13 15:15:51 +08:00
|
|
|
inline bool is_endian(const Endian& endian)
|
2022-02-12 08:03:27 +08:00
|
|
|
{
|
|
|
|
return
|
|
|
|
(endian == Endian::Little && is_little_endian()) ||
|
|
|
|
(endian == Endian::Big && is_big_endian());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|