1 /* Copyright (C) 2015 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19 Shapes.h
20 
21 --Overview--
22 
23 	Classes mostly useful for representing 2D screen overlays;
24 	includes functionality for overlay position, color, texture and borders.
25 
26 	CColor is used more widely for various game systems.
27 */
28 
29 #ifndef INCLUDED_SHAPES
30 #define INCLUDED_SHAPES
31 
32 #include "graphics/SColor.h"
33 
34 class CStr8;
35 
36 struct CColor
37 {
CColorCColor38 	CColor() : r(-1.f), g(-1.f), b(-1.f), a(1.f) {}
CColorCColor39 	CColor(float cr,float cg,float cb,float ca) : r(cr), g(cg), b(cb), a(ca) {}
40 
41 	bool ParseString(const CStr8& Value, int DefaultAlpha = 255);
42 
43 	bool operator == (const CColor &color) const;
44 
45 	bool operator != (const CColor &color) const
46 	{
47 		return !(*this==color);
48 	}
49 
50 	// For passing to glColor[34]fv:
FloatArrayCColor51 	const float* FloatArray() const { return &r; }
52 
53 	// For passing to CRenderer:
AsSColor4ubCColor54 	SColor4ub AsSColor4ub() const
55 	{
56 		return SColor4ub((u8)(r*255.0), (u8)(g*255.0), (u8)(b*255.0), (u8)(a*255.0));
57 	}
58 
59 	float r, g, b, a;
60 };
61 
62 class CPos;
63 class CSize;
64 
65 
66 /**
67  * Rectangle class used for screen rectangles. It's very similar to the MS
68  * CRect, but with FLOATS because it's meant to be used with OpenGL which
69  * takes float values.
70  *
71  * Changed to floats 2004-08-31 /GL
72  */
73 class CRect
74 {
75 public:
76 	CRect();
77 	CRect(const CPos &pos);
78 	CRect(const CSize &size);
79 	CRect(const CPos &upperleft, const CPos &bottomright);
80 	CRect(const CPos &pos, const CSize &size);
81 	CRect(const float l, const float t, const float r, const float b);
82 
83 	// Operators
84 	CRect&				operator =  (const CRect& a);
85 	bool				operator == (const CRect& a) const;
86 	bool				operator != (const CRect& a) const;
87 	CRect				operator -	(void) const;
88 	CRect				operator +	(void) const;
89 
90 	CRect				operator +  (const CRect& a) const;
91 	CRect				operator +  (const CPos& a) const;
92 	CRect				operator +  (const CSize& a) const;
93 	CRect				operator -  (const CRect& a) const;
94 	CRect				operator -  (const CPos& a) const;
95 	CRect				operator -  (const CSize& a) const;
96 
97 	void				operator += (const CRect& a);
98 	void				operator += (const CPos& a);
99 	void				operator += (const CSize& a);
100 	void				operator -= (const CRect& a);
101 	void				operator -= (const CPos& a);
102 	void				operator -= (const CSize& a);
103 
104 	/**
105 	 * @return Width of Rectangle
106 	 */
107 	float GetWidth() const;
108 
109 	/**
110 	 * @return Height of Rectangle
111 	 */
112 	float GetHeight() const;
113 
114 	/**
115 	 * Get Size
116 	 */
117 	CSize GetSize() const;
118 
119 	/**
120 	 * Get Position equivalent to top/left corner
121 	 */
122 	CPos TopLeft() const;
123 
124 	/**
125 	 * Get Position equivalent to top/right corner
126 	 */
127 	CPos TopRight() const;
128 
129 	/**
130 	 * Get Position equivalent to bottom/left corner
131 	 */
132 	CPos BottomLeft() const;
133 
134 	/**
135 	 * Get Position equivalent to bottom/right corner
136 	 */
137 	CPos BottomRight() const;
138 
139 	/**
140 	 * Get Position equivalent to the center of the rectangle
141 	 */
142 	CPos CenterPoint() const;
143 
144 	/**
145 	 * Evalutates if point is within the rectangle
146 	 * @param point CPos representing point
147 	 * @return true if inside.
148 	 */
149 	bool PointInside(const CPos &point) const;
150 
151 	CRect Scale(float x, float y) const;
152 
153 	/**
154 	 * Returning CPos representing each corner.
155 	 */
156 
157 public:
158 	/**
159 	 * Dimensions
160 	 */
161 	float left, top, right, bottom;
162 };
163 
164 /**
165  * Made to represent screen positions and delta values.
166  * @see CRect
167  * @see CSize
168  */
169 class CPos
170 {
171 public:
172 	CPos();
173 	CPos(const CSize &pos);
174 	CPos(const float &_x, const float &_y);
175 
176 	// Operators
177 	CPos&				operator =  (const CPos& a);
178 	bool				operator == (const CPos& a) const;
179 	bool				operator != (const CPos& a) const;
180 	CPos				operator -	(void) const;
181 	CPos				operator +	(void) const;
182 
183 	CPos				operator +  (const CPos& a) const;
184 	CPos				operator +  (const CSize& a) const;
185 	CPos				operator -  (const CPos& a) const;
186 	CPos				operator -  (const CSize& a) const;
187 
188 	void				operator += (const CPos& a);
189 	void				operator += (const CSize& a);
190 	void				operator -= (const CPos& a);
191 	void				operator -= (const CSize& a);
192 
193 public:
194 	/**
195 	 * Position
196 	 */
197 	float x, y;
198 };
199 
200 /**
201  * Made to represent a screen size, should in philosophy
202  * be made of unsigned ints, but for the sake of compatibility
203  * with CRect and CPos it's not.
204  * @see CRect
205  * @see CPos
206  */
207 class CSize
208 {
209 public:
210 	CSize();
211 	CSize(const CRect &rect);
212 	CSize(const CPos &pos);
213 	CSize(const float &_cx, const float &_cy);
214 
215 	// Operators
216 	CSize&				operator =  (const CSize& a);
217 	bool				operator == (const CSize& a) const;
218 	bool				operator != (const CSize& a) const;
219 	CSize				operator -	(void) const;
220 	CSize				operator +	(void) const;
221 
222 	CSize				operator +  (const CSize& a) const;
223 	CSize				operator -  (const CSize& a) const;
224 	CSize				operator /  (const float &a) const;
225 	CSize				operator *  (const float &a) const;
226 
227 	void				operator += (const CSize& a);
228 	void				operator -= (const CSize& a);
229 	void				operator /= (const float& a);
230 	void				operator *= (const float& a);
231 
232 public:
233 	/**
234 	 * Size
235 	 */
236 	float cx, cy;
237 };
238 
239 
240 #endif // INCLUDED_SHAPES
241