1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementa PackedValue

This commit is contained in:
Danilo 2024-05-18 18:14:11 -03:00
parent 9e3b820a8f
commit bceb6b122f
3 changed files with 46 additions and 36 deletions

View File

@ -47,7 +47,7 @@ add_executable (xna WIN32
"common/color.cpp"
"common/collision.cpp"
"common/gjk.cpp"
"common/numerics.cpp")
"common/numerics.cpp" "common/packedvalue.cpp")
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET xna PROPERTY CXX_STANDARD 20)

View File

@ -0,0 +1,38 @@
#include "common/packedvalue.hpp"
namespace xna {
Uint PackUtils::PackUnsigned(float bitmask, float value) {
return static_cast<Uint>(ClampAndRound(value, 0.0f, bitmask));
}
Uint PackUtils::PackSigned(Uint bitmask, float value) {
const auto max = static_cast<float>(bitmask >> 1);
const auto min = -max - 1.0F;
return static_cast<Uint>(ClampAndRound(value, min, max)) & bitmask;
}
Uint PackUtils::PackUNorm(float bitmask, float value) {
value *= bitmask;
return static_cast<Uint>(ClampAndRound(value, 0.0f, bitmask));
}
Uint PackUtils::PackSNorm(Uint bitmask, float value) {
const auto max = static_cast<float>(bitmask >> 1);
value *= max;
return static_cast<Uint>(ClampAndRound(value, -max, max)) & bitmask;
}
double PackUtils::ClampAndRound(float value, float min, float max) {
if (isnan(value))
return 0.0;
if (isinf(value))
return value < 0 ? static_cast<double>(min) : static_cast<double>(max);
if (value < min)
return static_cast<double>(min);
return value > max ? static_cast<double>(max) : std::round(static_cast<double>(value));
}
}

View File

@ -17,33 +17,12 @@ namespace xna {
virtual void PackedValue(T const& value) = 0;
};
struct PackUtils {
static Uint PackUnsigned(float bitmask, float value) {
return static_cast<Uint>(ClampAndRound(value, 0.0f, bitmask));
}
static Uint PackSigned(Uint bitmask, float value) {
const auto max = static_cast<float>(bitmask >> 1);
const auto min = -max - 1.0F;
return static_cast<Uint>(ClampAndRound(value, min, max)) & bitmask;
}
static Uint PackUNorm(float bitmask, float value) {
value *= bitmask;
return static_cast<Uint>(ClampAndRound(value, 0.0f, bitmask));
}
struct PackUtils {
static constexpr float UnpackUNorm(Uint bitmask, Uint value) {
value &= bitmask;
return static_cast<float>(value) / static_cast<float>(bitmask);
}
static Uint PackSNorm(Uint bitmask, float value) {
const auto max = static_cast<float>(bitmask >> 1);
value *= max;
return static_cast<Uint>(ClampAndRound(value, -max, max)) & bitmask;
}
}
static constexpr float UnpackSNorm(Uint bitmask, Uint value) {
const auto num1 = (bitmask + 1U) >> 1;
@ -66,18 +45,11 @@ namespace xna {
return static_cast<Int>(value) / num2;
}
static double ClampAndRound(float value, float min, float max) {
if (isnan(value))
return 0.0;
if (isinf(value))
return value < 0 ? static_cast<double>(min) : static_cast<double>(max);
if (value < min)
return static_cast<double>(min);
return value > max ? static_cast<double>(max) : std::round(static_cast<double>(value));
}
static Uint PackUnsigned(float bitmask, float value);
static Uint PackSigned(Uint bitmask, float value);
static Uint PackUNorm(float bitmask, float value);
static Uint PackSNorm(Uint bitmask, float value);
static double ClampAndRound(float value, float min, float max);
};
}