mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-28 16:58:57 +08:00
cleaned up Calc header, renamed numerics folder to math
This commit is contained in:
parent
cedc57e322
commit
edee79b237
|
@ -30,9 +30,6 @@ add_library(blah
|
|||
src/images/image.cpp
|
||||
src/images/packer.cpp
|
||||
|
||||
src/numerics/calc.cpp
|
||||
src/numerics/color.cpp
|
||||
|
||||
src/streams/bufferstream.cpp
|
||||
src/streams/filestream.cpp
|
||||
src/streams/memorystream.cpp
|
||||
|
|
|
@ -28,10 +28,10 @@
|
|||
#include "blah/images/image.h"
|
||||
#include "blah/images/packer.h"
|
||||
|
||||
#include "blah/numerics/calc.h"
|
||||
#include "blah/numerics/spatial.h"
|
||||
#include "blah/numerics/color.h"
|
||||
#include "blah/numerics/ease.h"
|
||||
#include "blah/math/calc.h"
|
||||
#include "blah/math/spatial.h"
|
||||
#include "blah/math/color.h"
|
||||
#include "blah/math/ease.h"
|
||||
|
||||
#include "blah/streams/bufferstream.h"
|
||||
#include "blah/streams/filestream.h"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/spatial.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
// In-Place constructor new, used by Vector/StackVector
|
||||
#include <new>
|
||||
#include <new> // for in-place constructors, for Vector/StackVector
|
||||
#include <utility> // for std::move
|
||||
|
||||
// Asserts
|
||||
#if defined(DEBUG) || defined(_DEBUG)
|
||||
|
@ -27,6 +27,7 @@ namespace Blah
|
|||
using u64 = uint64_t;
|
||||
using f32 = float;
|
||||
using f64 = double;
|
||||
using size_t = std::size_t;
|
||||
}
|
||||
|
||||
// Logging
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/numerics/color.h>
|
||||
#include <blah/math/spatial.h>
|
||||
#include <blah/math/color.h>
|
||||
#include <blah/graphics/subtexture.h>
|
||||
#include <blah/graphics/spritefont.h>
|
||||
#include <blah/containers/vector.h>
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <blah/graphics/shader.h>
|
||||
#include <blah/graphics/sampler.h>
|
||||
#include <blah/containers/vector.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/spatial.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/spatial.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/graphics/texture.h>
|
||||
#include <blah/graphics/target.h>
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
#include <blah/containers/str.h>
|
||||
#include <blah/containers/vector.h>
|
||||
#include <blah/graphics/subtexture.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/spatial.h>
|
||||
#include <blah/filesystem.h>
|
||||
|
||||
namespace Blah
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
#include <blah/graphics/texture.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/spatial.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include <blah/common.h>
|
||||
#include <blah/graphics/texture.h>
|
||||
#include <blah/containers/stackvector.h>
|
||||
#include <blah/numerics/color.h>
|
||||
#include <blah/math/color.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#pragma once
|
||||
#include <blah/numerics/color.h>
|
||||
#include <blah/math/color.h>
|
||||
#include <blah/images/image.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/streams/stream.h>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
#include <blah/numerics/color.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/color.h>
|
||||
#include <blah/math/spatial.h>
|
||||
#include <blah/filesystem.h>
|
||||
|
||||
namespace Blah
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
#include <blah/images/image.h>
|
||||
#include <blah/numerics/color.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/color.h>
|
||||
#include <blah/math/spatial.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/containers/vector.h>
|
||||
#include <blah/streams/bufferstream.h>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/spatial.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/containers/stackvector.h>
|
||||
|
||||
|
|
88
include/blah/math/calc.h
Normal file
88
include/blah/math/calc.h
Normal file
|
@ -0,0 +1,88 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <math.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
constexpr bool is_big_endian() { return (*((short*)"AB") == 0x4243); }
|
||||
constexpr bool is_little_endian() { return (*((short*)"AB") != 0x4243); }
|
||||
|
||||
constexpr bool is_endian(const Endian& endian)
|
||||
{
|
||||
return
|
||||
(endian == Endian::Little && is_little_endian()) ||
|
||||
(endian == Endian::Big && is_big_endian());
|
||||
}
|
||||
};
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <blah/math/spatial.h>
|
||||
|
||||
#define BLAH_HEX_VALUE(n) ((n >= '0' && n <= '9') ? (n - '0') : ((n >= 'A' && n <= 'F') ? (10 + n - 'A') : ((n >= 'a' && n <= 'f') ? (10 + n - 'a') : 0)))
|
||||
|
||||
|
@ -92,10 +92,36 @@ namespace Blah
|
|||
}
|
||||
|
||||
// Returns an RGBA hex string of the color
|
||||
String to_hex_rgba() const;
|
||||
String to_hex_rgba() const
|
||||
{
|
||||
static const char* hex = "0123456789ABCDEF";
|
||||
|
||||
String str = "00000000";
|
||||
str[0] = hex[(r & 0xF0) >> 4];
|
||||
str[1] = hex[(r & 0x0F) >> 0];
|
||||
str[2] = hex[(g & 0xF0) >> 4];
|
||||
str[3] = hex[(g & 0x0F) >> 0];
|
||||
str[4] = hex[(b & 0xF0) >> 4];
|
||||
str[5] = hex[(b & 0x0F) >> 0];
|
||||
str[6] = hex[(a & 0xF0) >> 4];
|
||||
str[7] = hex[(a & 0x0F) >> 0];
|
||||
return str;
|
||||
}
|
||||
|
||||
// Returns an RGB hex string of the color
|
||||
String to_hex_rgb() const;
|
||||
String Color::to_hex_rgb() const
|
||||
{
|
||||
static const char* hex = "0123456789ABCDEF";
|
||||
|
||||
String str = "000000";
|
||||
str[0] = hex[(r & 0xF0) >> 4];
|
||||
str[1] = hex[(r & 0x0F) >> 0];
|
||||
str[2] = hex[(g & 0xF0) >> 4];
|
||||
str[3] = hex[(g & 0x0F) >> 0];
|
||||
str[4] = hex[(b & 0xF0) >> 4];
|
||||
str[5] = hex[(b & 0x0F) >> 0];
|
||||
return str;
|
||||
}
|
||||
|
||||
// Converts the Color to a Vec3 (RGB)
|
||||
constexpr Vec3f to_vec3() const
|
||||
|
@ -206,6 +232,16 @@ namespace Blah
|
|||
static const Color purple;
|
||||
static const Color teal;
|
||||
};
|
||||
|
||||
inline const Color Color::transparent = Color(0, 0, 0, 0);
|
||||
inline const Color Color::white = Color(255, 255, 255, 255);
|
||||
inline const Color Color::black = Color(0, 0, 0, 255);
|
||||
inline const Color Color::red = Color(255, 0, 0, 255);
|
||||
inline const Color Color::green = Color(0, 255, 0, 255);
|
||||
inline const Color Color::blue = Color(0, 0, 255, 255);
|
||||
inline const Color Color::yellow = Color(255, 255, 0, 255);
|
||||
inline const Color Color::purple = Color(255, 0, 255, 255);
|
||||
inline const Color Color::teal = Color(0, 255, 255, 255);
|
||||
}
|
||||
|
||||
#undef BLAH_HEX_VALUE
|
|
@ -1,5 +1,5 @@
|
|||
#pragma once
|
||||
#include <blah/numerics/calc.h>
|
||||
#include <blah/math/calc.h>
|
||||
#include <blah/common.h>
|
||||
|
||||
namespace Blah
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <cmath>
|
||||
#include <blah/math/calc.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
|
@ -440,7 +440,7 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
constexpr bool Vec2<T>::operator ==(const Vec2& rhs) const {
|
||||
return std::abs(x - rhs.x) < epsilon && std::abs(y - rhs.y) < epsilon;
|
||||
return Calc::abs(x - rhs.x) < epsilon && Calc::abs(y - rhs.y) < epsilon;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -450,12 +450,12 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
constexpr Vec2<T> Vec2<T>::abs() const {
|
||||
return Vec2(std::abs(x), std::abs(y));
|
||||
return Vec2(Calc::abs(x), Calc::abs(y));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vec2<T> Vec2<T>::normal() const {
|
||||
auto len = std::sqrt(x * x + y * y);
|
||||
auto len = Calc::sqrt(x * x + y * y);
|
||||
if (len <= 0)
|
||||
return Vec2<T>(0, 0);
|
||||
return Vec2(x / len, y / len);
|
||||
|
@ -473,7 +473,7 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
T Vec2<T>::length() const {
|
||||
return std::sqrt(x * x + y * y);
|
||||
return static_cast<T>(Calc::sqrt(static_cast<f32>(x * x + y * y)));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -483,7 +483,7 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
T Vec2<T>::angle() const {
|
||||
return std::atan2(y, x);
|
||||
return Calc::atan2(y, x);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -508,8 +508,8 @@ namespace Blah
|
|||
template<class T>
|
||||
Vec2<T> Vec2<T>::from_angle(T radians, T length) {
|
||||
return Vec2(
|
||||
std::cos(radians) * length,
|
||||
std::sin(radians) * length);
|
||||
Calc::cos(radians) * length,
|
||||
Calc::sin(radians) * length);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -549,12 +549,12 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
constexpr Vec2<T> Vec2<T>::min(const Vec2& a, const Vec2& b) {
|
||||
return Vec2(std::fmin(a.x, b.x), std::fmin(a.y, b.y));
|
||||
return Vec2(Calc::min(a.x, b.x), Calc::min(a.y, b.y));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
constexpr Vec2<T> Vec2<T>::max(const Vec2& a, const Vec2& b) {
|
||||
return Vec2(std::fmax(a.x, b.x), std::fmax(a.y, b.y));
|
||||
return Vec2(Calc::max(a.x, b.x), Calc::max(a.y, b.y));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -583,7 +583,7 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
T Vec3<T>::length() const {
|
||||
return std::sqrt(x * x + y * y + z * z);
|
||||
return Calc::sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -663,7 +663,7 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
constexpr bool Rect<T>::operator==(const Rect& rhs) const {
|
||||
return std::abs(x - rhs.x) < epsilon && std::abs(y - rhs.y) < epsilon && std::abs(w - rhs.w) < epsilon && std::abs(h - rhs.h) < epsilon;
|
||||
return Calc::abs(x - rhs.x) < epsilon && Calc::abs(y - rhs.y) < epsilon && Calc::abs(w - rhs.w) < epsilon && Calc::abs(h - rhs.h) < epsilon;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -730,14 +730,14 @@ namespace Blah
|
|||
|
||||
if (x + w >= against.x && x < against.x + against.w)
|
||||
{
|
||||
result.x = std::fmax(x, against.x);
|
||||
result.w = std::fmin(x + w, against.x + against.w) - result.x;
|
||||
result.x = Calc::max(x, against.x);
|
||||
result.w = Calc::min(x + w, against.x + against.w) - result.x;
|
||||
}
|
||||
|
||||
if (y + h >= against.y && y < against.y + against.h)
|
||||
{
|
||||
result.y = std::fmax(y, against.y);
|
||||
result.h = std::fmin(y + h, against.y + against.h) - result.y;
|
||||
result.y = Calc::max(y, against.y);
|
||||
result.h = Calc::min(y + h, against.y + against.h) - result.y;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -1008,7 +1008,7 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
T Mat3x2<T>::scaling_factor() const {
|
||||
return std::sqrt(m11 * m11 + m12 * m12);
|
||||
return Calc::sqrt(m11 * m11 + m12 * m12);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -1083,8 +1083,8 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
Mat3x2<T> Mat3x2<T>::create_rotation(T radians) {
|
||||
auto c = std::cos(radians);
|
||||
auto s = std::sin(radians);
|
||||
auto c = Calc::cos(radians);
|
||||
auto s = Calc::sin(radians);
|
||||
return Mat3x2<T>(c, s, -s, c, 0, 0);
|
||||
}
|
||||
|
||||
|
@ -1158,12 +1158,12 @@ namespace Blah
|
|||
template<class T>
|
||||
constexpr bool Mat3x2<T>::operator==(const Mat3x2& rhs) {
|
||||
return
|
||||
std::abs(m11 - rhs.m11) < epsilon &&
|
||||
std::abs(m12 - rhs.m12) < epsilon &&
|
||||
std::abs(m21 - rhs.m21) < epsilon &&
|
||||
std::abs(m22 - rhs.m22) < epsilon &&
|
||||
std::abs(m31 - rhs.m31) < epsilon &&
|
||||
std::abs(m32 - rhs.m32) < epsilon;
|
||||
Calc::abs(m11 - rhs.m11) < epsilon &&
|
||||
Calc::abs(m12 - rhs.m12) < epsilon &&
|
||||
Calc::abs(m21 - rhs.m21) < epsilon &&
|
||||
Calc::abs(m22 - rhs.m22) < epsilon &&
|
||||
Calc::abs(m31 - rhs.m31) < epsilon &&
|
||||
Calc::abs(m32 - rhs.m32) < epsilon;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
|
@ -1222,7 +1222,7 @@ namespace Blah
|
|||
|
||||
template<class T>
|
||||
Mat4x4<T> Mat4x4<T>::create_perspective(T field_of_view, T ratio, T z_near_plane, T z_far_plane) {
|
||||
auto scale_x = 1 / std::tan(field_of_view * 0.5);
|
||||
auto scale_x = 1 / Calc::tan(field_of_view * 0.5);
|
||||
auto scale_y = scale_x / ratio;
|
||||
|
||||
Mat4x4 result;
|
|
@ -1,107 +0,0 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
enum class Endian
|
||||
{
|
||||
Little,
|
||||
Big
|
||||
};
|
||||
|
||||
namespace Calc
|
||||
{
|
||||
constexpr float PI = 3.141592653f;
|
||||
constexpr float TAU = PI * 2.0f;
|
||||
constexpr float RIGHT = 0;
|
||||
constexpr float LEFT = PI;
|
||||
constexpr float UP = PI / -2;
|
||||
constexpr float DOWN = PI / 2;
|
||||
|
||||
float approach(float t, float target, float delta);
|
||||
|
||||
float map(float t, float old_min, float old_max, float new_min, float new_max);
|
||||
|
||||
float clamped_map(float t, float old_min, float old_max, float new_min, float new_max);
|
||||
|
||||
int sign(int x);
|
||||
|
||||
float sign(float x);
|
||||
|
||||
int abs(int x);
|
||||
|
||||
float abs(float 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...)); }
|
||||
|
||||
float round(float x);
|
||||
|
||||
float floor(float x);
|
||||
|
||||
float ceiling(float x);
|
||||
|
||||
float mod(float x, float m);
|
||||
|
||||
float sin(float x);
|
||||
|
||||
float cos(float x);
|
||||
|
||||
float tan(float x);
|
||||
|
||||
float atan2(float y, float x);
|
||||
|
||||
float pow(float x, float n);
|
||||
|
||||
float sqrt(float x);
|
||||
|
||||
float snap(float val, float interval);
|
||||
|
||||
float angle_diff(float radians_a, float radians_b);
|
||||
|
||||
float angle_lerp(float radians_a, float radians_b, float p);
|
||||
|
||||
float lerp(float a, float b, float t);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool is_big_endian()
|
||||
{
|
||||
return (*((short*)"AB") == 0x4243);
|
||||
}
|
||||
|
||||
inline bool is_little_endian()
|
||||
{
|
||||
return (*((short*)"AB") != 0x4243);
|
||||
}
|
||||
|
||||
inline bool is_endian(const Endian& endian)
|
||||
{
|
||||
return
|
||||
(endian == Endian::Little && is_little_endian()) ||
|
||||
(endian == Endian::Big && is_big_endian());
|
||||
}
|
||||
};
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
#include <blah/common.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/numerics/calc.h>
|
||||
#include <blah/math/calc.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Blah
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <blah/graphics/mesh.h>
|
||||
#include <blah/graphics/shader.h>
|
||||
#include <blah/graphics/material.h>
|
||||
#include <blah/numerics/calc.h>
|
||||
#include <blah/math/calc.h>
|
||||
#include <blah/app.h>
|
||||
#include "../internal/renderer.h"
|
||||
#include <cmath>
|
||||
|
@ -54,10 +54,10 @@ namespace
|
|||
*_i++ = (u32)m_vertices.size() + 3; \
|
||||
Vertex* _v = m_vertices.expand(4); \
|
||||
if (integerize) { \
|
||||
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash, std::round); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash, std::round); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px2, py2, tx2, ty2, col2, mult, fill, wash, std::round); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px3, py3, tx3, ty3, col3, mult, fill, wash, std::round); \
|
||||
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash, Calc::floor); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash, Calc::floor); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px2, py2, tx2, ty2, col2, mult, fill, wash, Calc::floor); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px3, py3, tx3, ty3, col3, mult, fill, wash, Calc::floor); \
|
||||
} else { \
|
||||
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash, float); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash, float); _v++; \
|
||||
|
@ -75,9 +75,9 @@ namespace
|
|||
*_i++ = (u32)m_vertices.size() + 2; \
|
||||
Vertex* _v = m_vertices.expand(3); \
|
||||
if (integerize) { \
|
||||
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash, std::floor); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash, std::floor); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px2, py2, tx2, ty2, col2, mult, fill, wash, std::floor); \
|
||||
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash, Calc::floor); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash, Calc::floor); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px2, py2, tx2, ty2, col2, mult, fill, wash, Calc::floor); \
|
||||
} else { \
|
||||
MAKE_VERTEX(_v, m_matrix, px0, py0, tx0, ty0, col0, mult, fill, wash, float); _v++; \
|
||||
MAKE_VERTEX(_v, m_matrix, px1, py1, tx1, ty1, col1, mult, fill, wash, float); _v++; \
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include <blah/graphics/subtexture.h>
|
||||
#include <blah/numerics/calc.h>
|
||||
#include <blah/math/calc.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
|
|
|
@ -5,7 +5,9 @@ using namespace Blah;
|
|||
|
||||
TargetRef Target::create(int width, int height)
|
||||
{
|
||||
return create(width, height, { TextureFormat::RGBA });
|
||||
AttachmentFormats formats;
|
||||
formats.push_back(TextureFormat::RGBA);
|
||||
return create(width, height, formats);
|
||||
}
|
||||
|
||||
TargetRef Target::create(int width, int height, const AttachmentFormats& textures)
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include <blah/streams/filestream.h>
|
||||
#include <blah/filesystem.h>
|
||||
#include <blah/common.h>
|
||||
#include <blah/numerics/calc.h>
|
||||
#include <blah/math/calc.h>
|
||||
|
||||
#define STBI_NO_STDIO
|
||||
#define STBI_ONLY_ZLIB
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <blah/images/font.h>
|
||||
#include <blah/streams/filestream.h>
|
||||
#include <blah/numerics/calc.h>
|
||||
#include <blah/math/calc.h>
|
||||
#include <blah/common.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include <blah/app.h>
|
||||
#include <blah/time.h>
|
||||
#include <blah/common.h>
|
||||
#include <blah/numerics/calc.h>
|
||||
#include <blah/math/calc.h>
|
||||
#include "internal/input.h"
|
||||
#include "internal/platform.h"
|
||||
#include <blah/graphics/target.h>
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include <blah/graphics/shader.h>
|
||||
#include <blah/graphics/mesh.h>
|
||||
#include <blah/graphics/material.h>
|
||||
#include <blah/numerics/color.h>
|
||||
#include <blah/math/color.h>
|
||||
|
||||
#define BLAH_ASSERT_RENDERER() BLAH_ASSERT(Renderer::instance, "Renderer has not been created")
|
||||
|
||||
|
|
|
@ -1,115 +0,0 @@
|
|||
#include <blah/numerics/calc.h>
|
||||
#include <blah/numerics/spatial.h>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
float Calc::approach(float t, float target, float delta)
|
||||
{
|
||||
return t < target ? min(t + delta, target) : max(t - delta, target);
|
||||
}
|
||||
|
||||
float Calc::map(float t, float old_min, float old_max, float new_min, float new_max)
|
||||
{
|
||||
return new_min + ((t - old_min) / (old_max - old_min)) * (new_max - new_min);
|
||||
}
|
||||
|
||||
float Calc::clamped_map(float t, float old_min, float old_max, float new_min, float new_max)
|
||||
{
|
||||
return map(Calc::clamp(t, old_min, old_max), old_min, old_max, new_min, new_max);
|
||||
}
|
||||
|
||||
int Calc::sign(int x)
|
||||
{
|
||||
return (x < 0 ? -1 : (x > 0 ? 1 : 0));
|
||||
}
|
||||
|
||||
float Calc::sign(float x)
|
||||
{
|
||||
return (x < 0 ? -1.0f : (x > 0 ? 1.0f : 0.0f));
|
||||
}
|
||||
|
||||
int Calc::abs(int x)
|
||||
{
|
||||
return x < 0 ? -x : x;
|
||||
}
|
||||
|
||||
float Calc::abs(float x)
|
||||
{
|
||||
return x < 0 ? -x : x;
|
||||
}
|
||||
|
||||
float Calc::round(float x)
|
||||
{
|
||||
return roundf(x);
|
||||
}
|
||||
|
||||
float Calc::floor(float x)
|
||||
{
|
||||
return floorf(x);
|
||||
}
|
||||
|
||||
float Calc::ceiling(float x)
|
||||
{
|
||||
return ceilf(x);
|
||||
}
|
||||
|
||||
float Calc::mod(float x, float m)
|
||||
{
|
||||
return x - (int)(x / m) * m;
|
||||
}
|
||||
|
||||
float Calc::sin(float x)
|
||||
{
|
||||
return sinf(x);
|
||||
}
|
||||
|
||||
float Calc::cos(float x)
|
||||
{
|
||||
return cosf(x);
|
||||
}
|
||||
|
||||
float Calc::tan(float x)
|
||||
{
|
||||
return tanf(x);
|
||||
}
|
||||
|
||||
float Calc::atan2(float y, float x)
|
||||
{
|
||||
return atan2f(y, x);
|
||||
}
|
||||
|
||||
float Calc::pow(float x, float n)
|
||||
{
|
||||
return powf(x, n);
|
||||
}
|
||||
|
||||
float Calc::sqrt(float x)
|
||||
{
|
||||
return sqrtf(x);
|
||||
}
|
||||
|
||||
float Calc::snap(float val, float interval)
|
||||
{
|
||||
if (val > 0)
|
||||
return ((int)((val + interval / 2) / interval)) * interval;
|
||||
else
|
||||
return ((int)((val - interval / 2) / interval)) * interval;
|
||||
}
|
||||
|
||||
float Calc::angle_diff(float radians_a, float radians_b)
|
||||
{
|
||||
return mod((radians_b - radians_a) + PI, TAU) - PI;
|
||||
}
|
||||
|
||||
float Calc::angle_lerp(float radians_a, float radians_b, float p)
|
||||
{
|
||||
const auto shortest_angle = mod(mod(radians_b - radians_a, TAU) + (TAU + PI), TAU) - PI;
|
||||
return radians_a + mod(shortest_angle * p, TAU);
|
||||
}
|
||||
|
||||
float Calc::lerp(float a, float b, float t)
|
||||
{
|
||||
return a + (b - a) * t;
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
#include <blah/numerics/color.h>
|
||||
|
||||
using namespace Blah;
|
||||
|
||||
String Color::to_hex_rgba() const
|
||||
{
|
||||
static const char* hex = "0123456789ABCDEF";
|
||||
|
||||
String str = "00000000";
|
||||
str[0] = hex[(r & 0xF0) >> 4];
|
||||
str[1] = hex[(r & 0x0F) >> 0];
|
||||
str[2] = hex[(g & 0xF0) >> 4];
|
||||
str[3] = hex[(g & 0x0F) >> 0];
|
||||
str[4] = hex[(b & 0xF0) >> 4];
|
||||
str[5] = hex[(b & 0x0F) >> 0];
|
||||
str[6] = hex[(a & 0xF0) >> 4];
|
||||
str[7] = hex[(a & 0x0F) >> 0];
|
||||
return str;
|
||||
}
|
||||
|
||||
String Color::to_hex_rgb() const
|
||||
{
|
||||
static const char* hex = "0123456789ABCDEF";
|
||||
|
||||
String str = "000000";
|
||||
str[0] = hex[(r & 0xF0) >> 4];
|
||||
str[1] = hex[(r & 0x0F) >> 0];
|
||||
str[2] = hex[(g & 0xF0) >> 4];
|
||||
str[3] = hex[(g & 0x0F) >> 0];
|
||||
str[4] = hex[(b & 0xF0) >> 4];
|
||||
str[5] = hex[(b & 0x0F) >> 0];
|
||||
return str;
|
||||
}
|
||||
|
||||
const Color Color::transparent = Color(0, 0, 0, 0);
|
||||
const Color Color::white = Color(255, 255, 255, 255);
|
||||
const Color Color::black = Color(0, 0, 0, 255);
|
||||
const Color Color::red = Color(255, 0, 0, 255);
|
||||
const Color Color::green = Color(0, 255, 0, 255);
|
||||
const Color Color::blue = Color(0, 0, 255, 255);
|
||||
const Color Color::yellow = Color(255, 255, 0, 255);
|
||||
const Color Color::purple = Color(255, 0, 255, 255);
|
||||
const Color Color::teal = Color(0, 255, 255, 255);
|
Loading…
Reference in New Issue
Block a user