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

Author Topic: [Solved] Sprite Fade Out?  (Read 4761 times)

0 Members and 1 Guest are viewing this topic.

Legacy

  • Newbie
  • *
  • Posts: 4
    • MSN Messenger - legacy_x@hotmail.co.uk
    • View Profile
    • http://www.legacy-studios.org/
[Solved] Sprite Fade Out?
« on: September 28, 2011, 09:42:00 pm »
Hey, i'm kinda new to these forums, although i have been reading up a few threads here and there. But i couldn't quite find a thread to my problem that fixes it. So im sorry if you have read topics like this before.

Now, i have the Fade in working alright as it is, but i cant figure out how to get it to fade out. I am currently using SFML 1.6

heres my code from the splash scene's update method.

Code: [Select]
void SplashScene::Update(void)
{
// Check our App pointer
assert(NULL != engine && "SplashScene::Update() bad app pointer, init must be called first");

bool fadeComplete = false;

for(int a = 250; a > 0; a -= 5)
{
         mSplashSprite->SetColor(sf::Color(255,255,255, a));
if( a == 250)
{
fadeComplete = true;
}
}  

// Drop our state after 10 seconds have elapsed
if(fadeComplete == true && false == IsPaused() && GetElapsedTime() > 4.0f){

engine->mSceneManager.RemoveActiveState();
}
}


and my main loop looks like this.

Code: [Select]
// Loop while IsRunning returns true
while(IsRunning() && mWindow.IsOpened() && !mSceneManager.IsEmpty())
{
// Get the currently active state
IState* scene = mSceneManager.GetActiveState();

// Check for corrupt state returned by our StateManager
assert(NULL != scene && "App::Loop() received a bad pointer");

// Create a fixed rate Update loop
while(anUpdateClock.GetElapsedTime() > anUpdateNext)
{
// Handle some events and let the current active state handle the rest
sf::Event anEvent;
while(mWindow.GetEvent(anEvent))
{
// Switch on Event Type
switch(anEvent.Type)
{
case sf::Event::Closed:       // Window closed
Quit(StatusAppOK);
break;
case sf::Event::GainedFocus:  // Window gained focus
scene->Resume();
break;
case sf::Event::LostFocus:    // Window lost focus
scene->Pause();
break;
case sf::Event::Resized:      // Window resized
break;
default:                      // Current active state will handle
scene->HandleEvents(anEvent);
} // switch(anEvent.Type)
} // while(mWindow.GetEvent(anEvent))

// Let the current active state perform updates next
scene->Update();


// Update our update next time
anUpdateNext += mUpdateRate;
} // while(anUpdateClock.GetElapsedTime() > anUpdateNext)

// Let the current active state draw stuff
scene->Draw();

// Display Render window to the screen
mWindow.Display();

// Handle Cleanup of any recently removed states at this point as needed
mSceneManager.HandleCleanup();
}


I appreciate any support/help that i can get, and im sorry if its just me being stupid.

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Sprite Fade Out?
« Reply #1 on: September 29, 2011, 12:43:11 am »
Im not sure you 're doing it the right way. The first snippet, unless running in a thread, shouldn't do an animation at all.

You're trying to evolve the alpha channel of the sprite in a single pass, while that won't allow anything to be displayed in-between the begining and ending state of the alpha animation. You need to do the alpha change over time.

That meaning each frame, before you render everything, you should call the update, and pass it the elapsed time since the last frame, and update only by that fraction of time.

The fade in and fade out are exactly the same thing, but inverted. Try to solve it on your own with this information :)

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
[Solved] Sprite Fade Out?
« Reply #2 on: September 29, 2011, 09:46:04 am »
I think what you are missing is

Code: [Select]
mWindow.Clear()

which should be called once each frame before drawing

Legacy

  • Newbie
  • *
  • Posts: 4
    • MSN Messenger - legacy_x@hotmail.co.uk
    • View Profile
    • http://www.legacy-studios.org/
[Solved] Sprite Fade Out?
« Reply #3 on: September 29, 2011, 10:17:22 pm »
Quote from: "DevilWithin"
Im not sure you 're doing it the right way. The first snippet, unless running in a thread, shouldn't do an animation at all.

You're trying to evolve the alpha channel of the sprite in a single pass, while that won't allow anything to be displayed in-between the begining and ending state of the alpha animation. You need to do the alpha change over time.

That meaning each frame, before you render everything, you should call the update, and pass it the elapsed time since the last frame, and update only by that fraction of time.

The fade in and fade out are exactly the same thing, but inverted. Try to solve it on your own with this information :)


the scene->update() method is called each frame, the scene object holds the Splash Screen inside it, and when it is droped, it goes to the next in the list, which is the Title Screen.

I tried to invert the for statement, but it only displayed a black screen. I will look further into it, but thank you for your help anyway. :)

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Sprite Fade Out?
« Reply #4 on: September 29, 2011, 10:19:07 pm »
Does not matter, whats wrong is the animation friend.

The simple fact there is a for loop changing the alpha doesnt seem correct, its not done with a for loop at all, rather with calculations.

Hope it helps :)

Legacy

  • Newbie
  • *
  • Posts: 4
    • MSN Messenger - legacy_x@hotmail.co.uk
    • View Profile
    • http://www.legacy-studios.org/
[Solved] Sprite Fade Out?
« Reply #5 on: September 29, 2011, 10:37:28 pm »
Ah, i see, could you post a little example of what you mean. If im not using a for statement.

EDIT:

i was having a weird day yesterday. Just took another go at it, but this time instead of using a for statement, i switched it to an if, using a variable to control the alpha.

Code: [Select]
bool fadeComplete = false;
if(GetElapsedTime() > 1.5f)
{
if(mAlpha < 250)
{
mAlpha += 10;
mSplashSprite->SetColor(sf::Color(255,255,255, mAlpha));
if( mAlpha == 250)
{
fadeComplete = true;
}
}
}


thank you for your help, much appreciated.

But now i have another question related to fade in/out, how would i fade in or out, the entire contents of the screen?

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
[Solved] Sprite Fade Out?
« Reply #6 on: September 29, 2011, 11:44:00 pm »
More or less, this is the update in my engine's animation on colors.

Code: [Select]
void AnimationColorRange::Update(float Time){
if(!Playing)
return;

AnimationTime += Time;

Color color;

if(AnimationTime > Duration){
color = EndColor;
Stop();
}
else{
color.a = BeginColor.a + (unsigned int)(((EndColor.a-BeginColor.a)*AnimationTime)/Duration);
color.r = BeginColor.r + (unsigned int)(((EndColor.r-BeginColor.r)*AnimationTime)/Duration);
color.g = BeginColor.g + (unsigned int)(((EndColor.g-BeginColor.g)*AnimationTime)/Duration);
color.b = BeginColor.b + (unsigned int)(((EndColor.b-BeginColor.b)*AnimationTime)/Duration);


}

for(unsigned int i = 0; i < AnimatedObjects.size(); i++){
AnimatedObjects[i]->SetColor(color);
}
}


The function computes the right color for the time that has passed already, based on a BeginColor and a EndColor specified beforehand.

If you're sprites are in the hypothetic AnimatedObjects array, the whole screen fades in or out.

Hope it helps.

Legacy

  • Newbie
  • *
  • Posts: 4
    • MSN Messenger - legacy_x@hotmail.co.uk
    • View Profile
    • http://www.legacy-studios.org/
[Solved] Sprite Fade Out?
« Reply #7 on: September 29, 2011, 11:51:40 pm »
Ah thankyou for that. Ill see what i can do to get the same effect. :)