Marked many functions of point and rect constexpr
This commit is contained in:
parent
6876695e66
commit
965f3049c6
2 changed files with 53 additions and 30 deletions
|
@ -24,74 +24,74 @@
|
|||
struct point : SDL_Point
|
||||
{
|
||||
/** Initialize to 0 by default. */
|
||||
point() : SDL_Point{0, 0} {}
|
||||
constexpr point() : SDL_Point{0, 0} {}
|
||||
|
||||
point(int x, int y) : SDL_Point{x, y} {}
|
||||
constexpr point(int x, int y) : SDL_Point{x, y} {}
|
||||
|
||||
point(const SDL_Point& p) : SDL_Point{p} {}
|
||||
constexpr point(const SDL_Point& p) : SDL_Point{p} {}
|
||||
|
||||
bool operator==(const point& point) const
|
||||
constexpr bool operator==(const point& point) const
|
||||
{
|
||||
return x == point.x && y == point.y;
|
||||
}
|
||||
|
||||
bool operator!=(const point& point) const
|
||||
constexpr bool operator!=(const point& point) const
|
||||
{
|
||||
return !operator==(point);
|
||||
}
|
||||
|
||||
bool operator<(const point& point) const
|
||||
constexpr bool operator<(const point& point) const
|
||||
{
|
||||
return std::tie(x, y) < std::tie(point.x, point.y);
|
||||
}
|
||||
|
||||
bool operator<=(const point& point) const
|
||||
constexpr bool operator<=(const point& point) const
|
||||
{
|
||||
return x < point.x || (x == point.x && y <= point.y);
|
||||
}
|
||||
|
||||
point operator+(const point& point) const
|
||||
constexpr point operator+(const point& point) const
|
||||
{
|
||||
return {x + point.x, y + point.y};
|
||||
}
|
||||
|
||||
point& operator+=(const point& point)
|
||||
constexpr point& operator+=(const point& point)
|
||||
{
|
||||
x += point.x;
|
||||
y += point.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
point operator-(const point& point) const
|
||||
constexpr point operator-(const point& point) const
|
||||
{
|
||||
return {x - point.x, y - point.y};
|
||||
}
|
||||
|
||||
point& operator-=(const point& point)
|
||||
constexpr point& operator-=(const point& point)
|
||||
{
|
||||
x -= point.x;
|
||||
y -= point.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
point operator*(int s) const
|
||||
constexpr point operator*(int s) const
|
||||
{
|
||||
return {x * s, y * s};
|
||||
}
|
||||
|
||||
point& operator*=(int s)
|
||||
constexpr point& operator*=(int s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
point operator/(int s) const
|
||||
constexpr point operator/(int s) const
|
||||
{
|
||||
return {x / s, y / s};
|
||||
}
|
||||
|
||||
point& operator/=(int s)
|
||||
constexpr point& operator/=(int s)
|
||||
{
|
||||
x /= s;
|
||||
y /= s;
|
||||
|
@ -100,24 +100,24 @@ struct point : SDL_Point
|
|||
|
||||
// Multiplication and division of points works elementwise.
|
||||
|
||||
point operator*(const point& p) const
|
||||
constexpr point operator*(const point& p) const
|
||||
{
|
||||
return {x * p.x, y * p.y};
|
||||
}
|
||||
|
||||
point& operator*=(const point& p)
|
||||
constexpr point& operator*=(const point& p)
|
||||
{
|
||||
x *= p.x;
|
||||
y *= p.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
point operator/(const point& p) const
|
||||
constexpr point operator/(const point& p) const
|
||||
{
|
||||
return {x / p.x, y / p.y};
|
||||
}
|
||||
|
||||
point& operator/=(const point& p)
|
||||
constexpr point& operator/=(const point& p)
|
||||
{
|
||||
x /= p.x;
|
||||
y /= p.y;
|
||||
|
|
|
@ -47,35 +47,58 @@ struct rect : SDL_Rect
|
|||
{
|
||||
public:
|
||||
/** Explicitly initialize rects to 0. */
|
||||
rect() : SDL_Rect{0, 0, 0, 0} {}
|
||||
constexpr rect() : SDL_Rect{0, 0, 0, 0} {}
|
||||
|
||||
/** There's nothing extra when converting an SDL_Rect. */
|
||||
rect(const SDL_Rect& r) : SDL_Rect{r} {}
|
||||
constexpr rect(const SDL_Rect& r) : SDL_Rect{r} {}
|
||||
|
||||
/** Specify via (x, y, w, h). */
|
||||
rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} {}
|
||||
constexpr rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} {}
|
||||
|
||||
/** Specify via top-left corner position and size. */
|
||||
rect(const point& pos, const point& size)
|
||||
constexpr rect(const point& pos, const point& size)
|
||||
: SDL_Rect{pos.x, pos.y, size.x, size.y}
|
||||
{}
|
||||
|
||||
// subcomponent access
|
||||
point pos() const { return {x, y}; }
|
||||
point size() const { return {w, h}; }
|
||||
constexpr point pos() const { return {x, y}; }
|
||||
constexpr point size() const { return {w, h}; }
|
||||
|
||||
// Comparisons
|
||||
bool operator==(const rect& r) const;
|
||||
bool operator==(const SDL_Rect& r) const;
|
||||
|
||||
// Scalar multiplication and division
|
||||
rect operator*(int s) const { return {x*s, y*s, w*s, h*s}; }
|
||||
rect& operator*=(int s) { x*=s; y*=s; w*=s; h*=s; return *this; }
|
||||
rect operator/(int s) const { return {x/s, y/s, w/s, h/s}; }
|
||||
rect& operator/=(int s) { x/=s; y/=s; w/=s; h/=s; return *this; }
|
||||
constexpr rect operator*(int s) const
|
||||
{
|
||||
return {x * s, y * s, w * s, h * s};
|
||||
}
|
||||
|
||||
constexpr rect& operator*=(int s)
|
||||
{
|
||||
x *= s;
|
||||
y *= s;
|
||||
w *= s;
|
||||
h *= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr rect operator/(int s) const
|
||||
{
|
||||
return {x / s, y / s, w / s, h / s};
|
||||
}
|
||||
|
||||
constexpr rect& operator/=(int s)
|
||||
{
|
||||
x /= s;
|
||||
y /= s;
|
||||
w /= s;
|
||||
h /= s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** The area of this rectangle, in square pixels. */
|
||||
int area() const { return w * h; }
|
||||
constexpr int area() const { return w * h; }
|
||||
|
||||
/** False if both w and h are > 0, true otherwise. */
|
||||
bool empty() const;
|
||||
|
|
Loading…
Add table
Reference in a new issue