diff --git a/include/SFML/Graphics/Shape.hpp b/include/SFML/Graphics/Shape.hpp index 94990f29..20c933a1 100644 --- a/include/SFML/Graphics/Shape.hpp +++ b/include/SFML/Graphics/Shape.hpp @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -305,15 +306,17 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - const Texture* m_texture; ///< Texture of the shape - IntRect m_textureRect; ///< Rectangle defining the area of the source texture to display - Color m_fillColor; ///< Fill color - Color m_outlineColor; ///< Outline color - float m_outlineThickness; ///< Thickness of the shape's outline - VertexArray m_vertices; ///< Vertex array containing the fill geometry - VertexArray m_outlineVertices; ///< Vertex array containing the outline geometry - FloatRect m_insideBounds; ///< Bounding rectangle of the inside (fill) - FloatRect m_bounds; ///< Bounding rectangle of the whole shape (outline + fill) + const Texture* m_texture; ///< Texture of the shape + IntRect m_textureRect; ///< Rectangle defining the area of the source texture to display + Color m_fillColor; ///< Fill color + Color m_outlineColor; ///< Outline color + float m_outlineThickness; ///< Thickness of the shape's outline + VertexArray m_vertices; ///< Vertex array containing the fill geometry + VertexArray m_outlineVertices; ///< Vertex array containing the outline geometry + VertexBuffer m_verticesBuffer; ///< Vertex buffer containing the fill geometry + VertexBuffer m_outlineVerticesBuffer; ///< Vertex buffer containing the outline geometry + FloatRect m_insideBounds; ///< Bounding rectangle of the inside (fill) + FloatRect m_bounds; ///< Bounding rectangle of the whole shape (outline + fill) }; } // namespace sf diff --git a/include/SFML/Graphics/Sprite.hpp b/include/SFML/Graphics/Sprite.hpp index b44584be..65f8f90f 100644 --- a/include/SFML/Graphics/Sprite.hpp +++ b/include/SFML/Graphics/Sprite.hpp @@ -33,6 +33,7 @@ #include #include #include +#include namespace sf @@ -215,9 +216,10 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - Vertex m_vertices[4]; ///< Vertices defining the sprite's geometry - const Texture* m_texture; ///< Texture of the sprite - IntRect m_textureRect; ///< Rectangle defining the area of the source texture to display + Vertex m_vertices[4]; ///< Vertices defining the sprite's geometry + VertexBuffer m_verticesBuffer; ///< Vertex buffer containing the sprite's geometry + const Texture* m_texture; ///< Texture of the sprite + IntRect m_textureRect; ///< Rectangle defining the area of the source texture to display }; } // namespace sf diff --git a/include/SFML/Graphics/Text.hpp b/include/SFML/Graphics/Text.hpp index 158e149a..dfc08a75 100644 --- a/include/SFML/Graphics/Text.hpp +++ b/include/SFML/Graphics/Text.hpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -435,20 +436,22 @@ private: //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - String m_string; ///< String to display - const Font* m_font; ///< Font used to display the string - unsigned int m_characterSize; ///< Base size of characters, in pixels - float m_letterSpacingFactor; ///< Spacing factor between letters - float m_lineSpacingFactor; ///< Spacing factor between lines - Uint32 m_style; ///< Text style (see Style enum) - Color m_fillColor; ///< Text fill color - Color m_outlineColor; ///< Text outline color - float m_outlineThickness; ///< Thickness of the text's outline - mutable VertexArray m_vertices; ///< Vertex array containing the fill geometry - mutable VertexArray m_outlineVertices; ///< Vertex array containing the outline geometry - mutable FloatRect m_bounds; ///< Bounding rectangle of the text (in local coordinates) - mutable bool m_geometryNeedUpdate; ///< Does the geometry need to be recomputed? - mutable Uint64 m_fontTextureId; ///< The font texture id + String m_string; ///< String to display + const Font* m_font; ///< Font used to display the string + unsigned int m_characterSize; ///< Base size of characters, in pixels + float m_letterSpacingFactor; ///< Spacing factor between letters + float m_lineSpacingFactor; ///< Spacing factor between lines + Uint32 m_style; ///< Text style (see Style enum) + Color m_fillColor; ///< Text fill color + Color m_outlineColor; ///< Text outline color + float m_outlineThickness; ///< Thickness of the text's outline + mutable VertexArray m_vertices; ///< Vertex array containing the fill geometry + mutable VertexArray m_outlineVertices; ///< Vertex array containing the outline geometry + mutable VertexBuffer m_verticesBuffer; ///< Vertex buffer containing the fill geometry + mutable VertexBuffer m_outlineVerticesBuffer; ///< Vertex buffer containing the outline geometry + mutable FloatRect m_bounds; ///< Bounding rectangle of the text (in local coordinates) + mutable bool m_geometryNeedUpdate; ///< Does the geometry need to be recomputed? + mutable Uint64 m_fontTextureId; ///< The font texture id }; } // namespace sf diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index d0245d5d..10d743cc 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -87,6 +87,10 @@ void Shape::setTextureRect(const IntRect& rect) { m_textureRect = rect; updateTexCoords(); + + // Update the vertex buffers if they are being used + if (m_verticesBuffer.getVertexCount()) + m_verticesBuffer.update(&m_vertices[0]); } @@ -102,6 +106,10 @@ void Shape::setFillColor(const Color& color) { m_fillColor = color; updateFillColors(); + + // Update the vertex buffers if they are being used + if (m_verticesBuffer.getVertexCount()) + m_verticesBuffer.update(&m_vertices[0]); } @@ -117,6 +125,10 @@ void Shape::setOutlineColor(const Color& color) { m_outlineColor = color; updateOutlineColors(); + + // Update the vertex buffers if they are being used + if (m_outlineVerticesBuffer.getVertexCount()) + m_outlineVerticesBuffer.update(&m_outlineVertices[0]); } @@ -158,15 +170,17 @@ FloatRect Shape::getGlobalBounds() const //////////////////////////////////////////////////////////// Shape::Shape() : -m_texture (NULL), -m_textureRect (), -m_fillColor (255, 255, 255), -m_outlineColor (255, 255, 255), -m_outlineThickness(0), -m_vertices (TriangleFan), -m_outlineVertices (TriangleStrip), -m_insideBounds (), -m_bounds () +m_texture (NULL), +m_textureRect (), +m_fillColor (255, 255, 255), +m_outlineColor (255, 255, 255), +m_outlineThickness (0), +m_vertices (TriangleFan), +m_outlineVertices (TriangleStrip), +m_verticesBuffer (TriangleFan, VertexBuffer::Static), +m_outlineVerticesBuffer(TriangleStrip, VertexBuffer::Static), +m_insideBounds (), +m_bounds () { } @@ -180,6 +194,16 @@ void Shape::update() { m_vertices.resize(0); m_outlineVertices.resize(0); + + if (VertexBuffer::isAvailable()) + { + if (m_verticesBuffer.getVertexCount()) + m_verticesBuffer.create(0); + + if (m_outlineVerticesBuffer.getVertexCount()) + m_outlineVerticesBuffer.create(0); + } + return; } @@ -206,6 +230,21 @@ void Shape::update() // Outline updateOutline(); + + // Update the vertex buffers if they are being used + if (VertexBuffer::isAvailable()) + { + if (m_verticesBuffer.getVertexCount() != m_vertices.getVertexCount()) + m_verticesBuffer.create(m_vertices.getVertexCount()); + + m_verticesBuffer.update(&m_vertices[0]); + + if (m_outlineVerticesBuffer.getVertexCount() != m_outlineVertices.getVertexCount()) + m_outlineVerticesBuffer.create(m_outlineVertices.getVertexCount()); + + if (m_outlineVertices.getVertexCount()) + m_outlineVerticesBuffer.update(&m_outlineVertices[0]); + } } @@ -216,13 +255,29 @@ void Shape::draw(RenderTarget& target, RenderStates states) const // Render the inside states.texture = m_texture; - target.draw(m_vertices, states); + + if (VertexBuffer::isAvailable()) + { + target.draw(m_verticesBuffer, states); + } + else + { + target.draw(m_vertices, states); + } // Render the outline if (m_outlineThickness != 0) { states.texture = NULL; - target.draw(m_outlineVertices, states); + + if (VertexBuffer::isAvailable()) + { + target.draw(m_outlineVerticesBuffer, states); + } + else + { + target.draw(m_outlineVertices, states); + } } } diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index 1fdfc5d5..eae7a731 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -35,26 +35,37 @@ namespace sf { //////////////////////////////////////////////////////////// Sprite::Sprite() : -m_texture (NULL), -m_textureRect() +m_verticesBuffer(TrianglesStrip, VertexBuffer::Stream), +m_texture (NULL), +m_textureRect () { + if (VertexBuffer::isAvailable()) + m_verticesBuffer.create(4); } //////////////////////////////////////////////////////////// Sprite::Sprite(const Texture& texture) : -m_texture (NULL), -m_textureRect() +m_verticesBuffer(TrianglesStrip, VertexBuffer::Stream), +m_texture (NULL), +m_textureRect () { + if (VertexBuffer::isAvailable()) + m_verticesBuffer.create(4); + setTexture(texture); } //////////////////////////////////////////////////////////// Sprite::Sprite(const Texture& texture, const IntRect& rectangle) : -m_texture (NULL), -m_textureRect() +m_verticesBuffer(TrianglesStrip, VertexBuffer::Stream), +m_texture (NULL), +m_textureRect () { + if (VertexBuffer::isAvailable()) + m_verticesBuffer.create(4); + setTexture(texture); setTextureRect(rectangle); } @@ -80,6 +91,10 @@ void Sprite::setTextureRect(const IntRect& rectangle) m_textureRect = rectangle; updatePositions(); updateTexCoords(); + + // Update the vertex buffer if it is being used + if (VertexBuffer::isAvailable()) + m_verticesBuffer.update(m_vertices); } } @@ -92,6 +107,10 @@ void Sprite::setColor(const Color& color) m_vertices[1].color = color; m_vertices[2].color = color; m_vertices[3].color = color; + + // Update the vertex buffer if it is being used + if (VertexBuffer::isAvailable()) + m_verticesBuffer.update(m_vertices); } @@ -140,7 +159,15 @@ void Sprite::draw(RenderTarget& target, RenderStates states) const { states.transform *= getTransform(); states.texture = m_texture; - target.draw(m_vertices, 4, TriangleStrip, states); + + if (VertexBuffer::isAvailable()) + { + target.draw(m_verticesBuffer, states); + } + else + { + target.draw(m_vertices, 4, TriangleStrip, states); + } } } diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index 7cb4f889..5b929485 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -76,20 +76,22 @@ namespace sf { //////////////////////////////////////////////////////////// Text::Text() : -m_string (), -m_font (NULL), -m_characterSize (30), -m_letterSpacingFactor(1.f), -m_lineSpacingFactor (1.f), -m_style (Regular), -m_fillColor (255, 255, 255), -m_outlineColor (0, 0, 0), -m_outlineThickness (0), -m_vertices (Triangles), -m_outlineVertices (Triangles), -m_bounds (), -m_geometryNeedUpdate (false), -m_fontTextureId (0) +m_string (), +m_font (NULL), +m_characterSize (30), +m_letterSpacingFactor (1.f), +m_lineSpacingFactor (1.f), +m_style (Regular), +m_fillColor (255, 255, 255), +m_outlineColor (0, 0, 0), +m_outlineThickness (0), +m_vertices (Triangles), +m_outlineVertices (Triangles), +m_verticesBuffer (Triangles, VertexBuffer::Static), +m_outlineVerticesBuffer(Triangles, VertexBuffer::Static), +m_bounds (), +m_geometryNeedUpdate (false), +m_fontTextureId (0) { } @@ -97,20 +99,22 @@ m_fontTextureId (0) //////////////////////////////////////////////////////////// Text::Text(const String& string, const Font& font, unsigned int characterSize) : -m_string (string), -m_font (&font), -m_characterSize (characterSize), -m_letterSpacingFactor(1.f), -m_lineSpacingFactor (1.f), -m_style (Regular), -m_fillColor (255, 255, 255), -m_outlineColor (0, 0, 0), -m_outlineThickness (0), -m_vertices (Triangles), -m_outlineVertices (Triangles), -m_bounds (), -m_geometryNeedUpdate (true), -m_fontTextureId (0) +m_string (string), +m_font (&font), +m_characterSize (characterSize), +m_letterSpacingFactor (1.f), +m_lineSpacingFactor (1.f), +m_style (Regular), +m_fillColor (255, 255, 255), +m_outlineColor (0, 0, 0), +m_outlineThickness (0), +m_vertices (Triangles), +m_outlineVertices (Triangles), +m_verticesBuffer (Triangles, VertexBuffer::Static), +m_outlineVerticesBuffer(Triangles, VertexBuffer::Static), +m_bounds (), +m_geometryNeedUpdate (true), +m_fontTextureId (0) { } @@ -202,6 +206,14 @@ void Text::setFillColor(const Color& color) { for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) m_vertices[i].color = m_fillColor; + + if (VertexBuffer::isAvailable()) + { + if (m_verticesBuffer.getVertexCount() != m_vertices.getVertexCount()) + m_verticesBuffer.create(m_vertices.getVertexCount()); + + m_verticesBuffer.update(&m_vertices[0]); + } } } } @@ -220,6 +232,14 @@ void Text::setOutlineColor(const Color& color) { for (std::size_t i = 0; i < m_outlineVertices.getVertexCount(); ++i) m_outlineVertices[i].color = m_outlineColor; + + if (VertexBuffer::isAvailable()) + { + if (m_outlineVerticesBuffer.getVertexCount() != m_outlineVertices.getVertexCount()) + m_outlineVerticesBuffer.create(m_outlineVertices.getVertexCount()); + + m_outlineVerticesBuffer.update(&m_outlineVertices[0]); + } } } } @@ -382,9 +402,25 @@ void Text::draw(RenderTarget& target, RenderStates states) const // Only draw the outline if there is something to draw if (m_outlineThickness != 0) - target.draw(m_outlineVertices, states); + { + if (VertexBuffer::isAvailable()) + { + target.draw(m_outlineVerticesBuffer, states); + } + else + { + target.draw(m_outlineVertices, states); + } + } - target.draw(m_vertices, states); + if (VertexBuffer::isAvailable()) + { + target.draw(m_verticesBuffer, states); + } + else + { + target.draw(m_vertices, states); + } } } @@ -408,11 +444,23 @@ void Text::ensureGeometryUpdate() const // Clear the previous geometry m_vertices.clear(); m_outlineVertices.clear(); + m_bounds = FloatRect(); // No text: nothing to draw if (m_string.isEmpty()) + { + if (VertexBuffer::isAvailable()) + { + if (m_verticesBuffer.getVertexCount()) + m_verticesBuffer.create(0); + + if (m_outlineVerticesBuffer.getVertexCount()) + m_outlineVerticesBuffer.create(0); + } + return; + } // Compute values related to the text style bool isBold = m_style & Bold; @@ -562,6 +610,20 @@ void Text::ensureGeometryUpdate() const m_bounds.top = minY; m_bounds.width = maxX - minX; m_bounds.height = maxY - minY; + + // Update the vertex buffer if it is being used + if (VertexBuffer::isAvailable()) + { + if (m_verticesBuffer.getVertexCount() != m_vertices.getVertexCount()) + m_verticesBuffer.create(m_vertices.getVertexCount()); + + m_verticesBuffer.update(&m_vertices[0]); + + if (m_outlineVerticesBuffer.getVertexCount() != m_outlineVertices.getVertexCount()) + m_outlineVerticesBuffer.create(m_outlineVertices.getVertexCount()); + + m_outlineVerticesBuffer.update(&m_outlineVertices[0]); + } } } // namespace sf