]> git.tdb.fi Git - libs/gl.git/blob - source/core/rect.h
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / rect.h
1 #ifndef MSP_GL_RECT_H_
2 #define MSP_GL_RECT_H_
3
4 #include <algorithm>
5 #include <limits>
6
7 namespace Msp {
8 namespace GL {
9
10 struct Rect
11 {
12         int left = 0;
13         int bottom = 0;
14         unsigned width = 0;
15         unsigned height = 0;
16
17         Rect() = default;
18         Rect(int l, int b, unsigned w, unsigned h): left(l), bottom(b), width(w), height(h) { }
19
20         static Rect max() { unsigned s = std::numeric_limits<unsigned>::max(); return Rect(0, 0, s, s); }
21
22         bool operator==(const Rect &) const;
23         bool operator!=(const Rect &r) const { return !operator==(r); }
24
25         Rect intersect(const Rect &) const;
26 };
27
28 inline bool Rect::operator==(const Rect &other) const
29 {
30         return left==other.left && bottom==other.bottom && width==other.width && height==other.height;
31 }
32
33 inline Rect Rect::intersect(const Rect &other) const
34 {
35         auto intersect_axis = [](int &s1, unsigned &z1, int s2, unsigned z2){
36                 if(s2>s1)
37                 {
38                         unsigned d = s2-s1;
39                         z1 = (d>z1 ? 0 : std::min(z1-d, z2));
40                         s1 = s2;
41                 }
42                 else
43                 {
44                         unsigned d = s1-s2;
45                         z1 = (d>z2 ? 0 : std::min(z2-d, z1));
46                 }
47         };
48
49         Rect result = *this;
50         intersect_axis(result.left, result.width, other.left, other.width);
51         intersect_axis(result.bottom, result.height, other.bottom, other.height);
52         return result;
53 }
54
55 } // namespace GL
56 } // namespace Msp
57
58 #endif