Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: VertexArray change Vertices  (Read 1649 times)

0 Members and 1 Guest are viewing this topic.

ERROR

  • Newbie
  • *
  • Posts: 7
    • View Profile
VertexArray change Vertices
« on: August 26, 2014, 08:02:02 pm »
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?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: VertexArray change Vertices
« Reply #1 on: August 26, 2014, 09:08:03 pm »
Why are you using append() to add to the VertexArray, which is technically a std::vector?

If the problem is in the ClearText() method, you'll probably need to show it to people before they can work out where the problem comes from.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

ERROR

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: VertexArray change Vertices
« Reply #2 on: August 28, 2014, 10:19:43 am »
Well, the problem occurs in the first line which is "Vertices.clear();".

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: VertexArray change Vertices
« Reply #3 on: August 28, 2014, 02:05:03 pm »
Going to have to see more, preferably a minimal and complete example.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*