1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_SHAPE_HPP
\r
26 #define SFML_SHAPE_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/Graphics/Drawable.hpp>
\r
32 #include <SFML/System/Vector2.hpp>
\r
38 ////////////////////////////////////////////////////////////
\r
39 /// Shape defines a drawable convex shape ; it also defines
\r
40 /// helper functions to draw simple shapes like
\r
41 /// lines, rectangles, circles, etc.
\r
42 ////////////////////////////////////////////////////////////
\r
43 class SFML_API Shape : public sf::Drawable
\r
47 ////////////////////////////////////////////////////////////
\r
48 /// Default constructor
\r
50 ////////////////////////////////////////////////////////////
\r
53 ////////////////////////////////////////////////////////////
\r
54 /// Add a point to the shape
\r
56 /// \param X, Y : Position of the point
\r
57 /// \param Col : Color of the point (white by default)
\r
58 /// \param OutlineCol : Outline color of the point (black by default)
\r
60 ////////////////////////////////////////////////////////////
\r
61 void AddPoint(float X, float Y, const Color& Col = Color(255, 255, 255), const Color& OutlineCol = Color(0, 0, 0));
\r
63 ////////////////////////////////////////////////////////////
\r
64 /// Add a point to the shape
\r
66 /// \param Position : Position of the point
\r
67 /// \param Col : Color of the point (white by default)
\r
68 /// \param OutlineCol : Outline color of the point (black by default)
\r
70 ////////////////////////////////////////////////////////////
\r
71 void AddPoint(const Vector2f& Position, const Color& Col = Color(255, 255, 255), const Color& OutlineCol = Color(0, 0, 0));
\r
73 ////////////////////////////////////////////////////////////
\r
74 /// Get the number of points composing the shape
\r
76 /// \param Total number of points
\r
78 ////////////////////////////////////////////////////////////
\r
79 unsigned int GetNbPoints() const;
\r
81 ////////////////////////////////////////////////////////////
\r
82 /// Enable or disable filling the shape.
\r
83 /// Fill is enabled by default
\r
85 /// \param Enable : True to enable, false to disable
\r
87 ////////////////////////////////////////////////////////////
\r
88 void EnableFill(bool Enable);
\r
90 ////////////////////////////////////////////////////////////
\r
91 /// Enable or disable drawing the shape outline.
\r
92 /// Outline is enabled by default
\r
94 /// \param Enable : True to enable, false to disable
\r
96 ////////////////////////////////////////////////////////////
\r
97 void EnableOutline(bool Enable);
\r
99 ////////////////////////////////////////////////////////////
\r
100 /// Set the position of a point
\r
102 /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]
\r
103 /// \param Position : New position of the Index-th point
\r
105 ////////////////////////////////////////////////////////////
\r
106 void SetPointPosition(unsigned int Index, const Vector2f& Position);
\r
108 ////////////////////////////////////////////////////////////
\r
109 /// Set the position of a point
\r
111 /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]
\r
112 /// \param X : New X coordinate of the Index-th point
\r
113 /// \param Y : New Y coordinate of the Index-th point
\r
115 ////////////////////////////////////////////////////////////
\r
116 void SetPointPosition(unsigned int Index, float X, float Y);
\r
118 ////////////////////////////////////////////////////////////
\r
119 /// Set the color of a point
\r
121 /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]
\r
122 /// \param Col : New color of the Index-th point
\r
124 ////////////////////////////////////////////////////////////
\r
125 void SetPointColor(unsigned int Index, const Color& Col);
\r
127 ////////////////////////////////////////////////////////////
\r
128 /// Set the outline color of a point
\r
130 /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]
\r
131 /// \param OutlineCol : New outline color of the Index-th point
\r
133 ////////////////////////////////////////////////////////////
\r
134 void SetPointOutlineColor(unsigned int Index, const Color& OutlineCol);
\r
136 ////////////////////////////////////////////////////////////
\r
137 /// Change the width of the shape outline
\r
139 /// \param Width : New width
\r
141 ////////////////////////////////////////////////////////////
\r
142 void SetOutlineWidth(float Width);
\r
144 ////////////////////////////////////////////////////////////
\r
145 /// Get the position of a point
\r
147 /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]
\r
149 /// \return Position of the Index-th point
\r
151 ////////////////////////////////////////////////////////////
\r
152 const Vector2f& GetPointPosition(unsigned int Index) const;
\r
154 ////////////////////////////////////////////////////////////
\r
155 /// Get the color of a point
\r
157 /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]
\r
159 /// \return Color of the Index-th point
\r
161 ////////////////////////////////////////////////////////////
\r
162 const Color& GetPointColor(unsigned int Index) const;
\r
164 ////////////////////////////////////////////////////////////
\r
165 /// Get the outline color of a point
\r
167 /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]
\r
169 /// \return Outline color of the Index-th point
\r
171 ////////////////////////////////////////////////////////////
\r
172 const Color& GetPointOutlineColor(unsigned int Index) const;
\r
174 ////////////////////////////////////////////////////////////
\r
175 /// Get the width of the shape outline
\r
177 /// \return Current outline width
\r
179 ////////////////////////////////////////////////////////////
\r
180 float GetOutlineWidth() const;
\r
182 ////////////////////////////////////////////////////////////
\r
183 /// Create a shape made of a single line (use floats)
\r
185 /// \param P1X, P1Y : Position of the first point
\r
186 /// \param P2X, P2Y : Position second point
\r
187 /// \param Thickness : Line thickness
\r
188 /// \param Col : Color used to draw the line
\r
189 /// \param Outline : Outline width (0 by default)
\r
190 /// \param OutlineCol : Color used to draw the outline (black by default)
\r
192 ////////////////////////////////////////////////////////////
\r
193 static Shape Line(float P1X, float P1Y, float P2X, float P2Y, float Thickness, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0));
\r
195 ////////////////////////////////////////////////////////////
\r
196 /// Create a shape made of a single line (use vectors)
\r
198 /// \param P1X, P1Y : Position of the first point
\r
199 /// \param P2X, P2Y : Position second point
\r
200 /// \param Thickness : Line thickness
\r
201 /// \param Col : Color used to draw the line
\r
202 /// \param Outline : Outline width (0 by default)
\r
203 /// \param OutlineCol : Color used to draw the outline (black by default)
\r
205 ////////////////////////////////////////////////////////////
\r
206 static Shape Line(const Vector2f& P1, const Vector2f& P2, float Thickness, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0));
\r
208 ////////////////////////////////////////////////////////////
\r
209 /// Create a shape made of a single rectangle (use floats)
\r
211 /// \param P1X, P1Y : Position of the first point
\r
212 /// \param P2X, P2Y : Position second point
\r
213 /// \param Col : Color used to fill the rectangle
\r
214 /// \param Outline : Outline width (0 by default)
\r
215 /// \param OutlineCol : Color used to draw the outline (black by default)
\r
217 ////////////////////////////////////////////////////////////
\r
218 static Shape Rectangle(float P1X, float P1Y, float P2X, float P2Y, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0));
\r
220 ////////////////////////////////////////////////////////////
\r
221 /// Create a shape made of a single rectangle (use vectors)
\r
223 /// \param P1 : Position of the first point
\r
224 /// \param P2 : Position second point
\r
225 /// \param Col : Color used to fill the rectangle
\r
226 /// \param Outline : Outline width (0 by default)
\r
227 /// \param OutlineCol : Color used to draw the outline (black by default)
\r
229 ////////////////////////////////////////////////////////////
\r
230 static Shape Rectangle(const Vector2f& P1, const Vector2f& P2, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0));
\r
232 ////////////////////////////////////////////////////////////
\r
233 /// Create a shape made of a single circle (use floats)
\r
235 /// \param X, Y : Position of the center
\r
236 /// \param Radius : Radius
\r
237 /// \param Col : Color used to fill the circle
\r
238 /// \param Outline : Outline width (0 by default)
\r
239 /// \param OutlineCol : Color used to draw the outline (black by default)
\r
241 ////////////////////////////////////////////////////////////
\r
242 static Shape Circle(float X, float Y, float Radius, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0));
\r
244 ////////////////////////////////////////////////////////////
\r
245 /// Create a shape made of a single circle (use vectors)
\r
247 /// \param Center : Position of the center
\r
248 /// \param Radius : Radius
\r
249 /// \param Col : Color used to fill the circle
\r
250 /// \param Outline : Outline width (0 by default)
\r
251 /// \param OutlineCol : Color used to draw the outline (black by default)
\r
253 ////////////////////////////////////////////////////////////
\r
254 static Shape Circle(const Vector2f& Center, float Radius, const Color& Col, float Outline = 0.f, const Color& OutlineCol = sf::Color(0, 0, 0));
\r
258 ////////////////////////////////////////////////////////////
\r
259 /// /see Drawable::Render
\r
261 ////////////////////////////////////////////////////////////
\r
262 virtual void Render(RenderTarget& Target) const;
\r
266 ////////////////////////////////////////////////////////////
\r
267 /// Compile the shape : compute its center and its outline
\r
269 ////////////////////////////////////////////////////////////
\r
272 ////////////////////////////////////////////////////////////
\r
273 /// Compute the normal of a given 2D segment
\r
275 /// \param P1 : First point of the segment
\r
276 /// \param P2 : Second point of the segment
\r
277 /// \param Normal : Calculated normal
\r
279 /// \return False if the normal couldn't be calculated (segment is null)
\r
281 ////////////////////////////////////////////////////////////
\r
282 static bool ComputeNormal(const Vector2f& P1, const Vector2f& P2, Vector2f& Normal);
\r
284 ////////////////////////////////////////////////////////////
\r
285 /// Defines a simple 2D point
\r
286 ////////////////////////////////////////////////////////////
\r
289 Point(const Vector2f& Pos = Vector2f(0, 0), const Color& C = Color(255, 255, 255), const Color& OutlineC = Color(255, 255, 255));
\r
291 Vector2f Position; ///< Position
\r
292 Vector2f Normal; ///< Extruded normal
\r
293 Color Col; ///< Color of the point
\r
294 Color OutlineCol; ///< Outline color of the point
\r
297 ////////////////////////////////////////////////////////////
\r
299 ////////////////////////////////////////////////////////////
\r
300 std::vector<Point> myPoints; ///< Points composing the shape
\r
301 float myOutline; ///< Outline width
\r
302 bool myIsFillEnabled; ///< Should we draw the inside if the shape ?
\r
303 bool myIsOutlineEnabled; ///< Should we draw the outline if the shape ?
\r
304 bool myIsCompiled; ///< Compiled state of the shape
\r
310 #endif // SFML_SHAPE_HPP
\r