From aec444f2b5e3262c32150b7d9466ce05613a8c01 Mon Sep 17 00:00:00 2001 From: ChestnutYueyue <952134128@qq.com> Date: Thu, 26 Feb 2026 20:06:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E9=87=8D=E6=9E=84=E6=95=B0?= =?UTF-8?q?=E5=AD=A6=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=8B=86=E5=88=86=E4=B8=BA?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=E7=8B=AC=E7=AB=8B=E5=A4=B4=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 math_types.h 拆分为 vec2.h, vec3.h, rect.h, size.h 和 transform.h 移除 ColorConstants 结构体,直接使用 Colors 命名空间 优化颜色结构体的运算符实现 --- include/core/color.h | 35 +-- include/core/math_types.h | 335 ---------------------- include/core/rect.h | 77 +++++ include/core/size.h | 26 ++ include/core/transform.h | 87 ++++++ include/core/vec2.h | 86 ++++++ include/core/vec3.h | 76 +++++ include/event/event.h | 2 +- include/extra2d.h | 6 +- include/graphics/alpha_mask.h | 3 +- include/graphics/font.h | 3 +- include/graphics/opengl/gl_font_atlas.h | 3 +- include/graphics/opengl/gl_sprite_batch.h | 2 +- include/graphics/texture.h | 2 +- include/graphics/texture_atlas.h | 3 +- include/platform/input.h | 2 +- include/platform/window.h | 3 +- include/renderer/camera.h | 4 +- include/renderer/render_command.h | 3 +- include/renderer/renderer.h | 4 +- include/scene/node.h | 3 +- include/scene/shape.h | 2 +- include/ui/widget.h | 3 +- src/event/event_dispatcher.cpp | 1 + 24 files changed, 395 insertions(+), 376 deletions(-) delete mode 100644 include/core/math_types.h create mode 100644 include/core/rect.h create mode 100644 include/core/size.h create mode 100644 include/core/transform.h create mode 100644 include/core/vec2.h create mode 100644 include/core/vec3.h diff --git a/include/core/color.h b/include/core/color.h index 1c1afab..c962c34 100644 --- a/include/core/color.h +++ b/include/core/color.h @@ -15,28 +15,26 @@ struct Color3B { constexpr Color3B() = default; constexpr Color3B(uint8_t r, uint8_t g, uint8_t b) : r(r), g(g), b(b) {} - constexpr bool operator==(const Color3B& other) const { + constexpr bool operator==(const Color3B &other) const { return r == other.r && g == other.g && b == other.b; } - constexpr bool operator!=(const Color3B& other) const { + constexpr bool operator!=(const Color3B &other) const { return !(*this == other); } - Color3B operator+(const Color3B& other) const { + Color3B operator+(const Color3B &other) const { return Color3B( - static_cast(std::min(255, static_cast(r) + other.r)), - static_cast(std::min(255, static_cast(g) + other.g)), - static_cast(std::min(255, static_cast(b) + other.b)) - ); + static_cast(std::min(255, static_cast(r) + other.r)), + static_cast(std::min(255, static_cast(g) + other.g)), + static_cast(std::min(255, static_cast(b) + other.b))); } - Color3B operator-(const Color3B& other) const { + Color3B operator-(const Color3B &other) const { return Color3B( - static_cast(std::max(0, static_cast(r) - other.r)), - static_cast(std::max(0, static_cast(g) - other.g)), - static_cast(std::max(0, static_cast(b) - other.b)) - ); + static_cast(std::max(0, static_cast(r) - other.r)), + static_cast(std::max(0, static_cast(g) - other.g)), + static_cast(std::max(0, static_cast(b) - other.b))); } }; @@ -155,17 +153,4 @@ inline constexpr Color Coral{1.0f, 0.498f, 0.314f, 1.0f}; inline constexpr Color Transparent{0.0f, 0.0f, 0.0f, 0.0f}; } // namespace Colors -// 为了向后兼容,在 Color 结构体内提供静态引用 -struct ColorConstants { - static const Color &White; - static const Color &Black; - static const Color &Red; - static const Color &Green; - static const Color &Blue; - static const Color &Yellow; - static const Color &Cyan; - static const Color &Magenta; - static const Color &Transparent; -}; - } // namespace extra2d diff --git a/include/core/math_types.h b/include/core/math_types.h deleted file mode 100644 index ef5a225..0000000 --- a/include/core/math_types.h +++ /dev/null @@ -1,335 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace extra2d { - -// --------------------------------------------------------------------------- -// 常量 -// --------------------------------------------------------------------------- -constexpr float PI_F = 3.14159265358979323846f; -constexpr float DEG_TO_RAD = PI_F / 180.0f; -constexpr float RAD_TO_DEG = 180.0f / PI_F; - -// --------------------------------------------------------------------------- -// 2D 向量 -// --------------------------------------------------------------------------- -struct Vec2 { - float x = 0.0f; - float y = 0.0f; - - constexpr Vec2() = default; - constexpr Vec2(float x, float y) : x(x), y(y) {} - explicit Vec2(const glm::vec2 &v) : x(v.x), y(v.y) {} - - glm::vec2 toGlm() const { return {x, y}; } - static Vec2 fromGlm(const glm::vec2 &v) { return {v.x, v.y}; } - - // 基础运算 - Vec2 operator+(const Vec2 &v) const { return {x + v.x, y + v.y}; } - Vec2 operator-(const Vec2 &v) const { return {x - v.x, y - v.y}; } - Vec2 operator*(float s) const { return {x * s, y * s}; } - Vec2 operator/(float s) const { return {x / s, y / s}; } - Vec2 operator-() const { return {-x, -y}; } - - Vec2 &operator+=(const Vec2 &v) { - x += v.x; - y += v.y; - return *this; - } - Vec2 &operator-=(const Vec2 &v) { - x -= v.x; - y -= v.y; - return *this; - } - Vec2 &operator*=(float s) { - x *= s; - y *= s; - return *this; - } - Vec2 &operator/=(float s) { - x /= s; - y /= s; - return *this; - } - - bool operator==(const Vec2 &v) const { return x == v.x && y == v.y; } - bool operator!=(const Vec2 &v) const { return !(*this == v); } - - // 向量运算 - float length() const { return std::sqrt(x * x + y * y); } - float lengthSquared() const { return x * x + y * y; } - - Vec2 normalized() const { - float len = length(); - if (len > 0.0f) - return {x / len, y / len}; - return {0.0f, 0.0f}; - } - - float dot(const Vec2 &v) const { return x * v.x + y * v.y; } - float cross(const Vec2 &v) const { return x * v.y - y * v.x; } - - float distance(const Vec2 &v) const { return (*this - v).length(); } - float angle() const { return std::atan2(y, x) * RAD_TO_DEG; } - - static Vec2 lerp(const Vec2 &a, const Vec2 &b, float t) { - return a + (b - a) * t; - } - - static constexpr Vec2 Zero() { return {0.0f, 0.0f}; } - static constexpr Vec2 One() { return {1.0f, 1.0f}; } - static constexpr Vec2 UnitX() { return {1.0f, 0.0f}; } - static constexpr Vec2 UnitY() { return {0.0f, 1.0f}; } -}; - -inline Vec2 operator*(float s, const Vec2 &v) { return v * s; } - -using Point = Vec2; - -// --------------------------------------------------------------------------- -// 3D 向量 (用于3D动作) -// --------------------------------------------------------------------------- -struct Vec3 { - float x = 0.0f; - float y = 0.0f; - float z = 0.0f; - - constexpr Vec3() = default; - constexpr Vec3(float x, float y, float z) : x(x), y(y), z(z) {} - explicit Vec3(const glm::vec3 &v) : x(v.x), y(v.y), z(v.z) {} - - glm::vec3 toGlm() const { return {x, y, z}; } - static Vec3 fromGlm(const glm::vec3 &v) { return {v.x, v.y, v.z}; } - - Vec3 operator+(const Vec3 &v) const { return {x + v.x, y + v.y, z + v.z}; } - Vec3 operator-(const Vec3 &v) const { return {x - v.x, y - v.y, z - v.z}; } - Vec3 operator*(float s) const { return {x * s, y * s, z * s}; } - Vec3 operator/(float s) const { return {x / s, y / s, z / s}; } - Vec3 operator-() const { return {-x, -y, -z}; } - - Vec3 &operator+=(const Vec3 &v) { - x += v.x; - y += v.y; - z += v.z; - return *this; - } - Vec3 &operator-=(const Vec3 &v) { - x -= v.x; - y -= v.y; - z -= v.z; - return *this; - } - Vec3 &operator*=(float s) { - x *= s; - y *= s; - z *= s; - return *this; - } - Vec3 &operator/=(float s) { - x /= s; - y /= s; - z /= s; - return *this; - } - - bool operator==(const Vec3 &v) const { - return x == v.x && y == v.y && z == v.z; - } - bool operator!=(const Vec3 &v) const { return !(*this == v); } - - float length() const { return std::sqrt(x * x + y * y + z * z); } - float lengthSquared() const { return x * x + y * y + z * z; } - - Vec3 normalized() const { - float len = length(); - if (len > 0.0f) - return {x / len, y / len, z / len}; - return {0.0f, 0.0f, 0.0f}; - } - - float dot(const Vec3 &v) const { return x * v.x + y * v.y + z * v.z; } - - static Vec3 lerp(const Vec3 &a, const Vec3 &b, float t) { - return a + (b - a) * t; - } - - static constexpr Vec3 Zero() { return {0.0f, 0.0f, 0.0f}; } - static constexpr Vec3 One() { return {1.0f, 1.0f, 1.0f}; } -}; - -inline Vec3 operator*(float s, const Vec3 &v) { return v * s; } - -// --------------------------------------------------------------------------- -// 2D 尺寸 -// --------------------------------------------------------------------------- -struct Size { - float width = 0.0f; - float height = 0.0f; - - constexpr Size() = default; - constexpr Size(float w, float h) : width(w), height(h) {} - - bool operator==(const Size &s) const { - return width == s.width && height == s.height; - } - bool operator!=(const Size &s) const { return !(*this == s); } - - float area() const { return width * height; } - bool empty() const { return width <= 0.0f || height <= 0.0f; } - - static constexpr Size Zero() { return {0.0f, 0.0f}; } -}; - -// --------------------------------------------------------------------------- -// 2D 矩形 -// --------------------------------------------------------------------------- -struct Rect { - Point origin; - Size size; - - constexpr Rect() = default; - constexpr Rect(float x, float y, float w, float h) - : origin(x, y), size(w, h) {} - constexpr Rect(const Point &o, const Size &s) : origin(o), size(s) {} - - float left() const { return origin.x; } - float top() const { return origin.y; } - float right() const { return origin.x + size.width; } - float bottom() const { return origin.y + size.height; } - float width() const { return size.width; } - float height() const { return size.height; } - Point center() const { - return {origin.x + size.width * 0.5f, origin.y + size.height * 0.5f}; - } - - bool empty() const { return size.empty(); } - - bool containsPoint(const Point &p) const { - return p.x >= left() && p.x <= right() && p.y >= top() && p.y <= bottom(); - } - - bool contains(const Rect &r) const { - return r.left() >= left() && r.right() <= right() && r.top() >= top() && - r.bottom() <= bottom(); - } - - bool intersects(const Rect &r) const { - return !(left() > r.right() || right() < r.left() || top() > r.bottom() || - bottom() < r.top()); - } - - Rect intersection(const Rect &r) const { - float l = std::max(left(), r.left()); - float t = std::max(top(), r.top()); - float ri = std::min(right(), r.right()); - float b = std::min(bottom(), r.bottom()); - if (l < ri && t < b) - return {l, t, ri - l, b - t}; - return {}; - } - - Rect unionWith(const Rect &r) const { - if (empty()) - return r; - if (r.empty()) - return *this; - float l = std::min(left(), r.left()); - float t = std::min(top(), r.top()); - float ri = std::max(right(), r.right()); - float b = std::max(bottom(), r.bottom()); - return {l, t, ri - l, b - t}; - } - - bool operator==(const Rect &r) const { - return origin == r.origin && size == r.size; - } - bool operator!=(const Rect &r) const { return !(*this == r); } - - static constexpr Rect Zero() { return {0, 0, 0, 0}; } -}; - -// --------------------------------------------------------------------------- -// 2D 变换矩阵(基于 glm::mat4,兼容 OpenGL) -// --------------------------------------------------------------------------- -struct Transform2D { - glm::mat4 matrix{1.0f}; // 单位矩阵 - - Transform2D() = default; - explicit Transform2D(const glm::mat4 &m) : matrix(m) {} - - static Transform2D identity() { return Transform2D{}; } - - static Transform2D translation(float x, float y) { - Transform2D t; - t.matrix = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, 0.0f)); - return t; - } - - static Transform2D translation(const Vec2 &v) { - return translation(v.x, v.y); - } - - static Transform2D rotation(float degrees) { - Transform2D t; - t.matrix = glm::rotate(glm::mat4(1.0f), degrees * DEG_TO_RAD, - glm::vec3(0.0f, 0.0f, 1.0f)); - return t; - } - - static Transform2D scaling(float sx, float sy) { - Transform2D t; - t.matrix = glm::scale(glm::mat4(1.0f), glm::vec3(sx, sy, 1.0f)); - return t; - } - - static Transform2D scaling(float s) { return scaling(s, s); } - - static Transform2D skewing(float skewX, float skewY) { - Transform2D t; - t.matrix = glm::mat4(1.0f); - t.matrix[1][0] = std::tan(skewX * DEG_TO_RAD); - t.matrix[0][1] = std::tan(skewY * DEG_TO_RAD); - return t; - } - - Transform2D operator*(const Transform2D &other) const { - return Transform2D(matrix * other.matrix); - } - - Transform2D &operator*=(const Transform2D &other) { - matrix *= other.matrix; - return *this; - } - - Vec2 transformPoint(const Vec2 &p) const { - glm::vec4 result = matrix * glm::vec4(p.x, p.y, 0.0f, 1.0f); - return {result.x, result.y}; - } - - Transform2D inverse() const { return Transform2D(glm::inverse(matrix)); } -}; - -// --------------------------------------------------------------------------- -// 数学工具函数 -// --------------------------------------------------------------------------- -namespace math { - -inline float clamp(float value, float minVal, float maxVal) { - return std::clamp(value, minVal, maxVal); -} - -inline float lerp(float a, float b, float t) { return a + (b - a) * t; } - -inline float degrees(float radians) { return radians * RAD_TO_DEG; } - -inline float radians(float degrees) { return degrees * DEG_TO_RAD; } - -} // namespace math - -} // namespace extra2d diff --git a/include/core/rect.h b/include/core/rect.h new file mode 100644 index 0000000..2a01e86 --- /dev/null +++ b/include/core/rect.h @@ -0,0 +1,77 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { + +// --------------------------------------------------------------------------- +// 2D 矩形 +// --------------------------------------------------------------------------- +struct Rect { + Point origin; + Size size; + + constexpr Rect() = default; + constexpr Rect(float x, float y, float w, float h) + : origin(x, y), size(w, h) {} + constexpr Rect(const Point &o, const Size &s) : origin(o), size(s) {} + + float left() const { return origin.x; } + float top() const { return origin.y; } + float right() const { return origin.x + size.width; } + float bottom() const { return origin.y + size.height; } + float width() const { return size.width; } + float height() const { return size.height; } + Point center() const { + return {origin.x + size.width * 0.5f, origin.y + size.height * 0.5f}; + } + + bool empty() const { return size.empty(); } + + bool containsPoint(const Point &p) const { + return p.x >= left() && p.x <= right() && p.y >= top() && p.y <= bottom(); + } + + bool contains(const Rect &r) const { + return r.left() >= left() && r.right() <= right() && r.top() >= top() && + r.bottom() <= bottom(); + } + + bool intersects(const Rect &r) const { + return !(left() > r.right() || right() < r.left() || top() > r.bottom() || + bottom() < r.top()); + } + + Rect intersection(const Rect &r) const { + float l = std::max(left(), r.left()); + float t = std::max(top(), r.top()); + float ri = std::min(right(), r.right()); + float b = std::min(bottom(), r.bottom()); + if (l < ri && t < b) + return {l, t, ri - l, b - t}; + return {}; + } + + Rect unionWith(const Rect &r) const { + if (empty()) + return r; + if (r.empty()) + return *this; + float l = std::min(left(), r.left()); + float t = std::min(top(), r.top()); + float ri = std::max(right(), r.right()); + float b = std::max(bottom(), r.bottom()); + return {l, t, ri - l, b - t}; + } + + bool operator==(const Rect &r) const { + return origin == r.origin && size == r.size; + } + bool operator!=(const Rect &r) const { return !(*this == r); } + + static constexpr Rect Zero() { return {0, 0, 0, 0}; } +}; + +} // namespace extra2d diff --git a/include/core/size.h b/include/core/size.h new file mode 100644 index 0000000..a09dd5d --- /dev/null +++ b/include/core/size.h @@ -0,0 +1,26 @@ +#pragma once + +namespace extra2d { + +// --------------------------------------------------------------------------- +// 2D 尺寸 +// --------------------------------------------------------------------------- +struct Size { + float width = 0.0f; + float height = 0.0f; + + constexpr Size() = default; + constexpr Size(float w, float h) : width(w), height(h) {} + + bool operator==(const Size &s) const { + return width == s.width && height == s.height; + } + bool operator!=(const Size &s) const { return !(*this == s); } + + float area() const { return width * height; } + bool empty() const { return width <= 0.0f || height <= 0.0f; } + + static constexpr Size Zero() { return {0.0f, 0.0f}; } +}; + +} // namespace extra2d diff --git a/include/core/transform.h b/include/core/transform.h new file mode 100644 index 0000000..f2e29e8 --- /dev/null +++ b/include/core/transform.h @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include + +namespace extra2d { + +// --------------------------------------------------------------------------- +// 2D 变换矩阵(基于 glm::mat4,兼容 OpenGL) +// --------------------------------------------------------------------------- +struct Transform2D { + glm::mat4 matrix{1.0f}; // 单位矩阵 + + Transform2D() = default; + explicit Transform2D(const glm::mat4 &m) : matrix(m) {} + + static Transform2D identity() { return Transform2D{}; } + + static Transform2D translation(float x, float y) { + Transform2D t; + t.matrix = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, 0.0f)); + return t; + } + + static Transform2D translation(const Vec2 &v) { + return translation(v.x, v.y); + } + + static Transform2D rotation(float degrees) { + Transform2D t; + t.matrix = glm::rotate(glm::mat4(1.0f), degrees * DEG_TO_RAD, + glm::vec3(0.0f, 0.0f, 1.0f)); + return t; + } + + static Transform2D scaling(float sx, float sy) { + Transform2D t; + t.matrix = glm::scale(glm::mat4(1.0f), glm::vec3(sx, sy, 1.0f)); + return t; + } + + static Transform2D scaling(float s) { return scaling(s, s); } + + static Transform2D skewing(float skewX, float skewY) { + Transform2D t; + t.matrix = glm::mat4(1.0f); + t.matrix[1][0] = std::tan(skewX * DEG_TO_RAD); + t.matrix[0][1] = std::tan(skewY * DEG_TO_RAD); + return t; + } + + Transform2D operator*(const Transform2D &other) const { + return Transform2D(matrix * other.matrix); + } + + Transform2D &operator*=(const Transform2D &other) { + matrix *= other.matrix; + return *this; + } + + Vec2 transformPoint(const Vec2 &p) const { + glm::vec4 result = matrix * glm::vec4(p.x, p.y, 0.0f, 1.0f); + return {result.x, result.y}; + } + + Transform2D inverse() const { return Transform2D(glm::inverse(matrix)); } +}; + +// --------------------------------------------------------------------------- +// 数学工具函数 +// --------------------------------------------------------------------------- +namespace math { + +inline float clamp(float value, float minVal, float maxVal) { + return value < minVal ? minVal : (value > maxVal ? maxVal : value); +} + +inline float lerp(float a, float b, float t) { return a + (b - a) * t; } + +inline float degrees(float radians) { return radians * RAD_TO_DEG; } + +inline float radians(float degrees) { return degrees * DEG_TO_RAD; } + +} // namespace math + +} // namespace extra2d diff --git a/include/core/vec2.h b/include/core/vec2.h new file mode 100644 index 0000000..ef4cc14 --- /dev/null +++ b/include/core/vec2.h @@ -0,0 +1,86 @@ +#pragma once + +#include + +namespace extra2d { + +// --------------------------------------------------------------------------- +// 常量 +// --------------------------------------------------------------------------- +constexpr float PI_F = 3.14159265358979323846f; +constexpr float DEG_TO_RAD = PI_F / 180.0f; +constexpr float RAD_TO_DEG = 180.0f / PI_F; + +// --------------------------------------------------------------------------- +// 2D 向量 +// --------------------------------------------------------------------------- +struct Vec2 { + float x = 0.0f; + float y = 0.0f; + + constexpr Vec2() = default; + constexpr Vec2(float x, float y) : x(x), y(y) {} + + // 基础运算 + Vec2 operator+(const Vec2 &v) const { return {x + v.x, y + v.y}; } + Vec2 operator-(const Vec2 &v) const { return {x - v.x, y - v.y}; } + Vec2 operator*(float s) const { return {x * s, y * s}; } + Vec2 operator/(float s) const { return {x / s, y / s}; } + Vec2 operator-() const { return {-x, -y}; } + + Vec2 &operator+=(const Vec2 &v) { + x += v.x; + y += v.y; + return *this; + } + Vec2 &operator-=(const Vec2 &v) { + x -= v.x; + y -= v.y; + return *this; + } + Vec2 &operator*=(float s) { + x *= s; + y *= s; + return *this; + } + Vec2 &operator/=(float s) { + x /= s; + y /= s; + return *this; + } + + bool operator==(const Vec2 &v) const { return x == v.x && y == v.y; } + bool operator!=(const Vec2 &v) const { return !(*this == v); } + + // 向量运算 + float length() const { return std::sqrt(x * x + y * y); } + float lengthSquared() const { return x * x + y * y; } + + Vec2 normalized() const { + float len = length(); + if (len > 0.0f) + return {x / len, y / len}; + return {0.0f, 0.0f}; + } + + float dot(const Vec2 &v) const { return x * v.x + y * v.y; } + float cross(const Vec2 &v) const { return x * v.y - y * v.x; } + + float distance(const Vec2 &v) const { return (*this - v).length(); } + float angle() const { return std::atan2(y, x) * RAD_TO_DEG; } + + static Vec2 lerp(const Vec2 &a, const Vec2 &b, float t) { + return a + (b - a) * t; + } + + static constexpr Vec2 Zero() { return {0.0f, 0.0f}; } + static constexpr Vec2 One() { return {1.0f, 1.0f}; } + static constexpr Vec2 UnitX() { return {1.0f, 0.0f}; } + static constexpr Vec2 UnitY() { return {0.0f, 1.0f}; } +}; + +inline Vec2 operator*(float s, const Vec2 &v) { return v * s; } + +using Point = Vec2; + +} // namespace extra2d diff --git a/include/core/vec3.h b/include/core/vec3.h new file mode 100644 index 0000000..f1908e5 --- /dev/null +++ b/include/core/vec3.h @@ -0,0 +1,76 @@ +#pragma once + +#include + +namespace extra2d { + +// --------------------------------------------------------------------------- +// 3D 向量 (用于3D动作) +// --------------------------------------------------------------------------- +struct Vec3 { + float x = 0.0f; + float y = 0.0f; + float z = 0.0f; + + constexpr Vec3() = default; + constexpr Vec3(float x, float y, float z) : x(x), y(y), z(z) {} + + Vec3 operator+(const Vec3 &v) const { return {x + v.x, y + v.y, z + v.z}; } + Vec3 operator-(const Vec3 &v) const { return {x - v.x, y - v.y, z - v.z}; } + Vec3 operator*(float s) const { return {x * s, y * s, z * s}; } + Vec3 operator/(float s) const { return {x / s, y / s, z / s}; } + Vec3 operator-() const { return {-x, -y, -z}; } + + Vec3 &operator+=(const Vec3 &v) { + x += v.x; + y += v.y; + z += v.z; + return *this; + } + Vec3 &operator-=(const Vec3 &v) { + x -= v.x; + y -= v.y; + z -= v.z; + return *this; + } + Vec3 &operator*=(float s) { + x *= s; + y *= s; + z *= s; + return *this; + } + Vec3 &operator/=(float s) { + x /= s; + y /= s; + z /= s; + return *this; + } + + bool operator==(const Vec3 &v) const { + return x == v.x && y == v.y && z == v.z; + } + bool operator!=(const Vec3 &v) const { return !(*this == v); } + + float length() const { return std::sqrt(x * x + y * y + z * z); } + float lengthSquared() const { return x * x + y * y + z * z; } + + Vec3 normalized() const { + float len = length(); + if (len > 0.0f) + return {x / len, y / len, z / len}; + return {0.0f, 0.0f, 0.0f}; + } + + float dot(const Vec3 &v) const { return x * v.x + y * v.y + z * v.z; } + + static Vec3 lerp(const Vec3 &a, const Vec3 &b, float t) { + return a + (b - a) * t; + } + + static constexpr Vec3 Zero() { return {0.0f, 0.0f, 0.0f}; } + static constexpr Vec3 One() { return {1.0f, 1.0f, 1.0f}; } +}; + +inline Vec3 operator*(float s, const Vec3 &v) { return v * s; } + +} // namespace extra2d diff --git a/include/event/event.h b/include/event/event.h index 67a4a24..8ef2623 100644 --- a/include/event/event.h +++ b/include/event/event.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include #include namespace extra2d { diff --git a/include/extra2d.h b/include/extra2d.h index 965a533..1f5b758 100644 --- a/include/extra2d.h +++ b/include/extra2d.h @@ -5,9 +5,13 @@ // Core #include -#include +#include +#include #include +#include #include +#include +#include // Platform #include diff --git a/include/graphics/alpha_mask.h b/include/graphics/alpha_mask.h index ee220b4..4f6f0c6 100644 --- a/include/graphics/alpha_mask.h +++ b/include/graphics/alpha_mask.h @@ -1,7 +1,8 @@ #pragma once -#include +#include #include +#include #include namespace extra2d { diff --git a/include/graphics/font.h b/include/graphics/font.h index 106ac66..ad3c547 100644 --- a/include/graphics/font.h +++ b/include/graphics/font.h @@ -1,8 +1,9 @@ #pragma once #include -#include +#include #include +#include namespace extra2d { diff --git a/include/graphics/opengl/gl_font_atlas.h b/include/graphics/opengl/gl_font_atlas.h index a5ec6e6..1e2f881 100644 --- a/include/graphics/opengl/gl_font_atlas.h +++ b/include/graphics/opengl/gl_font_atlas.h @@ -1,8 +1,9 @@ #pragma once #include -#include +#include #include +#include #include #include #include diff --git a/include/graphics/opengl/gl_sprite_batch.h b/include/graphics/opengl/gl_sprite_batch.h index 9ccfdc9..e63d080 100644 --- a/include/graphics/opengl/gl_sprite_batch.h +++ b/include/graphics/opengl/gl_sprite_batch.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include #include #include #include diff --git a/include/graphics/texture.h b/include/graphics/texture.h index 45b2fed..41768be 100644 --- a/include/graphics/texture.h +++ b/include/graphics/texture.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include namespace extra2d { diff --git a/include/graphics/texture_atlas.h b/include/graphics/texture_atlas.h index b9aab35..1111015 100644 --- a/include/graphics/texture_atlas.h +++ b/include/graphics/texture_atlas.h @@ -1,8 +1,9 @@ #pragma once #include -#include +#include #include +#include #include #include #include diff --git a/include/platform/input.h b/include/platform/input.h index 22a706b..2aea1c7 100644 --- a/include/platform/input.h +++ b/include/platform/input.h @@ -1,8 +1,8 @@ #pragma once #include -#include #include +#include #include #include diff --git a/include/platform/window.h b/include/platform/window.h index 3a1118a..b99912f 100644 --- a/include/platform/window.h +++ b/include/platform/window.h @@ -1,8 +1,9 @@ #pragma once -#include +#include #include #include +#include #include #include diff --git a/include/renderer/camera.h b/include/renderer/camera.h index eaf859f..0bd3a54 100644 --- a/include/renderer/camera.h +++ b/include/renderer/camera.h @@ -1,8 +1,10 @@ #pragma once #include -#include +#include +#include #include +#include #include namespace extra2d { diff --git a/include/renderer/render_command.h b/include/renderer/render_command.h index 43ec081..6f7521c 100644 --- a/include/renderer/render_command.h +++ b/include/renderer/render_command.h @@ -1,8 +1,9 @@ #pragma once #include -#include +#include #include +#include #include #include #include diff --git a/include/renderer/renderer.h b/include/renderer/renderer.h index 556bfe2..f0b28e0 100644 --- a/include/renderer/renderer.h +++ b/include/renderer/renderer.h @@ -1,8 +1,10 @@ #pragma once #include -#include +#include +#include #include +#include #include namespace extra2d { diff --git a/include/scene/node.h b/include/scene/node.h index 532dd26..31693b1 100644 --- a/include/scene/node.h +++ b/include/scene/node.h @@ -2,8 +2,9 @@ #include #include -#include +#include #include +#include #include #include #include diff --git a/include/scene/shape.h b/include/scene/shape.h index 0763833..4f56957 100644 --- a/include/scene/shape.h +++ b/include/scene/shape.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include diff --git a/include/ui/widget.h b/include/ui/widget.h index 23a540e..e66ed0c 100644 --- a/include/ui/widget.h +++ b/include/ui/widget.h @@ -1,7 +1,8 @@ #pragma once -#include +#include #include +#include #include #include diff --git a/src/event/event_dispatcher.cpp b/src/event/event_dispatcher.cpp index 740c889..5771e15 100644 --- a/src/event/event_dispatcher.cpp +++ b/src/event/event_dispatcher.cpp @@ -1,3 +1,4 @@ +#include #include #include