Geometries.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #pragma once
  2. #include <SDL_video.h>
  3. #include <SDL_events.h>
  4. #include "../../lib/int3.h"
  5. /*
  6. * Geometries.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. #ifdef max
  15. #undef max
  16. #endif
  17. #ifdef min
  18. #undef min
  19. #endif
  20. // A point with x/y coordinate, used mostly for graphic rendering
  21. struct Point
  22. {
  23. int x, y;
  24. //constructors
  25. Point()
  26. {
  27. x = y = 0;
  28. };
  29. Point(int X, int Y)
  30. :x(X),y(Y)
  31. {};
  32. Point(const int3 &a)
  33. :x(a.x),y(a.y)
  34. {}
  35. Point(const SDL_MouseMotionEvent &a)
  36. :x(a.x),y(a.y)
  37. {}
  38. template<typename T>
  39. Point operator+(const T &b) const
  40. {
  41. return Point(x+b.x,y+b.y);
  42. }
  43. template<typename T>
  44. Point operator*(const T &mul) const
  45. {
  46. return Point(x*mul, y*mul);
  47. }
  48. template<typename T>
  49. Point& operator+=(const T &b)
  50. {
  51. x += b.x;
  52. y += b.y;
  53. return *this;
  54. }
  55. template<typename T>
  56. Point operator-(const T &b) const
  57. {
  58. return Point(x - b.x, y - b.y);
  59. }
  60. template<typename T>
  61. Point& operator-=(const T &b)
  62. {
  63. x -= b.x;
  64. y -= b.y;
  65. return *this;
  66. }
  67. bool operator<(const Point &b) const //product order
  68. {
  69. return x < b.x && y < b.y;
  70. }
  71. template<typename T> Point& operator=(const T &t)
  72. {
  73. x = t.x;
  74. y = t.y;
  75. return *this;
  76. }
  77. template<typename T> bool operator==(const T &t) const
  78. {
  79. return x == t.x && y == t.y;
  80. }
  81. template<typename T> bool operator!=(const T &t) const
  82. {
  83. return !(*this == t);
  84. }
  85. };
  86. /// Rectangle class, which have a position and a size
  87. struct Rect : public SDL_Rect
  88. {
  89. Rect()//default c-tor
  90. {
  91. x = y = w = h = -1;
  92. }
  93. Rect(int X, int Y, int W, int H) //c-tor
  94. {
  95. x = X;
  96. y = Y;
  97. w = W;
  98. h = H;
  99. }
  100. Rect(const Point & position, const Point & size) //c-tor
  101. {
  102. x = position.x;
  103. y = position.y;
  104. w = size.x;
  105. h = size.y;
  106. }
  107. Rect(const SDL_Rect & r) //c-tor
  108. {
  109. x = r.x;
  110. y = r.y;
  111. w = r.w;
  112. h = r.h;
  113. }
  114. explicit Rect(const SDL_Surface * const &surf)
  115. {
  116. x = y = 0;
  117. w = surf->w;
  118. h = surf->h;
  119. }
  120. Rect centerIn(const Rect &r);
  121. static Rect createCentered(int w, int h);
  122. static Rect around(const Rect &r, int width = 1); //creates rect around another
  123. bool isIn(int qx, int qy) const //determines if given point lies inside rect
  124. {
  125. if (qx > x && qx<x+w && qy>y && qy<y+h)
  126. return true;
  127. return false;
  128. }
  129. bool isIn(const Point & q) const //determines if given point lies inside rect
  130. {
  131. return isIn(q.x,q.y);
  132. }
  133. Point topLeft() const //top left corner of this rect
  134. {
  135. return Point(x,y);
  136. }
  137. Point topRight() const //top right corner of this rect
  138. {
  139. return Point(x+w,y);
  140. }
  141. Point bottomLeft() const //bottom left corner of this rect
  142. {
  143. return Point(x,y+h);
  144. }
  145. Point bottomRight() const //bottom right corner of this rect
  146. {
  147. return Point(x+w,y+h);
  148. }
  149. Rect operator+(const Rect &p) const //moves this rect by p's rect position
  150. {
  151. return Rect(x+p.x,y+p.y,w,h);
  152. }
  153. Rect operator+(const Point &p) const //moves this rect by p's point position
  154. {
  155. return Rect(x+p.x,y+p.y,w,h);
  156. }
  157. Rect& operator=(const Point &p) //assignment operator
  158. {
  159. x = p.x;
  160. y = p.y;
  161. return *this;
  162. }
  163. Rect& operator=(const Rect &p) //assignment operator
  164. {
  165. x = p.x;
  166. y = p.y;
  167. w = p.w;
  168. h = p.h;
  169. return *this;
  170. }
  171. Rect& operator+=(const Rect &p) //works as operator+
  172. {
  173. x += p.x;
  174. y += p.y;
  175. return *this;
  176. }
  177. Rect& operator+=(const Point &p) //works as operator+
  178. {
  179. x += p.x;
  180. y += p.y;
  181. return *this;
  182. }
  183. Rect& operator-=(const Rect &p) //works as operator+
  184. {
  185. x -= p.x;
  186. y -= p.y;
  187. return *this;
  188. }
  189. Rect& operator-=(const Point &p) //works as operator+
  190. {
  191. x -= p.x;
  192. y -= p.y;
  193. return *this;
  194. }
  195. template<typename T> Rect operator-(const T &t)
  196. {
  197. return Rect(x - t.x, y - t.y, w, h);
  198. }
  199. Rect operator&(const Rect &p) const //rect intersection
  200. {
  201. bool intersect = true;
  202. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  203. {
  204. intersect = false;
  205. }
  206. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  207. {
  208. intersect = false;
  209. }
  210. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  211. {
  212. intersect = false;
  213. }
  214. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  215. {
  216. intersect = false;
  217. }
  218. if(intersect)
  219. {
  220. Rect ret;
  221. ret.x = std::max(this->x, p.x);
  222. ret.y = std::max(this->y, p.y);
  223. Point bR; //bottomRight point of returned rect
  224. bR.x = std::min(this->w+this->x, p.w+p.x);
  225. bR.y = std::min(this->h+this->y, p.h+p.y);
  226. ret.w = bR.x - ret.x;
  227. ret.h = bR.y - ret.y;
  228. return ret;
  229. }
  230. else
  231. {
  232. return Rect();
  233. }
  234. }
  235. Rect operator|(const Rect &p) const //union of two rects
  236. {
  237. Rect ret;
  238. ret.x = std::min(p.x, this->x);
  239. ret.y = std::min(p.y, this->y);
  240. int x2 = std::max(p.x+p.w, this->x+this->w);
  241. int y2 = std::max(p.y+p.h, this->y+this->h);
  242. ret.w = x2 -ret.x;
  243. ret.h = y2 -ret.y;
  244. return ret;
  245. }
  246. };