lezebulon
lezebulon

Reputation: 7994

how to use a vertexarray in SFML?

I'm trying to draw a texture on a sf::vertexarray if I call the below code (without the texture), I can display fine a rectangle with red border. When I add the texture, I see nothing.

What am I doing wrong?

sf::VertexArray v_array;
v_array.append(sf::Vertex(sf::Vector2f(1,1) , sf::Color::Red));
v_array.append(sf::Vertex(sf::Vector2f(60,1) , sf::Color::Red));
v_array.append(sf::Vertex(sf::Vector2f(60,10 ), sf::Color::Red));
v_array.append(sf::Vertex(sf::Vector2f(1,10), sf::Color::Red));
v_array.append(sf::Vertex(sf::Vector2f(1,1), sf::Color::Red ));

v_array.setPrimitiveType(sf::LinesStrip);
sf::Texture text;
text.loadFromFile("../ressources/tileset_base.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(v_array/*, rend*/);
    App.display();
}

Upvotes: 0

Views: 3707

Answers (1)

Hiura
Hiura

Reputation: 3530

You need to give some texture coordinate to your vertices. You can use one of the following two constructors:

Vertex (const Vector2f &thePosition, const Vector2f &theTexCoords)
Vertex (const Vector2f &thePosition, const Color &theColor, const Vector2f &theTexCoords)

See documentation here

Small tip: if you use only one render state (e.g. only the texture) you can use this shortcut:

App.draw(v_array, &text);

Upvotes: 3

Related Questions