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

Author Topic: sf::Vertex pointer problem  (Read 1287 times)

0 Members and 1 Guest are viewing this topic.

Doodlemeat

  • Guest
sf::Vertex pointer problem
« on: November 17, 2014, 12:16:54 pm »
Hello.
I am having problem using the sf::VertexArray object.
I am going to use it to render multiple snowflakes in one draw call and therefore I need every flake to have its own 4 vertices in the sf::VertexArray.
And so every snowflake needs to know each own sf::Vertex* quad array, but when I pass the sf::Vertex* to the snowflake, the array becomes invalid.

This is when I add a flake.
void Snowy::createFlake()
{
        int textureId = thor::random(0, 3);
        sf::Vertex v0, v1, v2, v3;

        v0.texCoords = sf::Vector2f(16 * textureId, 0);
        v1.texCoords = sf::Vector2f(16 * textureId + 16, 0);
        v2.texCoords = sf::Vector2f(16 * textureId + 16, 16);
        v3.texCoords = sf::Vector2f(16 * textureId, 16);
        m_vertices.append(v0);
        m_vertices.append(v1);
        m_vertices.append(v2);
        m_vertices.append(v3);
        sf::Vertex* verts = &m_vertices[m_vertices.getVertexCount() - 4]; // This seems to become invalid when I use it in the snowflake update function
        SnowFlake flake(verts);
        flake.setX(thor::random(0, m_width));
        m_snowFlakes.push_back(flake);
}

I have tried to base it from this tutorial http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php
A friend also told me that when I do sf::VertexArray::append(), the memory places get changed or something. What could be the problem and how will I be able to fix it?

EDIT: I should also mention that i´ve tried to make vertexArray::resize() to a very large number and then it worked. But that is not an option for me because I don't know how many snowflakes there can be :/
« Last Edit: November 17, 2014, 12:19:50 pm by Doodlemeat »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Vertex pointer problem
« Reply #1 on: November 17, 2014, 01:19:44 pm »
Just save the index and access it through that. sf::VertexArray uses a std::vector underneath, as such pushing back new entries can expand the std::vector memory footprint and thus it needs to be moved, thus invalidating the pointers.
As I said, storing the index will however work, since the order of the vertices won't change, regardless where it's located in memory.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Doodlemeat

  • Guest
Re: sf::Vertex pointer problem
« Reply #2 on: November 17, 2014, 01:29:03 pm »
Thank you! That is a good solution <3

 

anything