Skip to content

Commit

Permalink
more vector operations
Browse files Browse the repository at this point in the history
improve font documentation
  • Loading branch information
durkisneer1 committed Dec 1, 2024
1 parent 1bed81c commit 3d5fed0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 24 deletions.
12 changes: 8 additions & 4 deletions include/Font.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
64 changes: 44 additions & 20 deletions include/Math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,53 @@ class Vec2

template <typename T> Vec2 operator/(T scalar) const { return {x / scalar, y / scalar}; }

template <typename T> Vec2& operator/=(T scalar)
{
x /= scalar;
y /= scalar;

return *this;
}

template <typename T> Vec2 operator*(const T& lhs, const Vec2& rhs)
{
if (!isProductValid(static_cast<double>(lhs), rhs.x) ||
!isProductValid(static_cast<double>(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 <typename T> Vec2 operator*(const Vec2& lhs, const T& rhs) { return rhs * lhs; }

template <typename T> 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;
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -238,22 +279,5 @@ double angleBetween(const Vec2& a, const Vec2& b);
*/
double angleOfDifference(const Vec2& a, const Vec2& b);

template <typename T> Vec2 operator*(const T& lhs, const Vec2& rhs)
{
if (!isProductValid(static_cast<double>(lhs), rhs.x) ||
!isProductValid(static_cast<double>(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 <typename T> Vec2 operator*(const Vec2& lhs, const T& rhs) { return rhs * lhs; }

} // namespace math
} // namespace kn
8 changes: 8 additions & 0 deletions src/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 3d5fed0

Please sign in to comment.