25 #include <SDL2/SDL_rect.h>
36 bool operator==(
const SDL_Rect& a,
const SDL_Rect&
b);
37 bool operator!=(
const SDL_Rect& a,
const SDL_Rect&
b);
52 constexpr
rect() : SDL_Rect{0, 0, 0, 0} {}
55 constexpr
rect(
const SDL_Rect& r) : SDL_Rect{r} {}
58 constexpr
rect(
int x,
int y,
int w,
int h) : SDL_Rect{x, y,
w,
h} {}
62 : SDL_Rect{pos.x, pos.y,
size.x,
size.y}
76 return {x *
s, y *
s,
w *
s,
h *
s};
90 return {x /
s, y /
s,
w /
s,
h /
s};
103 constexpr
int area()
const {
return w *
h; }
108 return {x +
w / 2, y +
h / 2};
119 bool contains(
const SDL_Rect& r)
const;
122 bool overlaps(
const SDL_Rect& r)
const;
144 void clip(
const SDL_Rect& r);
160 return { x - dx, y - dy,
w + dx * 2,
h + dy * 2 };
177 #ifdef __cpp_concepts
179 concept Rectangle = requires(T r)
188 constexpr SDL_FRect
precise_subrect(
const Rectangle
auto& base,
const SDL_FPoint& tl,
const SDL_FPoint&
br)
190 template<
typename Rect>
191 constexpr SDL_FRect
precise_subrect(
const Rect& base,
const SDL_FPoint& tl,
const SDL_FPoint&
br)
194 const auto point_at = [&base](
auto x,
auto y) -> SDL_FPoint {
196 base.x + base.w * std::clamp(x, 0.0f, 1.0f),
197 base.y + base.h * std::clamp(y, 0.0f, 1.0f)
201 SDL_FPoint p1 = point_at(tl.x, tl.y);
202 SDL_FPoint p2 = point_at(
br.x,
br.y);
204 return { p1.x, p1.y, p2.x - p1.x, p2.y - p1.y };
constexpr std::string_view br
A Help markup tag corresponding to a linebreak.
constexpr const SDL_Rect empty_rect
constexpr SDL_FRect precise_subrect(const Rect &base, const SDL_FPoint &tl, const SDL_FPoint &br)
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
std::ostream & operator<<(std::ostream &s, const SDL_Rect &rect)
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
An abstract description of a rectangle with integer coordinates.
constexpr point center() const
The center point of the rectangle, accounting for origin.
bool empty() const
False if both w and h are > 0, true otherwise.
rect & expand_to_cover(const SDL_Rect &r)
Minimally expand this rect to fully contain another.
rect shifted_by(int x, int y) const
Returns a new rectangle shifted by the given relative position.
constexpr point origin() const
constexpr rect(const SDL_Rect &r)
There's nothing extra when converting an SDL_Rect.
point point_at(double x, double y) const
Returns the proper point that corresponds to the given [0.0, 1.0] coordinates.
constexpr rect & operator*=(int s)
rect minimal_cover(const SDL_Rect &r) const
Calculates the minimal rectangle that completely contains both this rectangle and the given rectangle...
constexpr rect(int x, int y, int w, int h)
Specify via (x, y, w, h).
constexpr rect operator/(int s) const
constexpr rect()
Explicitly initialize rects to 0.
bool contains(int x, int y) const
Whether the given point lies within the rectangle.
constexpr rect padded_by(int amount) const
Returns a new rectangle with equal amount horizontal and vertical padding.
bool operator==(const rect &r) const
constexpr rect padded_by(int dx, int dy) const
Returns a new rectangle with dx horizontal padding and dy vertical padding.
constexpr rect(const point &pos, const point &size)
Specify via top-left corner position and size.
void clip(const SDL_Rect &r)
Clip this rectangle by the given rectangle.
constexpr int area() const
The area of this rectangle, in square pixels.
constexpr rect & operator/=(int s)
constexpr point size() const
void shift(const point &p)
Shift the rectangle by the given relative position.
constexpr rect operator*(int s) const
rect intersect(const SDL_Rect &r) const
Calculates the intersection of this rectangle and another; that is, the maximal rectangle that is con...
bool overlaps(const SDL_Rect &r) const
Whether the given rectangle and this rectangle overlap.
static map_location::direction s