VertexArray的圆圈

问题描述 投票:0回答:3

我想知道是否可以在SFML中创建圆的VertexArray。我找到了答案,但我找不到任何有用的东西。此外,我不明白SFML文档中的部分,我写的是我可以创建自己的实体,我想这可能是我想要做的事实。

编辑:我想这样做,因为我必须绘制很多圈子。

谢谢你的帮助

c++ sfml vertex-array
3个回答
1
投票

虽然@nvoigt的回答是正确的,但我发现在我的实现中使用向量很有用(有关更多详细信息,请参阅http://en.cppreference.com/w/cpp/container/vector,查找“c ​​++容器”,有几种类型的容器可以优化读/写时间)。

您可能不需要它用于上述用例,但您可能在将来的实现中需要它,并考虑这是一个良好的编码实践。

#include <SFML/Graphics.hpp>
#include <vector>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");


    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // clear the window with black color
        window.clear(sf::Color::Black);

        // initialize myvector
        std::vector<sf::CircleShape> myvector;

        // add 10 circles
        for (int i = 0; i < 10; i++)
        {
          sf::CircleShape shape(50);
          // draw a circle every 100 pixels
          shape.setPosition(i * 100, 25);
          shape.setFillColor(sf::Color(100, 250, 50));

          // copy shape to vector
          myvector.push_back(shape);
        }

        // iterate through vector
        for (std::vector<sf::CircleShape>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
        {
          // draw all circles
          window.draw(*it);
        }
        window.display();
    }

    return 0;
}

0
投票

sf::CircleShape已经在使用顶点数组(感谢继承自sf::Shape)。你不需要做任何额外的事情。

如果您有很多圈子,请先尝试使用sf::CircleShape,只有在有真实用例的情况下才能进行优化,以便测量您的解决方案。


0
投票

此外,我将尝试解释为什么没有默认的VertexArray圆圈。

通过计算机图形的意识形态(在我们的例子中是SFML),顶点是具有最少必要功能的最小绘图原语。顶点的经典示例是point,line,triange,guad和polygone。前四个对于您的存储和绘制的视频来说非常简单。多边形可以是任何几何图形,但是处理起来会更重,这就是为什么例如3D图形多边形是三角形。

圆圈有点复杂。例如,videocard不知道她需要多少点来平滑地绘制你的圆圈。所以,正如@nvoigt回答的那样,存在一个sf :: CircleShape,它是从更原始的顶点构建的。

© www.soinside.com 2019 - 2024. All rights reserved.