From 09820d4e11e76a480942643a336429467812e700 Mon Sep 17 00:00:00 2001 From: TurtleP Date: Sat, 18 Nov 2023 10:45:31 -0500 Subject: [PATCH] move matrix back into hpp/cpp, add filesystem.exists --- CMakeLists.txt | 1 + include/common/drawable.hpp | 2 +- include/common/matrix.hpp | 192 +++++++ include/common/matrix.tcc | 517 ------------------ include/modules/filesystem/filesystem.hpp | 4 +- .../modules/filesystem/physfs/filesystem.hpp | 2 + .../modules/filesystem/wrap_filesystem.hpp | 2 + include/modules/graphics/graphics.tcc | 2 +- include/modules/graphics/wrap_graphics.hpp | 2 +- include/objects/spritebatch/spritebatch.hpp | 2 +- include/objects/texture/texture.tcc | 2 +- include/objects/transform/transform.hpp | 2 +- platform/cafe/include/common/matrix_ext.hpp | 2 +- .../include/utilities/driver/renderer_ext.hpp | 2 +- platform/cafe/source/modules/graphics_ext.cpp | 2 +- platform/hac/include/common/matrix_ext.hpp | 2 +- source/common/matrix.cpp | 408 ++++++++++++++ .../modules/filesystem/physfs/filesystem.cpp | 8 + source/modules/filesystem/wrap_filesystem.cpp | 10 + source/objects/textbatch/textbatch.cpp | 2 +- 20 files changed, 637 insertions(+), 529 deletions(-) create mode 100644 include/common/matrix.hpp delete mode 100644 include/common/matrix.tcc create mode 100644 source/common/matrix.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 32b181df7..59cd028ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -368,6 +368,7 @@ target_sources(${PROJECT_NAME} PRIVATE source/common/data.cpp source/common/exception.cpp source/common/luax.cpp + source/common/matrix.cpp source/common/module.cpp source/common/object.cpp source/common/pixelformat.cpp diff --git a/include/common/drawable.hpp b/include/common/drawable.hpp index aa4828f03..74d07b9b0 100644 --- a/include/common/drawable.hpp +++ b/include/common/drawable.hpp @@ -1,6 +1,6 @@ #pragma once -#include "matrix.tcc" +#include "matrix.hpp" #include "object.hpp" namespace love diff --git a/include/common/matrix.hpp b/include/common/matrix.hpp new file mode 100644 index 000000000..fd0990a1d --- /dev/null +++ b/include/common/matrix.hpp @@ -0,0 +1,192 @@ +#pragma once + +#include "console.hpp" +#include "math.hpp" +#include "vector.hpp" + +#include "utilities/driver/renderer/vertex.hpp" + +#include +#include +#include + +#include + +namespace love +{ + template + concept ValidTransformRange = + std::ranges::random_access_range && + (std::is_same_v>, + std::remove_cvref_t> || + (std::is_same_v, Vector3> && + std::is_same_v>, vertex::Vertex>)); + + template + concept Vector3TransformRange = ValidTransformRange; + + template + concept Vector2TransformRange = ValidTransformRange; + + template + concept VectorAtLeast2TransformRange = + ValidTransformRange || ValidTransformRange; + + class Matrix4 + { + protected: + template + static auto DeVertexize(R&& r) -> decltype(auto) + { + if constexpr (std::is_same_v>, + vertex::Vertex>) + { + return r | std::views::transform([](auto&& e) -> auto& { return e.position; }); + } + else + { + return std::forward(r); + } + } + + static void Multiply(const Matrix4& a, const Matrix4& b, float t[0x10]); + + public: + static void Multiply(const Matrix4& a, const Matrix4& b, Matrix4& result); + + Matrix4(); + + Matrix4(float t00, float t10, float t01, float t11, float x, float y); + + Matrix4(const float elements[0x10]); + + Matrix4(const Matrix4& a, const Matrix4& b); + + Matrix4(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, + float ky); + + Matrix4 operator*(const Matrix4& other) const; + + void operator*=(const Matrix4& other); + + const float* GetElements() const + { + return this->matrix; + } + + void SetRow(int row, const Vector4& vector); + + Vector4 GetRow(int row); + + void SetColumn(int column, const Vector4& vector); + + Vector4 GetColumn(int column); + + void SetIdentity(); + + void SetTranslation(float x, float y); + + void SetRotation(float rotation); + + void SetScale(float sx, float sy); + + void SetShear(float kx, float ky); + + void GetApproximateScale(float& sx, float& sy) const; + + void SetRawTransformation(float t00, float t10, float t01, float t11, float x, float y); + + void SetTransformation(float x, float y, float angle, float sx, float sy, float ox, + float oy, float kx, float ky); + + void Translate(float x, float y); + + void Rotate(float rotation); + + void Scale(float sx, float sy); + + void Shear(float kx, float ky); + + bool IsAffine2DTransform() const; + + bool IsAffine3DTransform() const; + + template + void TransformXY0(Vdst&&, const Vsrc&&, int size) const; + + template + /* transform Vector2 src into Vector2 dst */ + void TransformXY(Vdst&& dst, Vsrc&& src) const + { + if constexpr (std::is_same_v>, + vertex::Vertex> || + std::is_same_v>, + vertex::Vertex>) + { + TransformXY(DeVertexize(dst), DeVertexize(src)); + return; + } + else + { + // This might need to be an assert; jury's out + // static_assert(std::ranges::size(dst) == std::ranges::size(src)); + + for (size_t i = 0; i < std::ranges::size(dst); i++) + { + float x = (this->matrix[0] * src[i].x) + (this->matrix[4] * src[i].y) + (0) + + (this->matrix[12]); + + float y = (this->matrix[1] * src[i].x) + (this->matrix[5] * src[i].y) + (0) + + (this->matrix[13]); + + dst[i].x = x; + dst[i].y = y; + } + } + } + + template + /* transform Vector3 src into Vector3 dst */ + void TransformXYZ(Vdst&& dst, Vsrc&& src) const + { + if constexpr (std::is_same_v>, + vertex::Vertex> || + std::is_same_v>, + vertex::Vertex>) + { + TransformXYZ(DeVertexize(dst), DeVertexize(src)); + return; + } + else + { + // static_assert(std::ranges::size(dst) == std::ranges::size(src)); + + for (size_t i = 0; i < std::ranges::size(dst); i++) + { + float x = (this->matrix[0] * src[i].x) + (this->matrix[4] * src[i].y) + + (this->matrix[8] * src[i].z) + (this->matrix[12]); + + float y = (this->matrix[1] * src[i].x) + (this->matrix[5] * src[i].y) + + (this->matrix[9] * src[i].z) + (this->matrix[13]); + + float z = (this->matrix[2] * src[i].x) + (this->matrix[6] * src[i].y) + + (this->matrix[10] * src[i].z) + (this->matrix[14]); + + dst[i].x = x; + dst[i].y = y; + dst[i].z = z; + } + } + } + + Matrix4 Inverse() const; + + static Matrix4 Ortho(float left, float right, float bottom, float top, float near, + float far); + + static Matrix4 Perspective(float verticalfov, float aspect, float near, float far); + + protected: + float matrix[0x10]; + }; +} // namespace love diff --git a/include/common/matrix.tcc b/include/common/matrix.tcc deleted file mode 100644 index 8a9548c56..000000000 --- a/include/common/matrix.tcc +++ /dev/null @@ -1,517 +0,0 @@ -#pragma once - -#include "console.hpp" -#include "math.hpp" -#include "utilities/driver/renderer/vertex.hpp" -#include "vector.hpp" - -#include -#include -#include - -using Elements = float[0x10]; - -#if defined(__SWITCH__) - #include -#endif - -#include - -namespace love -{ - template - concept ValidTransformRange = - std::ranges::random_access_range && - (std::is_same_v>, - std::remove_cvref_t> || - (std::is_same_v, Vector3> && - std::is_same_v>, vertex::Vertex>)); - - template - concept Vector3TransformRange = ValidTransformRange; - template - concept Vector2TransformRange = ValidTransformRange; - template - concept VectorAtLeast2TransformRange = - ValidTransformRange || ValidTransformRange; - - class Matrix4 - { - protected: - template - static auto DeVertexize(R&& r) -> decltype(auto) - { - if constexpr (std::is_same_v>, - vertex::Vertex>) - { - return r | std::views::transform([](auto&& e) -> auto& { return e.position; }); - } - else - { - return std::forward(r); - } - } - static void Multiply(const Matrix4& a, const Matrix4& b, Elements t) - { -#if defined(__SWITCH__) - float32x4_t cola1 = vld1q_f32(&a.matrix[0]); - float32x4_t cola2 = vld1q_f32(&a.matrix[4]); - float32x4_t cola3 = vld1q_f32(&a.matrix[8]); - float32x4_t cola4 = vld1q_f32(&a.matrix[12]); - - float32x4_t col1 = vmulq_n_f32(cola1, b.matrix[0]); - col1 = vmlaq_n_f32(col1, cola2, b.matrix[1]); - col1 = vmlaq_n_f32(col1, cola3, b.matrix[2]); - col1 = vmlaq_n_f32(col1, cola4, b.matrix[3]); - - float32x4_t col2 = vmulq_n_f32(cola1, b.matrix[4]); - col2 = vmlaq_n_f32(col2, cola2, b.matrix[5]); - col2 = vmlaq_n_f32(col2, cola3, b.matrix[6]); - col2 = vmlaq_n_f32(col2, cola4, b.matrix[7]); - - float32x4_t col3 = vmulq_n_f32(cola1, b.matrix[8]); - col3 = vmlaq_n_f32(col3, cola2, b.matrix[9]); - col3 = vmlaq_n_f32(col3, cola3, b.matrix[10]); - col3 = vmlaq_n_f32(col3, cola4, b.matrix[11]); - - float32x4_t col4 = vmulq_n_f32(cola1, b.matrix[12]); - col4 = vmlaq_n_f32(col4, cola2, b.matrix[13]); - col4 = vmlaq_n_f32(col4, cola3, b.matrix[14]); - col4 = vmlaq_n_f32(col4, cola4, b.matrix[15]); - - vst1q_f32(&t[0], col1); - vst1q_f32(&t[4], col2); - vst1q_f32(&t[8], col3); - vst1q_f32(&t[12], col4); -#else - t[0] = (a.matrix[0] * b.matrix[0]) + (a.matrix[4] * b.matrix[1]) + - (a.matrix[8] * b.matrix[2]) + (a.matrix[12] * b.matrix[3]); - t[4] = (a.matrix[0] * b.matrix[4]) + (a.matrix[4] * b.matrix[5]) + - (a.matrix[8] * b.matrix[6]) + (a.matrix[12] * b.matrix[7]); - t[8] = (a.matrix[0] * b.matrix[8]) + (a.matrix[4] * b.matrix[9]) + - (a.matrix[8] * b.matrix[10]) + (a.matrix[12] * b.matrix[11]); - t[12] = (a.matrix[0] * b.matrix[12]) + (a.matrix[4] * b.matrix[13]) + - (a.matrix[8] * b.matrix[14]) + (a.matrix[12] * b.matrix[15]); - - t[1] = (a.matrix[1] * b.matrix[0]) + (a.matrix[5] * b.matrix[1]) + - (a.matrix[9] * b.matrix[2]) + (a.matrix[13] * b.matrix[3]); - t[5] = (a.matrix[1] * b.matrix[4]) + (a.matrix[5] * b.matrix[5]) + - (a.matrix[9] * b.matrix[6]) + (a.matrix[13] * b.matrix[7]); - t[9] = (a.matrix[1] * b.matrix[8]) + (a.matrix[5] * b.matrix[9]) + - (a.matrix[9] * b.matrix[10]) + (a.matrix[13] * b.matrix[11]); - t[13] = (a.matrix[1] * b.matrix[12]) + (a.matrix[5] * b.matrix[13]) + - (a.matrix[9] * b.matrix[14]) + (a.matrix[13] * b.matrix[15]); - - t[2] = (a.matrix[2] * b.matrix[0]) + (a.matrix[6] * b.matrix[1]) + - (a.matrix[10] * b.matrix[2]) + (a.matrix[14] * b.matrix[3]); - t[6] = (a.matrix[2] * b.matrix[4]) + (a.matrix[6] * b.matrix[5]) + - (a.matrix[10] * b.matrix[6]) + (a.matrix[14] * b.matrix[7]); - t[10] = (a.matrix[2] * b.matrix[8]) + (a.matrix[6] * b.matrix[9]) + - (a.matrix[10] * b.matrix[10]) + (a.matrix[14] * b.matrix[11]); - t[14] = (a.matrix[2] * b.matrix[12]) + (a.matrix[6] * b.matrix[13]) + - (a.matrix[10] * b.matrix[14]) + (a.matrix[14] * b.matrix[15]); - - t[3] = (a.matrix[3] * b.matrix[0]) + (a.matrix[7] * b.matrix[1]) + - (a.matrix[11] * b.matrix[2]) + (a.matrix[15] * b.matrix[3]); - t[7] = (a.matrix[3] * b.matrix[4]) + (a.matrix[7] * b.matrix[5]) + - (a.matrix[11] * b.matrix[6]) + (a.matrix[15] * b.matrix[7]); - t[11] = (a.matrix[3] * b.matrix[8]) + (a.matrix[7] * b.matrix[9]) + - (a.matrix[11] * b.matrix[10]) + (a.matrix[15] * b.matrix[11]); - t[15] = (a.matrix[3] * b.matrix[12]) + (a.matrix[7] * b.matrix[13]) + - (a.matrix[11] * b.matrix[14]) + (a.matrix[15] * b.matrix[15]); -#endif - } - - public: - static void Multiply(const Matrix4& a, const Matrix4& b, Matrix4& result) - { - Multiply(a, b, result.matrix); - } - - Matrix4() - { - this->SetIdentity(); - } - - Matrix4(float t00, float t10, float t01, float t11, float x, float y) - { - this->SetRawTransformation(t00, t10, t01, t11, x, y); - } - - Matrix4(const Elements elements) - { - std::copy_n(elements, 16, this->matrix); - } - - Matrix4(const Matrix4& a, const Matrix4& b) - { - this->Multiply(a, b, this->matrix); - } - - Matrix4(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, - float ky) - { - this->SetTransformation(x, y, angle, sx, sy, ox, oy, kx, ky); - } - - Matrix4 operator*(const Matrix4& other) const - { - return Matrix4(*this, other); - } - - void operator*=(const Matrix4& other) - { - Elements elements; - this->Multiply(*this, other, elements); - std::copy_n(elements, 16, this->matrix); - } - - const Elements& GetElements() const - { - return this->matrix; - } - - void SetRow(int row, const Vector4& vector) - { - this->matrix[0 * 4 + row] = vector.x; - this->matrix[1 * 4 + row] = vector.y; - this->matrix[2 * 4 + row] = vector.z; - this->matrix[3 * 4 + row] = vector.w; - } - - Vector4 GetRow(int row) - { - return Vector4(this->matrix[0 * 4 + row], this->matrix[1 * 4 + row], - this->matrix[2 * 4 + row], this->matrix[3 * 4 + row]); - } - - void SetColumn(int column, const Vector4& vector) - { - this->matrix[column * 4 + 0] = vector.x; - this->matrix[column * 4 + 1] = vector.y; - this->matrix[column * 4 + 2] = vector.z; - this->matrix[column * 4 + 3] = vector.w; - } - - Vector4 GetColumn(int column) - { - return Vector4(this->matrix[column * 4 + 0], this->matrix[column * 4 + 1], - this->matrix[column * 4 + 2], this->matrix[column * 4 + 3]); - } - - void SetIdentity() - { - std::fill_n(this->matrix, 16, 0.0f); - this->matrix[15] = this->matrix[10] = this->matrix[5] = this->matrix[0] = 1; - } - - void SetTranslation(float x, float y) - { - this->SetIdentity(); - this->matrix[12] = x; - this->matrix[13] = y; - } - - void SetRotation(float rotation) - { - this->SetIdentity(); - - float cos = std::cos(rotation); - float sin = std::sin(rotation); - - this->matrix[0] = cos; - this->matrix[4] = -sin; - this->matrix[1] = sin; - this->matrix[5] = cos; - } - - void SetScale(float sx, float sy) - { - this->SetIdentity(); - - this->matrix[0] = sx; - this->matrix[5] = sy; - } - - void SetShear(float kx, float ky) - { - this->SetIdentity(); - - this->matrix[1] = ky; - this->matrix[4] = kx; - } - - void GetApproximateScale(float& sx, float& sy) const - { - sx = std::sqrt(this->matrix[0] * this->matrix[0] + this->matrix[4] * this->matrix[4]); - sy = std::sqrt(this->matrix[1] * this->matrix[1] + this->matrix[5] * this->matrix[5]); - } - - void SetRawTransformation(float t00, float t10, float t01, float t11, float x, float y) - { - std::fill_n(this->matrix, 16, 0.0f); - - this->matrix[10] = this->matrix[15] = 1.0f; - this->matrix[0] = t00; - this->matrix[1] = t10; - this->matrix[4] = t01; - this->matrix[5] = t11; - this->matrix[12] = x; - this->matrix[13] = y; - } - - void SetTransformation(float x, float y, float angle, float sx, float sy, float ox, - float oy, float kx, float ky) - { - std::fill_n(this->matrix, 16, 0.0f); - - float cos = std::cos(angle); - float sin = std::sin(angle); - - this->matrix[10] = this->matrix[15] = 1.0f; - this->matrix[0] = cos * sx - ky * sin * sy; // = a - this->matrix[1] = sin * sx + ky * cos * sy; // = b - this->matrix[4] = kx * cos * sx - sin * sy; // = c - this->matrix[5] = kx * sin * sx + cos * sy; // = d - this->matrix[12] = x - ox * this->matrix[0] - oy * this->matrix[4]; - this->matrix[13] = y - ox * this->matrix[1] - oy * this->matrix[5]; - } - - void Translate(float x, float y) - { - Matrix4 t {}; - t.SetTranslation(x, y); - this->operator*=(t); - } - - void Rotate(float rotation) - { - Matrix4 t {}; - t.SetRotation(rotation); - this->operator*=(t); - } - - void Scale(float sx, float sy) - { - Matrix4 t {}; - t.SetScale(sx, sy); - this->operator*=(t); - } - - void Shear(float kx, float ky) - { - Matrix4 t {}; - t.SetShear(kx, ky); - this->operator*=(t); - } - - bool IsAffine2DTransform() const - { - return fabsf(this->matrix[2] + this->matrix[3] + this->matrix[6] + this->matrix[7] + - this->matrix[8] + this->matrix[9] + this->matrix[11] + this->matrix[14]) < - 0.00001f && - fabsf(this->matrix[10] + this->matrix[15] - 2.0f) < 0.00001f; - } - - bool IsAffine3DTransform() const; - - template - void TransformXY0(Vdst&&, const Vsrc&&, int size) const; - - template - /* transform Vector2 src into Vector2 dst */ - void TransformXY(Vdst&& dst, Vsrc&& src) const - { - if constexpr (std::is_same_v>, - vertex::Vertex> || - std::is_same_v>, - vertex::Vertex>) - { - TransformXY(DeVertexize(dst), DeVertexize(src)); - return; - } - else - { - // This might need to be an assert; jury's out - // static_assert(std::ranges::size(dst) == std::ranges::size(src)); - - for (size_t i = 0; i < std::ranges::size(dst); i++) - { - float x = (this->matrix[0] * src[i].x) + (this->matrix[4] * src[i].y) + (0) + - (this->matrix[12]); - - float y = (this->matrix[1] * src[i].x) + (this->matrix[5] * src[i].y) + (0) + - (this->matrix[13]); - - dst[i].x = x; - dst[i].y = y; - } - } - } - - template - /* transform Vector3 src into Vector3 dst */ - void TransformXYZ(Vdst&& dst, Vsrc&& src) const - { - if constexpr (std::is_same_v>, - vertex::Vertex> || - std::is_same_v>, - vertex::Vertex>) - { - TransformXYZ(DeVertexize(dst), DeVertexize(src)); - return; - } - else - { - // static_assert(std::ranges::size(dst) == std::ranges::size(src)); - - for (size_t i = 0; i < std::ranges::size(dst); i++) - { - float x = (this->matrix[0] * src[i].x) + (this->matrix[4] * src[i].y) + - (this->matrix[8] * src[i].z) + (this->matrix[12]); - - float y = (this->matrix[1] * src[i].x) + (this->matrix[5] * src[i].y) + - (this->matrix[9] * src[i].z) + (this->matrix[13]); - - float z = (this->matrix[2] * src[i].x) + (this->matrix[6] * src[i].y) + - (this->matrix[10] * src[i].z) + (this->matrix[14]); - - dst[i].x = x; - dst[i].y = y; - dst[i].z = z; - } - } - } - - Matrix4 Inverse() const - { - Matrix4 inv; - - inv.matrix[0] = - matrix[5] * matrix[10] * matrix[15] - matrix[5] * matrix[11] * matrix[14] - - matrix[9] * matrix[6] * matrix[15] + matrix[9] * matrix[7] * matrix[14] + - matrix[13] * matrix[6] * matrix[11] - matrix[13] * matrix[7] * matrix[10]; - - inv.matrix[4] = - -matrix[4] * matrix[10] * matrix[15] + matrix[4] * matrix[11] * matrix[14] + - matrix[8] * matrix[6] * matrix[15] - matrix[8] * matrix[7] * matrix[14] - - matrix[12] * matrix[6] * matrix[11] + matrix[12] * matrix[7] * matrix[10]; - - inv.matrix[8] = - matrix[4] * matrix[9] * matrix[15] - matrix[4] * matrix[11] * matrix[13] - - matrix[8] * matrix[5] * matrix[15] + matrix[8] * matrix[7] * matrix[13] + - matrix[12] * matrix[5] * matrix[11] - matrix[12] * matrix[7] * matrix[9]; - - inv.matrix[12] = - -matrix[4] * matrix[9] * matrix[14] + matrix[4] * matrix[10] * matrix[13] + - matrix[8] * matrix[5] * matrix[14] - matrix[8] * matrix[6] * matrix[13] - - matrix[12] * matrix[5] * matrix[10] + matrix[12] * matrix[6] * matrix[9]; - - inv.matrix[1] = - -matrix[1] * matrix[10] * matrix[15] + matrix[1] * matrix[11] * matrix[14] + - matrix[9] * matrix[2] * matrix[15] - matrix[9] * matrix[3] * matrix[14] - - matrix[13] * matrix[2] * matrix[11] + matrix[13] * matrix[3] * matrix[10]; - - inv.matrix[5] = - matrix[0] * matrix[10] * matrix[15] - matrix[0] * matrix[11] * matrix[14] - - matrix[8] * matrix[2] * matrix[15] + matrix[8] * matrix[3] * matrix[14] + - matrix[12] * matrix[2] * matrix[11] - matrix[12] * matrix[3] * matrix[10]; - - inv.matrix[9] = - -matrix[0] * matrix[9] * matrix[15] + matrix[0] * matrix[11] * matrix[13] + - matrix[8] * matrix[1] * matrix[15] - matrix[8] * matrix[3] * matrix[13] - - matrix[12] * matrix[1] * matrix[11] + matrix[12] * matrix[3] * matrix[9]; - - inv.matrix[13] = - matrix[0] * matrix[9] * matrix[14] - matrix[0] * matrix[10] * matrix[13] - - matrix[8] * matrix[1] * matrix[14] + matrix[8] * matrix[2] * matrix[13] + - matrix[12] * matrix[1] * matrix[10] - matrix[12] * matrix[2] * matrix[9]; - - inv.matrix[2] = - matrix[1] * matrix[6] * matrix[15] - matrix[1] * matrix[7] * matrix[14] - - matrix[5] * matrix[2] * matrix[15] + matrix[5] * matrix[3] * matrix[14] + - matrix[13] * matrix[2] * matrix[7] - matrix[13] * matrix[3] * matrix[6]; - - inv.matrix[6] = - -matrix[0] * matrix[6] * matrix[15] + matrix[0] * matrix[7] * matrix[14] + - matrix[4] * matrix[2] * matrix[15] - matrix[4] * matrix[3] * matrix[14] - - matrix[12] * matrix[2] * matrix[7] + matrix[12] * matrix[3] * matrix[6]; - - inv.matrix[10] = - matrix[0] * matrix[5] * matrix[15] - matrix[0] * matrix[7] * matrix[13] - - matrix[4] * matrix[1] * matrix[15] + matrix[4] * matrix[3] * matrix[13] + - matrix[12] * matrix[1] * matrix[7] - matrix[12] * matrix[3] * matrix[5]; - - inv.matrix[14] = - -matrix[0] * matrix[5] * matrix[14] + matrix[0] * matrix[6] * matrix[13] + - matrix[4] * matrix[1] * matrix[14] - matrix[4] * matrix[2] * matrix[13] - - matrix[12] * matrix[1] * matrix[6] + matrix[12] * matrix[2] * matrix[5]; - - inv.matrix[3] = - -matrix[1] * matrix[6] * matrix[11] + matrix[1] * matrix[7] * matrix[10] + - matrix[5] * matrix[2] * matrix[11] - matrix[5] * matrix[3] * matrix[10] - - matrix[9] * matrix[2] * matrix[7] + matrix[9] * matrix[3] * matrix[6]; - - inv.matrix[7] = - matrix[0] * matrix[6] * matrix[11] - matrix[0] * matrix[7] * matrix[10] - - matrix[4] * matrix[2] * matrix[11] + matrix[4] * matrix[3] * matrix[10] + - matrix[8] * matrix[2] * matrix[7] - matrix[8] * matrix[3] * matrix[6]; - - inv.matrix[11] = - -matrix[0] * matrix[5] * matrix[11] + matrix[0] * matrix[7] * matrix[9] + - matrix[4] * matrix[1] * matrix[11] - matrix[4] * matrix[3] * matrix[9] - - matrix[8] * matrix[1] * matrix[7] + matrix[8] * matrix[3] * matrix[5]; - - inv.matrix[15] = - matrix[0] * matrix[5] * matrix[10] - matrix[0] * matrix[6] * matrix[9] - - matrix[4] * matrix[1] * matrix[10] + matrix[4] * matrix[2] * matrix[9] + - matrix[8] * matrix[1] * matrix[6] - matrix[8] * matrix[2] * matrix[5]; - - float det = matrix[0] * inv.matrix[0] + matrix[1] * inv.matrix[4] + - matrix[2] * inv.matrix[8] + matrix[3] * inv.matrix[12]; - - float invdet = 1.0f / det; - - for (int i = 0; i < 16; i++) - inv.matrix[i] *= invdet; - - return inv; - } - - static Matrix4 Ortho(float left, float right, float bottom, float top, float near, - float far) - { - Matrix4 t {}; - - t.matrix[0] = 2.0f / (right - left); - t.matrix[5] = 2.0f / (top - bottom); - t.matrix[10] = -2.0f / (far - near); - - t.matrix[12] = -(right + left) / (right - left); - t.matrix[13] = -(top + bottom) / (top - bottom); - t.matrix[14] = -(far + near) / (far - near); - - return t; - } - - static Matrix4 Perspective(float verticalfov, float aspect, float near, float far) - { - Matrix4 t {}; - - float cotangent = 1.0f / tanf(verticalfov * 0.5f); - - t.matrix[0] = cotangent / aspect; - t.matrix[5] = cotangent; - t.matrix[10] = (far + near) / (near - far); - t.matrix[11] = -1.0f; - - t.matrix[14] = 2.0f * near * far / (near - far); - t.matrix[15] = 0.0f; - - return t; - } - - protected: - Elements matrix; - }; -} // namespace love diff --git a/include/modules/filesystem/filesystem.hpp b/include/modules/filesystem/filesystem.hpp index 1cbaace03..55998c829 100644 --- a/include/modules/filesystem/filesystem.hpp +++ b/include/modules/filesystem/filesystem.hpp @@ -113,6 +113,8 @@ namespace love virtual std::string GetRealDirectory(const char* filename) const = 0; + virtual bool Exists(const char* filename) const = 0; + virtual bool GetInfo(const char* filename, Info& info) const = 0; virtual bool CreateDirectory(const char* name) = 0; @@ -161,7 +163,7 @@ namespace love "userhome", Filesystem::USER_HOME, "userdocuments", Filesystem::USER_DOCUMENTS }; - + static constexpr BidirectionalMap mountPermissions = { "read", Filesystem::MOUNT_READ, "readwrite", Filesystem::MOUNT_READWRITE diff --git a/include/modules/filesystem/physfs/filesystem.hpp b/include/modules/filesystem/physfs/filesystem.hpp index 580ac1646..75f4c908c 100644 --- a/include/modules/filesystem/physfs/filesystem.hpp +++ b/include/modules/filesystem/physfs/filesystem.hpp @@ -72,6 +72,8 @@ namespace love::physfs std::string GetRealDirectory(const char* filename) const override; + bool Exists(const char* filename) const override; + bool GetInfo(const char* filepath, Info& info) const override; bool CreateDirectory(const char* directory) override; diff --git a/include/modules/filesystem/wrap_filesystem.hpp b/include/modules/filesystem/wrap_filesystem.hpp index 9a66f4259..7d2430ba1 100644 --- a/include/modules/filesystem/wrap_filesystem.hpp +++ b/include/modules/filesystem/wrap_filesystem.hpp @@ -33,6 +33,8 @@ namespace Wrap_Filesystem int Load(lua_State* L); + int Exists(lua_State* L); + int GetInfo(lua_State* L); int GetSaveDirectory(lua_State* L); diff --git a/include/modules/graphics/graphics.tcc b/include/modules/graphics/graphics.tcc index 467bc39d8..2fd1f722b 100644 --- a/include/modules/graphics/graphics.tcc +++ b/include/modules/graphics/graphics.tcc @@ -7,7 +7,7 @@ #include #include -#include +#include #include #include diff --git a/include/modules/graphics/wrap_graphics.hpp b/include/modules/graphics/wrap_graphics.hpp index 630b8735e..42d21d660 100644 --- a/include/modules/graphics/wrap_graphics.hpp +++ b/include/modules/graphics/wrap_graphics.hpp @@ -2,7 +2,7 @@ #include -#include +#include #include namespace Wrap_Graphics diff --git a/include/objects/spritebatch/spritebatch.hpp b/include/objects/spritebatch/spritebatch.hpp index e6f97bead..ff4a98bbb 100644 --- a/include/objects/spritebatch/spritebatch.hpp +++ b/include/objects/spritebatch/spritebatch.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include diff --git a/include/objects/texture/texture.tcc b/include/objects/texture/texture.tcc index b856ac1e8..22daeb979 100644 --- a/include/objects/texture/texture.tcc +++ b/include/objects/texture/texture.tcc @@ -17,7 +17,7 @@ #include #include -#include +#include namespace love { diff --git a/include/objects/transform/transform.hpp b/include/objects/transform/transform.hpp index 889831fe4..f4eae408b 100644 --- a/include/objects/transform/transform.hpp +++ b/include/objects/transform/transform.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/platform/cafe/include/common/matrix_ext.hpp b/platform/cafe/include/common/matrix_ext.hpp index d8f10a68b..3f09223e2 100644 --- a/platform/cafe/include/common/matrix_ext.hpp +++ b/platform/cafe/include/common/matrix_ext.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace love { diff --git a/platform/cafe/include/utilities/driver/renderer_ext.hpp b/platform/cafe/include/utilities/driver/renderer_ext.hpp index cbbeb3523..46d3309bd 100644 --- a/platform/cafe/include/utilities/driver/renderer_ext.hpp +++ b/platform/cafe/include/utilities/driver/renderer_ext.hpp @@ -253,7 +253,7 @@ namespace love Framebuffer* current; GX2ContextState* state; - static inline std::vector m_commands {}; static inline CommonFormat m_format = CommonFormat::NONE; static inline GX2RBuffer m_buffer {}; static inline size_t m_vertexOffset = 0; diff --git a/platform/cafe/source/modules/graphics_ext.cpp b/platform/cafe/source/modules/graphics_ext.cpp index d8da4ecb7..e39da4167 100644 --- a/platform/cafe/source/modules/graphics_ext.cpp +++ b/platform/cafe/source/modules/graphics_ext.cpp @@ -2,7 +2,7 @@ #include -#include +#include #include #include diff --git a/platform/hac/include/common/matrix_ext.hpp b/platform/hac/include/common/matrix_ext.hpp index 70a10d044..4b4f552fb 100644 --- a/platform/hac/include/common/matrix_ext.hpp +++ b/platform/hac/include/common/matrix_ext.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace love { diff --git a/source/common/matrix.cpp b/source/common/matrix.cpp new file mode 100644 index 000000000..f200093c6 --- /dev/null +++ b/source/common/matrix.cpp @@ -0,0 +1,408 @@ +#include + +using namespace love; + +#if defined(__SWITCH__) + #include +#endif + +void Matrix4::Multiply(const Matrix4& a, const Matrix4& b, float t[0x10]) +{ +#if defined(__SWITCH__) + float32x4_t cola1 = vld1q_f32(&a.matrix[0]); + float32x4_t cola2 = vld1q_f32(&a.matrix[4]); + float32x4_t cola3 = vld1q_f32(&a.matrix[8]); + float32x4_t cola4 = vld1q_f32(&a.matrix[12]); + + float32x4_t col1 = vmulq_n_f32(cola1, b.matrix[0]); + col1 = vmlaq_n_f32(col1, cola2, b.matrix[1]); + col1 = vmlaq_n_f32(col1, cola3, b.matrix[2]); + col1 = vmlaq_n_f32(col1, cola4, b.matrix[3]); + + float32x4_t col2 = vmulq_n_f32(cola1, b.matrix[4]); + col2 = vmlaq_n_f32(col2, cola2, b.matrix[5]); + col2 = vmlaq_n_f32(col2, cola3, b.matrix[6]); + col2 = vmlaq_n_f32(col2, cola4, b.matrix[7]); + + float32x4_t col3 = vmulq_n_f32(cola1, b.matrix[8]); + col3 = vmlaq_n_f32(col3, cola2, b.matrix[9]); + col3 = vmlaq_n_f32(col3, cola3, b.matrix[10]); + col3 = vmlaq_n_f32(col3, cola4, b.matrix[11]); + + float32x4_t col4 = vmulq_n_f32(cola1, b.matrix[12]); + col4 = vmlaq_n_f32(col4, cola2, b.matrix[13]); + col4 = vmlaq_n_f32(col4, cola3, b.matrix[14]); + col4 = vmlaq_n_f32(col4, cola4, b.matrix[15]); + + vst1q_f32(&t[0], col1); + vst1q_f32(&t[4], col2); + vst1q_f32(&t[8], col3); + vst1q_f32(&t[12], col4); +#else + // clang-format off + t[0] = (a.matrix[0] * b.matrix[0]) + (a.matrix[4] * b.matrix[1]) + (a.matrix[8] * b.matrix[2]) + (a.matrix[12] * b.matrix[3]); + t[4] = (a.matrix[0] * b.matrix[4]) + (a.matrix[4] * b.matrix[5]) + (a.matrix[8] * b.matrix[6]) + (a.matrix[12] * b.matrix[7]); + t[8] = (a.matrix[0] * b.matrix[8]) + (a.matrix[4] * b.matrix[9]) + (a.matrix[8] * b.matrix[10]) + (a.matrix[12] * b.matrix[11]); + t[12] = (a.matrix[0] * b.matrix[12]) + (a.matrix[4] * b.matrix[13]) + (a.matrix[8] * b.matrix[14]) + (a.matrix[12] * b.matrix[15]); + + t[1] = (a.matrix[1] * b.matrix[0]) + (a.matrix[5] * b.matrix[1]) + (a.matrix[9] * b.matrix[2]) + (a.matrix[13] * b.matrix[3]); + t[5] = (a.matrix[1] * b.matrix[4]) + (a.matrix[5] * b.matrix[5]) + (a.matrix[9] * b.matrix[6]) + (a.matrix[13] * b.matrix[7]); + t[9] = (a.matrix[1] * b.matrix[8]) + (a.matrix[5] * b.matrix[9]) + (a.matrix[9] * b.matrix[10]) + (a.matrix[13] * b.matrix[11]); + t[13] = (a.matrix[1] * b.matrix[12]) + (a.matrix[5] * b.matrix[13]) + (a.matrix[9] * b.matrix[14]) + (a.matrix[13] * b.matrix[15]); + + t[2] = (a.matrix[2] * b.matrix[0]) + (a.matrix[6] * b.matrix[1]) + (a.matrix[10] * b.matrix[2]) + (a.matrix[14] * b.matrix[3]); + t[6] = (a.matrix[2] * b.matrix[4]) + (a.matrix[6] * b.matrix[5]) + (a.matrix[10] * b.matrix[6]) + (a.matrix[14] * b.matrix[7]); + t[10] = (a.matrix[2] * b.matrix[8]) + (a.matrix[6] * b.matrix[9]) + (a.matrix[10] * b.matrix[10]) + (a.matrix[14] * b.matrix[11]); + t[14] = (a.matrix[2] * b.matrix[12]) + (a.matrix[6] * b.matrix[13]) + (a.matrix[10] * b.matrix[14]) + (a.matrix[14] * b.matrix[15]); + + t[3] = (a.matrix[3] * b.matrix[0]) + (a.matrix[7] * b.matrix[1]) + (a.matrix[11] * b.matrix[2]) + (a.matrix[15] * b.matrix[3]); + t[7] = (a.matrix[3] * b.matrix[4]) + (a.matrix[7] * b.matrix[5]) + (a.matrix[11] * b.matrix[6]) + (a.matrix[15] * b.matrix[7]); + t[11] = (a.matrix[3] * b.matrix[8]) + (a.matrix[7] * b.matrix[9]) + (a.matrix[11] * b.matrix[10]) + (a.matrix[15] * b.matrix[11]); + t[15] = (a.matrix[3] * b.matrix[12]) + (a.matrix[7] * b.matrix[13]) + (a.matrix[11] * b.matrix[14]) + (a.matrix[15] * b.matrix[15]); + // clang-format on +#endif +} + +void Matrix4::Multiply(const Matrix4& a, const Matrix4& b, Matrix4& dst) +{ + Multiply(a, b, dst.matrix); +} + +Matrix4::Matrix4() +{ + this->SetIdentity(); +} + +Matrix4::Matrix4(const float matrix[0x10]) +{ + std::copy_n(matrix, 0x10, this->matrix); +} + +Matrix4::Matrix4(float t00, float t10, float t01, float t11, float x, float y) +{ + this->SetRawTransformation(t00, t10, t01, t11, x, y); +} + +Matrix4::Matrix4(const Matrix4& a, const Matrix4& b) +{ + Matrix4::Multiply(a, b, this->matrix); +} + +Matrix4::Matrix4(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, + float ky) +{ + this->SetTransformation(x, y, angle, sx, sy, ox, oy, kx, ky); +} + +Matrix4 Matrix4::operator*(const Matrix4& other) const +{ + return Matrix4(*this, other); +} + +void Matrix4::operator*=(const Matrix4& other) +{ + float temp[0x10] { 0 }; + Matrix4::Multiply(*this, other, temp); + std::copy_n(temp, 0x10, this->matrix); +} + +void Matrix4::SetRow(int row, const Vector4& vector) +{ + this->matrix[0 * 4 + row] = vector.x; + this->matrix[1 * 4 + row] = vector.y; + this->matrix[2 * 4 + row] = vector.z; + this->matrix[3 * 4 + row] = vector.w; +} + +Vector4 Matrix4::GetRow(int row) +{ + return Vector4(this->matrix[0 * 4 + row], this->matrix[1 * 4 + row], this->matrix[2 * 4 + row], + this->matrix[3 * 4 + row]); +} + +void Matrix4::SetColumn(int column, const Vector4& vector) +{ + this->matrix[column * 4 + 0] = vector.x; + this->matrix[column * 4 + 1] = vector.y; + this->matrix[column * 4 + 2] = vector.z; + this->matrix[column * 4 + 3] = vector.w; +} + +Vector4 Matrix4::GetColumn(int column) +{ + return Vector4(this->matrix[column * 4 + 0], this->matrix[column * 4 + 1], + this->matrix[column * 4 + 2], this->matrix[column * 4 + 3]); +} + +void Matrix4::SetIdentity() +{ + std::fill_n(this->matrix, 0x10, 0.0f); + this->matrix[15] = this->matrix[10] = this->matrix[5] = this->matrix[0] = 1; +} + +void Matrix4::SetTranslation(float x, float y) +{ + this->SetIdentity(); + this->matrix[12] = x; + this->matrix[13] = y; +} + +void Matrix4::SetRotation(float rotation) +{ + this->SetIdentity(); + float c = std::cos(rotation); + float s = std::sin(rotation); + + this->matrix[0] = c; + this->matrix[1] = s; + this->matrix[4] = -s; + this->matrix[5] = c; +} + +void Matrix4::SetScale(float sx, float sy) +{ + this->SetIdentity(); + + this->matrix[0] = sx; + this->matrix[5] = sy; +} + +void Matrix4::SetShear(float kx, float ky) +{ + this->SetIdentity(); + + this->matrix[1] = ky; + this->matrix[4] = kx; +} + +void Matrix4::GetApproximateScale(float& sx, float& sy) const +{ + sx = std::sqrt(this->matrix[0] * this->matrix[0] + this->matrix[4] * this->matrix[4]); + sy = std::sqrt(this->matrix[1] * this->matrix[1] + this->matrix[5] * this->matrix[5]); +} + +void Matrix4::SetRawTransformation(float t00, float t10, float t01, float t11, float x, float y) +{ + std::fill_n(this->matrix, 0x10, 0.0f); + this->matrix[10] = this->matrix[15] = 1.0f; + + this->matrix[0] = t00; + this->matrix[1] = t10; + this->matrix[4] = t01; + this->matrix[5] = t11; + this->matrix[12] = x; + this->matrix[13] = y; +} + +void Matrix4::SetTransformation(float x, float y, float angle, float sx, float sy, float ox, + float oy, float kx, float ky) +{ + std::fill_n(this->matrix, 0x10, 0.0f); + + float c = std::cos(angle); + float s = std::sin(angle); + + this->matrix[10] = this->matrix[15] = 1.0f; + + this->matrix[0] = c * sx - ky * s * sy; // = a + this->matrix[1] = s * sx + ky * c * sy; // = b + this->matrix[4] = kx * c * sx - s * sy; // = c + this->matrix[5] = kx * s * sx + c * sy; // = d + this->matrix[12] = x - ox * this->matrix[0] - oy * this->matrix[4]; + this->matrix[13] = y - ox * this->matrix[1] - oy * this->matrix[5]; +} + +void Matrix4::Translate(float x, float y) +{ + Matrix4 temp {}; + temp.SetTranslation(x, y); + this->operator*=(temp); +} + +void Matrix4::Rotate(float rotation) +{ + Matrix4 temp {}; + temp.SetRotation(rotation); + this->operator*=(temp); +} + +void Matrix4::Scale(float sx, float sy) +{ + Matrix4 temp {}; + temp.SetScale(sx, sy); + this->operator*=(temp); +} + +void Matrix4::Shear(float kx, float ky) +{ + Matrix4 temp {}; + temp.SetShear(kx, ky); + this->operator*=(temp); +} + +bool Matrix4::IsAffine2DTransform() const +{ + return fabsf(this->matrix[2] + this->matrix[3] + this->matrix[6] + this->matrix[7] + + this->matrix[8] + this->matrix[9] + this->matrix[11] + this->matrix[14]) < + 0.00001f && + fabsf(this->matrix[10] + this->matrix[15] - 2.0f) < 0.00001f; +} + +Matrix4 Matrix4::Inverse() const +{ + Matrix4 inverse {}; + + inverse.matrix[0] = this->matrix[5] * this->matrix[10] * this->matrix[15] - + this->matrix[5] * this->matrix[11] * this->matrix[14] - + this->matrix[9] * this->matrix[6] * this->matrix[15] + + this->matrix[9] * this->matrix[7] * this->matrix[14] + + this->matrix[13] * this->matrix[6] * this->matrix[11] - + this->matrix[13] * this->matrix[7] * this->matrix[10]; + + inverse.matrix[4] = -this->matrix[4] * this->matrix[10] * this->matrix[15] + + this->matrix[4] * this->matrix[11] * this->matrix[14] + + this->matrix[8] * this->matrix[6] * this->matrix[15] - + this->matrix[8] * this->matrix[7] * this->matrix[14] - + this->matrix[12] * this->matrix[6] * this->matrix[11] + + this->matrix[12] * this->matrix[7] * this->matrix[10]; + + inverse.matrix[8] = this->matrix[4] * this->matrix[9] * this->matrix[15] - + this->matrix[4] * this->matrix[11] * this->matrix[13] - + this->matrix[8] * this->matrix[5] * this->matrix[15] + + this->matrix[8] * this->matrix[7] * this->matrix[13] + + this->matrix[12] * this->matrix[5] * this->matrix[11] - + this->matrix[12] * this->matrix[7] * this->matrix[9]; + + inverse.matrix[12] = -this->matrix[4] * this->matrix[9] * this->matrix[14] + + this->matrix[4] * this->matrix[10] * this->matrix[13] + + this->matrix[8] * this->matrix[5] * this->matrix[14] - + this->matrix[8] * this->matrix[6] * this->matrix[13] - + this->matrix[12] * this->matrix[5] * this->matrix[10] + + this->matrix[12] * this->matrix[6] * this->matrix[9]; + + inverse.matrix[1] = -this->matrix[1] * this->matrix[10] * this->matrix[15] + + this->matrix[1] * this->matrix[11] * this->matrix[14] + + this->matrix[9] * this->matrix[2] * this->matrix[15] - + this->matrix[9] * this->matrix[3] * this->matrix[14] - + this->matrix[13] * this->matrix[2] * this->matrix[11] + + this->matrix[13] * this->matrix[3] * this->matrix[10]; + + inverse.matrix[5] = this->matrix[0] * this->matrix[10] * this->matrix[15] - + this->matrix[0] * this->matrix[11] * this->matrix[14] - + this->matrix[8] * this->matrix[2] * this->matrix[15] + + this->matrix[8] * this->matrix[3] * this->matrix[14] + + this->matrix[12] * this->matrix[2] * this->matrix[11] - + this->matrix[12] * this->matrix[3] * this->matrix[10]; + + inverse.matrix[9] = -this->matrix[0] * this->matrix[9] * this->matrix[15] + + this->matrix[0] * this->matrix[11] * this->matrix[13] + + this->matrix[8] * this->matrix[1] * this->matrix[15] - + this->matrix[8] * this->matrix[3] * this->matrix[13] - + this->matrix[12] * this->matrix[1] * this->matrix[11] + + this->matrix[12] * this->matrix[3] * this->matrix[9]; + + inverse.matrix[13] = this->matrix[0] * this->matrix[9] * this->matrix[14] - + this->matrix[0] * this->matrix[10] * this->matrix[13] - + this->matrix[8] * this->matrix[1] * this->matrix[14] + + this->matrix[8] * this->matrix[2] * this->matrix[13] + + this->matrix[12] * this->matrix[1] * this->matrix[10] - + this->matrix[12] * this->matrix[2] * this->matrix[9]; + + inverse.matrix[2] = this->matrix[1] * this->matrix[6] * this->matrix[15] - + this->matrix[1] * this->matrix[7] * this->matrix[14] - + this->matrix[5] * this->matrix[2] * this->matrix[15] + + this->matrix[5] * this->matrix[3] * this->matrix[14] + + this->matrix[13] * this->matrix[2] * this->matrix[7] - + this->matrix[13] * this->matrix[3] * this->matrix[6]; + + inverse.matrix[6] = -this->matrix[0] * this->matrix[6] * this->matrix[15] + + this->matrix[0] * this->matrix[7] * this->matrix[14] + + this->matrix[4] * this->matrix[2] * this->matrix[15] - + this->matrix[4] * this->matrix[3] * this->matrix[14] - + this->matrix[12] * this->matrix[2] * this->matrix[7] + + this->matrix[12] * this->matrix[3] * this->matrix[6]; + + inverse.matrix[10] = this->matrix[0] * this->matrix[5] * this->matrix[15] - + this->matrix[0] * this->matrix[7] * this->matrix[13] - + this->matrix[4] * this->matrix[1] * this->matrix[15] + + this->matrix[4] * this->matrix[3] * this->matrix[13] + + this->matrix[12] * this->matrix[1] * this->matrix[7] - + this->matrix[12] * this->matrix[3] * this->matrix[5]; + + inverse.matrix[14] = -this->matrix[0] * this->matrix[5] * this->matrix[14] + + this->matrix[0] * this->matrix[6] * this->matrix[13] + + this->matrix[4] * this->matrix[1] * this->matrix[14] - + this->matrix[4] * this->matrix[2] * this->matrix[13] - + this->matrix[12] * this->matrix[1] * this->matrix[6] + + this->matrix[12] * this->matrix[2] * this->matrix[5]; + + inverse.matrix[3] = -this->matrix[1] * this->matrix[6] * this->matrix[11] + + this->matrix[1] * this->matrix[7] * this->matrix[10] + + this->matrix[5] * this->matrix[2] * this->matrix[11] - + this->matrix[5] * this->matrix[3] * this->matrix[10] - + this->matrix[9] * this->matrix[2] * this->matrix[7] + + this->matrix[9] * this->matrix[3] * this->matrix[6]; + + inverse.matrix[7] = this->matrix[0] * this->matrix[6] * this->matrix[11] - + this->matrix[0] * this->matrix[7] * this->matrix[10] - + this->matrix[4] * this->matrix[2] * this->matrix[11] + + this->matrix[4] * this->matrix[3] * this->matrix[10] + + this->matrix[8] * this->matrix[2] * this->matrix[7] - + this->matrix[8] * this->matrix[3] * this->matrix[6]; + + inverse.matrix[11] = -this->matrix[0] * this->matrix[5] * this->matrix[11] + + this->matrix[0] * this->matrix[7] * this->matrix[9] + + this->matrix[4] * this->matrix[1] * this->matrix[11] - + this->matrix[4] * this->matrix[3] * this->matrix[9] - + this->matrix[8] * this->matrix[1] * this->matrix[7] + + this->matrix[8] * this->matrix[3] * this->matrix[5]; + + inverse.matrix[15] = this->matrix[0] * this->matrix[5] * this->matrix[10] - + this->matrix[0] * this->matrix[6] * this->matrix[9] - + this->matrix[4] * this->matrix[1] * this->matrix[10] + + this->matrix[4] * this->matrix[2] * this->matrix[9] + + this->matrix[8] * this->matrix[1] * this->matrix[6] - + this->matrix[8] * this->matrix[2] * this->matrix[5]; + + float det = this->matrix[0] * inverse.matrix[0] + this->matrix[1] * inverse.matrix[4] + + this->matrix[2] * inverse.matrix[8] + this->matrix[3] * inverse.matrix[12]; + + float invDet = 1.0f / det; + + for (int i = 0; i < 16; i++) + inverse.matrix[i] *= invDet; + + return inverse; +} + +Matrix4 Matrix4::Ortho(float left, float right, float bottom, float top, float near, float far) +{ + Matrix4 result {}; + + result.matrix[0] = 2.0f / (right - left); + result.matrix[5] = 2.0f / (top - bottom); + result.matrix[10] = -2.0f / (far - near); + + result.matrix[12] = -(right + left) / (right - left); + result.matrix[13] = -(top + bottom) / (top - bottom); + result.matrix[14] = -(far + near) / (far - near); + + return result; +} + +Matrix4 Matrix4::Perspective(float verticalfov, float aspect, float near, float far) +{ + Matrix4 result {}; + + float coTangent = 1.0f / std::tan(verticalfov * 0.5f); + + result.matrix[0] = coTangent / aspect; + result.matrix[5] = coTangent; + result.matrix[10] = (near + far) / (near - far); + result.matrix[11] = -1.0f; + + result.matrix[14] = (2.0f * near * far) / (near - far); + result.matrix[15] = 0.0f; + + return result; +} diff --git a/source/modules/filesystem/physfs/filesystem.cpp b/source/modules/filesystem/physfs/filesystem.cpp index f401be2bc..436ff7f60 100644 --- a/source/modules/filesystem/physfs/filesystem.cpp +++ b/source/modules/filesystem/physfs/filesystem.cpp @@ -576,6 +576,14 @@ std::string Filesystem::GetRealDirectory(const char* filename) const return std::string(directory); } +bool Filesystem::Exists(const char* filepath) const +{ + if (!PHYSFS_isInit()) + return false; + + return PHYSFS_exists(filepath) != 0; +} + bool Filesystem::GetInfo(const char* filepath, Info& info) const { if (!PHYSFS_isInit()) diff --git a/source/modules/filesystem/wrap_filesystem.cpp b/source/modules/filesystem/wrap_filesystem.cpp index c31506bfa..9c9efecc1 100644 --- a/source/modules/filesystem/wrap_filesystem.cpp +++ b/source/modules/filesystem/wrap_filesystem.cpp @@ -345,6 +345,15 @@ int Wrap_Filesystem::GetIdentity(lua_State* L) return 1; } +int Wrap_Filesystem::Exists(lua_State* L) +{ + const char* filename = luaL_checkstring(L, 1); + + lua_pushboolean(L, instance()->Exists(filename)); + + return 1; +} + int Wrap_Filesystem::GetInfo(lua_State* L) { const char* filepath = luaL_checkstring(L, 1); @@ -797,6 +806,7 @@ static constexpr luaL_Reg functions[] = { { "append", Wrap_Filesystem::Append }, { "createDirectory", Wrap_Filesystem::CreateDirectory }, + { "exists", Wrap_Filesystem::Exists }, { "getDirectoryItems", Wrap_Filesystem::GetDirectoryItems }, { "getExecutablePath", Wrap_Filesystem::GetExecutablePath }, { "getIdentity", Wrap_Filesystem::GetIdentity }, diff --git a/source/objects/textbatch/textbatch.cpp b/source/objects/textbatch/textbatch.cpp index e76f3322c..b6e347d32 100644 --- a/source/objects/textbatch/textbatch.cpp +++ b/source/objects/textbatch/textbatch.cpp @@ -1,4 +1,4 @@ -#include +#include #include