关于c ++:在sfml中使用vertexArray的正确方法是什么?

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

经过研究后,似乎可以以最有效的方式使用vertexArrays一次将许多精灵绘制到屏幕上,但是我在努力做到这一点。 我已经尝试使用sfml论坛,但是到目前为止,我所看到的每个示例都是来自过时的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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();
    }
}

我有形状图,但是当我也尝试将其应用于纹理时,什么也没画。


只是这样使用:

1
2
3
4
5
6
7
 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);


在构造sf :: Vector2f时,您不提供任何纹理坐标。 传递一些纹理坐标作为第三个参数,例如:

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

纹理坐标应为0,00,11,01,1之类。

您可能还必须将形状类型更改为sf::Quads(自从我使用SFML以来已经有一段时间了,所以这可能是正确的,也可能不是正确的)。