Hi, i tried to implement a Custom Shape:
class Hexagon : public sf::Shape {
private:
static std::vector<sf::Vector2f> m_Coordinates;
protected:
virtual unsigned int getPointCount() const {
return 6;
}
virtual sf::Vector2f getPoint(unsigned int index) const {
return m_Coordinates[index];
}
};
//In the Constructor
if(m_Coordinates.empty()) {
//More Details in image /Misc/hex_geometry.jpg
const float s = 1.f;
const float h = 0.5f; //sin(30°)
const float r = 0.86602540f; //cos(30°)
m_Coordinates.reserve(6);
m_Coordinates.push_back(sf::Vector2f(0.f, -s)); //Top
m_Coordinates.push_back(sf::Vector2f( +r, -h)); //Top-Right
m_Coordinates.push_back(sf::Vector2f( +r, +h)); //Bottom-Right
m_Coordinates.push_back(sf::Vector2f(0.f, +s)); //Bottom
m_Coordinates.push_back(sf::Vector2f( -r, +h)); //Bottom-Left
m_Coordinates.push_back(sf::Vector2f( -r, -h)); //Top-Left
Shape::scale(1000.f, 1000.f);
Shape::setFillColor(sf::Color(255.f, 0.f, 0.f));
Shape::setPosition(sf::Vector2f(200.f, 200.f));
}
but it won't appear if i use
Hexagon myShape;
App.draw(myShape);
i scaled it to 1000.f to be sure i can see it...
Indeed, i did not update the Shape, inserted it at the end of the Constructor, but still can't see any Hexagon...
also tryed to align the Points CCW and CW, both doesn't work.
And i tried that one, which works:
sf::ConvexShape polygon;
polygon.setPointCount(6);
const float s = 1.f*100.f;
const float h = 0.5f*100.f; //sin(30°)
const float r = 0.86602540f*100.f; //cos(30°)
polygon.setPoint(0, sf::Vector2f(0.f, -s));
polygon.setPoint(1, sf::Vector2f( -r, -h));
polygon.setPoint(2, sf::Vector2f( -r, +h));
polygon.setPoint(3, sf::Vector2f(0.f, +s));
polygon.setPoint(4, sf::Vector2f( +r, +h));
polygon.setPoint(5, sf::Vector2f( +r, -h));
//polygon.setOutlineColor(sf::Color::Red);
//polygon.setOutlineThickness(5);
polygon.setPosition(200, 200);
App.draw(polygon);
so, i decided to update imediatly after inserting a point:
class Hexagon : public sf::Shape {
private:
static std::vector<sf::Vector2f> m_Coordinates;
protected:
virtual unsigned int getPointCount() const {
return m_Coordinates.size();
}
virtual sf::Vector2f getPoint(unsigned int index) const {
return m_Coordinates[index];
}
};
//In the Constructor
if(m_Coordinates.empty()) {
//More Details in image /Misc/hex_geometry.jpg
const float s = 1.f;
const float h = 0.5f; //sin(30°)
const float r = 0.86602540f; //cos(30°)
m_Coordinates.reserve(6);
m_Coordinates.push_back(sf::Vector2f(0.f, -s)); //Top
Shape::update();
m_Coordinates.push_back(sf::Vector2f( -r, -h)); //Top-Left
Shape::update();
m_Coordinates.push_back(sf::Vector2f( -r, +h)); //Bottom-Left
Shape::update();
m_Coordinates.push_back(sf::Vector2f(0.f, +s)); //Bottom
Shape::update();
m_Coordinates.push_back(sf::Vector2f( +r, +h)); //Bottom-Right
Shape::update();
m_Coordinates.push_back(sf::Vector2f( +r, -h)); //Top-Right
Shape::update();
Shape::scale(1000.f, 1000.f);
Shape::setFillColor(sf::Color(255.f, 0.f, 0.f));
Shape::setPosition(sf::Vector2f(200.f, 200.f));
}
But it does not work...
Well, now i derived from sf::ConvexShape. It works, but i still want to know why it doesn't work with sf::Shape, also i don't want to create the same points for every Hexagon over and over again, it's a waste, the solution would be to make ConvexShape a static member, but that's ugly again. I really want to inherit from sf::Shape, as it makes the most sense...