galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Rect.hpp
Go to the documentation of this file.
1
7
8#ifndef GALAXY_MATH_RECT_HPP_
9#define GALAXY_MATH_RECT_HPP_
10
11#include <compare>
12
13#include <glm/vec2.hpp>
14#include <SDL3/SDL_rect.h>
15
17
18namespace galaxy
19{
25 template<meta::is_arithmetic Type>
26 class Rect final
27 {
28 public:
32 Rect() noexcept;
33
42 Rect(const Type x, const Type y, const Type width, const Type height) noexcept;
43
47 ~Rect() noexcept;
48
57 [[nodiscard]]
58 bool contains(const Type _x, const Type _y) const noexcept;
59
67 [[nodiscard]]
68 bool contains(const Rect<Type>& b) const noexcept;
69
77 [[nodiscard]]
78 bool overlaps(const Rect<Type>& b) noexcept;
79
87 [[nodiscard]]
88 bool intersects(const Rect<Type>& b) const noexcept;
89
95 void set_top_left(const glm::vec<2, Type, glm::defaultp>& pos) noexcept;
96
102 [[nodiscard]]
103 Type get_right() const noexcept;
104
110 [[nodiscard]]
111 Type get_bottom() const noexcept;
112
118 [[nodiscard]]
119 glm::vec<2, Type, glm::defaultp> get_top_left() const noexcept;
120
126 [[nodiscard]]
127 glm::vec<2, Type, glm::defaultp> get_center() const noexcept;
128
134 [[nodiscard]]
135 glm::vec<2, Type, glm::defaultp> get_size() const noexcept;
136
142 [[nodiscard]]
143 SDL_Rect to_sdl() noexcept;
144
148 [[nodiscard]]
149 auto operator<=>(const Rect&) const noexcept = default;
150
151 public:
155 Type m_xpos;
156
160 Type m_ypos;
161
166
171
172 private:
183 [[nodiscard]]
184 bool value_in_range(const Type value, const Type min, const Type max) noexcept;
185 };
186
190 typedef Rect<int> iRect;
191
195 typedef Rect<float> fRect;
196
197 template<meta::is_arithmetic Type>
198 inline Rect<Type>::Rect() noexcept
199 : m_xpos {0}
200 , m_ypos {0}
201 , m_width {0}
202 , m_height {0}
203 {
204 }
205
206 template<meta::is_arithmetic Type>
207 inline Rect<Type>::Rect(const Type x, const Type y, const Type width, const Type height) noexcept
208 : m_xpos {x}
209 , m_ypos {y}
210 , m_width {width}
211 , m_height {height}
212 {
213 }
214
215 template<meta::is_arithmetic Type>
216 inline Rect<Type>::~Rect() noexcept
217 {
218 }
219
220 template<meta::is_arithmetic Type>
221 inline bool Rect<Type>::contains(const Type x, const Type y) const noexcept
222 {
223 // Checks if the rectangle contains the point (x, y) using some basic math.
224 return ((x > m_xpos) && (x < (m_xpos + m_width)) && (y > m_ypos) && (y < (m_ypos + m_height)));
225 }
226
227 template<meta::is_arithmetic Type>
228 inline bool Rect<Type>::contains(const Rect<Type>& b) const noexcept
229 {
230 return m_xpos <= b.m_xpos && b.get_right() <= get_right() && m_ypos <= b.m_ypos && b.get_bottom() <= get_bottom();
231 }
232
233 template<meta::is_arithmetic Type>
234 inline bool Rect<Type>::overlaps(const Rect<Type>& b) noexcept
235 {
236 // Check for overlaps using math.
237 const auto x = value_in_range(m_xpos, b.m_xpos, b.m_xpos + b.m_width) || value_in_range(b.m_xpos, m_xpos, m_xpos + m_width);
238 const auto y = value_in_range(m_ypos, b.m_ypos, b.m_ypos + b.m_height) || value_in_range(b.m_ypos, m_ypos, m_ypos + m_height);
239
240 return x && y;
241 }
242
243 template<meta::is_arithmetic Type>
244 inline bool Rect<Type>::intersects(const Rect<Type>& b) const noexcept
245 {
246 return !(m_xpos >= b.get_right() || get_right() <= b.m_xpos || m_ypos >= b.get_bottom() || get_bottom() <= b.m_ypos);
247 }
248
249 template<meta::is_arithmetic Type>
250 inline void Rect<Type>::set_top_left(const glm::vec<2, Type, glm::defaultp>& pos) noexcept
251 {
252 m_xpos = pos.x;
253 m_ypos = pos.y;
254 }
255
256 template<meta::is_arithmetic Type>
257 inline Type Rect<Type>::get_right() const noexcept
258 {
259 return m_xpos + m_width;
260 }
261
262 template<meta::is_arithmetic Type>
263 inline Type Rect<Type>::get_bottom() const noexcept
264 {
265 return m_ypos + m_height;
266 }
267
268 template<meta::is_arithmetic Type>
269 inline glm::vec<2, Type, glm::defaultp> Rect<Type>::get_top_left() const noexcept
270 {
271 return {m_xpos, m_ypos};
272 }
273
274 template<meta::is_arithmetic Type>
275 inline glm::vec<2, Type, glm::defaultp> Rect<Type>::get_center() const noexcept
276 {
277 return {(m_xpos + m_width) / 2.0, (m_ypos + m_height) / 2.0};
278 }
279
280 template<meta::is_arithmetic Type>
281 inline glm::vec<2, Type, glm::defaultp> Rect<Type>::get_size() const noexcept
282 {
283 return {m_width, m_height};
284 }
285
286 template<meta::is_arithmetic Type>
287 inline SDL_Rect Rect<Type>::to_sdl() noexcept
288 {
289 SDL_Rect rect {.x = m_xpos, .y = m_ypos, .w = m_width, .h = m_height};
290 return rect;
291 }
292
293 template<meta::is_arithmetic Type>
294 inline bool Rect<Type>::value_in_range(const Type value, const Type min, const Type max) noexcept
295 {
296 // Check if a value is between min and max - i.e. in range.
297 return (value >= min) && (value <= max);
298 }
299} // namespace galaxy
300
301#endif
Represents a rectangle object.
Definition Rect.hpp:27
bool intersects(const Rect< Type > &b) const noexcept
bool contains(const Type _x, const Type _y) const noexcept
bool overlaps(const Rect< Type > &b) noexcept
void set_top_left(const glm::vec< 2, Type, glm::defaultp > &pos) noexcept
Type get_right() const noexcept
bool value_in_range(const Type value, const Type min, const Type max) noexcept
glm::vec< 2, Type, glm::defaultp > get_size() const noexcept
~Rect() noexcept
Destructor.
Definition Rect.hpp:216
glm::vec< 2, Type, glm::defaultp > get_top_left() const noexcept
SDL_Rect to_sdl() noexcept
Type get_bottom() const noexcept
glm::vec< 2, Type, glm::defaultp > get_center() const noexcept
Rect() noexcept
Constructor.
Definition Rect.hpp:198
Tag.cpp galaxy.
Definition Tag.cpp:13
Rect< int > iRect
Type definition for an int rectangle.
Definition Rect.hpp:190
Rect< float > fRect
Type definition for a floating point rectangle.
Definition Rect.hpp:195