Hey Guys,
I wrote my own Text class which draws a string that came from a png file as a VertexArray. Font is also my own class.
This is the class:
class Text : public sf::Drawable, public sf::Transformable
{
public:
bool load(std::string text, Font font);
bool load(std::string text, Font font, sf::IntRect bounds);
bool ChangeText(std::string text, Font font, sf::IntRect bounds);
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
//apply the transformation
states.transform *= getTransform();
//apply the Font Texture
states.texture = &FontTexture;
//draw the vertex array
target.draw(Vertices, states);
}
sf::Texture FontTexture;
sf::VertexArray Vertices;
};
This is how I fill the VertexArray:
//define the 4 corners of the quad
sf::Vector2f topleft = sf::Vector2f(curRec.left, curRec.top);
sf::Vector2f topright = sf::Vector2f(curRec.left + curRec.width, curRec.top);
sf::Vector2f bottomright = sf::Vector2f(curRec.left + curRec.width, curRec.top + curRec.height);
sf::Vector2f bottomleft = sf::Vector2f(curRec.left, curRec.top + curRec.height);
//define the 4 texture coordinates
sf::Vector2f tex_topleft = sf::Vector2f(rect.left, rect.top);
sf::Vector2f tex_topright = sf::Vector2f(rect.left + rect.width, rect.top);
sf::Vector2f tex_bottomright = sf::Vector2f(rect.left + rect.width, rect.top + rect.height);
sf::Vector2f tex_bottomleft = sf::Vector2f(rect.left, rect.top + rect.height);
Vertices.append(sf::Vertex(topleft, tex_topleft));
Vertices.append(sf::Vertex(topright, tex_topright));
Vertices.append(sf::Vertex(bottomright, tex_bottomright));
Vertices.append(sf::Vertex(bottomleft, tex_bottomleft));
It works fine, but when I try to change the displayed text, my program crashes. To change the text I obviously call the "Text::ChangeText" method. At the beginning oft he method I call "Vertices.clear();" and when the programm trys to execute this line it crahs.
When I don't call the clear() method the displayed text doesn't change at all. For example I want to display "Gold:100" and change it to "Gold:90" but it stays at "Gold100".
What am i doing wrong?