VertexArray of circles

1.5k Views Asked by At

I am wondering if it is possible to create a VertexArray of circles in SFML. I have looked for answers but I didn't find anything that could help. Moreover, I don't understand the part on the SFML documentation where it is written that I can create my own entities, I think this is maybe what I want to do in fact.

EDIT : I want to do that because I have to draw a lot of circles.

Thanks for helping me

3

There are 3 best solutions below

0
On

In addition two previous answers I will try to explain why there is no default VertexArray of circles.

By ideology of computer graphics (and SFML in our case) vertex is a smallest drawing primitive with least necessary functionality. Classical example of vertices are point, line, triange, guad, and polygone. The first four are really simple for your videocard to store and to draw. Polygon can be any geometrical figure, but it will be heavier to process, that's why e.g in 3D grapichs polygons are triangles.

Circle is a bit more complicated. For example videocard doesn't know how much points she need to draw your circle smooth enough. So, as @nvoigt answered there exists a sf::CircleShape that is being built from more primitive verticies.

6
On

sf::CircleShape is already using a vertex array (thanks to being inherited from sf::Shape). There is nothing extra you need to do.

If you have a lot of circles, try using sf::CircleShape first and only optimize when you have a real use-case that you can measure your solution against.

1
On

While @nvoigt answer is correct, I found it useful in my implementations to work with vectors (see http://en.cppreference.com/w/cpp/container/vector for more details, look up "c++ containers", there are several types of containers to optimize read/write times).

You probably do not need it for the above described use case, but you could need it in future implementations and consider this for a good coding practice.

#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;
}