Vec2.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <cstdint>
  18. struct CSMap {};
  19. struct CSProjected {};
  20. template<class CS, class T>
  21. struct Vec2
  22. {
  23. public:
  24. Vec2() = default;
  25. Vec2(T x_, T y_) : x(x_), y(y_) {}
  26. bool operator==(const Vec2& other) const = default;
  27. inline Vec2& operator +=(const Vec2& other) {
  28. x += other.x;
  29. y += other.y;
  30. return *this;
  31. }
  32. inline Vec2& operator -=(const Vec2& other) {
  33. x -= other.x;
  34. y -= other.y;
  35. return *this;
  36. }
  37. inline Vec2& operator *=(const T v)
  38. {
  39. x *= v;
  40. y *= v;
  41. return *this;
  42. }
  43. inline Vec2& operator *=(const Vec2& other)
  44. {
  45. x *= other.x;
  46. y *= other.y;
  47. return *this;
  48. }
  49. inline Vec2& operator /=(const Vec2& other)
  50. {
  51. x /= other.x;
  52. y /= other.y;
  53. return *this;
  54. }
  55. inline Vec2& operator /=(const T v)
  56. {
  57. x /= v;
  58. y /= v;
  59. return *this;
  60. }
  61. void set(const T x_, const T y_)
  62. {
  63. x = x_;
  64. y = y_;
  65. }
  66. template<class T1>
  67. inline Vec2<CS, T1> convertT() const
  68. {
  69. return Vec2<CS, T1>(static_cast<T1>(x), static_cast<T1>(y));
  70. }
  71. inline Vec2<CS, float> inverted() const
  72. {
  73. return Vec2<CS, float>(1.0f / static_cast<float>(x), 1.0f / static_cast<float>(y));
  74. }
  75. inline Vec2 negated() const
  76. {
  77. return Vec2(-x, -y);
  78. }
  79. public:
  80. T x = 0;
  81. T y = 0;
  82. };
  83. template<class CS, class T>
  84. inline Vec2<CS, T> operator+(const Vec2<CS, T>& l, const Vec2<CS, T>& r)
  85. {
  86. auto res = l;
  87. res += r;
  88. return res;
  89. }
  90. template<class CS, class T>
  91. inline Vec2<CS, T> operator-(const Vec2<CS, T>& l, const Vec2<CS, T>& r)
  92. {
  93. auto res = l;
  94. res -= r;
  95. return res;
  96. }
  97. template<class CS, class T>
  98. inline Vec2<CS, T> operator*(const Vec2<CS, T>& l, const Vec2<CS, T>& r)
  99. {
  100. auto res = l;
  101. res *= r;
  102. return res;
  103. }
  104. template<class CS>
  105. inline Vec2<CS, float> operator*(const Vec2<CS, std::int32_t>& l, const Vec2<CS, float>& r)
  106. {
  107. return Vec2<CS, float>(l.x * r.x, l.y * r.y);
  108. }
  109. template<class CS, class T>
  110. inline Vec2<CS, T> operator*(const Vec2<CS, T>& l, const T r)
  111. {
  112. auto res = l;
  113. res *= r;
  114. return res;
  115. }
  116. template<class CS, class T>
  117. inline Vec2<CS, T> operator/(const Vec2<CS, T>& l, const Vec2<CS, T>& r)
  118. {
  119. auto res = l;
  120. res /= r;
  121. return res;
  122. }
  123. template<class CS>
  124. inline Vec2<CS, float> operator/(const Vec2<CS, std::int32_t>& l, const Vec2<CS, float>& r)
  125. {
  126. return Vec2<CS, float>(l.x / r.x, l.y / r.y);
  127. }
  128. template<class CS, class T>
  129. inline Vec2<CS, T> operator/(const Vec2<CS, T>& l, const T r)
  130. {
  131. auto res = l;
  132. res /= r;
  133. return res;
  134. }
  135. template<class CS, class T>
  136. struct Coords2
  137. {
  138. Coords2() = default;
  139. Coords2(T x_, T y_) : x(x_), y(y_) {}
  140. bool operator==(const Coords2& other) const = default;
  141. T x = 0;
  142. T y = 0;
  143. Coords2& operator +=(const Vec2<CS, T>& other) {
  144. x += other.x;
  145. y += other.y;
  146. return *this;
  147. }
  148. Coords2& operator -=(const Vec2<CS, T>& other) {
  149. x -= other.x;
  150. y -= other.y;
  151. return *this;
  152. }
  153. void set(const T x_, const T y_)
  154. {
  155. x = x_;
  156. y = y_;
  157. }
  158. template<class T1>
  159. inline Coords2<CS, T1> convertT() const
  160. {
  161. return Coords2<CS, T1>(static_cast<T1>(x), static_cast<T1>(y));
  162. }
  163. };
  164. template<class CS, class T>
  165. inline Coords2<CS, T> operator+(const Coords2<CS, T>& l, const Vec2<CS, T>& r)
  166. {
  167. auto res = l;
  168. res += r;
  169. return res;
  170. }
  171. template<class CS, class T>
  172. inline Coords2<CS, T> operator-(const Coords2<CS, T>& l, const Vec2<CS, T>& r)
  173. {
  174. auto res = l;
  175. res -= r;
  176. return res;
  177. }
  178. template<class CS, class T>
  179. inline Vec2<CS, T> operator-(const Coords2<CS, T>& l, const Coords2<CS, T >& r)
  180. {
  181. return Vec2<CS, T>(l.x - r.x, l.y - r.y);
  182. }
  183. template<class CS, class T>
  184. inline Coords2<CS, T> operator*(const Coords2<CS, T>& l, const Vec2<CS, T>& r)
  185. {
  186. return Coords2<CS, float>(l.x * r.x, l.y * r.y);
  187. }
  188. template<class CS>
  189. inline Coords2<CS, float> operator*(const Coords2<CS, std::int32_t>& l, const Vec2<CS, float>& r)
  190. {
  191. return Coords2<CS, float>(l.x * r.x, l.y * r.y);
  192. }
  193. template<class CS>
  194. inline Coords2<CS, float> operator/(const Coords2<CS, std::int32_t>& l, const Vec2<CS, float>& r)
  195. {
  196. return Coords2<CS, float>(l.x / r.x, l.y / r.y);
  197. }
  198. typedef Vec2<CSMap, std::int16_t> MapVec;
  199. /// <summary>
  200. /// Logical map coordinates in tiles
  201. /// </summary>
  202. typedef Coords2<CSMap, std::int16_t> MapCoords;
  203. typedef Vec2<CSProjected, std::int32_t> ProjectedVec;
  204. typedef Coords2<CSProjected, std::int32_t> ProjectedCoords;