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

Author Topic: sf::shape placeholder point  (Read 1729 times)

0 Members and 1 Guest are viewing this topic.

short

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::shape placeholder point
« on: September 22, 2011, 10:11:49 pm »
Hi,

I am curious if I am missing something obvious.

Sf::Shape when constructed has the default "point" added to it at 0,0.

I'm reading a bunch of points out of a file, and constructing the corresponding shape based on the number of points read from the file.

When I read 4 points (which should be a rect), then go to render it, there are 5 points total.

Here's the code:

Code: [Select]
void SafeSetProperties(std::map<std::string, boost::variant<int, float, std::string, sf::Vector2f> > properties)
{
// constant string values for the function
static const std::string verticeCount = "VerticeCount";
static const std::string vertices = "Vertices";

// read in all of the vertices
if(properties.count(verticeCount) > 0)
{
const int numVertices = boost::get<int>(properties[verticeCount]);
for(auto i = 0; i < numVertices; ++i) {
std::ostringstream oss;
oss << vertices << i;
_polygonShape.AddPoint(boost::get<sf::Vector2f>(properties[oss.str()]));

}

}


Here's the resulting image (ignore the red triangle, different shape):


I'm aware of the static function sf::Shape::Rectangle, but this means I need to add a switch based on the number of points (that would make the code less maintainable, more complex, however you want to say it).

My question, why is the default placeholder point of (0,0) added to all shape's? Is there a reason why there is no way to override this behavior?

I could dive into the source code, remove the point being added in the constructor, but is that the only way?

I would like not to have to modify my sfml code (if I ever distribute, I wouldn't want a custom version of SFML).

Opening Shape.cpp we can see where this happens

Code: [Select]
////////////////////////////////////////////////////////////
Shape::Shape() :
myOutline         (0.f),
myIsFillEnabled   (true),
myIsOutlineEnabled(true),
myIsCompiled      (false)
{
    // Put a placeholder for the center of the shape
    myPoints.push_back(Point());
}

Haikarainen

  • Guest
sf::shape placeholder point
« Reply #1 on: September 22, 2011, 10:37:27 pm »
Have a look at sf::Shape::SetPointPosition.

Basically, when you create your shapes in the code, just set the first and only point to the first position of the points you load from your file.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::shape placeholder point
« Reply #2 on: September 22, 2011, 11:03:41 pm »
The "first point" is an implementation detail, it is recomputed later to be the center of gravity of all the points, and it has no effect on the shape that you see on screen.

I suspect that your shape looks wrong because the 4 points are not given in order.
Laurent Gomila - SFML developer

short

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::shape placeholder point
« Reply #3 on: September 23, 2011, 01:32:56 am »
Yup. Thanks for the clarification. They were indeed loading out of order, thank you for the explanation.

 

anything