SFML community forums

Help => Graphics => Topic started by: lsmod on July 07, 2015, 06:19:31 pm

Title: playing with color alpha value
Post by: lsmod on July 07, 2015, 06:19:31 pm
Hello,

I'm playing with SFML to create animation for my project. I have an issue trying to increase very slowly alpha value of a sprite.

while (window.isOpen())
{              
// Get elapsed time
float delta = clock.restart().asSeconds();

auto alpha_etoile = etoile2_sprite.getColor().a;
etoile2_sprite.setColor(sf::Color(255, 255, 255, (alpha_etoile + 1 * delta)));
}

it work like this (but it's not what want):
etoile2_sprite.setColor(sf::Color(255, 255, 255, (alpha_etoile + 1)));

I believe it's because alpha is an unsigned char. So when I add only decimal to its value it's cast an value doesn't change. So I can really use distance by second for alpha because it's integer value only. But maybe I'm missing something.

otherwise It works for sprite's scale, rotation and position (those are all float variables I believe)

Title: Re: playing with color alpha value
Post by: eXpl0it3r on July 07, 2015, 06:26:12 pm
Use a float variable that you increase slowly and static_cast that as the color value.
Title: Re: playing with color alpha value
Post by: lsmod on July 07, 2015, 06:37:07 pm
thx, yes sure that's a solution. But It means I have to store that variable and link it to my sprite, while all the others sprite's parameters are store in sprite class. It's not really elegant 
Title: Re: playing with color alpha value
Post by: eXpl0it3r on July 07, 2015, 06:46:56 pm
I don't quite understand your post then.

Color components can have a integer value between 0 and 255, thus we use an Uint8 which matches these requirements exactly. If you now want to slowly add up some small value, you can do that, but we won't change the implementation to just use floats everywhere (if you were asking for that). ;)

You can always create your own entity class and add whatever function you need.
Title: Re: playing with color alpha value
Post by: lsmod on July 07, 2015, 07:14:44 pm
Quote
but we won't change the implementation to just use floats everywhere (if you were asking for that). ;)

why not it would be more convenient ? :p just kidding

It's just that was wondering if I was missing something. I will store variable like you said. Perhaps in object related to my animation. And I will do the same for Red, Green and Blue value.
Title: Re: playing with color alpha value
Post by: Nexus on July 11, 2015, 09:54:08 am
It's just that was wondering if I was missing something.
Yes, you're mixing logical data (animation progress) with visual representation (sf::Sprite alpha value). Don't use sf::Sprite to store your game logic. In a good design, logic and graphics are decoupled; this has many advantages such as the ability to change the visualization on its own.

By the way, there's no need to reinvent the wheel: in the Thor library, I've written the Animations module (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-animations.html) that helps you easily animate sprites and other objects. Oh, and it also solves the issue of this thread ;)