Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How does sf::Shape work?  (Read 1619 times)

0 Members and 1 Guest are viewing this topic.

WannableDev

  • Newbie
  • *
  • Posts: 2
    • View Profile
How does sf::Shape work?
« 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?

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: How does sf::Shape work?
« Reply #1 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

WannableDev

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How does sf::Shape work?
« Reply #2 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
« Last Edit: January 27, 2016, 08:00:59 pm by WannableDev »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How does sf::Shape work?
« Reply #3 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.
Laurent Gomila - SFML developer