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

Author Topic: sf::RectangleShape.setFillColor() is not working  (Read 6430 times)

0 Members and 1 Guest are viewing this topic.

Turbo

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
sf::RectangleShape.setFillColor() is not working
« on: September 25, 2021, 05:03:04 pm »
Hello people from the SFML community. Recently I was meddling with the library and I was trying to make an sf::RectangleShape change its color whenever I hover my mouse over it, which is something I have done before without any issues. However, this time I don't understand what seems to be going wrong.
Here is the bit of code where I change the shape's color:
void Node::update(sf::RenderWindow*& window)
{
        bool mouse_over = IsMouseOver(this->rect.getGlobalBounds(), window);

        if (mouse_over)
        {
                this->rect.setFillColor(sf::Color( //Doesn't work
                        this->color.r + 30.0f,
                        this->color.g + 30.0f,
                        this->color.b + 30.0f
                ));
                printf("r: %d, g: %d, b:%d\n",
                        rect.getFillColor().r,
                        rect.getFillColor().g,
                        rect.getFillColor().b);
        }
        else if (!mouse_over)
        {
                this->rect.setFillColor(this->color); //Doesn't work either
                printf("r: %d, g: %d, b:%d\n",
                        rect.getFillColor().r,
                        rect.getFillColor().g,
                        rect.getFillColor().b);
        }
}
 
Before anyone suggests that the order of execution is wrong, let me show you how I'm using this method:
void NodeManager::updateNodes(sf::RenderWindow*& window) //Updates every node in the nodes matrix
{
        for (auto i : nodes)
                for (auto j : i)
                        j.update(window);
}
 
void Program::update(void)
{
    nodeManager->updateNodes(window);
}
void Program::run(void)
{
    while (window->isOpen())
    {
        sf::Event event;
        while (window->pollEvent(event))
            this->events(event);
        this->update(); //Method is executed within the update method before anything is drawn to the screen
        this->draw();
    }
}
 
void Program::draw(void) //The draw method
{
    window->clear();
    //Draw things here ==============================
    nodeManager->drawNodes(window);
    //===============================================
    window->display();
}
 
I need your help, because I have no idea what the issue is. I've been trying to figure this out for a while now and I'm starting to question why I'm even doing this in the first place.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: sf::RectangleShape.setFillColor() is not working
« Reply #1 on: September 25, 2021, 07:23:12 pm »
What do you mean "it doesn't work"?
You don't notice any visual color change?
The rectangle isn't displayed?
Are you sure your IsMouseOver function is correct and you enter the first part of your "if"?
The color in your printf is properly modified but you don't notice any visual color change?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: sf::RectangleShape.setFillColor() is not working
« Reply #2 on: September 26, 2021, 07:48:58 am »
You haven't shown any of the member declarations so I'm just guessing at types here, but the problem is the for loops in updateNodes.
In a loop like:
for (auto j : i)
j is a copy of the data from i, not the original.
So when you call update on j, you are setting the colour on a temporary duplicate of the node that is then thrown away. The original node is never updated.

Changing the loop to:
for (auto &j : i)
will make it use the actual nodes instead of copies. You'll probably want that for the for(auto i:nodes) one as well, but no idea what the actual data is there.