Search code examples
c++sfml

SFML setFillColor fails to fill color of sf::RectangleShape 2D array


For school I got the assignment to make a tic-tac-toe game. I have a grid layout which consists of 9 squares (3 at the top, 3 in the middle and 3 at the bottom), drawn on a background. This way a grid is obtained. But setting the color does not work. In my constructor GridLayout I ask for a sf::Color, like this:

GridLayout(sf::RenderWindow& window, const sf::Color& fillColor, const int& spacing):
// some init
{
    setFillColor(fillColor);
}

Also, in my private attributes, I have a grid, which looks like this:

sf::RectangleShape[3][3] rectangleShapes;

To set the fill color of all the 9 shapes, I have created the following function:

void GridLayout::setFillColor(const sf::Color& fillColor) {
    for (auto& row : rectangleShapes) 
        for (auto& rect : row) 
            rect.setFillColor(fillColor);


}

But as shown on the following image, it still appears white, even though sf::Color::Black is passed.

enter image description here


Solution

  • I figured out how to fix it. For some reason, the color resets to white according to the order of how these two functions were called:

    GridLayout::GridLayout(sf::RenderWindow& window, const sf::Color& fillColor, const int& spacing):
    //init
    {       
        setFillColor(fillColor);
        createGridLayout();
    }
    

    Should be:

    {       
        createGridLayout();
        setFillColor(fillColor);
    
    }