Hello, I would like to share with my solution which allows for inheriting from sf::RectangleShape class. I met troubles with deriving from sf::Drawable and sf::Transformable, because then I couldn't use sf::Shape::setFillColor, so I tried to inherit from sf::Shape like our friend said above. Inheriting from sf::Shape was still problematic, because I had to define points like in eclipse example, I didn't want to do this in my case of drawing rectangle with grid. Finally, inheriting from sf::RectangleShape gave me a possibility for overriding draw function:
void CustomRectangleShape::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
states.transform *= this->getTransform();
sf::VertexArray rect_shape(sf::Quads, 4);
sf::Color rect_color = this->getFillColor();
rect_shape[0].position = this->getPoint(0);
rect_shape[1].position = this->getPoint(1);
rect_shape[2].position = this->getPoint(2);
rect_shape[3].position = this->getPoint(3);
rect_shape[0].color = rect_color;
rect_shape[1].color = rect_color;
rect_shape[2].color = rect_color;
rect_shape[3].color = rect_color;
target.draw(rect_shape,states);
if(this->grid_){
this->drawGrid(target,states);
}
}
void CustomRectangleShape::drawGrid(sf::RenderTarget &target, sf::RenderStates states) const
{
states.transform = grid_rec_->getTransform().Identity;
for(int i = 0; i < grid_size_.x; i++){
for(int j = 0; j < grid_size_.y; j++){
grid_rec_->setPosition(this->getPosition().x + i * grid_rec_->getSize().x, this->getPosition().y + j * grid_rec_->getSize().y);
target.draw(*grid_rec_, states);
}
}
}
I also created class fields for grid code, to be clear:
bool grid_;
sf::RectangleShape* grid_rec_;
sf::Vector2i grid_size_;
.