From 3d5fed01f3d3887ce02820fc015fb2e42be31284 Mon Sep 17 00:00:00 2001 From: durk Date: Sun, 1 Dec 2024 15:07:47 -0600 Subject: [PATCH] more vector operations improve font documentation --- include/Font.hpp | 12 ++++++--- include/Math.hpp | 64 +++++++++++++++++++++++++++++++++--------------- src/math.cpp | 8 ++++++ 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/include/Font.hpp b/include/Font.hpp index 692378a..6bf7cb7 100644 --- a/include/Font.hpp +++ b/include/Font.hpp @@ -16,7 +16,7 @@ class Font final { public: /** - * @brief Create a font. + * @brief Create a font object. * * @param fileDir The directory of the font file. * @param ptSize The point size of the font. @@ -29,16 +29,20 @@ class Font final } /** - * @brief Render text. + * @brief Generate a texture with rendered text. * * @param text The text to render. * @param antialias Whether to antialias the text. * @param color The color of the text. * @param wrapLength The length to wrap the text. + * If 0, the text will not be wrapped. * - * @return The rendered text. + * @return The generated texture object. + * + * @note Do not confuse this with rendering text to the screen. */ - [[nodiscard]] Texture render(const std::string& text, bool antialias, Color color, int wrapLength = 0) const; + [[nodiscard]] Texture render(const std::string& text, bool antialias, Color color, + int wrapLength = 0) const; private: TTF_Font* font; diff --git a/include/Math.hpp b/include/Math.hpp index 3265f5e..1a4469a 100644 --- a/include/Math.hpp +++ b/include/Math.hpp @@ -106,12 +106,53 @@ class Vec2 template Vec2 operator/(T scalar) const { return {x / scalar, y / scalar}; } + template Vec2& operator/=(T scalar) + { + x /= scalar; + y /= scalar; + + return *this; + } + + template Vec2 operator*(const T& lhs, const Vec2& rhs) + { + if (!isProductValid(static_cast(lhs), rhs.x) || + !isProductValid(static_cast(lhs), rhs.y)) + { + WARN("Multiplication would result in overflow") + return {}; + } + + const double x = lhs * rhs.x; + const double y = lhs * rhs.y; + + return {x, y}; + } + + template Vec2 operator*(const Vec2& lhs, const T& rhs) { return rhs * lhs; } + + template Vec2& operator*=(T scalar) + { + if (!isProductValid(x, scalar) || !isProductValid(y, scalar)) + { + WARN("Multiplication would result in overflow") + return *this; + } + + x *= scalar; + y *= scalar; + + return *this; + } + Vec2 operator+(const Vec2& other) const; Vec2 operator-(const Vec2& other) const; Vec2& operator+=(const Vec2& other); + Vec2& operator-=(const Vec2& other); + bool operator==(const Vec2& other) const; bool operator!=(const Vec2& other) const; @@ -143,7 +184,7 @@ class Vec2 * @param radius The radius. * @return Returns a vector from its polar representation. */ -Vec2 fromPolar(double angle, double radius); +[[nodiscard]] Vec2 fromPolar(double angle, double radius); /** * @brief Get a normalized transformed vector from the given vector. @@ -160,7 +201,7 @@ Vec2 fromPolar(double angle, double radius); * @param max The maximum value. * @return The clamped vector. */ -Vec2 clampVec(const Vec2& vec, const Vec2& min, const Vec2& max); +[[nodiscard]] Vec2 clampVec(const Vec2& vec, const Vec2& min, const Vec2& max); /** * @brief Linearly interpolate a vector. @@ -169,7 +210,7 @@ Vec2 clampVec(const Vec2& vec, const Vec2& min, const Vec2& max); * @param t The time in seconds. * @return The interpolated vector. */ -Vec2 lerpVec(const Vec2& a, const Vec2& b, double t); +[[nodiscard]] Vec2 lerpVec(const Vec2& a, const Vec2& b, double t); /** * @brief Linear interpolate a value @@ -238,22 +279,5 @@ double angleBetween(const Vec2& a, const Vec2& b); */ double angleOfDifference(const Vec2& a, const Vec2& b); -template Vec2 operator*(const T& lhs, const Vec2& rhs) -{ - if (!isProductValid(static_cast(lhs), rhs.x) || - !isProductValid(static_cast(lhs), rhs.y)) - { - WARN("Multiplication would result in overflow") - return {}; - } - - const double x = lhs * rhs.x; - const double y = lhs * rhs.y; - - return {x, y}; -} - -template Vec2 operator*(const Vec2& lhs, const T& rhs) { return rhs * lhs; } - } // namespace math } // namespace kn diff --git a/src/math.cpp b/src/math.cpp index e32033c..7400661 100644 --- a/src/math.cpp +++ b/src/math.cpp @@ -173,6 +173,14 @@ Vec2& Vec2::operator+=(const Vec2& other) return *this; } +Vec2& Vec2::operator-=(const Vec2& other) +{ + x -= other.x; + y -= other.y; + + return *this; +} + bool Vec2::operator==(const Vec2& other) const { if (!isSumValid(x, -other.x) || !isSumValid(y, -other.y))