How about something like this:
In the header:
int mMode;
int mSkipper;
float mRGBA;
enum { Incrementing = 1, Decrementing = -1 };
In the implementation (more or less):
if( getId == selectedId )
{
if( ++mSkipper % 10 == 0 )
{
if( mRGBA > 10 )
mMode = Decrementing;
else if( mRGB < -10 )
mMode = Incrementing;
mRGBA += mMode;
}
}
I probably missed lots of details here, but you get the point. Use an
incrementer variable that is either -1 or +1, and just always add that
value to the RGB intensity. Saves you some extra logic. Also note the
use of mod (%) to check mSkipper for every 10th run instead of checking
for equality with 10. This is the de facto standard way for doing
something every Nth time, and you don't have to ever assign the
variable, which is a fun hack. :)
By the way, I always use names (ie, enums) instead of bools to indicate
this kind of state (incrementing vs. decrementing) unless the state is
clearly a boolean type of state.
Also, are you sure this isnt' C++ and not just C? It looks like it to me
based on this comment: //Con::printf("RGBA is %g",mRGBA);
Enjoy!
--Dave
Steve wrote:
Hello all recently I was tasked with causing an object to glow in a
pulsating pattern.
The graphics bits aside it basically boils down to the following 3 steps.
#1 Determine If this object is the one we want to glow
#2 If this object is supposed to glow then increment it's RGBA
intensity once every 10th frame (otherwise it will flicker if it's
less than 10 and it looks crappy > 10).
#3 If a certain RGB intensity has been reached then start decrementing
until we get back to 0 then repeat step 2
This is what I've come up with, and I hate to admit it, but I think
the code looks like hell. It seems like there HAS to be a better way,
but I'm not seeing it.
Anyways this code is in c (Obviously) and it's called for each frame
the object is actually going to be rendered in.
mSkipper++;
if (getId() == selectedId){ //Are we selected?
if(mSkipper == 10){ // Yes so allow for a color change every nth
frame
if(mDummy < -1){
mDummyBool = true; //Set inc or dec
}
if(mDummy > 50){ //Anything beyond 50 and the effect is no
longer noticable
mDummyBool = false; //set inc or dec
}
if(mDummyBool){
mDummy++;//Inc
}else{
mDummy--;//Dec
}
//if(mDummy ==0){mDummy = -1;}
mRGBA = mDummy/10;
//Con::printf("RGBA is %g",mRGBA);
}
}else{
mRGBA = 1.0;
}
if(mSkipper >= 10){ //Allow for a color change every nth frame
mSkipper = 1;
}
if(mRGBA > 10 || mRGBA < -10){ //Clamp it to legal ranges
mRGBA = 1.0;
}
In this code sample mSkipper is an Integer counter that is used to
skip the function for n frames until n in this case 10 frames have
passed.
mDummy is the actual value counter, each time mSkipper is equal to 10
mDummy is incremented or decremented by 1.
mDummyBool is a helper value that lets us know if we increment or
decrement.
There is a spread of 50 distinct possible intensity values represented
by mDummy /10 which would be floats from 0.0- 5.0 This is of course
stored in the mRGBA value.
Anyways my instincts tell me there is a much easier way to accomplish
what I'm trying to do here, but for the life of me I can't seem to
figure out what it would be.
Any thoughts?
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/