Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Rect.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
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  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_RECT_H
25 #define GF_RECT_H
26 
27 #include <cstddef>
28 #include <algorithm>
29 
30 #include "Anchor.h"
31 #include "Box.h"
32 #include "Portability.h"
33 #include "Vector.h"
34 
35 namespace gf {
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37 inline namespace v1 {
38 #endif
39 
83  template<typename T>
84  struct Rect : Box<T, 2> {
90  constexpr Rect() noexcept
91  : Box<T, 2>({ T(0), T(0) })
92  {
93  }
94 
100  constexpr Rect(const Box<T, 2>& box)
101  : Box<T, 2>(box)
102  {
103  }
104 
112  static constexpr Rect<T> fromMinMax(Vector<T, 2> min, Vector<T, 2> max) noexcept {
113  return Rect<T>(min, max);
114  }
115 
123  static constexpr Rect<T> fromPositionSize(Vector<T, 2> position, Vector<T, 2> size) noexcept {
124  return Rect<T>(position, position + size);
125  }
126 
134  static constexpr Rect<T> fromCenterSize(Vector<T, 2> center, Vector<T, 2> size) noexcept {
135  return Rect<T>(center - size / T(2), center + size / T(2));
136  }
137 
144  constexpr Vector<T, 2> getPositionFromAnchor(Anchor anchor) const noexcept {
145  switch (anchor) {
146  case Anchor::TopLeft:
147  return this->min;
148  case Anchor::TopCenter:
149  return { (this->min.x + this->max.x) / T(2), this->min.y };
150  case Anchor::TopRight:
151  return { this->max.x, this->min.y };
152  case Anchor::CenterLeft:
153  return { this->min.x, (this->min.y + this->max.y) / T(2) };
154  case Anchor::Center:
155  return this->getCenter();
156  case Anchor::CenterRight:
157  return { this->max.x, (this->min.y + this->max.y) / T(2) };
158  case Anchor::BottomLeft:
159  return { this->min.x, this->max.y };
161  return { (this->min.x + this->max.x) / T(2), this->max.y };
162  case Anchor::BottomRight:
163  return this->max;
164  }
165 
166  return this->min;
167  }
168 
174  constexpr Vector<T, 2> getTopLeft() const noexcept {
175  return getPositionFromAnchor(Anchor::TopLeft);
176  }
177 
183  constexpr Vector<T, 2> getTopRight() const noexcept {
184  return getPositionFromAnchor(Anchor::TopRight);
185  }
186 
192  constexpr Vector<T, 2> getBottomLeft() const noexcept {
193  return getPositionFromAnchor(Anchor::BottomLeft);
194  }
195 
201  constexpr Vector<T, 2> getBottomRight() const noexcept {
202  return getPositionFromAnchor(Anchor::BottomRight);
203  }
204 
210  constexpr T getWidth() const noexcept {
211  return this->max.x - this->min.x;
212  }
213 
219  constexpr T getHeight() const noexcept {
220  return this->max.y - this->min.y;
221  }
222 
223  private:
224  constexpr Rect(Vector<T, 2> min, Vector<T, 2> max) noexcept
225  : Box<T, 2>(min, max)
226  {
227  }
228  };
229 
237 
245 
252  using RectI = Rect<int>;
253 
261 
269 
270 // MSVC does not like extern template
271 #ifndef _MSC_VER
272  extern template struct GF_API Rect<float>;
273  extern template struct GF_API Rect<double>;
274  extern template struct GF_API Rect<int>;
275  extern template struct GF_API Rect<unsigned>;
276 #endif
277 
286  template<typename T>
287  inline
288  bool operator==(const Rect<T>& lhs, const Rect<T>& rhs) {
289  return lhs.min == rhs.min && lhs.max == rhs.max;
290  }
291 
300  template<typename T>
301  inline
302  bool operator!=(const Rect<T>& lhs, const Rect<T>& rhs) {
303  return !(lhs == rhs);
304  }
305 
314  template<typename Archive, typename T>
315  Archive& operator|(Archive& ar, Rect<T>& rect) {
316  return ar | rect.min.x | rect.min.y | rect.max.x | rect.max.y;
317  }
318 
319 
320 #ifndef DOXYGEN_SHOULD_SKIP_THIS
321 }
322 #endif
323 }
324 
325 #endif // GF_RECT_H
constexpr Vector< T, 2 > getBottomLeft() const noexcept
Get the bottom left corner.
Definition: Rect.h:192
bool operator!=(const Rect< T > &lhs, const Rect< T > &rhs)
Inequality operator.
Definition: Rect.h:302
constexpr Vector< T, 2 > getPositionFromAnchor(Anchor anchor) const noexcept
Get a position from the rectangle and an anchor.
Definition: Rect.h:144
Vector< T, N > min
The minimum point of the box.
Definition: Box.h:53
Archive & operator|(Archive &ar, Rect< T > &rect)
Serialize a rectangle.
Definition: Rect.h:315
static constexpr Rect< T > fromMinMax(Vector< T, 2 > min, Vector< T, 2 > max) noexcept
Create a rectangle from a min point and a max point.
Definition: Rect.h:112
constexpr Rect(const Box< T, 2 > &box)
Constructor from a box.
Definition: Rect.h:100
constexpr Vector< T, 2 > getTopRight() const noexcept
Get the top right corner.
Definition: Rect.h:183
Utility class for manipulating 2D axis aligned rectangles.
Definition: Rect.h:84
The namespace for gf classes.
Definition: Action.h:35
static constexpr Rect< T > fromPositionSize(Vector< T, 2 > position, Vector< T, 2 > size) noexcept
Create a rectangle from a position (top-left) and a size.
Definition: Rect.h:123
Vector< T, N > max
The maximum point of the box.
Definition: Box.h:54
A 2D vector.
Definition: Vector.h:316
constexpr T getWidth() const noexcept
Get the width of the rectangle.
Definition: Rect.h:210
constexpr Rect() noexcept
Default constructor.
Definition: Rect.h:90
constexpr Vector< T, 2 > getTopLeft() const noexcept
Get the top left corner.
Definition: Rect.h:174
static constexpr Rect< T > fromCenterSize(Vector< T, 2 > center, Vector< T, 2 > size) noexcept
Create a rectangle from a center and a size.
Definition: Rect.h:134
constexpr Vector< T, 2 > getBottomRight() const noexcept
Get the bottom right corner.
Definition: Rect.h:201
Anchor
An anchor of a box.
Definition: Anchor.h:38
A multi-dimensional box.
Definition: Box.h:52
bool operator==(const Rect< T > &lhs, const Rect< T > &rhs)
Equality operator.
Definition: Rect.h:288
constexpr T getHeight() const noexcept
Get the height of the rectangle.
Definition: Rect.h:219