SFML community forums

Help => Graphics => Topic started by: WannableDev on January 27, 2016, 07:42:48 pm

Title: How does sf::Shape work?
Post by: WannableDev on January 27, 2016, 07:42:48 pm
Hi guys I'm trying to create my own version of sf::Shape for more specific needs.

I had a look at the source and I don't understand the following:
void sf::Shape::update()
 [...]
    m_vertices.resize(count + 2); // + 2 for center and repeated first point

    // Position
    for (std::size_t i = 0; i < count; ++i)
        m_vertices[i + 1].position = getPoint(i);
    m_vertices[count + 1].position = m_vertices[1].position;

    // Update the bounding rectangle
    m_vertices[0] = m_vertices[1]; // so that the result of getBounds() is correct
    m_insideBounds = m_vertices.getBounds();

    // Compute the center and make it the first vertex
    m_vertices[0].position.x = m_insideBounds.left + m_insideBounds.width / 2;
    m_vertices[0].position.y = m_insideBounds.top + m_insideBounds.height / 2;
[...]
 
In my shape class I have the bounding rect already calculated but I guess it's here for texture mapping and other features, what I want to ask is why you need the point in the center? Is it required? I've played with my raw vertices and it seems to work fine without it. Same with the "first repeated point" why do you need it? Can I have a polygon or circle just from "raw" vertices and render it with sf::PrimitiveType::TriangleFan and have it work fine?
Title: Re: How does sf::Shape work?
Post by: victorlevasseur on January 27, 2016, 07:48:50 pm
Hi,

In the doc of sf::Shape, it's written how to rewrite a custom shape inheriting sf::Shape :
Quote
You can write your own derived shape class, there are only two virtual functions to override:

    getPointCount must return the number of points of the shape
    getPoint must return the points of the shape
Title: Re: How does sf::Shape work?
Post by: WannableDev on January 27, 2016, 07:54:53 pm
Hi, Thanks but I'm not trying to create any new shape, my class does not inherits from sf::Shape but from sf::Drawable only and I want it to stay this way. I render with RenderTarget.draw(vertices[]...). I just wonder for what  the center and the repeated point is, my shape seems to work fine without them but I don't want to have any problems later
Title: Re: How does sf::Shape work?
Post by: Laurent on January 27, 2016, 08:03:32 pm
sf::Shape uses the TRIANGLE_FAN OpenGL primitive type. From that, you can easily understand why it needs the center and repeated first point. And yes, in certain cases this is indeed not required.