Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I want to create a continuous fade in and out.

I'd like to have some function F(time) that returns values increasing from 0 to 255, then from 255 back down to -255, and repeats, as time progresses.

share|improve this question
1  
Rephrase your question, so we can make better answers. –  Gustavo Maciel Mar 20 '12 at 5:09
    
Rephrased the question. : ) –  2600th Mar 20 '12 at 5:20
1  
This isn't really related to game dev and has a certain lack of details. –  user14170 Mar 20 '12 at 9:51
add comment

3 Answers 3

up vote 1 down vote accepted

I know the question has been answered but just for old times sake here is the optimized classic way (works only with fixed steps but takes only 2 bytes ;-) )

//init:
unsigned char value=0;
unsigned char d=1;


//in loop:
value+=d;
if((value==0) || (value==255))
 d=-d;
share|improve this answer
    
I'm sorry if I'm completely missing something, but wouldn't d=-d; result in an unsigned underflow? I'd understand if d were a (signed) char. –  Koarl Mar 21 '12 at 10:57
1  
Well, it would be simpler to read but no difference actually except the need for extra conversions... Adding the underflowed d (which for '-1' has a value of 255) will act as a -1 to the value 'value'. –  Valmond Mar 21 '12 at 12:59
    
I see, of course value will also wrap around and end up one less than before if you just add 255. Thanks for the clarification! –  Koarl Mar 21 '12 at 13:02
add comment

What you're looking for is a periodic function like Sine. Here's how you can use Sine to achieve what you're looking for:

Create a loop counter
Begin your fade loop
    Increment your loop counter
    fade_amount = 255 * sin(loop_counter)

It's as simple as that. Since sin(loop_counter) will cycle from -1.0 through 0 to 1.0 and back again, you just multiply it by the maximum value you want.

share|improve this answer
    
What about loop_counter won't it overflow? –  2600th Mar 20 '12 at 5:35
4  
@Beast The sine function repeats every 2π radians or 360 degrees, so you could always add something like while(loop_counter >= twoPi) loopCounter -= twoPi at the end to wrap it back and prevent an eventual overflow. –  David Gouveia Mar 20 '12 at 5:46
3  
In addition to Davids comment: If you want your values to go through -1 and 1 exactly, then you get best results if you use fractions of π as your loop_counter. Eg. loop_counter += PI * 0.1. The smaller the fraction, the slower the fade in-out. –  bummzack Mar 20 '12 at 7:48
add comment

You can also use a tweening library to smoothly fade in and out such as cpptweener.

http://code.google.com/p/cpptweener/

share|improve this answer
1  
-1 for very poor quality C++. I would not recommend this library; it can't even get basic arithmetic correct. –  user744 Mar 20 '12 at 14:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.