Simple C++ Animation Class Not Working!

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
seventoes
Posts: 79
Joined: Sun Oct 02, 2005 4:50 am

Simple C++ Animation Class Not Working!

Post by seventoes »

All right.. im trying to make a generic number animation class for a small game im working on, but im still pretty rusty when it comes to C++. To me it seems like this class SHOULD work fine, but for some reason its not!

Code: Select all

class Animation {
	private:
  int frame;
  float beginValue;
  float targetValue;
  int length;
  int animType;
	public:
  float currentValue;
  bool done;
  bool paused;
  Animation(float startNum, float endNum, int totalSteps, int type) {
  	Reset(startNum,endNum,totalSteps,type);
  }
  void Reset(float startNum, float endNum, int totalSteps, int type) { // Make object reusable
  	frame = 0;
  	done = false;
  	paused = true;
  	currentValue = startNum;
  	beginValue = startNum;
  	targetValue = endNum;
  	length = totalSteps;
  	animType = type;
  }
  void Start() {
  	paused = false;
  	done = false;
  }
  void Stop() {
  	frame = 0;
  	done = true;
  	currentValue = 0;
  	beginValue = 0;
  	targetValue = 0;
  	length = 0;
  }
  void Pause() {
  	paused = true;
  }
  void Unpause() {
  	paused = false;
  }
  float Step() {
  	if (paused || done) return currentValue;
  	frame++;
  	if (frame>=length) {
    done = true;
    currentValue = targetValue;
    return 0;
  	}
  	float delta = targetValue-beginValue;
  	switch (animType) {
    case ANIM_LINEAR:
    	// TODO: Other animations
    	break;
    case ANIM_EASEIN:
    	float n = frame/length;
    	currentValue = delta*n*n*n+beginValue;
    	return currentValue;
  	}
  	return currentValue;
  }
};
Then i do:

Code: Select all

Animation *animator = new Animation(0,255,50,ANIM_EASEIN); // Create new animator for alpha, 50 frames long, using ANIM_EASEIN
animator->Start();

...

//Then, every frame,
animator->Step();

// Then when drawing my sprite, i use animator->currentValue for the alpha
What should happen is i should get a value that increases up to 255 on the 50th frame, but what is happening is I just get '255' constantly! The code looks fine to me, ive been messing with it all night!

Anyone got an idea why it wont work?
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

remember those function definitions inside a class declaration
will make those functions INLINED so for simple things its best
to use like stop function and stuff but for a function that could
see growth like you Step() it is wise to have this outside your
class header file and in its own seperate aniStep.c file

its also nice to have all private accesable variables AFTER the
destructor and constructor of the class and then have functions
after or you could decide to have the private variables AFTER
all the functions right at end ...either way at the top just makes
things a slight harder to scan thru when reading

sorry for this C++ tips but i am a syntax junkie :P

as for your problem
it looks to me like this is what is happening
// if (paused || done) return currentValue;
when paused and done are OR'ed together (bitwise OR)
then they seem to always return true so then your currentValue
is always returned and no incrementation is done

hope maybe that helps you
10011011 00101010 11010111 10001001 10111010
seventoes
Posts: 79
Joined: Sun Oct 02, 2005 4:50 am

Post by seventoes »

false or false returns true??
Bitwise that would be something like
00000000
or
00000000

..which should return
00000000
which equals false. Right :?

thanks for the suggestions, i moved my files around, and i seperated (paused || done) into if (paused).. if (done).., but that didnt help, im still getting the targetValue returned for some strange strange reason...
seventoes
Posts: 79
Joined: Sun Oct 02, 2005 4:50 am

Post by seventoes »

AArrg! Its not not working the way i thought it was (huh?)! It seems for the duration of the animation, im just getting beginValue, then at the end (when frame>=length), currentValue is being set to targetValue, and the animation thinks its finished.

Silly me was printing the debug info on top of the image that was getting the alpha applied to it, so in this case beginValue is 0, so the image is 100% transparent (right at the beginning of the program, so i didnt notice it..), and the text is disappearing along with it..

Anyways, now its something wrong with the acctual animation part, in the switch statement. Anyone help with that?
memon
Posts: 63
Joined: Mon Oct 03, 2005 10:51 pm

Post by memon »

Hint:

Code: Select all

...
int frame;
int length;
...
float n = frame/length;
...
:)
seventoes
Posts: 79
Joined: Sun Oct 02, 2005 4:50 am

Post by seventoes »

frame and length are declared as members of Animation though, and int's can be typecasted to floats just fine, right?
sturatt
Posts: 46
Joined: Thu Jul 13, 2006 4:21 pm

Post by sturatt »

seventoes wrote:frame and length are declared as members of Animation though, and int's can be typecasted to floats just fine, right?
yes, but you need to actually typecast it:

n = (float)frame/length;

the way you have it now, n will always be 0 unless frame >= length
seventoes
Posts: 79
Joined: Sun Oct 02, 2005 4:50 am

Post by seventoes »

ooo, i didnt know that! I assumed that it would be done automatically. ill try that out when i get the chance. thanks!
Tinnus
Posts: 67
Joined: Sat Jul 29, 2006 1:12 am

Post by Tinnus »

When you do something like

a = expression;

"expression" is evaluated and calculated, with the biggest precision of the two operands for each arithmetic binary operation (+, -, *, /), REGARDLESS of the type in the left, and THEN the final value is copied, being converted if needed. Which means:

int/int = int
float/int = float
int/float = float
double/int = double
float/double = double

and so on. And your case:

float = int/int

means

float = res , where
res = int/int , hence
res = int , and then
float = int

On the other hand, you can do

float = (float)int/int
float = res , where
res = (float)int/int
res = float/int
res = float ,then
float = float

(note the difference in the time and place the conversion is done)
Let's see what the PSP reserves... well, I'd say anything is better than Palm OS.
seventoes
Posts: 79
Joined: Sun Oct 02, 2005 4:50 am

Post by seventoes »

Ohhhhh! Thanks! Makes sense now!
Post Reply