【问题标题】:SFML drawing primitives from VertexArraySFML 从 VertexArray 绘制图元
【发布时间】:2018-05-01 22:35:42
【问题描述】:

如何从构造的 VertexArray 中绘制我选择的图元?在下面的示例中,我将两个顶点添加到 'vertices' 数组中,并尝试使用 'window.draw(vertices, 2, sf::Lines)' 绘制它,但它给了我一个错误。我知道我可以使用 'sf::Vertex foo[] = {..}' 创建线对象,但我希望能够继续将顶点附加到数组而不是一次全部初始化。

#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML");
    sf::Clock clock;

    sf::VertexArray vertices;
    sf::Vertex vertex;

    vertex.position = sf::Vector2f(0, 0);
    vertex.color = sf::Color(100, 0, 200);
    vertices.append(vertex);
    vertex.position = sf::Vector2f(100, 100);
    vertex.color = sf::Color(100, 0, 200);
    vertices.append(vertex);

    // Start the game loop
    bool running = true;
    while (running)
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                running = false;
        }

        window.clear(sf::Color::Black);
        window.draw(vertices,2 ,sf::Lines);

        window.display();
    }

    return 0;
}

【问题讨论】:

    标签: c++ sfml vertex-array


    【解决方案1】:

    你可以在声明sf::VertexArray vertices;之后调用vertices.setPrimitiveType(sf::Lines);,然后画出来:window.draw(vertices);

    或者您可以在构造函数中设置其原始类型以及点数,然后您可以使用operator[] 访问这些点:

    sf::VertexArray line(sf::Lines, 2); //or sf::LineStrip
    line[0].position = sf::Vector2f(0, 0);
    line[0].color = sf::Color(100, 0, 200);
    line[1].position = sf::Vector2f(100, 100);
    line[1].color = sf::Color(100, 0, 200);
    ...
    window.draw(line);
    

    参考:enum sf::PrimitiveTypesf::VertexArrayVertexArray tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多