There has got to be a better way (Programming challenge?)

Steve smorrey at gmail.com
Sat Jul 15 20:51:57 MDT 2006


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?


More information about the PLUG mailing list