I'm trying to draw text using the sf::VertexArray because then, my text is drawed in the same place and way as my sprites and its easier for me to manage things like draw order and its overal more consistent.
I have sprites working but with text it looks as if the vertices are in the wrong order (see attachment)
I have simply taken the origin sf::Text source code and modified it to fit my needs (I kept the vertex append order the same).
// Add a quad for the current character
m_vertices.append(Vertex(Vector2f(x + left - italic * top, y + top), m_color, Vector2f(u1, v1)));
m_vertices.append(Vertex(Vector2f(x + right - italic * top, y + top), m_color, Vector2f(u2, v1)));
m_vertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), m_color, Vector2f(u1, v2)));
m_vertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), m_color, Vector2f(u1, v2)));
m_vertices.append(Vertex(Vector2f(x + right - italic * top, y + top), m_color, Vector2f(u2, v1)));
m_vertices.append(Vertex(Vector2f(x + right - italic * bottom, y + bottom), m_color, Vector2f(u2, v2)));
for (size_t i = 0; i < m_vertices.getVertexCount(); i++)
m_batch->addVertex(m_vertices[i]);
Can anyone help me figure out the correct order to add the vertices?
(the text should spell "Hello!")
Are you trying to draw text with a vertex array so that you can re-arrange a vector of vertex arrays or something?
its easier for me to manage things like draw order
more consistent
You could order a group of pointers to drawable objects and sort those into the order you want and then draw from them.
sf::RectangleShape rectangle;
sf::Sprite sprite;
sf::Text text;
std::vector<sf::Drawable*> pDrawableOrder;
pDrawableOrder.push_back(&rectangle);
pDrawableOrder.push_back(&sprite);
pDrawableOrder.push_back(&text);
{
// re-arrange the order of the pointers as you see fit whenever you like (before each drawing)
for (auto& pDrawable : pDrawableOrder)
window.draw(*pDrawable);
}
That aside, which primitive type are you using to draw the text?
Hi, thanks for your reply, im attempting to draw the text in the same way as the sprites. I created a spritebatch to reduce draw calls.
I use quads as primitive type
if(m_vertexCount > 0)
m_target->draw(&m_vertices[0], m_vertices.getVertexCount(), sf::PrimitiveType::Quads, m_states);
edit: I changed it to triangles and now it works perfectly! thanks I forgot about the primitive type being different for text.
I have simply taken the origin sf::Text source code and modified it to fit my needs
If all you need is access to the vertices of sf::Text, you can manage without duplicating its code by doing something along the lines of:
#define getString getString() const; \
friend class YourClass; \
void doNothingJustAHack
#include <SFML/Graphics/Text.hpp>
#undef getString
Then YourClass will have access to the private members of sf::Text. It's really fun, and you may be nominated to the Nobel prize in software engineering ;)