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

Author Topic: Fade in - fade out transition function  (Read 2779 times)

0 Members and 1 Guest are viewing this topic.

Nero

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Fade in - fade out transition function
« on: August 31, 2018, 02:28:09 pm »
Hello there!
I have a rectangle shape with the size of the window.
I'm trying to create a transition effect that fades in the rectangle and then fades out when the first fade is fully completed.
This whole effect starts when someBool == superiorBool::True and ends when someBool == superiorBool::Unitialized.

void Application::transition()
{
        transitionRectangle.setFillColor(sf::Color(0,0,0, transitionRectangleAlphaChannel)); // transitionRectangleAlphaChannel is initialized as 0 by default

        if (transitionClock.getElapsedTime().asSeconds() > 0.1f && transitionRectangleAlphaChannel < 255.f && someBool == superiorBool::True)
        {
                transitionClock.restart();
                transitionRectangleAlphaChannel += 5;
        }
        else if (transitionClock.getElapsedTime().asSeconds() > 0.1f && transitionRectangleAlphaChannel > 0.f && someBool == superiorBool::False)
        {
                transitionClock.restart();
                transitionRectangleAlphaChannel -= 5;
        }

        if (someBool == superiorBool::True && transitionRectangleAlphaChannel == 255)
        {
                someBool = superiorBool::False;
        }
        else if (someBool == superiorBool::False && transitionRectangleAlphaChannel == 0)
        {
                someBool = superiorBool::Uninitialized;
        }
 

For some reasons it doesn't work. Any ideas?
Thanks in advance!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Fade in - fade out transition function
« Reply #1 on: August 31, 2018, 11:12:42 pm »
Is transitionRectangleAlphaChannel a float?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Fade in - fade out transition function
« Reply #2 on: September 02, 2018, 08:53:57 am »
Here you go. You owe me 5 mins  :P

#include <SFML/Graphics.hpp>

void transition();

sf::RectangleShape transitionRectangle{ sf::Vector2f(100, 100) };
sf::Uint8 transitionRectangleAlphaChannel = 0;

enum class superiorBool { Uninitialized, True, False };
superiorBool someBool = superiorBool::True;

sf::Clock transitionClock;

int main()
{
        sf::RenderWindow WINDOW(sf::VideoMode(800, 450), "SFML works!");
        WINDOW.setFramerateLimit(20);

        while (WINDOW.isOpen())
        {
                sf::Event EVENT;

                while (WINDOW.pollEvent(EVENT))
                {
                        switch (EVENT.type)     { case sf::Event::Closed: WINDOW.close(); break; }
                }

                transition();
                transitionRectangle.setFillColor(sf::Color(255, 255, 255, transitionRectangleAlphaChannel));

                WINDOW.clear();        
                WINDOW.draw(transitionRectangle);              
                WINDOW.display();
        }

        return 0;
}

void transition()
{
        transitionRectangle.setFillColor(sf::Color(0, 0, 0, transitionRectangleAlphaChannel));

        if (transitionClock.getElapsedTime().asSeconds() > 0.1f && transitionRectangleAlphaChannel < 255.f && someBool == superiorBool::True)
        {
                transitionRectangleAlphaChannel += 5;
                transitionClock.restart();

                if (transitionRectangleAlphaChannel == 255)
                {
                        someBool = superiorBool::False;
                }

        }
        else if (transitionClock.getElapsedTime().asSeconds() > 0.1f && transitionRectangleAlphaChannel > 0.f && someBool == superiorBool::False)
        {
                transitionRectangleAlphaChannel -= 5;
                transitionClock.restart();

                if (transitionRectangleAlphaChannel == 0)
                {
                        someBool = superiorBool::Uninitialized;
                }
        }
}
 
« Last Edit: September 02, 2018, 08:59:40 am by NGM88 »