Skip to content

Commit

Permalink
Make sure Rectf width and height never go below 0
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Vankata453 committed Aug 4, 2023
1 parent ecb9350 commit d232695
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
18 changes: 17 additions & 1 deletion src/math/rectf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,26 @@

Rectf::Rectf(const Rect& rect) :
m_p1(static_cast<float>(rect.left),
static_cast<float>(rect.top)),
static_cast<float>(rect.top)),
m_size(static_cast<float>(rect.get_width()),
static_cast<float>(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)
Expand Down
37 changes: 30 additions & 7 deletions src/math/rectf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "math/anchor_point.hpp"
#include "math/sizef.hpp"
#include "math/vector.hpp"
#include "util/log.hpp"

class Rect;

Expand All @@ -37,6 +38,9 @@ class Rectf final
center.y + size.height / 2.0f);
}

private:
void initialize();

public:
Rectf() :
m_p1(0.0f, 0.0f),
Expand All @@ -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);
Expand Down Expand Up @@ -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; }
Expand Down

0 comments on commit d232695

Please sign in to comment.