1 // Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef UNISON_VIDEO_RECT_HPP
7 #define UNISON_VIDEO_RECT_HPP
11 #include <unison/video/Coord.hpp>
19 /// Represents a rectangular area
23 /// The position of the rectangle
26 /// The size of the rectangle
29 /// Default constructor
37 /// Create a rectangle from the given coordinates
38 /// \param[in] pos The position of the rectangle
39 /// \param[in] size The size of the rectangle
40 Rect(const Point &pos, const Area &size) :
47 /// Create a rectangle from the given values
48 /// \param[in] x The x-position of the rectangle
49 /// \param[in] y The y-position of the rectangle
50 /// \param[in] w The width of the rectangle
51 /// \param[in] h The height of the rectangle
52 Rect(int x, int y, int w, int h) :
60 /// \param[in] rhs The rectangle to test
61 /// \return Whether the rectangles are equal
62 bool operator == (const Rect &rhs) const
64 return pos == rhs.pos && size == rhs.size;
68 /// \param[in] rhs The rectangle to test
69 /// \return Whether the rectangles are not equal
70 bool operator != (const Rect &rhs) const
72 return !(*this == rhs);
75 /// Get the left edge of the rectnagle
76 /// \return The location of the left edge
82 /// Get the top edge of the rectnagle
83 /// \return The location of the top edge
89 /// Get the right edge of the rectnagle
90 /// \return The location of the right edge
93 return pos.x + size.x;
96 /// Get the bottom edge of the rectnagle
97 /// \return The location of the bottom edge
98 int get_bottom() const
100 return pos.y + size.y;
103 /// Calculate the overlap between the rectangles
104 /// \param[in] rhs The rectangle to check
105 /// \return The part of the rectangle that is overlapping
106 Rect get_overlap(const Rect &rhs)
117 if(get_left() < rhs.get_right())
119 overlap.pos.x = std::max(get_left(), rhs.get_left());
125 if(rhs.get_left() < get_right())
127 overlap.size.x = std::min(rhs.get_right(), get_right()) - overlap.pos.x;
133 if(get_top() < rhs.get_bottom())
135 overlap.pos.y = std::max(get_top(), rhs.get_top());
141 if(rhs.get_top() < get_bottom())
143 overlap.size.y = std::min(rhs.get_bottom(), get_bottom()) - overlap.pos.y;
152 /// Allow rectangles to be treated like SDL_Rect
153 /// \return The equavalent SDL_Rect
154 operator SDL_Rect () const
163 /// Allow rectangles to be treated like SDL_Rect
164 /// \return The internal SDL_Rect
165 SDL_Rect *operator &() const
181 mutable SDL_Rect rect;