From d2326958e1239244e8b3ee011ba5894d6b3bc079 Mon Sep 17 00:00:00 2001 From: Vankata453 <78196474+Vankata453@users.noreply.github.com> Date: Fri, 4 Aug 2023 21:37:43 +0300 Subject: [PATCH] Make sure `Rectf` width and height never go below 0 Previously, assertion in two of the initializers of `Rectf` was put in place, which checked if the rectangle has been initialized with negative width or height. It was discovered that the assertion causes problems. If a hitbox has a width or height of 0, and is grown with a negative value as `border`. The newly initialized `Rectf` in the function would have negative width or height, so the game crashes on Debug builds, because of the assertions put in place. The checks are now performed using `if` statements, and if the width or height go below 0, they are set to 0. Additionally, these checks are also implemented in the width, height and size setters of `Rectf`. --- src/math/rectf.cpp | 18 +++++++++++++++++- src/math/rectf.hpp | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/math/rectf.cpp b/src/math/rectf.cpp index 1e89e9ad719..d6008545b53 100644 --- a/src/math/rectf.cpp +++ b/src/math/rectf.cpp @@ -22,10 +22,26 @@ Rectf::Rectf(const Rect& rect) : m_p1(static_cast(rect.left), - static_cast(rect.top)), + static_cast(rect.top)), m_size(static_cast(rect.get_width()), static_cast(rect.get_height())) { + initialize(); +} + +void +Rectf::initialize() +{ + if (m_size.width < 0.f) + { + log_warning << "Attempted to initialize with negative width: " << m_size.width << ". Setting to 0." << std::endl; + m_size.width = 0.f; + } + if (m_size.height < 0.f) + { + log_warning << "Attempted to initialize with negative height: " << m_size.height << ". Setting to 0." << std::endl; + m_size.height = 0.f; + } } std::ostream& operator<<(std::ostream& out, const Rectf& rect) diff --git a/src/math/rectf.hpp b/src/math/rectf.hpp index d171e3ce597..98775645e0d 100644 --- a/src/math/rectf.hpp +++ b/src/math/rectf.hpp @@ -23,6 +23,7 @@ #include "math/anchor_point.hpp" #include "math/sizef.hpp" #include "math/vector.hpp" +#include "util/log.hpp" class Rect; @@ -37,6 +38,9 @@ class Rectf final center.y + size.height / 2.0f); } +private: + void initialize(); + public: Rectf() : m_p1(0.0f, 0.0f), @@ -49,21 +53,20 @@ class Rectf final Rectf(const Vector& np1, const Vector& np2) : m_p1(np1), m_size(np2.x - np1.x, np2.y - np1.y) { - assert(m_size.width >= 0 && - m_size.height >= 0); + initialize(); } Rectf(float x1, float y1, float x2, float y2) : m_p1(x1, y1), m_size(x2 - x1, y2 - y1) { - assert(m_size.width >= 0 && - m_size.height >= 0); + initialize(); } Rectf(const Vector& p1, const Sizef& size) : m_p1(p1), m_size(size) { + initialize(); } Rectf(const Rect& rect); @@ -97,9 +100,29 @@ class Rectf final void set_pos(const Vector& v) { m_p1 = v; } - void set_width(float width) { m_size.width = width; } - void set_height(float height) { m_size.height = height; } - void set_size(float width, float height) { m_size = Sizef(width, height); } + void set_width(float width) + { + if (width < 0.f) + { + log_warning << "Attempted to set width to negative value: " << width << ". Setting to 0." << std::endl; + width = 0.f; + } + m_size.width = width; + } + void set_height(float height) + { + if (height < 0.f) + { + log_warning << "Attempted to set height to negative value: " << height << ". Setting to 0." << std::endl; + height = 0.f; + } + m_size.height = height; + } + void set_size(float width, float height) + { + set_width(width); + set_height(height); + } Sizef get_size() const { return m_size; } void move(const Vector& v) { m_p1 += v; }