libSDL2pp
C++11 bindings/wrapper for SDL2
Rect.hh
1/*
2 libSDL2pp - C++11 bindings/wrapper for SDL2
3 Copyright (C) 2013-2015 Dmitry Marakasov <amdmi3@amdmi3.ru>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef SDL2PP_RECT_HH
23#define SDL2PP_RECT_HH
24
25#include <functional>
26
27#include <SDL_rect.h>
28
29#include <SDL2pp/Optional.hh>
30#include <SDL2pp/Point.hh>
31#include <SDL2pp/Export.hh>
32
33namespace SDL2pp {
34
50class SDL2PP_EXPORT Rect : public SDL_Rect {
51public:
58 constexpr Rect() : SDL_Rect{0, 0, 0, 0} {
59 }
60
67 constexpr Rect(const SDL_Rect& rect) : SDL_Rect{rect.x, rect.y, rect.w, rect.h} {
68 }
69
77 constexpr Rect(const Point& corner, const Point& size) : SDL_Rect{corner.x, corner.y, size.x, size.y} {
78 }
79
89 constexpr Rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} {
90 }
91
101 static constexpr Rect FromCenter(int cx, int cy, int w, int h) {
102 return Rect(cx - w/2, cy - h/2, w, h);
103 }
104
112 static constexpr Rect FromCenter(const Point& center, const Point& size) {
113 return Rect(center - size / 2, size);
114 }
115
125 static constexpr Rect FromCorners(int x1, int y1, int x2, int y2) {
126 return Rect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
127 }
128
136 static constexpr Rect FromCorners(const Point& p1, const Point& p2) {
137 return Rect(p1, p2 - p1 + Point(1, 1));
138 }
139
144 Rect(const Rect&) noexcept = default;
145
150 Rect(Rect&&) noexcept = default;
151
158 Rect& operator=(const Rect&) noexcept = default;
159
166 Rect& operator=(Rect&&) noexcept = default;
167
174 constexpr int GetX() const {
175 return x;
176 }
177
186 Rect& SetX(int nx) {
187 x = nx;
188 return *this;
189 }
190
197 constexpr int GetY() const {
198 return y;
199 }
200
209 Rect& SetY(int ny) {
210 y = ny;
211 return *this;
212 }
213
220 constexpr int GetW() const {
221 return w;
222 }
223
232 Rect& SetW(int nw) {
233 w = nw;
234 return *this;
235 }
236
243 constexpr int GetH() const {
244 return h;
245 }
246
255 Rect& SetH(int nh) {
256 h = nh;
257 return *this;
258 }
259
266 constexpr int GetX2() const {
267 return x + w - 1;
268 }
269
280 Rect& SetX2(int x2) {
281 w = x2 - x + 1;
282 return *this;
283 }
284
291 constexpr int GetY2() const {
292 return y + h - 1;
293 }
294
305 Rect& SetY2(int y2) {
306 h = y2 - y + 1;
307 return *this;
308 }
309
316 constexpr Point GetTopLeft() const {
317 return Point(x, y);
318 }
319
326 constexpr Point GetTopRight() const {
327 return Point(GetX2(), y);
328 }
329
336 constexpr Point GetBottomLeft() const {
337 return Point(x, GetY2());
338 }
339
346 constexpr Point GetBottomRight() const {
347 return Point(GetX2(), GetY2());
348 }
349
356 constexpr Point GetSize() const {
357 return Point(w, h);
358 }
359
366 constexpr Point GetCentroid() const {
367 return Point(x + w/2, y + h/2);
368 }
369
379 constexpr bool Contains(int px, int py) const {
380 return px >= x && py >= y && px <= GetX2() && py <= GetY2();
381 }
382
391 constexpr bool Contains(const Point& point) const {
392 return point.x >= x && point.y >= y && point.x <= GetX2() && point.y <= GetY2();
393 }
394
403 constexpr bool Contains(const Rect& rect) const {
404 return rect.x >= x && rect.y >= y && rect.GetX2() <= GetX2() && rect.GetY2() <= GetY2();
405 }
406
415 constexpr bool Intersects(const Rect& rect) const {
416 return !(rect.GetX2() < x || rect.GetY2() < y || rect.x > GetX2() || rect.y > GetY2());
417 }
418
427 Rect GetUnion(const Rect& rect) const;
428
437 Rect& Union(const Rect& rect);
438
447 Rect GetExtension(unsigned int amount) const;
448
460 Rect GetExtension(unsigned int hamount, unsigned int vamount) const;
461
470 Rect& Extend(unsigned int amount);
471
483 Rect& Extend(unsigned int hamount, unsigned int vamount);
484
493 Optional<Rect> GetIntersection(const Rect& rect) const;
494
514 bool IntersectLine(int& x1, int& y1, int& x2, int& y2) const;
515
532 bool IntersectLine(Point& p1, Point& p2) const;
533
542 constexpr Rect operator+(const Point& offset) const {
543 return Rect(x + offset.x, y + offset.y, w, h);
544 }
545
554 constexpr Rect operator-(const Point& offset) const {
555 return Rect(x - offset.x, y - offset.y, w, h);
556 }
557
566 Rect& operator+=(const Point& offset) {
567 x += offset.x;
568 y += offset.y;
569 return *this;
570 }
571
580 Rect& operator-=(const Point& offset) {
581 x -= offset.x;
582 y -= offset.y;
583 return *this;
584 }
585};
586
587}
588
598constexpr bool operator==(const SDL2pp::Rect& a, const SDL2pp::Rect& b) {
599 return a.x == b.x && a.y == b.y && a.w == b.w && a.h == b.h;
600}
601
611constexpr bool operator!=(const SDL2pp::Rect& a, const SDL2pp::Rect& b) {
612 return !(a == b);
613}
614
624SDL2PP_EXPORT bool operator<(const SDL2pp::Rect& a, const SDL2pp::Rect& b);
625
635SDL2PP_EXPORT std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect);
636
637namespace std {
638
643template<>
644struct hash<SDL2pp::Rect> {
653 size_t operator()(const SDL2pp::Rect& r) const {
654 size_t seed = std::hash<int>()(r.x);
655 seed ^= std::hash<int>()(r.y) + 0x9e3779b9 + (seed<<6) + (seed>>2);
656 seed ^= std::hash<int>()(r.w) + 0x9e3779b9 + (seed<<6) + (seed>>2);
657 seed ^= std::hash<int>()(r.h) + 0x9e3779b9 + (seed<<6) + (seed>>2);
658 return seed;
659 }
660};
661
662}
663
664#endif
2D point
Definition: Point.hh:51
2D rectangle
Definition: Rect.hh:50
static constexpr Rect FromCorners(int x1, int y1, int x2, int y2)
Construct the rect from given corners coordinates.
Definition: Rect.hh:125
Rect(Rect &&) noexcept=default
Move constructor.
Rect & operator+=(const Point &offset)
Move by then given offset.
Definition: Rect.hh:566
constexpr int GetY() const
Get Y coordinate of the rect corner.
Definition: Rect.hh:197
Rect & operator-=(const Point &offset)
Move by an opposite of the given offset.
Definition: Rect.hh:580
constexpr bool Intersects(const Rect &rect) const
Check whether the rect intersects another rect.
Definition: Rect.hh:415
Rect(const Rect &) noexcept=default
Copy constructor.
constexpr Point GetTopRight() const
Get top right corner of the rect.
Definition: Rect.hh:326
Rect & SetW(int nw)
Set width of the rect.
Definition: Rect.hh:232
constexpr Rect operator+(const Point &offset) const
Get rectangle moved by a given offset.
Definition: Rect.hh:542
Rect & SetH(int nh)
Set height of the rect.
Definition: Rect.hh:255
constexpr Point GetBottomLeft() const
Get bottom left corner of the rect.
Definition: Rect.hh:336
constexpr int GetY2() const
Get Y coordinate of the rect second corner.
Definition: Rect.hh:291
static constexpr Rect FromCenter(const Point &center, const Point &size)
Construct the rect from given center coordinates and size.
Definition: Rect.hh:112
constexpr Point GetCentroid() const
Get centroid of the rect.
Definition: Rect.hh:366
Rect & SetY(int ny)
Set Y coordinate of the rect corner.
Definition: Rect.hh:209
static constexpr Rect FromCorners(const Point &p1, const Point &p2)
Construct the rect from given centers coordinates.
Definition: Rect.hh:136
Rect & SetX(int nx)
Set X coordinate of the rect corner.
Definition: Rect.hh:186
constexpr int GetH() const
Get height of the rect.
Definition: Rect.hh:243
constexpr Rect(int x, int y, int w, int h)
Construct the rect from given corner coordinates, width and height.
Definition: Rect.hh:89
constexpr bool Contains(const Rect &rect) const
Check whether the rect contains another rect.
Definition: Rect.hh:403
constexpr Rect(const SDL_Rect &rect)
Construct a rect from existing SDL_Rect.
Definition: Rect.hh:67
Rect & SetX2(int x2)
Set X coordinate of the rect second corner.
Definition: Rect.hh:280
constexpr Point GetSize() const
Get size of the rect.
Definition: Rect.hh:356
constexpr Point GetBottomRight() const
Get bottom right corner of the rect.
Definition: Rect.hh:346
constexpr Rect operator-(const Point &offset) const
Get rectangle moved by an opposite of given offset.
Definition: Rect.hh:554
constexpr int GetX2() const
Get X coordinate of the rect second corner.
Definition: Rect.hh:266
constexpr Point GetTopLeft() const
Get top left corner of the rect.
Definition: Rect.hh:316
constexpr int GetW() const
Get width of the rect.
Definition: Rect.hh:220
constexpr bool Contains(int px, int py) const
Check whether the rect contains given point.
Definition: Rect.hh:379
Rect & SetY2(int y2)
Set Y coordinate of the rect second corner.
Definition: Rect.hh:305
constexpr Rect(const Point &corner, const Point &size)
Construct the rect from given corner coordinates, and size.
Definition: Rect.hh:77
constexpr Rect()
Default constructor.
Definition: Rect.hh:58
static constexpr Rect FromCenter(int cx, int cy, int w, int h)
Construct the rect from given center coordinates, width and height.
Definition: Rect.hh:101
constexpr bool Contains(const Point &point) const
Check whether the rect contains given point.
Definition: Rect.hh:391
size_t operator()(const SDL2pp::Rect &r) const
Hash function for SDL2pp::Rect.
Definition: Rect.hh:653