argh, clean out copy
[supertux.git] / src / unison / include / unison / video / Rect.hpp
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)
5
6 #ifndef UNISON_VIDEO_RECT_HPP
7 #define UNISON_VIDEO_RECT_HPP
8
9 #include <algorithm>
10
11 #include <unison/video/Coord.hpp>
12
13 #include "SDL.h"
14
15 namespace Unison
16 {
17    namespace Video
18    {
19       /// Represents a rectangular area
20       class Rect
21       {
22          public:
23             /// The position of the rectangle
24             Point pos;
25
26             /// The size of the rectangle
27             Area size;
28
29             /// Default constructor
30             Rect() :
31                pos(),
32                size(),
33                rect()
34             {
35             }
36
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) :
41                pos(pos),
42                size(size),
43                rect()
44             {
45             }
46
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) :
53                pos(x, y),
54                size(w, h),
55                rect()
56             {
57             }
58
59             /// Equality operator
60             /// \param[in] rhs The rectangle to test
61             /// \return Whether the rectangles are equal
62             bool operator == (const Rect &rhs) const
63             {
64                return pos == rhs.pos && size == rhs.size;
65             }
66
67             /// Equality operator
68             /// \param[in] rhs The rectangle to test
69             /// \return Whether the rectangles are not equal
70             bool operator != (const Rect &rhs) const
71             {
72                return !(*this == rhs);
73             }
74
75             /// Get the left edge of the rectnagle
76             /// \return The location of the left edge
77             int get_left() const
78             {
79                return pos.x;
80             }
81
82             /// Get the top edge of the rectnagle
83             /// \return The location of the top edge
84             int get_top() const
85             {
86                return pos.y;
87             }
88
89             /// Get the right edge of the rectnagle
90             /// \return The location of the right edge
91             int get_right() const
92             {
93                return pos.x + size.x;
94             }
95
96             /// Get the bottom edge of the rectnagle
97             /// \return The location of the bottom edge
98             int get_bottom() const
99             {
100                return pos.y + size.y;
101             }
102
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)
107             {
108                if(*this == Rect())
109                {
110                   return rhs;
111                }
112                if(rhs == Rect())
113                {
114                   return *this;
115                }
116                Rect overlap;
117                if(get_left() < rhs.get_right())
118                {
119                   overlap.pos.x = std::max(get_left(), rhs.get_left());
120                }
121                else
122                {
123                   return Rect();
124                }
125                if(rhs.get_left() < get_right())
126                {
127                   overlap.size.x = std::min(rhs.get_right(), get_right()) - overlap.pos.x;
128                }
129                else
130                {
131                   return Rect();
132                }
133                if(get_top() < rhs.get_bottom())
134                {
135                   overlap.pos.y = std::max(get_top(), rhs.get_top());
136                }
137                else
138                {
139                   return Rect();
140                }
141                if(rhs.get_top() < get_bottom())
142                {
143                   overlap.size.y = std::min(rhs.get_bottom(), get_bottom()) - overlap.pos.y;
144                }
145                else
146                {
147                   return Rect();
148                }
149                return overlap;
150             }
151
152             /// Allow rectangles to be treated like SDL_Rect
153             /// \return The equavalent SDL_Rect
154             operator SDL_Rect () const
155             {
156                rect.x = pos.x;
157                rect.y = pos.y;
158                rect.w = size.x;
159                rect.h = size.y;
160                return rect;
161             }
162
163             /// Allow rectangles to be treated like SDL_Rect
164             /// \return The internal SDL_Rect
165             SDL_Rect *operator &() const
166             {
167                if(*this == Rect())
168                {
169                   return 0;
170                }
171                else
172                {
173                   rect.x = pos.x;
174                   rect.y = pos.y;
175                   rect.w = size.x;
176                   rect.h = size.y;
177                   return &rect;
178                }
179             }
180          private:
181             mutable SDL_Rect rect;
182       };
183    }
184 }
185
186 #endif