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 struct FLOAT_RECT { float left,right,top,bottom; };
26 
27 class C4Rect
28 {
29 public:
30  int32_t x = 0, y = 0, Wdt = 0, Hgt = 0;
31 public:
32  void Set(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt);
33  void Default();
34  bool Overlap(C4Rect &rTarget);
35  void Intersect(const C4Rect &r2);
36  void Add(const C4Rect &r2);
37  bool operator ==(const C4Rect &r2) const { return !((x-r2.x) | (y-r2.y) | (Wdt-r2.Wdt) | (Hgt-r2.Hgt)); }
38  bool operator !=(const C4Rect &r2) const { return 0 != ((x-r2.x) | (y-r2.y) | (Wdt-r2.Wdt) | (Hgt-r2.Hgt)); }
39 
40  bool Contains(int32_t iX, int32_t iY) const
41  { return iX>=x && iX<x+Wdt && iY>=y && iY<y+Hgt; }
42  bool Contains(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt) const
43  { return iX>=x && iX+iWdt<x+Wdt && iY>=y && iY+iHgt<y+Hgt; }
44  bool Contains(const C4Rect &rect) const
45  { return Contains(rect.x, rect.y, rect.Wdt, rect.Hgt); }
46  bool IntersectsLine(int32_t iX, int32_t iY, int32_t iX2, int32_t iY2);
47 
48  void Normalize()
49  { if (Wdt < 0) { x+=Wdt+1; Wdt=-Wdt; } if (Hgt < 0) { y+=Hgt+1; Hgt=-Hgt; } }
50 
51  void Enlarge(int32_t iByX, int32_t iByY)
52  { x -= iByX; y -= iByY; Wdt += 2*iByX; Hgt += 2*iByY; }
53  void Enlarge(int32_t iBy)
54  { Enlarge(iBy, iBy); }
55 
56  int32_t GetMiddleX() const { return x+Wdt/2; }
57  int32_t GetMiddleY() const { return y + Hgt / 2; }
58  int32_t GetBottom() const { return y + Hgt; }
59  int32_t GetTop() const { return y; }
60  int32_t GetLeft() const { return x; }
61  int32_t GetRight() const { return x + Wdt; }
62 
63  C4Rect() = default;
64  C4Rect(int32_t tx, int32_t ty, int32_t twdt, int32_t thgt) // ctor
65  { x=tx; y=ty; Wdt=twdt; Hgt=thgt; }
66  C4Rect(const FLOAT_RECT &rcfOuter) // set to surround floating point rectangle
67  {
68  x=static_cast<int32_t>(rcfOuter.left); y=static_cast<int32_t>(rcfOuter.top);
69  Wdt=static_cast<int32_t>(ceilf(rcfOuter.right)-floorf(rcfOuter.left));
70  Hgt=static_cast<int32_t>(ceilf(rcfOuter.bottom)-floorf(rcfOuter.top));
71  }
72 
73  void CompileFunc(StdCompiler *pComp);
74 };
75 
76 class C4TargetRect: public C4Rect
77 {
78 public:
79  int32_t tx,ty;
80 public:
81  C4TargetRect(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, int32_t iTX, int32_t iTY)
82  : C4Rect(iX, iY, iWdt, iHgt), tx(iTX), ty(iTY) { }
83  C4TargetRect() = default; // default ctor; doesn't initialize
84  void Set(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt, int32_t iTX, int32_t iTY);
85  void Default();
86  bool ClipBy(C4TargetRect &rClip); // clip this rectangle by the given one (adding target positions) - return false if they don't overlap
87 
88  void Set(const C4TargetFacet &rSrc); // copy contents from facet
89 
90  void CompileFunc(StdCompiler *pComp);
91 };
92 
93 const C4Rect Rect0(0,0,0,0);
94 const C4TargetRect TargetRect0(0,0,0,0,0,0);
95 
96 // a bunch of rectangles
97 // rects NOT including pos+size-point
98 class C4RectList : public std::vector<C4Rect>
99 {
100 public:
101  void AddRect(const C4Rect &rNewRect)
102  { push_back(rNewRect); }
103  void RemoveIndexedRect(int32_t idx)
104  { if (idx<int32_t(GetCount()-1)) Get(idx)=Get(GetCount()-1); pop_back(); }
105  void Clear() { clear(); }
106  size_t GetCount() const { return size(); }
107  C4Rect &Get(size_t idx) { return (*this)[idx]; } // access w/o range check
108 
109  void ClipByRect(const C4Rect &rClip); // split up rectangles
110 };
111 
112 
113 #endif // INC_C4Rect
void AddRect(const C4Rect &rNewRect)
Definition: C4Rect.h:101
int32_t GetLeft() const
Definition: C4Rect.h:60
bool Contains(int32_t iX, int32_t iY) const
Definition: C4Rect.h:40
bool Contains(int32_t iX, int32_t iY, int32_t iWdt, int32_t iHgt) const
Definition: C4Rect.h:42
size_t GetCount() const
Definition: C4Rect.h:106
float bottom
Definition: C4Rect.h:25
float right
Definition: C4Rect.h:25
float left
Definition: C4Rect.h:25
void CompileFunc(StdCompiler *pComp)
Definition: C4Rect.cpp:31
Definition: C4Rect.h:27
C4Rect(const FLOAT_RECT &rcfOuter)
Definition: C4Rect.h:66
void Normalize()
Definition: C4Rect.h:48
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:53
int32_t GetRight() const
Definition: C4Rect.h:61
int32_t GetMiddleX() const
Definition: C4Rect.h:56
int32_t Wdt
Definition: C4Rect.h:30
int32_t y
Definition: C4Rect.h:30
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:79
void RemoveIndexedRect(int32_t idx)
Definition: C4Rect.h:103
int32_t ty
Definition: C4Rect.h:79
void Default()
Definition: C4Rect.cpp:26
C4Rect & Get(size_t idx)
Definition: C4Rect.h:107
int32_t GetMiddleY() const
Definition: C4Rect.h:57
int32_t x
Definition: C4Rect.h:30
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:51
void CompileFunc(StdCompiler *pComp)
Definition: C4Rect.cpp:79
bool operator!=(const C4Rect &r2) const
Definition: C4Rect.h:38
C4Rect(int32_t tx, int32_t ty, int32_t twdt, int32_t thgt)
Definition: C4Rect.h:64
bool operator==(const C4Rect &r2) const
Definition: C4Rect.h:37
int32_t GetBottom() const
Definition: C4Rect.h:58
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:81
int32_t Hgt
Definition: C4Rect.h:30
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:105
float top
Definition: C4Rect.h:25
bool Contains(const C4Rect &rect) const
Definition: C4Rect.h:44
int32_t GetTop() const
Definition: C4Rect.h:59
C4TargetRect()=default