1 ////////////////////////////////////////////////////////////
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
21 // 3. This notice may not be removed or altered from any source distribution.
23 ////////////////////////////////////////////////////////////
25 #ifndef SFML_VERTEXARRAY_HPP
26 #define SFML_VERTEXARRAY_HPP
28 ////////////////////////////////////////////////////////////
30 ////////////////////////////////////////////////////////////
31 #include <SFML/Graphics/Export.hpp>
32 #include <SFML/Graphics/Vertex.hpp>
33 #include <SFML/Graphics/PrimitiveType.hpp>
34 #include <SFML/Graphics/Rect.hpp>
35 #include <SFML/Graphics/Drawable.hpp>
41 ////////////////////////////////////////////////////////////
42 /// \brief Define a set of one or more 2D primitives
44 ////////////////////////////////////////////////////////////
45 class SFML_GRAPHICS_API VertexArray : public Drawable
49 ////////////////////////////////////////////////////////////
50 /// \brief Default constructor
52 /// Creates an empty vertex array.
54 ////////////////////////////////////////////////////////////
57 ////////////////////////////////////////////////////////////
58 /// \brief Construct the vertex array with a type and an initial number of vertices
60 /// \param type Type of primitives
61 /// \param vertexCount Initial number of vertices in the array
63 ////////////////////////////////////////////////////////////
64 explicit VertexArray(PrimitiveType type, unsigned int vertexCount = 0);
66 ////////////////////////////////////////////////////////////
67 /// \brief Return the vertex count
69 /// \return Number of vertices in the array
71 ////////////////////////////////////////////////////////////
72 unsigned int getVertexCount() const;
74 ////////////////////////////////////////////////////////////
75 /// \brief Get a read-write access to a vertex by its index
77 /// This function doesn't check \a index, it must be in range
78 /// [0, getVertexCount() - 1]. The behaviour is undefined
81 /// \param index Index of the vertex to get
83 /// \return Reference to the index-th vertex
85 /// \see getVertexCount
87 ////////////////////////////////////////////////////////////
88 Vertex& operator [](unsigned int index);
90 ////////////////////////////////////////////////////////////
91 /// \brief Get a read-only access to a vertex by its index
93 /// This function doesn't check \a index, it must be in range
94 /// [0, getVertexCount() - 1]. The behaviour is undefined
97 /// \param index Index of the vertex to get
99 /// \return Const reference to the index-th vertex
101 /// \see getVertexCount
103 ////////////////////////////////////////////////////////////
104 const Vertex& operator [](unsigned int index) const;
106 ////////////////////////////////////////////////////////////
107 /// \brief Clear the vertex array
109 /// This function removes all the vertices from the array.
110 /// It doesn't deallocate the corresponding memory, so that
111 /// adding new vertices after clearing doesn't involve
112 /// reallocating all the memory.
114 ////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////
118 /// \brief Resize the vertex array
120 /// If \a vertexCount is greater than the current size, the previous
121 /// vertices are kept and new (default-constructed) vertices are
123 /// If \a vertexCount is less than the current size, existing vertices
124 /// are removed from the array.
126 /// \param vertexCount New size of the array (number of vertices)
128 ////////////////////////////////////////////////////////////
129 void resize(unsigned int vertexCount);
131 ////////////////////////////////////////////////////////////
132 /// \brief Add a vertex to the array
134 /// \param vertex Vertex to add
136 ////////////////////////////////////////////////////////////
137 void append(const Vertex& vertex);
139 ////////////////////////////////////////////////////////////
140 /// \brief Set the type of primitives to draw
142 /// This function defines how the vertices must be interpreted
143 /// when it's time to draw them:
148 /// The default primitive type is sf::Points.
150 /// \param type Type of primitive
152 ////////////////////////////////////////////////////////////
153 void setPrimitiveType(PrimitiveType type);
155 ////////////////////////////////////////////////////////////
156 /// \brief Get the type of primitives drawn by the vertex array
158 /// \return Primitive type
160 ////////////////////////////////////////////////////////////
161 PrimitiveType getPrimitiveType() const;
163 ////////////////////////////////////////////////////////////
164 /// \brief Compute the bounding rectangle of the vertex array
166 /// This function returns the axis-aligned rectangle that
167 /// contains all the vertices of the array.
169 /// \return Bounding rectangle of the vertex array
171 ////////////////////////////////////////////////////////////
172 FloatRect getBounds() const;
176 ////////////////////////////////////////////////////////////
177 /// \brief Draw the vertex array to a render target
179 /// \param target Render target to draw to
180 /// \param states Current render states
182 ////////////////////////////////////////////////////////////
183 virtual void draw(RenderTarget& target, RenderStates states) const;
187 ////////////////////////////////////////////////////////////
189 ////////////////////////////////////////////////////////////
190 std::vector<Vertex> m_vertices; ///< Vertices contained in the array
191 PrimitiveType m_primitiveType; ///< Type of primitives to draw
197 #endif // SFML_VERTEXARRAY_HPP
200 ////////////////////////////////////////////////////////////
201 /// \class sf::VertexArray
202 /// \ingroup graphics
204 /// sf::VertexArray is a very simple wrapper around a dynamic
205 /// array of vertices and a primitives type.
207 /// It inherits sf::Drawable, but unlike other drawables it
208 /// is not transformable.
212 /// sf::VertexArray lines(sf::LinesStrip, 4);
213 /// lines[0].position = sf::Vector2f(10, 0);
214 /// lines[1].position = sf::Vector2f(20, 0);
215 /// lines[2].position = sf::Vector2f(30, 5);
216 /// lines[3].position = sf::Vector2f(40, 2);
218 /// window.draw(lines);
223 ////////////////////////////////////////////////////////////