Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Circ.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_CIRC_H
25 #define GF_CIRC_H
26 
27 #include "Ball.h"
28 #include "Portability.h"
29 #include "Vector.h"
30 #include "VectorOps.h"
31 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
61  template<typename T>
62  struct Circ : Ball<T, 2> {
69  constexpr Circ() noexcept
70  : Ball<T, 2>()
71  {
72  }
73 
80  constexpr Circ(const Vector<T, 2>& circCenter, T circRadius) noexcept
81  : Ball<T, 2>(circCenter, circRadius)
82  {
83  }
84 
90  constexpr Vector<T, 2> getTop() const noexcept {
91  return this->center - gf::diry(this->radius);
92  }
93 
99  constexpr Vector<T, 2> getBottom() const noexcept {
100  return this->center + gf::diry(this->radius);
101  }
102 
108  constexpr Vector<T, 2> getLeft() const noexcept {
109  return this->center - gf::dirx(this->radius);
110  }
111 
117  constexpr Vector<T, 2> getRight() const noexcept {
118  return this->center + gf::dirx(this->radius);
119  }
120 
121  };
122 
123 
131 
139 
146  using CircI = Circ<int>;
147 
155 
163 
164 // MSVC does not like extern template
165 #ifndef _MSC_VER
166  extern template struct GF_API Circ<float>;
167  extern template struct GF_API Circ<double>;
168  extern template struct GF_API Circ<int>;
169  extern template struct GF_API Circ<unsigned>;
170 #endif
171 
180  template<typename T>
181  inline
182  bool operator==(const Circ<T>& lhs, const Circ<T>& rhs) {
183  return lhs.center == rhs.center && lhs.radius == rhs.radius;
184  }
185 
194  template<typename T>
195  inline
196  bool operator!=(const Circ<T>& lhs, const Circ<T>& rhs) {
197  return lhs.center != rhs.center || lhs.radius != rhs.radius;
198  }
199 
208  template<typename Archive, typename T>
209  Archive& operator|(Archive& ar, Circ<T>& circ) {
210  return ar | circ.center | circ.radius;
211  }
212 
213 #ifndef DOXYGEN_SHOULD_SKIP_THIS
214 }
215 #endif
216 }
217 
218 
219 #endif // GF_CIRC_H
Archive & operator|(Archive &ar, Circ< T > &circ)
Serialize a circle.
Definition: Circ.h:209
constexpr Vector< T, 2 > getBottom() const noexcept
Get the bottom of the circle.
Definition: Circ.h:99
constexpr Vector< T, 2 > getLeft() const noexcept
Get the left of the circle.
Definition: Circ.h:108
constexpr Vector< T, 2 > getTop() const noexcept
Get the top of the circle.
Definition: Circ.h:90
constexpr Circ() noexcept
Default constructor.
Definition: Circ.h:69
bool operator==(const Circ< T > &lhs, const Circ< T > &rhs)
Equality operator.
Definition: Circ.h:182
A n-dimension ball.
Definition: Ball.h:46
Utility class for manipulating circles.
Definition: Circ.h:62
constexpr Vector< T, 2 > getRight() const noexcept
Get the right of the circle.
Definition: Circ.h:117
bool operator!=(const Circ< T > &lhs, const Circ< T > &rhs)
Inequality operator.
Definition: Circ.h:196
The namespace for gf classes.
Definition: Action.h:35
T radius
Radius of the ball.
Definition: Ball.h:48
A 2D vector.
Definition: Vector.h:316
constexpr Circ(const Vector< T, 2 > &circCenter, T circRadius) noexcept
Construct the circle from center and radius.
Definition: Circ.h:80
Vector< T, N > center
Center of the ball.
Definition: Ball.h:47