]> git.sesse.net Git - casparcg/blob - SFML-1.6/include/SFML/Graphics/Shape.hpp
(no commit message)
[casparcg] / SFML-1.6 / include / SFML / Graphics / Shape.hpp
1 ////////////////////////////////////////////////////////////\r
2 //\r
3 // SFML - Simple and Fast Multimedia Library\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)\r
5 //\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
8 //\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
12 //\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
17 //\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
20 //\r
21 // 3. This notice may not be removed or altered from any source distribution.\r
22 //\r
23 ////////////////////////////////////////////////////////////\r
24 \r
25 #ifndef SFML_SHAPE_HPP\r
26 #define SFML_SHAPE_HPP\r
27 \r
28 ////////////////////////////////////////////////////////////\r
29 // Headers\r
30 ////////////////////////////////////////////////////////////\r
31 #include <SFML/Graphics/Drawable.hpp>\r
32 #include <SFML/System/Vector2.hpp>\r
33 #include <vector>\r
34 \r
35 \r
36 namespace sf\r
37 {\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
44 {\r
45 public :\r
46 \r
47     ////////////////////////////////////////////////////////////\r
48     /// Default constructor\r
49     ///\r
50     ////////////////////////////////////////////////////////////\r
51     Shape();\r
52 \r
53     ////////////////////////////////////////////////////////////\r
54     /// Add a point to the shape\r
55     ///\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
59     ///\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
62 \r
63     ////////////////////////////////////////////////////////////\r
64     /// Add a point to the shape\r
65     ///\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
69     ///\r
70     ////////////////////////////////////////////////////////////\r
71     void AddPoint(const Vector2f& Position, const Color& Col = Color(255, 255, 255), const Color& OutlineCol = Color(0, 0, 0));\r
72 \r
73     ////////////////////////////////////////////////////////////\r
74     /// Get the number of points composing the shape\r
75     ///\r
76     /// \param Total number of points\r
77     ///\r
78     ////////////////////////////////////////////////////////////\r
79     unsigned int GetNbPoints() const;\r
80 \r
81     ////////////////////////////////////////////////////////////\r
82     /// Enable or disable filling the shape.\r
83     /// Fill is enabled by default\r
84     ///\r
85     /// \param Enable : True to enable, false to disable\r
86     ///\r
87     ////////////////////////////////////////////////////////////\r
88     void EnableFill(bool Enable);\r
89 \r
90     ////////////////////////////////////////////////////////////\r
91     /// Enable or disable drawing the shape outline.\r
92     /// Outline is enabled by default\r
93     ///\r
94     /// \param Enable : True to enable, false to disable\r
95     ///\r
96     ////////////////////////////////////////////////////////////\r
97     void EnableOutline(bool Enable);\r
98 \r
99     ////////////////////////////////////////////////////////////\r
100     /// Set the position of a point\r
101     ///\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
104     ///\r
105     ////////////////////////////////////////////////////////////\r
106     void SetPointPosition(unsigned int Index, const Vector2f& Position);\r
107 \r
108     ////////////////////////////////////////////////////////////\r
109     /// Set the position of a point\r
110     ///\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
114     ///\r
115     ////////////////////////////////////////////////////////////\r
116     void SetPointPosition(unsigned int Index, float X, float Y);\r
117 \r
118     ////////////////////////////////////////////////////////////\r
119     /// Set the color of a point\r
120     ///\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
123     ///\r
124     ////////////////////////////////////////////////////////////\r
125     void SetPointColor(unsigned int Index, const Color& Col);\r
126 \r
127     ////////////////////////////////////////////////////////////\r
128     /// Set the outline color of a point\r
129     ///\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
132     ///\r
133     ////////////////////////////////////////////////////////////\r
134     void SetPointOutlineColor(unsigned int Index, const Color& OutlineCol);\r
135 \r
136     ////////////////////////////////////////////////////////////\r
137     /// Change the width of the shape outline\r
138     ///\r
139     /// \param Width : New width\r
140     ///\r
141     ////////////////////////////////////////////////////////////\r
142     void SetOutlineWidth(float Width);\r
143 \r
144     ////////////////////////////////////////////////////////////\r
145     /// Get the position of a point\r
146     ///\r
147     /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]\r
148     ///\r
149     /// \return Position of the Index-th point\r
150     ///\r
151     ////////////////////////////////////////////////////////////\r
152     const Vector2f& GetPointPosition(unsigned int Index) const;\r
153 \r
154     ////////////////////////////////////////////////////////////\r
155     /// Get the color of a point\r
156     ///\r
157     /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]\r
158     ///\r
159     /// \return Color of the Index-th point\r
160     ///\r
161     ////////////////////////////////////////////////////////////\r
162     const Color& GetPointColor(unsigned int Index) const;\r
163 \r
164     ////////////////////////////////////////////////////////////\r
165     /// Get the outline color of a point\r
166     ///\r
167     /// \param Index : Index of the point, in range [0, GetNbPoints() - 1]\r
168     ///\r
169     /// \return Outline color of the Index-th point\r
170     ///\r
171     ////////////////////////////////////////////////////////////\r
172     const Color& GetPointOutlineColor(unsigned int Index) const;\r
173 \r
174     ////////////////////////////////////////////////////////////\r
175     /// Get the width of the shape outline\r
176     ///\r
177     /// \return Current outline width\r
178     ///\r
179     ////////////////////////////////////////////////////////////\r
180     float GetOutlineWidth() const;\r
181 \r
182     ////////////////////////////////////////////////////////////\r
183     /// Create a shape made of a single line (use floats)\r
184     ///\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
191     ///\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
194 \r
195     ////////////////////////////////////////////////////////////\r
196     /// Create a shape made of a single line (use vectors)\r
197     ///\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
204     ///\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
207 \r
208     ////////////////////////////////////////////////////////////\r
209     /// Create a shape made of a single rectangle (use floats)\r
210     ///\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
216     ///\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
219 \r
220     ////////////////////////////////////////////////////////////\r
221     /// Create a shape made of a single rectangle (use vectors)\r
222     ///\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
228     ///\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
231 \r
232     ////////////////////////////////////////////////////////////\r
233     /// Create a shape made of a single circle (use floats)\r
234     ///\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
240     ///\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
243 \r
244     ////////////////////////////////////////////////////////////\r
245     /// Create a shape made of a single circle (use vectors)\r
246     ///\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
252     ///\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
255 \r
256 protected :\r
257 \r
258     ////////////////////////////////////////////////////////////\r
259     /// /see Drawable::Render\r
260     ///\r
261     ////////////////////////////////////////////////////////////\r
262     virtual void Render(RenderTarget& Target) const;\r
263 \r
264 private :\r
265 \r
266     ////////////////////////////////////////////////////////////\r
267     /// Compile the shape : compute its center and its outline\r
268     ///\r
269     ////////////////////////////////////////////////////////////\r
270     void Compile();\r
271 \r
272     ////////////////////////////////////////////////////////////\r
273     /// Compute the normal of a given 2D segment\r
274     ///\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
278     ///\r
279     /// \return False if the normal couldn't be calculated (segment is null)\r
280     ///\r
281     ////////////////////////////////////////////////////////////\r
282     static bool ComputeNormal(const Vector2f& P1, const Vector2f& P2, Vector2f& Normal);\r
283 \r
284     ////////////////////////////////////////////////////////////\r
285     /// Defines a simple 2D point\r
286     ////////////////////////////////////////////////////////////\r
287     struct Point\r
288     {\r
289         Point(const Vector2f& Pos = Vector2f(0, 0), const Color& C = Color(255, 255, 255), const Color& OutlineC = Color(255, 255, 255));\r
290 \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
295     };\r
296 \r
297     ////////////////////////////////////////////////////////////\r
298     // Member data\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
305 };\r
306 \r
307 } // namespace sf\r
308 \r
309 \r
310 #endif // SFML_SHAPE_HPP\r