Search code examples
c++sfml

What is the correct way to use a vertexArray in sfml?


After doing some research it would appear that using vertexArrays in the most efficientways of drawing many sprites onto the screen at once but I'm struggling how to do it. I've attempted using the sfml forums but every example I've seen has been from outdated code I have this so far.

int main() 
{
    sf::VertexArray lines(sf::LinesStrip, 4);
    lines.append(sf::Vertex(sf::Vector2f(0, 0),sf::Vector2f(0, 0)));
    lines.append(sf::Vector2f(0, 50));
    lines.append(sf::Vector2f(250, 50));
    lines.append(sf::Vector2f(250, 0));

    sf::Texture text;
    text.loadFromFile("Content/StartGame.png");

    sf::RenderStates rend(&text);

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    // Start game loop
    while (App.isOpen())
    {
        // Process events
        sf::Event Event;
        while (App.pollEvent(Event))
        {
            // Close window : exit
            if (Event.type == sf::Event::Closed)
                App.close();
        }
        App.clear();
        App.draw(lines, &text);
        App.display();
    }
}

I have the shape drawing but when I attempt to apply to the texture too it, nothing draws.


Solution

  • Just use like this:

     sf::VertexArray lines(sf::LinesStrip, 4);
     lines[0].position = sf::Vector2f(10, 0);
     lines[1].position = sf::Vector2f(20, 0);
     lines[2].position = sf::Vector2f(30, 5);
     lines[3].position = sf::Vector2f(40, 2);
    
     window.draw(lines);