SRect.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #pragma once
  2. #include <SDL_video.h>
  3. #include "SPoint.h"
  4. /*
  5. * SRect.h, part of VCMI engine
  6. *
  7. * Authors: listed in file AUTHORS in main folder
  8. *
  9. * License: GNU General Public License v2.0 or later
  10. * Full text of license available in license.txt file, in main folder
  11. *
  12. */
  13. #ifdef max
  14. #undef max
  15. #endif
  16. #ifdef min
  17. #undef min
  18. #endif
  19. /// Rectangle class, which have a position and a size
  20. struct SRect : public SDL_Rect
  21. {
  22. SRect()//default c-tor
  23. {
  24. x = y = w = h = -1;
  25. }
  26. SRect(int X, int Y, int W, int H) //c-tor
  27. {
  28. x = X;
  29. y = Y;
  30. w = W;
  31. h = H;
  32. }
  33. SRect(const SPoint & position, const SPoint & size) //c-tor
  34. {
  35. x = position.x;
  36. y = position.y;
  37. w = size.x;
  38. h = size.y;
  39. }
  40. SRect(const SDL_Rect & r) //c-tor
  41. {
  42. x = r.x;
  43. y = r.y;
  44. w = r.w;
  45. h = r.h;
  46. }
  47. explicit SRect(const SDL_Surface * const &surf)
  48. {
  49. x = y = 0;
  50. w = surf->w;
  51. h = surf->h;
  52. }
  53. SRect centerIn(const SRect &r);
  54. static SRect createCentered(int w, int h);
  55. static SRect around(const SRect &r, int width = 1); //creates rect around another
  56. bool isIn(int qx, int qy) const //determines if given point lies inside rect
  57. {
  58. if (qx > x && qx<x+w && qy>y && qy<y+h)
  59. return true;
  60. return false;
  61. }
  62. bool isIn(const SPoint & q) const //determines if given point lies inside rect
  63. {
  64. return isIn(q.x,q.y);
  65. }
  66. SPoint topLeft() const //top left corner of this rect
  67. {
  68. return SPoint(x,y);
  69. }
  70. SPoint topRight() const //top right corner of this rect
  71. {
  72. return SPoint(x+w,y);
  73. }
  74. SPoint bottomLeft() const //bottom left corner of this rect
  75. {
  76. return SPoint(x,y+h);
  77. }
  78. SPoint bottomRight() const //bottom right corner of this rect
  79. {
  80. return SPoint(x+w,y+h);
  81. }
  82. SRect operator+(const SRect &p) const //moves this rect by p's rect position
  83. {
  84. return SRect(x+p.x,y+p.y,w,h);
  85. }
  86. SRect operator+(const SPoint &p) const //moves this rect by p's point position
  87. {
  88. return SRect(x+p.x,y+p.y,w,h);
  89. }
  90. SRect& operator=(const SPoint &p) //assignment operator
  91. {
  92. x = p.x;
  93. y = p.y;
  94. return *this;
  95. }
  96. SRect& operator=(const SRect &p) //assignment operator
  97. {
  98. x = p.x;
  99. y = p.y;
  100. w = p.w;
  101. h = p.h;
  102. return *this;
  103. }
  104. SRect& operator+=(const SRect &p) //works as operator+
  105. {
  106. x += p.x;
  107. y += p.y;
  108. return *this;
  109. }
  110. SRect& operator+=(const SPoint &p) //works as operator+
  111. {
  112. x += p.x;
  113. y += p.y;
  114. return *this;
  115. }
  116. SRect& operator-=(const SRect &p) //works as operator+
  117. {
  118. x -= p.x;
  119. y -= p.y;
  120. return *this;
  121. }
  122. SRect& operator-=(const SPoint &p) //works as operator+
  123. {
  124. x -= p.x;
  125. y -= p.y;
  126. return *this;
  127. }
  128. template<typename T> SRect operator-(const T &t)
  129. {
  130. return SRect(x - t.x, y - t.y, w, h);
  131. }
  132. SRect operator&(const SRect &p) const //rect intersection
  133. {
  134. bool intersect = true;
  135. if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
  136. {
  137. intersect = false;
  138. }
  139. else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
  140. {
  141. intersect = false;
  142. }
  143. else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
  144. {
  145. intersect = false;
  146. }
  147. else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
  148. {
  149. intersect = false;
  150. }
  151. if(intersect)
  152. {
  153. SRect ret;
  154. ret.x = std::max(this->x, p.x);
  155. ret.y = std::max(this->y, p.y);
  156. SPoint bR; //bottomRight point of returned rect
  157. bR.x = std::min(this->w+this->x, p.w+p.x);
  158. bR.y = std::min(this->h+this->y, p.h+p.y);
  159. ret.w = bR.x - ret.x;
  160. ret.h = bR.y - ret.y;
  161. return ret;
  162. }
  163. else
  164. {
  165. return SRect();
  166. }
  167. }
  168. SRect operator|(const SRect &p) const //union of two rects
  169. {
  170. SRect ret;
  171. ret.x = std::min(p.x, this->x);
  172. ret.y = std::min(p.y, this->y);
  173. int x2 = std::max(p.x+p.w, this->x+this->w);
  174. int y2 = std::max(p.y+p.h, this->y+this->h);
  175. ret.w = x2 -ret.x;
  176. ret.h = y2 -ret.y;
  177. return ret;
  178. }
  179. };