libSDL2pp  0.16.0
C++11 bindings/wrapper for SDL2
Point.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_POINT_HH
23 #define SDL2PP_POINT_HH
24 
25 #include <iostream>
26 #include <functional>
27 
28 #include <SDL_rect.h>
29 
30 #include <SDL2pp/Export.hh>
31 
32 namespace SDL2pp {
33 
34 class Rect;
35 
51 class SDL2PP_EXPORT Point : public SDL_Point {
52 public:
59  constexpr Point() : SDL_Point{0, 0} {
60  }
61 
68  constexpr Point(const SDL_Point& point) : SDL_Point{point.x, point.y} {
69  }
70 
78  constexpr Point(int x, int y) : SDL_Point{x, y} {
79  }
80 
85  Point(const Point&) noexcept = default;
86 
91  Point(Point&&) noexcept = default;
92 
99  Point& operator=(const Point&) noexcept = default;
100 
107  Point& operator=(Point&&) noexcept = default;
108 
115  constexpr int GetX() const {
116  return x;
117  }
118 
127  Point& SetX(int nx) {
128  x = nx;
129  return *this;
130  }
131 
138  constexpr int GetY() const {
139  return y;
140  }
141 
150  Point& SetY(int ny) {
151  y = ny;
152  return *this;
153  }
154 
161  constexpr Point operator-() const {
162  return Point(-x, -y);
163  }
164 
173  constexpr Point operator+(const Point& other) const {
174  return Point(x + other.x, y + other.y);
175  }
176 
185  constexpr Point operator-(const Point& other) const {
186  return Point(x - other.x, y - other.y);
187  }
188 
198  constexpr Point operator/(int value) const {
199  return Point(x / value, y / value);
200  }
201 
211  constexpr Point operator/(const Point& other) const {
212  return Point(x / other.x, y / other.y);
213  }
214 
225  constexpr Point operator%(int value) const {
226  return Point(x % value, y % value);
227  }
228 
239  constexpr Point operator%(const Point& other) const {
240  return Point(x % other.x, y % other.y);
241  }
242 
253  constexpr Point operator*(int value) const {
254  return Point(x * value, y * value);
255  }
256 
267  constexpr Point operator*(const Point& other) const {
268  return Point(x * other.x, y * other.y);
269  }
270 
279  Point& operator+=(const Point& other) {
280  x += other.x;
281  y += other.y;
282  return *this;
283  }
284 
293  Point& operator-=(const Point& other) {
294  x -= other.x;
295  y -= other.y;
296  return *this;
297  }
298 
307  Point& operator/=(int value) {
308  x /= value;
309  y /= value;
310  return *this;
311  }
312 
321  Point& operator/=(const Point& other) {
322  x /= other.x;
323  y /= other.y;
324  return *this;
325  }
326 
335  Point& operator%=(int value) {
336  x %= value;
337  y %= value;
338  return *this;
339  }
340 
350  Point& operator%=(const Point& other) {
351  x %= other.x;
352  y %= other.y;
353  return *this;
354  }
355 
364  Point& operator*=(int value) {
365  x *= value;
366  y *= value;
367  return *this;
368  }
369 
378  Point& operator*=(const Point& other) {
379  x *= other.x;
380  y *= other.y;
381  return *this;
382  }
383 
393  Point GetClamped(const Rect& rect) const;
394 
404  Point& Clamp(const Rect& rect);
405 
414  Point GetWrapped(const Rect& rect) const;
415 
424  Point& Wrap(const Rect& rect);
425 };
426 
427 }
428 
438 constexpr bool operator==(const SDL2pp::Point&a, const SDL2pp::Point& b) {
439  return a.x == b.x && a.y == b.y;
440 }
441 
451 constexpr bool operator!=(const SDL2pp::Point& a, const SDL2pp::Point& b) {
452  return !(a == b);
453 }
454 
464 SDL2PP_EXPORT bool operator<(const SDL2pp::Point& a, const SDL2pp::Point& b);
465 
475 SDL2PP_EXPORT std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point);
476 
477 namespace std {
478 
483 template<>
484 struct hash<SDL2pp::Point> {
493  size_t operator()(const SDL2pp::Point& p) const {
494  size_t seed = std::hash<int>()(p.x);
495  seed ^= std::hash<int>()(p.y) + 0x9e3779b9 + (seed<<6) + (seed>>2);
496  return seed;
497  }
498 };
499 
500 }
501 
502 #endif
Point & SetX(int nx)
Set X coordinate of the point.
Definition: Point.hh:127
constexpr Point(const SDL_Point &point)
Construct a point from existing SDL_Point.
Definition: Point.hh:68
size_t operator()(const SDL2pp::Point &p) const
Hash function for SDL2pp::Point.
Definition: Point.hh:493
2D rectangle
Definition: Rect.hh:50
Point & operator/=(int value)
Memberwise divide by an integer.
Definition: Point.hh:307
constexpr Point operator-(const Point &other) const
Get point's memberwise subtraction with another point.
Definition: Point.hh:185
Point & operator+=(const Point &other)
Memberwise add another point.
Definition: Point.hh:279
Point & operator%=(int value)
Memberwise remainder from division by an integer.
Definition: Point.hh:335
constexpr Point operator%(const Point &other) const
Get point's memberwise remainder from division by another point.
Definition: Point.hh:239
constexpr Point operator-() const
Get point's memberwise negation.
Definition: Point.hh:161
Point & operator-=(const Point &other)
Memberwise subtract another point.
Definition: Point.hh:293
constexpr Point()
Default constructor.
Definition: Point.hh:59
Point & operator/=(const Point &other)
Definition: Point.hh:321
constexpr Point operator/(const Point &other) const
Get point's memberwise division by another point.
Definition: Point.hh:211
Point & SetY(int ny)
Set Y coordinate of the point.
Definition: Point.hh:150
constexpr Point operator+(const Point &other) const
Get point's memberwise addition with another point.
Definition: Point.hh:173
constexpr Point operator/(int value) const
Get point's memberwise division by an integer.
Definition: Point.hh:198
2D point
Definition: Point.hh:51
constexpr Point operator%(int value) const
Get point's memberwise remainder from division by an integer.
Definition: Point.hh:225
constexpr Point(int x, int y)
Construct the point from given coordinates.
Definition: Point.hh:78
Point & operator%=(const Point &other)
Memberwise remainder from division by another point.
Definition: Point.hh:350
constexpr int GetY() const
Get Y coordinate of the point.
Definition: Point.hh:138