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

Author Topic: sf::Color member not updating on sf::CircleShape draw  (Read 1323 times)

0 Members and 1 Guest are viewing this topic.

RATIU5

  • Newbie
  • *
  • Posts: 2
    • View Profile
sf::Color member not updating on sf::CircleShape draw
« on: February 03, 2021, 09:06:57 pm »
I am trying to change the color of a circle when the mouse is over the circle. I am already successfully detecting when the mouse is over the shape.

// Entity.h:
sf::Color GetSightColor() const
{
    std::cout << "Get m_SightColor: ";
    PrintColor(m_SightColor);
    return m_SightColor;
}
void SetSightColor(const sf::Color& val)
{
    m_SightColor = val;
    std::cout << "Set m_SightColor: ";
    PrintColor(m_SightColor);
}

// Main.cpp:
void Update()
{
...
if (entity.IsInSight(glm::vec2(mouse.x, mouse.y)))
{
    entity.SetSightColor(sf::Color(0, 0, 0, 0));
    std::cout << "Mouse Over Color: ";
    PrintColor(entity.GetSightColor());
} } }

// Entity.cpp:
sf::CircleShape Entity::DrawSight() const
{
    std::cout << "Draw with color: ";
    PrintColor(m_SightColor);
    float radius = GetSightSize();
    sf::CircleShape shape;
    shape.setPosition(GetLoc().x, GetLoc().y);
    shape.setRadius(radius);
    shape.setOrigin(radius, radius);
    shape.setFillColor(GetSightColor());
    return shape;
}
 

The problem is that the circle stays white when the mouse is over it. I logged the colors to make sure the color changes:

Draw with color: (Red: 255, Green: 255, Blue: 255, Alpha: 255)
Get m_SightColor: (Red: 255, Green: 255, Blue: 255, Alpha: 255)
Set m_SightColor: (Red: 0, Green: 0, Blue: 0, Alpha: 0)
Mouse Over Color: Get m_SightColor: (Red: 0, Green: 0, Blue: 0, Alpha: 0)
(Red: 0, Green: 0, Blue: 0, Alpha: 0)
Draw with color: (Red: 255, Green: 255, Blue: 255, Alpha: 255)
Get m_SightColor: (Red: 255, Green: 255, Blue: 255, Alpha: 255)

I don't see what I'm doing wrong. Is this my mistake or a bug?

Specs: Windows 10 x64
SFML: 2.5.1
« Last Edit: February 03, 2021, 09:12:20 pm by RATIU5 »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: sf::Color member not updating on sf::CircleShape draw
« Reply #1 on: February 04, 2021, 02:30:00 am »
hard to tell with only this code. i don't know where DrawSight function is being used, what variables the Entity class has, among other things

are you sure you're changing the color of the circle shape, and not only an arbitrary color you created inside the class?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

RATIU5

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: sf::Color member not updating on sf::CircleShape draw
« Reply #2 on: February 04, 2021, 09:04:31 pm »
Right after the Update() function gets called, I call the Draw() function which calls the DrawSight() function. The Entity class has quite a few variables, and functions which would have been quite a lot of code if I posted it all.

In the Entity class, I have a private variable
sf::Color m_SightColor;
which by default is (Red: 255, Green: 255, Blue: 255, Alpha: 255). I have setters/getter for this variable, and when I return the sf::CircleShape shape; to draw, I set the color of the shape with the GetSightColor() function. I tried this using the private member on its own without the function (m_SightColor) and it yielded the same result: (Red: 255, Green: 255, Blue: 255, Alpha: 255); even though I change it when the mouse moves over it.

The log shows that it changes, but it somehow resets after. I tried passing the color not as a reference to the setter to copy it, but it still resets. Would any other functions help you understand what's going on that I should show?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Color member not updating on sf::CircleShape draw
« Reply #3 on: February 13, 2021, 01:15:51 pm »
Can you provide the full entity class or even better a complete and minimal example?

My guess is either that you're updating and drawing a different entity, be it because you copy it or have different references.
Or that a different color property is being used.

Also keep in mind that a color with alpha channel = 0 means, that it will be fully transparent.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything