0

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

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Leop
  • 75
  • 7

3 Answers3

1

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;
}
Martin Sand
  • 482
  • 1
  • 8
  • 14
  • Thank you, I already use a vector in fact but as I mentionned in another comment the thing is that I want to call the draw method the least. – Leop Dec 18 '17 at 16:46
0

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.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Thank you but there's really no way I can call only once the draw method to draw all of my circles ? – Leop Dec 18 '17 at 16:45
  • You can certainly draw all your circles in one call, but the method behind that call will need to do it circle by circle. Again, why not do it that way, even just as an exercise to measure your solution against? Computers do complex 3D worlds, you will need *a lot* of circles for your graphics card to even spin up it's fan. – nvoigt Dec 18 '17 at 17:35
  • No I mean, calling the draw funciton in SFML cost a lot because it has to set up alot of things, therefore I would like to call it only once on a VertexArray of circles so it sets up everything only once. I already do that with squares and it is indeed a lot faster. – Leop Dec 18 '17 at 18:22
  • Well if you already do it with squares, why not give it a try with circles and ask a specific question with a [mcve] when you have problems? – nvoigt Dec 18 '17 at 18:38
  • I can't give it a try since I don't know how to create a VertexArray of circles, I don't even know if it is possible. – Leop Dec 18 '17 at 18:47
  • Well we cannot help you if we don't know where you are stuck. Can you create a vertex array with a single triangle? Can you make a circle out of multiple triangles? Multiple circles? Can you write all of those triangles to the vertex array and then call `draw` and pass that array and `sf::Triangles`? – nvoigt Dec 18 '17 at 22:52
0

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.

Nikita Smirnov
  • 813
  • 8
  • 17