cpp-sdl2
C++ header-only SDL2 wrapper
rect.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <SDL_rect.h>
4 #include <algorithm>
5 
6 #include "vec2.hpp"
7 
8 namespace sdl
9 {
11 class Rect : public SDL_Rect
12 {
13 public:
15  constexpr Rect() : SDL_Rect{0, 0, 0, 0} {}
16 
22  constexpr Rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} {}
23 
27  constexpr Rect(Vec2i const& corner, Vec2i const& size)
28  : SDL_Rect{corner.x, corner.y, size.x, size.y}
29  {
30  }
31 
33  constexpr Rect(SDL_Rect const& r) : SDL_Rect{r} {}
34 
36  Rect(Rect const&) noexcept = default;
37 
39  Rect(Rect&&) noexcept = default;
40 
46  static constexpr Rect from_center(int cx, int cy, int w, int h)
47  {
48  return Rect{cx - w / 2, cy - h / 2, w, h};
49  }
50 
53  static constexpr Rect from_center(Vec2i const& center, Vec2i const& size)
54  {
55  return Rect{center - size / 2, size};
56  }
57 
63  static constexpr Rect from_corners(int x1, int y1, int x2, int y2)
64  {
65  return Rect(x1, y1, x2 - x1, y2 - y1);
66  }
67 
71  static constexpr Rect from_corners(Vec2i const& corner1, Vec2i const& corner2)
72  {
73  return Rect(corner1.x, corner1.y, corner2.x - corner1.x, corner2.y - corner1.y);
74  }
75 
77  Rect& operator=(Rect const&) noexcept = default;
79  Rect& operator=(Rect&&) noexcept = default;
80 
82  bool operator==(Rect const& other) const { return SDL_RectEquals(this, &other); }
83 
85  constexpr int x1() const { return x; }
87  constexpr int x2() const { return x + w; }
89  constexpr int y1() const { return y; }
91  constexpr int y2() const { return y + h; }
92 
94  Vec2i botleft() const { return Vec2i{x1(), y1()}; }
96  Vec2i botright() const { return Vec2i{x2(), y1()}; }
98  Vec2i topleft() const { return Vec2i{x1(), y2()}; }
100  Vec2i topright() const { return Vec2i{x2(), y2()}; }
101 
103  Vec2i size() const { return Vec2i{w, h}; }
105  Vec2i center() const { return Vec2i{x + w / 2, y + h / 2}; }
106 
108  bool is_empty() const { return SDL_RectEmpty(this); }
109 
113  bool contains(int px, int py) const
114  {
115  return px >= x1() && px < x2() && py >= y1() && py < y2();
116  }
117 
120  bool contains(Vec2i const& point) const { return contains(point.x, point.y); }
121 
123  bool intersects(Rect const& r) const
124  {
125  return x1() < r.x2() && x2() > r.x1() && y1() < r.y2() && y2() > r.y1();
126  }
127 
129  bool intersects(Vec2i const& p1, Vec2i const& p2) const
130  {
131  /* Even if SDL_IntersectRectAndLine don't modify it's arguments,
132  it doesn't use const* of int */
133  auto p1mut = const_cast<Vec2i&>(p1);
134  auto p2mut = const_cast<Vec2i&>(p2);
135 
136  return SDL_IntersectRectAndLine(this, &p1mut.x, &p1mut.y, &p2mut.x, &p2mut.y);
137  }
138 
140  Rect inter(Rect const& r) const
141  {
142  Rect tmp;
143  SDL_IntersectRect(this, &r, &tmp);
144  return tmp;
145  }
146 
147  // TODO : find a way to name this ...
149  Rect get_union(Rect const& r) const
150  {
151  Rect tmp;
152  SDL_UnionRect(this, &r, &tmp);
153  return tmp;
154  }
155 };
156 
157 } // namespace sdl
Vec2i topright() const
Get the top right corner position.
Definition: rect.hpp:100
bool contains(int px, int py) const
Return true if this rect contains the given point.
Definition: rect.hpp:113
constexpr int y1() const
Return the &#39;min Y&#39; position of the Rect.
Definition: rect.hpp:89
bool intersects(Vec2i const &p1, Vec2i const &p2) const
Return true if this rect intersect the line.
Definition: rect.hpp:129
Rect get_union(Rect const &r) const
Return the union of the two rects.
Definition: rect.hpp:149
bool is_empty() const
Return true if this Rect is empty.
Definition: rect.hpp:108
sdl::Rect, C++ wrapping of SDL_Rect
Definition: rect.hpp:11
constexpr int x2() const
Return the &#39;max X&#39; position of the Rect.
Definition: rect.hpp:87
Vec2i botleft() const
Get the bottom left corner position.
Definition: rect.hpp:94
constexpr int x1() const
Return the &#39;min X&#39; position of the Rect.
Definition: rect.hpp:85
bool intersects(Rect const &r) const
Return true if this rect intersect another rect.
Definition: rect.hpp:123
Generic templated 2D vector class.
Definition: vec2.hpp:24
static constexpr Rect from_corners(int x1, int y1, int x2, int y2)
Construct a rect from 2 corner points.
Definition: rect.hpp:63
constexpr Rect(Vec2i const &corner, Vec2i const &size)
Construct a Rect with the given dimensions.
Definition: rect.hpp:27
static constexpr Rect from_center(Vec2i const &center, Vec2i const &size)
Construct a Rect with dimensions around a center point.
Definition: rect.hpp:53
Rect & operator=(Rect const &) noexcept=default
Copy assign a Rect.
constexpr int y2() const
Return the &#39;max Y&#39; position of the Rect.
Definition: rect.hpp:91
Vec2i topleft() const
Get the top left corner position.
Definition: rect.hpp:98
Vec2i center() const
Get the center of the Rect.
Definition: rect.hpp:105
Vec2i botright() const
Get the bottom right corner position.
Definition: rect.hpp:96
bool operator==(Rect const &other) const
Returns true if the two rect are the same.
Definition: rect.hpp:82
Define to deactivate exception support.
Definition: color.hpp:7
static constexpr Rect from_center(int cx, int cy, int w, int h)
Contruct a Rect with dimensions around a center point.
Definition: rect.hpp:46
Vec2i size() const
Get the size of the Rect.
Definition: rect.hpp:103
constexpr Rect(SDL_Rect const &r)
Copy a Rect.
Definition: rect.hpp:33
constexpr Rect(int x, int y, int w, int h)
Construct a Rect with the given dimensions.
Definition: rect.hpp:22
Rect inter(Rect const &r) const
Return the intersection of the two rects.
Definition: rect.hpp:140
bool contains(Vec2i const &point) const
Return true if this rect contains the given point.
Definition: rect.hpp:120
static constexpr Rect from_corners(Vec2i const &corner1, Vec2i const &corner2)
Construct a rect from 2 corner points.
Definition: rect.hpp:71
constexpr Rect()
Construct a Rect initialized at 0.
Definition: rect.hpp:15