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

Author Topic: [SOLVED] Gradually changing hue  (Read 4034 times)

0 Members and 2 Guests are viewing this topic.

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
[SOLVED] Gradually changing hue
« on: October 12, 2014, 10:12:02 am »
Hi. I have a RectangleShape that I'm trying to make gradually change to white as I hold a key. Actually, I've succeeded in this, but now I'd like a different key to gradually turn it black. It seems, however, that the minus operator will not work with sf:Color like the plus operator does.

To change my shape to white, I've used this code in the game loop.

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            sf::Color shapeColor = shape.getFillColor();
            if(shapeColor != sf::Color::White)
                shape.setFillColor(shapeColor + sf::Color(1, 1, 1));
        }

I could only imagine this very same code with a "-" in place of the "+" (and sf::Color::Black instead of White, and a different Key) should do the opposite.

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            sf::Color shapeColor = shape.getFillColor();
            if(shapeColor != sf::Color::Black)
                shape.setFillColor(shapeColor - sf::Color(1, 1, 1));
        }

However, that only gives me compiler errors that I don't entirely understand (I will post them if needed). Could somebody tell me why this won't work?
« Last Edit: October 14, 2014, 12:31:12 am by Rabees »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Gradually changing hue
« Reply #1 on: October 12, 2014, 10:17:47 am »
However, that only gives me compiler errors that I don't entirely understand (I will post them if needed)

Always give us the error message, you can hide it in a spoiler tag if the message is too long.


Edit: I get no errors with the code provided above.



AlexAUT
« Last Edit: October 12, 2014, 10:20:14 am by AlexAUT »

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Gradually changing hue
« Reply #2 on: October 12, 2014, 11:23:16 pm »
tbh part of the reason I didn't post the compiler errors anyway is because I wasn't sure how to copy them to the clipboard haha

..turns out Code::Blocks has a right click option just especially for that, so here you go.

(click to show/hide)

I'm baffled by why it is mentioning sf::Vector2 or sf::Time at all.

To be clear, the code that produces that error is exactly like the working "+" code aside from the fact that it uses a "-" instead (and a different key and sf::Color::Black, but I've already changed these to the same and can confirm the only issue is the "-" symbol).

It's really weird that it works for you, so maybe I should try it in a fresher project and see what happens.

edit: Just tried a fresh project, but I get the same errors when trying to minus.

(click to show/hide)
« Last Edit: October 12, 2014, 11:35:30 pm by Rabees »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Gradually changing hue
« Reply #3 on: October 12, 2014, 11:38:37 pm »
C++ and GCC are a bit notorious for unclear messages (generously speaking... you can get 5 pages of garbage if you mistype a single character if you're really lucky).
It's mentioning all of these types because the compiler tries its' damnnesst to find an operator- for you but it can't, so it goes through them all and says that none of them works because you can't convert/use sf::Color to/as sf::Vector2, sf::Time, etc.

Your actual problem is that you are (probably...) using SFML 2.1 from the site, which is from middle of July 2013 (according to github) and operator- for color was added almost two weeks after that in August (so you don't have it).
To get operator- use latest SFML (or write your own function or operator to do the same thing, if you can't/don't want to build last SFML).
« Last Edit: October 12, 2014, 11:44:21 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Gradually changing hue
« Reply #4 on: October 13, 2014, 12:12:54 am »
I was hoping it wasn't a matter of being outdated  :'( Swapping out versions and fiddling with compiler and project settings confuse me to no end, but I'm absolutely not smart enough to implement it myself.
Guess I'll just have to take the time to rectify that. Thanks for the help.
« Last Edit: October 13, 2014, 12:16:16 am by Rabees »

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Gradually changing hue
« Reply #5 on: October 13, 2014, 07:19:32 am »
I grabbed the newest github SFML source (https://github.com/SFML/SFML/archive/master.zip) and built it and replaced my old one. There's no error anymore, but, strangely, the minus operation now turns the rectangle invisible. However, it appears to be changing the rectangle black despite this, as when I start holding the key to turn it white (which functions perfectly fine), it becomes visible again and is at a darker shade from before it turned invisible. ???

(I've made sure it wasn't just blending in with my black background. Even on blue, it still completely disappears when I press the key that should darken the shape.)

Is this yet another issue with SFML as it is now at its current version, or have I made a mistake?

Does this code behave properly on your end?

//Outside game loop
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(100,100));
rect.setFillColor(sf::Color(100,100,100));

...

//Inside game loop
if(sf::Keyboard::isKeyPressed(sf::Keyboard::O))
{
       sf::Color rectColor = rect.getFillColor();
       if(rectColor != sf::Color::White)
             rect.setFillColor(rectColor + sf::Color(1, 1, 1));
}

if(sf::Keyboard::isKeyPressed(sf::Keyboard::P))
{
       sf::Color rectColor = rect.getFillColor();
       if(rectColor != sf::Color::Black)
             rect.setFillColor(rectColor - sf::Color(1, 1, 1));
}
« Last Edit: October 13, 2014, 07:23:36 am by Rabees »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Gradually changing hue
« Reply #6 on: October 13, 2014, 09:51:23 am »
sf::Color(1, 1, 1) is implicitly (default 4th argument - a C++ feature) sf::Color(1, 1, 1, 255) so your alpha goes to 0 after a single subtraction, use sf::Color(1, 1, 1, 0) to not touch the alpha at all.
Back to C++ gamedev with SFML in May 2023

Rabees

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Gradually changing hue
« Reply #7 on: October 14, 2014, 12:30:53 am »
Perfect! Thank you.