0
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Rephrase your question, so we can make better answers. \$\endgroup\$ Commented Mar 20, 2012 at 5:09
  • \$\begingroup\$ Rephrased the question. : ) \$\endgroup\$
    – 2600th
    Commented Mar 20, 2012 at 5:20
  • 1
    \$\begingroup\$ This isn't really related to game dev and has a certain lack of details. \$\endgroup\$
    – user14170
    Commented Mar 20, 2012 at 9:51

3 Answers 3

1
\$\begingroup\$

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;
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Koarl
    Commented Mar 21, 2012 at 10:57
  • 1
    \$\begingroup\$ 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'. \$\endgroup\$
    – Valmond
    Commented Mar 21, 2012 at 12:59
  • \$\begingroup\$ 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! \$\endgroup\$
    – Koarl
    Commented Mar 21, 2012 at 13:02
10
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ What about loop_counter won't it overflow? \$\endgroup\$
    – 2600th
    Commented Mar 20, 2012 at 5:35
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ Commented Mar 20, 2012 at 5:46
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – bummzack
    Commented Mar 20, 2012 at 7:48
1
\$\begingroup\$

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

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ -1 for very poor quality C++. I would not recommend this library; it can't even get basic arithmetic correct. \$\endgroup\$
    – user744
    Commented Mar 20, 2012 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .