user1725794
user1725794

Reputation: 267

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.

Upvotes: 1

Views: 6200

Answers (3)

22fps
22fps

Reputation: 1

sf::Quads ,still works but its deprecated becouse it leads to inefficient rendering. It basicly doesnt make use of what new graphic cards are able to do.

Upvotes: 0

Mohammed Hossain
Mohammed Hossain

Reputation: 1319

You are not supplying any texture coordinates when you are constructing sf::Vector2f; pass in some texture coordinates as the third parameter, like:

lines.append(sf::Vertex(sf::Vector2f(0, 0),sf::Vector2f(0, 0), sf::Vector2f(0,0)));

The texture coordinates should be something like 0,0, 0,1, 1,0, and 1,1.

You might also have to change your shape type to sf::Quads (it's been a while since I've used SFML, so this may or may not be true).

Upvotes: 2

Klasik
Klasik

Reputation: 892

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);

Upvotes: 3

Related Questions