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

Author Topic: [SOLVED]Fading a sprite/image in or out  (Read 15392 times)

0 Members and 1 Guest are viewing this topic.

Saruto

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED]Fading a sprite/image in or out
« on: June 04, 2010, 07:16:14 pm »
Is there any specific way to fade a sprite or an image in SFML? I've been trying to do it for some time now and I can never get it to work.

Here's what I'm trying to:

Code: [Select]
sf::Image Overworld;
if(!Overworld.LoadFromFile("overworld.png"))

......

         int alpha = 0;
sf::Sprite OverworldSprite(Overworld);
OverworldSprite.SetColor(sf::Color(255, 255, 255, alpha));

                        float filterDelay = 0.02;
sf::Clock clockFilter;

if((alpha < 100) && (clockFilter.GetElapsedTime() > filterDelay))
{
alpha += 1;
OverworldSprite.SetColor(sf::Color(255, 255, 255, alpha));
clockFilter.Reset();
}


The above mentioned code just nets me a black screen. Thus, the alpha variable isn't being changed/the change is overridden by something. What am I doing wrong? Also, if there is a better way just let me know.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #1 on: June 04, 2010, 10:31:04 pm »
From a glance it looks like you need a loop and to define your clock outside of your loop.

Either that or use a better set value than 1. Try at least 60. It should be gray on a black background.

Saruto

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #2 on: June 04, 2010, 10:47:47 pm »
Quote from: "Ashenwraith"
From a glance it looks like you need a loop and to define your clock outside of your loop.

Either that or use a better set value than 1. Try at least 60. It should be gray on a black background.


I'm kind of a noob at this...if possible could you be a little more clear?

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #3 on: June 05, 2010, 02:55:06 am »
First try to set your alpha to at least 100 to see if it works at all (255 is max).

If you want to animate a fade you need a loop.

There is generally a while loop for display, but it looks like the problem is that you keep creating a clock every time it loops so it will never trigger. Put it before the loop starts. Same thing with your alpha being set to 0, etc

Saruto

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #4 on: June 05, 2010, 10:06:13 am »
Quote from: "Ashenwraith"
First try to set your alpha to at least 100 to see if it works at all (255 is max).

If you want to animate a fade you need a loop.

There is generally a while loop for display, but it looks like the problem is that you keep creating a clock every time it loops so it will never trigger. Put it before the loop starts. Same thing with your alpha being set to 0, etc


Will do.

Saruto

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #5 on: June 05, 2010, 06:27:20 pm »
Still can't get it to work, I think my loop is wrong.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #6 on: June 06, 2010, 03:29:02 am »
Post your fading code and loop.

If you're having a lot of problems I would get rid of the clock and just update it with the frames of the loop. Extra clocks in animation are not necessary but used for acceleration/deceleration.

Saruto

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #7 on: June 06, 2010, 05:54:03 am »
Quote from: "Ashenwraith"
Post your fading code and loop.

If you're having a lot of problems I would get rid of the clock and just update it with the frames of the loop. Extra clocks in animation are not necessary but used for acceleration/deceleration.


Code: [Select]
// Set Overworld alpha
// Alpha variables and delay
int alpha = 0;
double Delay = 0.02;

// Create sprites
sf::Sprite OverworldSprite(Overworld);
OverworldSprite.SetColor(sf::Color(255, 255, 255, alpha));
sf::Sprite LogoSprite(Logo);

// Fade loop

if ((alpha < 100) && (GameSplash.GetFrameTime() > Delay))
{
OverworldSprite.SetColor(sf::Color(255, 255, 255, alpha += 1));

}


That gets me a black screen (loop isn't being triggered) and when I remove the GetFrameTime function the loop simply sets the alpha to 1 (or whatever number I set the rate of change to).

I've included pictures of the two scenarios. http://i46.tinypic.com/rub7ma.jpg

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #8 on: June 06, 2010, 08:18:08 pm »
Here I just coded up an example, maybe someone can post it on the wiki or something. I noticed this has been asked three of four times on the forum.

In the future you should post the loop when people ask you because that's what makes it work. If you don't know what a loop is just search online.

Code: [Select]
#include <SFML/Graphics.hpp>

//***********************************
int main()
{
    sf::Sprite sprt;
    sf::Image img;

    img.LoadFromFile("image.png");
    sprt.SetImage(img);

    sf::RenderWindow App(sf::VideoMode(1600,800,32),"SFML Alpha Demo");
    App.SetFramerateLimit(60);
    App.UseVerticalSync(false);

    int a=255;
    while(App.IsOpened())
    {
        if(a<0){a=255;}
        sprt.SetColor(sf::Color(255,255,255,a));        
        a--;

        App.Clear(sf::Color::Green);
        App.Draw(sprt);
        App.Display();
    }

    return EXIT_SUCCESS;
}

Saruto

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #9 on: June 06, 2010, 09:52:43 pm »
Quote from: "Ashenwraith"
Here I just coded up an example, maybe someone can post it on the wiki or something. I noticed this has been asked three of four times on the forum.

In the future you should post the loop when people ask you because that's what makes it work. If you don't know what a loop is just search online.

Code: [Select]
#include <SFML/Graphics.hpp>

//***********************************
int main()
{
    sf::Sprite sprt;
    sf::Image img;

    img.LoadFromFile("image.png");
    sprt.SetImage(img);

    sf::RenderWindow App(sf::VideoMode(1600,800,32),"SFML Alpha Demo");
    App.SetFramerateLimit(60);
    App.UseVerticalSync(false);

    int a=255;
    while(App.IsOpened())
    {
        if(a<0){a=255;}
        sprt.SetColor(sf::Color(255,255,255,a));        
        a--;

        App.Clear(sf::Color::Green);
        App.Draw(sprt);
        App.Display();
    }

    return EXIT_SUCCESS;
}


Thanks for helping out.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]Fading a sprite/image in or out
« Reply #10 on: June 07, 2010, 12:12:09 am »
No prob.

If this fixed your problem edit the thread tittle and add [SOLVED]

This makes it so other people don't have to check it.