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

Author Topic: playing with color alpha value  (Read 2046 times)

0 Members and 1 Guest are viewing this topic.

lsmod

  • Newbie
  • *
  • Posts: 12
    • View Profile
playing with color alpha value
« 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)


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: playing with color alpha value
« Reply #1 on: July 07, 2015, 06:26:12 pm »
Use a float variable that you increase slowly and static_cast that as the color value.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lsmod

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: playing with color alpha value
« Reply #2 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 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: playing with color alpha value
« Reply #3 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lsmod

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: playing with color alpha value
« Reply #4 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: playing with color alpha value
« Reply #5 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 that helps you easily animate sprites and other objects. Oh, and it also solves the issue of this thread ;)
« Last Edit: July 11, 2015, 09:55:56 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: