GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
Vector2.h
1 // This is adapted from SFML (https://github.com/SFML/SFML).
2 
3 #ifndef GDCORE_VECTOR2_H
4 #define GDCORE_VECTOR2_H
5 
6 namespace gd {
7 
9 //
10 // SFML - Simple and Fast Multimedia Library
11 // Copyright (C) 2007-2016 Laurent Gomila ([email protected])
12 //
13 // This software is provided 'as-is', without any express or implied warranty.
14 // In no event will the authors be held liable for any damages arising from the
15 // use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented;
22 // you must not claim that you wrote the original software.
23 // If you use this software in a product, an acknowledgment
24 // in the product documentation would be appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such,
27 // and must not be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
32 
38 template <typename T>
39 class Vector2
40 {
41 public:
42 
49  inline Vector2() :
50  x(0),
51  y(0)
52  {
53 
54  }
62  inline Vector2(T X, T Y) :
63  x(X),
64  y(Y)
65  {
66 
67  }
79  template <typename U>
80  inline explicit Vector2(const Vector2<U>& vector) :
81  x(static_cast<T>(vector.x)),
82  y(static_cast<T>(vector.y))
83  {
84  }
86  // Member data
88  T x;
89  T y;
90 };
91 
101 template <typename T>
102 inline Vector2<T> operator -(const Vector2<T>& right)
103 {
104  return Vector2<T>(-right.x, -right.y);
105 }
106 
120 template <typename T>
121 inline Vector2<T>& operator +=(Vector2<T>& left, const Vector2<T>& right)
122 {
123  left.x += right.x;
124  left.y += right.y;
125 
126  return left;
127 }
128 
142 template <typename T>
143 inline Vector2<T>& operator -=(Vector2<T>& left, const Vector2<T>& right)
144 {
145  left.x -= right.x;
146  left.y -= right.y;
147 
148  return left;
149 }
150 
161 template <typename T>
162 inline Vector2<T> operator +(const Vector2<T>& left, const Vector2<T>& right)
163 {
164  return Vector2<T>(left.x + right.x, left.y + right.y);
165 }
166 
177 template <typename T>
178 inline Vector2<T> operator -(const Vector2<T>& left, const Vector2<T>& right)
179 {
180  return Vector2<T>(left.x - right.x, left.y - right.y);
181 }
182 
193 template <typename T>
194 inline Vector2<T> operator *(const Vector2<T>& left, T right)
195 {
196  return Vector2<T>(left.x * right, left.y * right);
197 }
198 
209 template <typename T>
210 inline Vector2<T> operator *(T left, const Vector2<T>& right)
211 {
212  return Vector2<T>(right.x * left, right.y * left);
213 }
214 
228 template <typename T>
229 inline Vector2<T>& operator *=(Vector2<T>& left, T right)
230 {
231  left.x *= right;
232  left.y *= right;
233 
234  return left;
235 }
236 
247 template <typename T>
248 inline Vector2<T> operator /(const Vector2<T>& left, T right)
249 {
250  return Vector2<T>(left.x / right, left.y / right);
251 }
252 
266 template <typename T>
267 inline Vector2<T>& operator /=(Vector2<T>& left, T right)
268 {
269  left.x /= right;
270  left.y /= right;
271 
272  return left;
273 }
274 
287 template <typename T>
288 inline bool operator ==(const Vector2<T>& left, const Vector2<T>& right)
289 {
290  return (left.x == right.x) && (left.y == right.y);
291 }
292 
305 template <typename T>
306 inline bool operator !=(const Vector2<T>& left, const Vector2<T>& right)
307 {
308  return (left.x != right.x) || (left.y != right.y);
309 }
310 
311 // Define the most common types
312 typedef Vector2<int> Vector2i;
314 typedef Vector2<float> Vector2f;
315 
316 } // namespace gd
317 
318 
319 #endif // GDCORE_VECTOR2_H
320 
321 
Utility template class for manipulating 2-dimensional vectors.
Definition: Vector2.h:40
Vector2()
Default constructor.
Definition: Vector2.h:49
Vector2(const Vector2< U > &vector)
Construct the vector from another type of vector.
Definition: Vector2.h:80
Vector2(T X, T Y)
Construct the vector from its coordinates.
Definition: Vector2.h:62
T y
Y coordinate of the vector.
Definition: Vector2.h:89
T x
X coordinate of the vector.
Definition: Vector2.h:88
Definition: CommonTools.h:24