OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Rect.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Basic classes for rectangles and vertex outlines */
19 
20 #ifndef INC_C4Rect
21 #define INC_C4Rect
22 
23 #define C4D_VertexCpyPos (C4D_MaxVertex/2)
24 
25 #include <vector>
26 
27 struct FLOAT_RECT { float left,right,top,bottom; };
28 
29 class C4Rect
30 {
31 public:
32  int32_t x = 0, y = 0, Wdt = 0, Hgt = 0;
33 public:
34  void Set(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt);
35  void Default();
36  bool Overlap(C4Rect &rTarget);
37  void Intersect(const C4Rect &r2);
38  void Add(const C4Rect &r2);
39  bool operator ==(const C4Rect &r2) const { return !((x-r2.x) | (y-r2.y) | (Wdt-r2.Wdt) | (Hgt-r2.Hgt)); }
40  bool operator !=(const C4Rect &r2) const { return 0 != ((x-r2.x) | (y-r2.y) | (Wdt-r2.Wdt) | (Hgt-r2.Hgt)); }
41 
42  bool Contains(int32_t iX, int32_t iY) const
43  { return iX>=x && iX<x+Wdt && iY>=y && iY<y+Hgt; }
44  bool Contains(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt) const
45  { return iX>=x && iX+iWdt<x+Wdt && iY>=y && iY+iHgt<y+Hgt; }
46  bool Contains(const C4Rect &rect) const
47  { return Contains(rect.x, rect.y, rect.Wdt, rect.Hgt); }
48  bool IntersectsLine(int32_t iX, int32_t iY, int32_t iX2, int32_t iY2);
49 
50  void Normalize()
51  { if (Wdt < 0) { x+=Wdt+1; Wdt=-Wdt; } if (Hgt < 0) { y+=Hgt+1; Hgt=-Hgt; } }
52 
53  void Enlarge(int32_t iByX, int32_t iByY)
54  { x -= iByX; y -= iByY; Wdt += 2*iByX; Hgt += 2*iByY; }
55  void Enlarge(int32_t iBy)
56  { Enlarge(iBy, iBy); }
57 
58  int32_t GetMiddleX() const { return x+Wdt/2; }
59  int32_t GetMiddleY() const { return y + Hgt / 2; }
60  int32_t GetBottom() const { return y + Hgt; }
61  int32_t GetTop() const { return y; }
62  int32_t GetLeft() const { return x; }
63  int32_t GetRight() const { return x + Wdt; }
64 
65  C4Rect() = default;
66  C4Rect(int32_t tx, int32_t ty, int32_t twdt, int32_t thgt) // ctor
67  { x=tx; y=ty; Wdt=twdt; Hgt=thgt; }
68  C4Rect(const FLOAT_RECT &rcfOuter) // set to surround floating point rectangle
69  {
70  x=static_cast<int32_t>(rcfOuter.left); y=static_cast<int32_t>(rcfOuter.top);
71  Wdt=static_cast<int32_t>(ceilf(rcfOuter.right)-floorf(rcfOuter.left));
72  Hgt=static_cast<int32_t>(ceilf(rcfOuter.bottom)-floorf(rcfOuter.top));
73  }
74 
75  void CompileFunc(StdCompiler *pComp);
76 };
77 
78 class C4TargetRect: public C4Rect
79 {
80 public:
81  int32_t tx,ty;
82 public:
83  C4TargetRect(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, int32_t iTX, int32_t iTY)
84  : C4Rect(iX, iY, iWdt, iHgt), tx(iTX), ty(iTY) { }
85  C4TargetRect() { } // default ctor; doesn't initialize
86  void Set(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, int32_t iTX, int32_t iTY);
87  void Default();
88  bool ClipBy(C4TargetRect &rClip); // clip this rectangle by the given one (adding target positions) - return false if they don't overlap
89 
90  void Set(const C4TargetFacet &rSrc); // copy contents from facet
91 
92  void CompileFunc(StdCompiler *pComp);
93 };
94 
95 const C4Rect Rect0(0,0,0,0);
96 const C4TargetRect TargetRect0(0,0,0,0,0,0);
97 
98 // a bunch of rectangles
99 // rects NOT including pos+size-point
100 class C4RectList : public std::vector<C4Rect>
101 {
102 public:
103  void AddRect(const C4Rect &rNewRect)
104  { push_back(rNewRect); }
105  void RemoveIndexedRect(int32_t idx)
106  { if (idx<int32_t(GetCount()-1)) Get(idx)=Get(GetCount()-1); pop_back(); }
107  void Clear() { clear(); }
108  size_t GetCount() const { return size(); }
109  C4Rect &Get(size_t idx) { return (*this)[idx]; } // access w/o range check
110 
111  void ClipByRect(const C4Rect &rClip); // split up rectangles
112 };
113 
114 
115 #endif // INC_C4Rect
void AddRect(const C4Rect &rNewRect)
Definition: C4Rect.h:103
int32_t GetLeft() const
Definition: C4Rect.h:62
bool Contains(int32_t iX, int32_t iY) const
Definition: C4Rect.h:42
bool Contains(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt) const
Definition: C4Rect.h:44
size_t GetCount() const
Definition: C4Rect.h:108
float bottom
Definition: C4Rect.h:27
float right
Definition: C4Rect.h:27
float left
Definition: C4Rect.h:27
void CompileFunc(StdCompiler *pComp)
Definition: C4Rect.cpp:31
Definition: C4Rect.h:29
C4Rect(const FLOAT_RECT &rcfOuter)
Definition: C4Rect.h:68
void Normalize()
Definition: C4Rect.h:50
void Intersect(const C4Rect &r2)
Definition: C4Rect.cpp:100
const C4TargetRect TargetRect0(0, 0, 0, 0, 0, 0)
void Enlarge(int32_t iBy)
Definition: C4Rect.h:55
int32_t GetRight() const
Definition: C4Rect.h:63
int32_t GetMiddleX() const
Definition: C4Rect.h:58
int32_t Wdt
Definition: C4Rect.h:32
int32_t y
Definition: C4Rect.h:32
bool ClipBy(C4TargetRect &rClip)
Definition: C4Rect.cpp:51
void Add(const C4Rect &r2)
Definition: C4Rect.cpp:144
const C4Rect Rect0(0, 0, 0, 0)
int32_t tx
Definition: C4Rect.h:81
void RemoveIndexedRect(int32_t idx)
Definition: C4Rect.h:105
int32_t ty
Definition: C4Rect.h:81
void Default()
Definition: C4Rect.cpp:26
C4Rect & Get(size_t idx)
Definition: C4Rect.h:109
int32_t GetMiddleY() const
Definition: C4Rect.h:59
int32_t x
Definition: C4Rect.h:32
void Set(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt)
Definition: C4Rect.cpp:86
void ClipByRect(const C4Rect &rClip)
Definition: C4Rect.cpp:173
C4Rect()=default
void Enlarge(int32_t iByX, int32_t iByY)
Definition: C4Rect.h:53
void CompileFunc(StdCompiler *pComp)
Definition: C4Rect.cpp:79
bool operator!=(const C4Rect &r2) const
Definition: C4Rect.h:40
C4Rect(int32_t tx, int32_t ty, int32_t twdt, int32_t thgt)
Definition: C4Rect.h:66
C4TargetRect()
Definition: C4Rect.h:85
bool operator==(const C4Rect &r2) const
Definition: C4Rect.h:39
int32_t GetBottom() const
Definition: C4Rect.h:60
bool Overlap(C4Rect &rTarget)
Definition: C4Rect.cpp:91
bool IntersectsLine(int32_t iX, int32_t iY, int32_t iX2, int32_t iY2)
Definition: C4Rect.cpp:122
C4TargetRect(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, int32_t iTX, int32_t iTY)
Definition: C4Rect.h:83
int32_t Hgt
Definition: C4Rect.h:32
void Set(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, int32_t iTX, int32_t iTY)
Definition: C4Rect.cpp:45
void Default()
Definition: C4Rect.cpp:39
void Clear()
Definition: C4Rect.h:107
float top
Definition: C4Rect.h:27
bool Contains(const C4Rect &rect) const
Definition: C4Rect.h:46
int32_t GetTop() const
Definition: C4Rect.h:61