diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 7935ea8..4d881ea 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -3,7 +3,7 @@ # # Add source to this project's executable. -add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizerstate-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp" "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" "platform/spritebatch-dx.cpp" "platform/spritefont-dx.cpp" "platform/depthstencilstate-dx.cpp" "platform/keyboard-dx.cpp" "platform/mouse-dx.cpp" "platform/gamepad-dx.cpp") +add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizerstate-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp" "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" "platform/spritebatch-dx.cpp" "platform/spritefont-dx.cpp" "platform/depthstencilstate-dx.cpp" "platform/keyboard-dx.cpp" "platform/mouse-dx.cpp" "platform/gamepad-dx.cpp" "common/vectors.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/common/matrix.cpp b/framework/common/matrix.cpp index 5aa447e..b2bcf1f 100644 --- a/framework/common/matrix.cpp +++ b/framework/common/matrix.cpp @@ -283,5 +283,5 @@ namespace xna { world.M44 = 1.0f; return world; - } + } } \ No newline at end of file diff --git a/framework/common/matrix.hpp b/framework/common/matrix.hpp index ea8de08..0cbe368 100644 --- a/framework/common/matrix.hpp +++ b/framework/common/matrix.hpp @@ -587,6 +587,19 @@ namespace xna { return Matrix::Divide(matrix, divider); } }; + + constexpr Vector2 Vector2::Transform(Vector2 const& position, Matrix const& matrix) { + const auto posx = (position.X * matrix.M11 + position.Y * matrix.M21) + matrix.M41; + const auto posy = (position.X * matrix.M12 + position.Y * matrix.M22) + matrix.M42; + + return{ posx, posy }; + } + + constexpr Vector2 Vector2::TransformNormal(Vector2 const& normal, Matrix const& matrix) { + const auto posx = normal.X * matrix.M11 + normal.Y * matrix.M21; + const auto posy = normal.X * matrix.M12 + normal.Y * matrix.M22; + return { posx, posy }; + } } #endif \ No newline at end of file diff --git a/framework/common/quaternion.hpp b/framework/common/quaternion.hpp index 4f32e18..f3c3506 100644 --- a/framework/common/quaternion.hpp +++ b/framework/common/quaternion.hpp @@ -1,6 +1,8 @@ #ifndef XNA_COMMON_QUATERNION_HPP #define XNA_COMMON_QUATERNION_HPP +#include "vectors.hpp" + namespace xna { struct Quaternion { float X{ 0 }; @@ -17,6 +19,22 @@ namespace xna { return X == other.X && Y == other.Y && Z == other.Z && W == other.W; } }; + + constexpr Vector2 Vector2::Transform(Vector2 const& value, Quaternion const& rotation) { + const auto rx = rotation.X + rotation.X; + const auto ry = rotation.Y + rotation.Y; + const auto rz = rotation.Z + rotation.Z; + const auto rwz = rotation.W * rz; + const auto rxx = rotation.X * rx; + const auto rxy = rotation.X * ry; + const auto ryy = rotation.Y * ry; + const auto rzz = rotation.Z * rz; + const auto x = value.X * (1.0F - ryy - rzz) + value.Y * (rxy - rwz); + const auto y = value.X * (rxy + rwz) + value.Y * (1.0F - rxx - rzz); + return{ x,y }; + } + + } #endif \ No newline at end of file diff --git a/framework/common/vectors.cpp b/framework/common/vectors.cpp new file mode 100644 index 0000000..d4a7272 --- /dev/null +++ b/framework/common/vectors.cpp @@ -0,0 +1,115 @@ +#include "vectors.hpp" +#include "matrix.hpp" +#include "quaternion.hpp" + +namespace xna { + bool Vector2::Transform(Vector2 const* sourceArray, size_t sourceArrayLength, Matrix const& matrix, Vector2* destinationArray, size_t destinationArrayLength) { + if (!sourceArray || !destinationArray || destinationArrayLength < sourceArrayLength) + return false; + + for (size_t index = 0; index < sourceArrayLength; ++index) + { + const auto& source = sourceArray[index]; + destinationArray[index].X = (source.X * matrix.M11 + source.Y * matrix.M21) + matrix.M41; + destinationArray[index].Y = (source.X * matrix.M12 + source.Y * matrix.M22) + matrix.M42; + } + + return true; + } + + bool Vector2::Transform(Vector2 const* sourceArray, size_t sourceArrayLength, size_t sourceIndex, Matrix const& matrix, + Vector2* destinationArray, size_t destinationArrayLength, size_t destinationIndex, size_t length) { + if (!sourceArray || !destinationArray || destinationArrayLength < sourceArrayLength + || sourceArrayLength < sourceIndex + length || destinationArrayLength < destinationIndex + length) + return false; + + for (size_t i = 0; i < length; ++i) { + const auto& source = sourceArray[sourceIndex + i]; + destinationArray[destinationIndex + i].X = (source.X * matrix.M11 + source.Y * matrix.M21) + matrix.M41; + destinationArray[destinationIndex + i].Y = (source.X * matrix.M12 + source.Y * matrix.M22) + matrix.M42; + } + + return true; + } + + bool Vector2::TransformNormal(Vector2 const* sourceArray, size_t sourceArrayLength, Matrix const& matrix, Vector2* destinationArray, size_t destinationArrayLength) { + if (!sourceArray || !destinationArray || destinationArrayLength < sourceArrayLength) + return false; + + for (size_t index = 0; index < sourceArrayLength; ++index) { + const auto& source = sourceArray[index]; + destinationArray[index].X = source.X * matrix.M11 + source.Y * matrix.M21; + destinationArray[index].Y = source.X * matrix.M12 + source.Y * matrix.M22; + } + + return true; + } + + bool Vector2::TransformNormal(Vector2 const* sourceArray, size_t sourceArrayLength, size_t sourceIndex, Matrix const& matrix, Vector2* destinationArray, size_t destinationArrayLength, size_t destinationIndex, size_t length) { + if (!sourceArray || !destinationArray || destinationArrayLength < sourceArrayLength + || sourceArrayLength < sourceIndex + length || destinationArrayLength < destinationIndex + length) + return false; + + for (size_t i = 0; i < length; ++i) + { + const auto& source = sourceArray[sourceIndex + i]; + destinationArray[destinationIndex].X = (source.X * matrix.M11 + source.Y * matrix.M21); + destinationArray[destinationIndex].Y = (source.X * matrix.M12 + source.Y * matrix.M22); + } + } + + bool Vector2::Transform(Vector2 const* sourceArray, size_t sourceArrayLength, Quaternion const& rotation, Vector2* destinationArray, size_t destinationArrayLength) { + if (!sourceArray || !destinationArray || destinationArrayLength < sourceArrayLength) + return false; + + const auto rx = rotation.X + rotation.X; + const auto ry = rotation.Y + rotation.Y; + const auto rz = rotation.Z + rotation.Z; + const auto rwz = rotation.W * rz; + const auto rxx = rotation.X * rx; + const auto rxy = rotation.X * ry; + const auto ryy = rotation.Y * ry; + const auto rzz = rotation.Z * rz; + const auto a = 1.0f - ryy - rzz; + const auto b = rxy - rwz; + const auto c = rxy + rwz; + const auto d = 1.0f - rxx - rzz; + + for (size_t index = 0; index < sourceArrayLength; ++index) { + const auto& source = sourceArray[index]; + destinationArray[index].X = source.X * a + source.Y * b; + destinationArray[index].Y = source.X * c + source.Y * d; + } + + return true; + } + + bool Vector2::Transform(Vector2 const* sourceArray, size_t sourceArrayLength, size_t sourceIndex, Quaternion const& rotation, + Vector2* destinationArray, size_t destinationArrayLength, size_t destinationIndex, size_t length) { + if (!sourceArray || !destinationArray || destinationArrayLength < sourceArrayLength + || sourceArrayLength < sourceIndex + length || destinationArrayLength < destinationIndex + length) + return false; + + const auto rx = rotation.X + rotation.X; + const auto ry = rotation.Y + rotation.Y; + const auto rz = rotation.Z + rotation.Z; + const auto rwz = rotation.W * rz; + const auto rxx = rotation.X * rx; + const auto rxy = rotation.X * ry; + const auto ryy = rotation.Y * ry; + const auto rzz = rotation.Z * rz; + const auto a = 1.0f - ryy - rzz; + const auto b = rxy - rwz; + const auto c = rxy + rwz; + const auto d = 1.0f - rxx - rzz; + + for (size_t i = 0; i < length; ++i) { + const auto& source = sourceArray[sourceIndex = i]; + + destinationArray[destinationIndex].X = source.X * a + source.Y* b; + destinationArray[destinationIndex].Y = source.X * c +source.Y * d; + ++sourceIndex; + ++destinationIndex; + } + } +} \ No newline at end of file diff --git a/framework/common/vectors.hpp b/framework/common/vectors.hpp index 3688745..cb02a77 100644 --- a/framework/common/vectors.hpp +++ b/framework/common/vectors.hpp @@ -1,12 +1,16 @@ #ifndef XNA_COMMON_VECTORS_HPP #define XNA_COMMON_VECTORS_HPP +#include +#include "../default.hpp" + namespace xna { struct Vector2 { float X{ 0 }; float Y{ 0 }; constexpr Vector2() = default; + constexpr Vector2(float value) : X(value), Y(value) {} constexpr Vector2(float X, float Y) : X(X), Y(Y) {} @@ -15,26 +19,194 @@ namespace xna { return X == other.X && Y == other.Y; } - static constexpr Vector2 One() { - return Vector2(1, 1); + static constexpr Vector2 Zero() { return {}; } + static constexpr Vector2 One() { return { 1 }; } + static constexpr Vector2 UnitX() { return { 1, 0 }; } + static constexpr Vector2 UnitY() { return { 0, 1 }; } + + inline float Length() const { + return sqrt(LengthSquared()); + } + + constexpr float LengthSquared() const { + return (X * X + Y * Y); + } + + static inline float Distance(Vector2 const& value1, Vector2 const& value2) { + return sqrt(DistanceSquared(value1, value2)); + } + + static constexpr float DistanceSquared(Vector2 const& value1, Vector2 const& value2) { + const auto x = value1.X - value2.X; + const auto y = value1.Y - value2.Y; + return x * x + y * y; + } + + static constexpr float Dot(Vector2 const& value1, Vector2 const& value2) { + return value1.X * value2.X + value1.Y * value2.Y; + } + + inline void Normalize() { + const auto normal = 1.0f / Length(); + X *= normal; + Y *= normal; + } + + static inline Vector2 Normalize(Vector2 const& value) { + const auto normal = 1.0F / value.Length(); + return { value.X * normal, value.Y * normal }; + } + + static constexpr Vector2 Reflect(Vector2 const& vector, Vector2 const& normal) { + const auto num = vector.X * normal.X + vector.Y * normal.Y; + return { vector.X - 2.0f * num * normal.X , vector.Y - 2.0f * num * normal.Y }; } static constexpr Vector2 Min(Vector2 const& value1, Vector2 const& value2) { - Vector2 vector2; - vector2.X = value1.X < value2.X ? value1.X : value2.X; - vector2.Y = value1.Y < value2.Y ? value1.Y : value2.Y; - return vector2; + return { + value1.X < value2.X ? value1.X : value2.X, + value1.Y < value2.Y ? value1.Y : value2.Y + }; } static constexpr Vector2 Max(Vector2 const& value1, Vector2 const& value2) { + return { + value1.X > value2.X ? value1.X : value2.X, + value1.Y > value2.Y ? value1.Y : value2.Y + }; + } + + static constexpr Vector2 Clamp(Vector2 const& value1, Vector2 const& min, Vector2 const& max) { + const auto _maxx = value1.X > max.X ? max.X : value1.X; + const auto _x = _maxx < min.X ? min.X : _maxx; + const auto _maxy = value1.Y > max.Y ? max.Y : value1.Y; + const auto _y = _maxy < min.Y ? min.Y : _maxy; + + return { _x, _y }; + } + + static constexpr Vector2 Lerp(Vector2 const& value1, Vector2 const& value2, float amount) { + return{ + value1.X + (value2.X - value1.X) * amount, + value1.Y + (value2.Y - value1.Y) * amount + }; + } + + static constexpr Vector2 Barycentric(Vector2 const& value1, Vector2 const& value2, Vector2 const& value3, float amount1, float amount2) { + return { + value1.X + amount1 * (value2.X - value1.X) + amount2 * (value3.X - value1.X), + value1.Y + amount1 * (value2.Y - value1.Y) + amount2 * (value3.Y - value1.Y) + }; + } + + static constexpr Vector2 SmoothStep(Vector2 const& value1, Vector2 const& value2, float amount) { + amount = amount > 1.0F ? 1.0f : (amount < 0.0F ? 0.0f : amount); + amount = amount * amount * (3.0F - 2.0F * amount); + + return { + value1.X + (value2.X - value1.X) * amount, + value1.Y + (value2.Y - value1.Y) * amount + }; + } + + static constexpr Vector2 CatmullRom(Vector2 const& value1, Vector2 const& value2, Vector2 const& value3, Vector2 const& value4, float amount) { + const auto am2 = amount * amount; + const auto am3 = amount * am2; Vector2 vector2; - vector2.X = value1.X > value2.X ? value1.X : value2.X; - vector2.Y = value1.Y > value2.Y ? value1.Y : value2.Y; + vector2.X = 0.5F * (2.0F * value2.X + (-value1.X + value3.X) * amount + (2.0F * value1.X - 5.0F * value2.X + 4.0F * value3.X - value4.X) * am2 + (-value1.X + 3.0F * value2.X - 3.0F * value3.X + value4.X) * am3); + vector2.Y = 0.5F * (2.0F * value2.Y + (-value1.Y + value3.Y) * amount + (2.0F * value1.Y - 5.0F * value2.Y + 4.0F * value3.Y - value4.Y) * am2 + (-value1.Y + 3.0F * value2.Y - 3.0F * value3.Y + value4.Y) * am3); return vector2; } + static constexpr Vector2 Hermite(Vector2 const& value1, Vector2 const& tangent1, Vector2 const& value2, Vector2 const& tangent2, float amount) { + const auto am2 = amount * amount; + const auto am3 = amount * am2; + const auto num3 = (2.0F * am3 - 3.0F * am2 + 1.0F); + const auto num4 = (-2.0F * am3 + 3.0F * am2); + const auto num5 = am3 - 2.0F * am2 + amount; + const auto am4 = am3 - am2; + + return{ + value1.X * num3 + value2.X * num4 + tangent1.X * num5 + tangent2.X * am4, + value1.Y * num3 + value2.Y * num4 + tangent1.Y * num5 + tangent2.Y * am4 + }; + } + + static constexpr Vector2 Transform(Vector2 const& position, Matrix const& matrix); + static constexpr Vector2 TransformNormal(Vector2 const& normal, Matrix const& matrix); + static constexpr Vector2 Transform(Vector2 const& value, Quaternion const& rotation); + static bool Transform(Vector2 const* sourceArray, size_t sourceArrayLength, Matrix const& matrix, Vector2* destinationArray, size_t destinationArrayLength); + static bool Transform(Vector2 const* sourceArray, size_t sourceArrayLength, size_t sourceIndex, Matrix const& matrix, + Vector2* destinationArray, size_t destinationArrayLength, size_t destinationIndex, size_t length); + static bool TransformNormal(Vector2 const* sourceArray, size_t sourceArrayLength, Matrix const& matrix, Vector2* destinationArray, size_t destinationArrayLength); + static bool TransformNormal(Vector2 const* sourceArray, size_t sourceArrayLength, size_t sourceIndex, Matrix const& matrix, + Vector2* destinationArray, size_t destinationArrayLength, size_t destinationIndex, size_t length); + static bool Transform(Vector2 const* sourceArray, size_t sourceArrayLength, Quaternion const& rotation, Vector2* destinationArray, size_t destinationArrayLength); + static bool Transform(Vector2 const* sourceArray, size_t sourceArrayLength, size_t sourceIndex, Quaternion const& rotation, + Vector2* destinationArray, size_t destinationArrayLength, size_t destinationIndex, size_t length); + + static constexpr Vector2 Negate(Vector2 const& value) { + return { -value.X, -value.Y }; + } + + static constexpr Vector2 Add(Vector2 const& value1, Vector2 const& value2) { + return { value1.X + value2.X, value1.Y + value2.Y }; + } + + static constexpr Vector2 Subtract(Vector2 const& value1, Vector2 const& value2) { + return { value1.X - value2.X, value1.Y - value2.Y }; + } + + static constexpr Vector2 Multiply(Vector2 const& value1, Vector2 const& value2) { + return { value1.X * value2.X, value1.Y * value2.Y }; + } + + static constexpr Vector2 Multiply(Vector2 const& value1, float scaleFactor) { + return { value1.X * scaleFactor, value1.Y * scaleFactor }; + } + + static constexpr Vector2 Divide(Vector2 const& value1, Vector2 const& value2) { + return { value1.X / value2.X, value1.Y / value2.Y }; + } + + static constexpr Vector2 Divide(Vector2 const& value1, float divider) { + return { value1.X / divider, value1.Y / divider }; + } + constexpr Vector2 operator-() const { - return Vector2(-X, -Y); + return Negate(*this); + } + + friend constexpr Vector2 operator+(Vector2 const& value1, Vector2 const& value2) { + return Vector2::Add(value1, value2); + } + + friend constexpr Vector2 operator-(Vector2 const& value1, Vector2 const& value2) { + return Vector2::Subtract(value1, value2); + } + + friend constexpr Vector2 operator*(Vector2 const& value1, Vector2 const& value2) { + return Vector2::Multiply(value1, value2); + } + + friend constexpr Vector2 operator/(Vector2 const& value1, Vector2 const& value2) { + return Vector2::Divide(value1, value2); + } + + friend constexpr Vector2 operator*(Vector2 const& value1, float factor) { + return Vector2::Multiply(value1, factor); + } + + friend constexpr Vector2 operator/(Vector2 const& value1, float divider) { + return Vector2::Divide(value1, divider); + } + + friend constexpr Vector2 operator*(float factor, Vector2 const& value1) { + return Vector2::Multiply(value1, factor); + } + + friend constexpr Vector2 operator/(float divider, Vector2 const& value1) { + return Vector2::Divide(value1, divider); } }; @@ -61,7 +233,7 @@ namespace xna { static Vector3 Multiply(Vector3 value1, Vector3 value2) { return {}; } static Vector3 Subtract(Vector3 value1, Vector3 value2) { return {}; } static Vector3 Multiply(Vector3 value1, float value) { return {}; } - void Normalize(){} + void Normalize() {} constexpr Vector3 operator-() const { return Vector3(); diff --git a/framework/forward.hpp b/framework/forward.hpp index 2d39bb9..b6ec522 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -37,24 +37,19 @@ namespace xna { using PPlane = std::shared_ptr; struct Point; using PPoint = std::shared_ptr; - class Quaternion; + struct Quaternion; using PQuaternion = std::shared_ptr; class Ray; using PRay = std::shared_ptr; struct Rectangle; using PRectangle = std::shared_ptr; - struct Vector2; - using Vec2 = Vector2; - using PVector2 = std::shared_ptr; - using PVec2 = std::shared_ptr; - struct Vector3; - using Vec3 = Vector3; - using PVector3 = std::shared_ptr; - using PVec3 = std::shared_ptr; - struct Vector4; - using Vec4 = Vector4; + struct Vector2; + using PVector2 = std::shared_ptr; + struct Vector3; + using PVector3 = std::shared_ptr; + struct Vector4; using PVector4 = std::shared_ptr; - using PVec4 = std::shared_ptr; + //Game class DrawableGameComponent;