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

Correções em color.hpp

This commit is contained in:
Danilo 2024-11-11 09:58:40 -03:00
parent e42d09196c
commit 27e432aa62
5 changed files with 84 additions and 83 deletions

View File

@ -1,18 +1,18 @@
#ifndef XNA_COMMON_COLOR_HPP #ifndef XNA_COMMON_COLOR_HPP
#define XNA_COMMON_COLOR_HPP #define XNA_COMMON_COLOR_HPP
#include "../default.hpp"
#include "packedvalue.hpp" #include "packedvalue.hpp"
#include <limits>
namespace xna { namespace xna {
//Represents a four-component color using red, green, blue, and alpha data. //Represents a four-component color using red, green, blue, and alpha data.
struct Color : public IPackedVector, public IPackedVectorT<Uint> { struct Color : public IPackedVector, public IPackedVectorT<uint32_t> {
constexpr Color() = default; constexpr Color() = default;
constexpr Color(Uint packedValue) constexpr Color(uint32_t packedValue)
: _packedValue(packedValue) {} : _packedValue(packedValue) {}
constexpr Color(Int r, Int g, Int b, Int a = 255U) { constexpr Color(int32_t r, int32_t g, int32_t b, int32_t a = 255U) {
if (((r | g | b | a) & -256) != 0) if (((r | g | b | a) & -256) != 0)
{ {
r = ClampToByte32(r); r = ClampToByte32(r);
@ -25,7 +25,7 @@ namespace xna {
b <<= 16; b <<= 16;
a <<= 24; a <<= 24;
_packedValue = static_cast<Uint>(r | g | b | a); _packedValue = static_cast<uint32_t>(r | g | b | a);
} }
Color(float r, float g, float b, float a = 1.0F); Color(float r, float g, float b, float a = 1.0F);
@ -38,12 +38,12 @@ namespace xna {
static Color FromNonPremultiplied(Vector4 const& vector); static Color FromNonPremultiplied(Vector4 const& vector);
//Converts a non-premultipled alpha color to a color that contains premultiplied alpha. //Converts a non-premultipled alpha color to a color that contains premultiplied alpha.
static Color FromNonPremultiplied(Int r, Int g, Int b, Int a); static Color FromNonPremultiplied(int32_t r, int32_t g, int32_t b, int32_t a);
//Gets a three-component vector representation for this object. //Gets a three-component vector representation for this object.
constexpr Vector3 ToVector3() const { constexpr Vector3 ToVector3() const {
Vector3 vector3; Vector3 vector3;
const auto byteMax = static_cast<Uint>(ByteMaxValue); const auto byteMax = static_cast<uint32_t>(ByteMaxValue);
vector3.X = PackUtils::UnpackUNorm(byteMax, _packedValue); vector3.X = PackUtils::UnpackUNorm(byteMax, _packedValue);
vector3.Y = PackUtils::UnpackUNorm(byteMax, _packedValue >> 8); vector3.Y = PackUtils::UnpackUNorm(byteMax, _packedValue >> 8);
vector3.Z = PackUtils::UnpackUNorm(byteMax, _packedValue >> 16); vector3.Z = PackUtils::UnpackUNorm(byteMax, _packedValue >> 16);
@ -53,7 +53,7 @@ namespace xna {
//Gets a four-component vector representation for this object. //Gets a four-component vector representation for this object.
constexpr virtual Vector4 ToVector4() const override { constexpr virtual Vector4 ToVector4() const override {
Vector4 vector4; Vector4 vector4;
const auto byteMax = static_cast<Uint>(ByteMaxValue); const auto byteMax = static_cast<uint32_t>(ByteMaxValue);
vector4.X = PackUtils::UnpackUNorm(byteMax, _packedValue); vector4.X = PackUtils::UnpackUNorm(byteMax, _packedValue);
vector4.Y = PackUtils::UnpackUNorm(byteMax, _packedValue >> 8); vector4.Y = PackUtils::UnpackUNorm(byteMax, _packedValue >> 8);
vector4.Z = PackUtils::UnpackUNorm(byteMax, _packedValue >> 16); vector4.Z = PackUtils::UnpackUNorm(byteMax, _packedValue >> 16);
@ -62,52 +62,52 @@ namespace xna {
} }
//Gets or sets the red component value of this Color. //Gets or sets the red component value of this Color.
constexpr Byte R() const { constexpr uint8_t R() const {
return static_cast<Byte>(_packedValue); return static_cast<uint8_t>(_packedValue);
} }
//Gets or sets the red component value of this Color. //Gets or sets the red component value of this Color.
constexpr void R(Byte value) { constexpr void R(uint8_t value) {
_packedValue = _packedValue & (UintMaxValue - 255) | static_cast<Uint>(value); _packedValue = _packedValue & (UintMaxValue - 255) | static_cast<uint32_t>(value);
} }
//Gets or sets the green component value of this Color. //Gets or sets the green component value of this Color.
constexpr Byte G() const { constexpr uint8_t G() const {
return static_cast<Byte>(_packedValue >> 8); return static_cast<uint8_t>(_packedValue >> 8);
} }
//Gets or sets the green component value of this Color. //Gets or sets the green component value of this Color.
constexpr void G(Byte value) { constexpr void G(uint8_t value) {
_packedValue = (static_cast<Int>(_packedValue) & -65281 | static_cast<Int>(value) << 8); _packedValue = (static_cast<int32_t>(_packedValue) & -65281 | static_cast<int32_t>(value) << 8);
} }
//Gets or sets the blue component value of this Color. //Gets or sets the blue component value of this Color.
constexpr Byte B() const { constexpr uint8_t B() const {
return static_cast<Byte>(_packedValue >> 16); return static_cast<uint8_t>(_packedValue >> 16);
} }
//Gets or sets the blue component value of this Color. //Gets or sets the blue component value of this Color.
constexpr void B(Byte value) { constexpr void B(uint8_t value) {
_packedValue = (static_cast<Int>(_packedValue) & -16711681 | static_cast<Int>(value) << 16); _packedValue = (static_cast<int32_t>(_packedValue) & -16711681 | static_cast<int32_t>(value) << 16);
} }
//Gets or sets the alpha component value. //Gets or sets the alpha component value.
constexpr Byte A() const { constexpr uint8_t A() const {
return static_cast<Byte>(_packedValue >> 24); return static_cast<uint8_t>(_packedValue >> 24);
} }
//Gets or sets the alpha component value. //Gets or sets the alpha component value.
constexpr void A(Byte value) { constexpr void A(uint8_t value) {
_packedValue = (static_cast<Int>(_packedValue) & 16777215 | static_cast<Int>(value) << 24); _packedValue = (static_cast<int32_t>(_packedValue) & 16777215 | static_cast<int32_t>(value) << 24);
} }
//Gets or sets the current color as a packed value. //Gets or sets the current color as a packed value.
virtual constexpr Uint PackedValue() const override { virtual constexpr uint32_t PackedValue() const override {
return _packedValue; return _packedValue;
} }
//Gets or sets the current color as a packed value. //Gets or sets the current color as a packed value.
virtual constexpr void PackedValue(Uint const& value) override { virtual constexpr void PackedValue(uint32_t const& value) override {
_packedValue = value; _packedValue = value;
} }
@ -116,19 +116,19 @@ namespace xna {
//Multiply each color component by the scale factor. //Multiply each color component by the scale factor.
static constexpr Color Multiply(Color const& value, float scale) { static constexpr Color Multiply(Color const& value, float scale) {
const Uint r = value.R(); const uint32_t r = value.R();
const Uint g = value.G(); const uint32_t g = value.G();
const Uint b = value.B(); const uint32_t b = value.B();
const Uint a = value.A(); const uint32_t a = value.A();
scale *= 65536.0f; scale *= 65536.0f;
const Uint num5 = scale >= 0.0F ? (scale <= 16777215.0F ? static_cast<Uint>(scale) : 16777215U) : 0U; const uint32_t num5 = scale >= 0.0F ? (scale <= 16777215.0F ? static_cast<uint32_t>(scale) : 16777215U) : 0U;
Uint r1 = r * num5 >> 16; uint32_t r1 = r * num5 >> 16;
Uint g1 = g * num5 >> 16; uint32_t g1 = g * num5 >> 16;
Uint b1 = b * num5 >> 16; uint32_t b1 = b * num5 >> 16;
Uint a1 = a * num5 >> 16; uint32_t a1 = a * num5 >> 16;
if (r1 > ByteMaxValue) if (r1 > ByteMaxValue)
r1 = ByteMaxValue; r1 = ByteMaxValue;
@ -142,13 +142,13 @@ namespace xna {
if (a1 > ByteMaxValue) if (a1 > ByteMaxValue)
a1 = ByteMaxValue; a1 = ByteMaxValue;
const auto r2 = static_cast<Int>(r1); const auto r2 = static_cast<int32_t>(r1);
const auto g2 = static_cast<Int>(g1); const auto g2 = static_cast<int32_t>(g1);
const auto b2 = static_cast<Int>(b1); const auto b2 = static_cast<int32_t>(b1);
const auto a2 = static_cast<Int>(a1); const auto a2 = static_cast<int32_t>(a1);
Color color; Color color;
color._packedValue = static_cast<Uint>(r2 | g2 << 8 | b2 << 16 | a2 << 24); color._packedValue = static_cast<uint32_t>(r2 | g2 << 8 | b2 << 16 | a2 << 24);
return color; return color;
} }
@ -160,21 +160,24 @@ namespace xna {
return Color::Multiply(value, scale); return Color::Multiply(value, scale);
} }
constexpr operator Uint() const { constexpr operator uint32_t() const {
return _packedValue; return _packedValue;
} }
private: private:
Uint _packedValue{ 0 }; uint32_t _packedValue{ 0 };
static constexpr Int ClampToByte32(Int value) noexcept { static constexpr int32_t ClampToByte32(int32_t value) noexcept {
if (value < 0) if (value < 0)
return 0; return 0;
return value > ByteMaxValue ? ByteMaxValue : value; return value > ByteMaxValue ? ByteMaxValue : value;
} }
static Uint PackHelper(float vectorX, float vectorY, float vectorZ, float vectorW); static uint32_t PackHelper(float vectorX, float vectorY, float vectorZ, float vectorW);
static constexpr uint8_t ByteMaxValue = (std::numeric_limits<uint8_t>::max)();
static constexpr uint32_t UintMaxValue = (std::numeric_limits<uint32_t>::max)();
}; };
struct Colors { struct Colors {
@ -315,7 +318,7 @@ namespace xna {
static constexpr Color Turquoise{ Color(4291878976U) }; static constexpr Color Turquoise{ Color(4291878976U) };
static constexpr Color Violet{ Color(4293821166U) }; static constexpr Color Violet{ Color(4293821166U) };
static constexpr Color Wheat{ Color(4289978101U) }; static constexpr Color Wheat{ Color(4289978101U) };
static constexpr Color White{ Color(UintMaxValue) }; static constexpr Color White{ Color((std::numeric_limits<uint32_t>::max)()) };
static constexpr Color WhiteSmoke{ Color(4294309365U) }; static constexpr Color WhiteSmoke{ Color(4294309365U) };
static constexpr Color Yellow{ Color(4278255615U) }; static constexpr Color Yellow{ Color(4278255615U) };
static constexpr Color YellowGreen{ Color(4281519514U) }; static constexpr Color YellowGreen{ Color(4281519514U) };

View File

@ -1,7 +1,6 @@
#ifndef CXNA_COMMON_PACKEDVECTOR_HPP #ifndef CXNA_COMMON_PACKEDVECTOR_HPP
#define CXNA_COMMON_PACKEDVECTOR_HPP #define CXNA_COMMON_PACKEDVECTOR_HPP
#include "../default.hpp"
#include "numerics.hpp" #include "numerics.hpp"
#include <cmath> #include <cmath>
@ -19,17 +18,17 @@ namespace xna {
struct PackUtils { struct PackUtils {
static constexpr float UnpackUNorm(Uint bitmask, Uint value) { static constexpr float UnpackUNorm(uint32_t bitmask, uint32_t value) {
value &= bitmask; value &= bitmask;
return static_cast<float>(value) / static_cast<float>(bitmask); return static_cast<float>(value) / static_cast<float>(bitmask);
} }
static constexpr float UnpackSNorm(Uint bitmask, Uint value) { static constexpr float UnpackSNorm(uint32_t bitmask, uint32_t value) {
const auto num1 = (bitmask + 1U) >> 1; const auto num1 = (bitmask + 1U) >> 1;
const auto ivalue = static_cast<Int>(value); const auto ivalue = static_cast<int32_t>(value);
const auto inum1 = static_cast<Int>(num1); const auto inum1 = static_cast<int32_t>(num1);
const auto ibitmask = static_cast<Int>(bitmask); const auto ibitmask = static_cast<int32_t>(bitmask);
if ((ivalue & inum1) != 0) { if ((ivalue & inum1) != 0) {
if ((ivalue & ibitmask) == inum1) if ((ivalue & ibitmask) == inum1)
@ -42,13 +41,13 @@ namespace xna {
const auto num2 = static_cast<float>(bitmask >> 1); const auto num2 = static_cast<float>(bitmask >> 1);
return static_cast<Int>(value) / num2; return static_cast<int32_t>(value) / num2;
} }
static Uint PackUnsigned(float bitmask, float value); static uint32_t PackUnsigned(float bitmask, float value);
static Uint PackSigned(Uint bitmask, float value); static uint32_t PackSigned(uint32_t bitmask, float value);
static Uint PackUNorm(float bitmask, float value); static uint32_t PackUNorm(float bitmask, float value);
static Uint PackSNorm(Uint bitmask, float value); static uint32_t PackSNorm(uint32_t bitmask, float value);
static double ClampAndRound(float value, float min, float max); static double ClampAndRound(float value, float min, float max);
}; };
} }

View File

@ -6,7 +6,6 @@
add_library (Xn65 STATIC add_library (Xn65 STATIC
"csharp/stream.cpp" "csharp/stream.cpp"
"csharp/binary.cpp" "csharp/binary.cpp"
"game/component.cpp" "game/component.cpp"
"game/servicecontainer.cpp" "game/servicecontainer.cpp"
"content/manager.cpp" "content/manager.cpp"

View File

@ -23,7 +23,7 @@ namespace xna {
return color; return color;
} }
Color Color::FromNonPremultiplied(Int r, Int g, Int b, Int a) { Color Color::FromNonPremultiplied(int32_t r, int32_t g, int32_t b, int32_t a) {
r = ClampToByte32(r * a / ByteMaxValue); r = ClampToByte32(r * a / ByteMaxValue);
g = ClampToByte32(g * a / ByteMaxValue); g = ClampToByte32(g * a / ByteMaxValue);
b = ClampToByte32(b * a / ByteMaxValue); b = ClampToByte32(b * a / ByteMaxValue);
@ -33,33 +33,33 @@ namespace xna {
a <<= 24; a <<= 24;
Color color; Color color;
color._packedValue = static_cast<Uint>(r | g | b | a); color._packedValue = static_cast<uint32_t>(r | g | b | a);
return color; return color;
} }
Color Color::Lerp(Color const& value1, Color const& value2, float amount) { Color Color::Lerp(Color const& value1, Color const& value2, float amount) {
const Int r1 = value1.R(); const int32_t r1 = value1.R();
const Int g1 = value1.G(); const int32_t g1 = value1.G();
const Int b1 = value1.B(); const int32_t b1 = value1.B();
const Int a1 = value1.A(); const int32_t a1 = value1.A();
const Int r2 = value2.R(); const int32_t r2 = value2.R();
const Int g2 = value2.G(); const int32_t g2 = value2.G();
const Int b2 = value2.B(); const int32_t b2 = value2.B();
const Int a2 = value2.A(); const int32_t a2 = value2.A();
const auto bitmask = static_cast<Int>(PackUtils::PackUNorm(65536.0f, amount)); const auto bitmask = static_cast<int32_t>(PackUtils::PackUNorm(65536.0f, amount));
const Int r = r1 + ((r2 - r1) * bitmask >> 16); const int32_t r = r1 + ((r2 - r1) * bitmask >> 16);
const Int g = g1 + ((g2 - g1) * bitmask >> 16); const int32_t g = g1 + ((g2 - g1) * bitmask >> 16);
const Int b = b1 + ((b2 - b1) * bitmask >> 16); const int32_t b = b1 + ((b2 - b1) * bitmask >> 16);
const Int a = a1 + ((a2 - a1) * bitmask >> 16); const int32_t a = a1 + ((a2 - a1) * bitmask >> 16);
Color color; Color color;
color._packedValue = static_cast<Uint>(r | g << 8 | b << 16 | a << 24); color._packedValue = static_cast<uint32_t>(r | g << 8 | b << 16 | a << 24);
return color; return color;
} }
Uint Color::PackHelper(float vectorX, float vectorY, float vectorZ, float vectorW) { uint32_t Color::PackHelper(float vectorX, float vectorY, float vectorZ, float vectorW) {
const auto byteMax = static_cast<float>(ByteMaxValue); const auto byteMax = static_cast<float>(ByteMaxValue);
const auto x = PackUtils::PackUNorm(byteMax, vectorX); const auto x = PackUtils::PackUNorm(byteMax, vectorX);
const auto y = PackUtils::PackUNorm(byteMax, vectorY) << 8; const auto y = PackUtils::PackUNorm(byteMax, vectorY) << 8;

View File

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