 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
seventoes
Joined: 02 Oct 2005 Posts: 79
|
Posted: Fri Apr 13, 2007 2:26 pm Post subject: Simple C++ Animation Class Not Working! |
|
|
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: |
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: |
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? |
|
| Back to top |
|
 |
dot_blank

Joined: 28 Sep 2005 Posts: 498 Location: Brasil
|
Posted: Fri Apr 13, 2007 3:39 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
seventoes
Joined: 02 Oct 2005 Posts: 79
|
Posted: Fri Apr 13, 2007 4:20 pm Post subject: |
|
|
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... |
|
| Back to top |
|
 |
seventoes
Joined: 02 Oct 2005 Posts: 79
|
Posted: Fri Apr 13, 2007 4:45 pm Post subject: |
|
|
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? |
|
| Back to top |
|
 |
memon
Joined: 03 Oct 2005 Posts: 63
|
Posted: Fri Apr 13, 2007 6:55 pm Post subject: |
|
|
Hint:
| Code: | ...
int frame;
int length;
...
float n = frame/length;
... |
:) |
|
| Back to top |
|
 |
seventoes
Joined: 02 Oct 2005 Posts: 79
|
Posted: Sat Apr 14, 2007 4:45 am Post subject: |
|
|
| frame and length are declared as members of Animation though, and int's can be typecasted to floats just fine, right? |
|
| Back to top |
|
 |
sturatt
Joined: 13 Jul 2006 Posts: 46
|
Posted: Sat Apr 14, 2007 5:58 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
seventoes
Joined: 02 Oct 2005 Posts: 79
|
Posted: Sat Apr 14, 2007 6:06 am Post subject: |
|
|
| ooo, i didnt know that! I assumed that it would be done automatically. ill try that out when i get the chance. thanks! |
|
| Back to top |
|
 |
Tinnus
Joined: 29 Jul 2006 Posts: 67
|
Posted: Sat Apr 14, 2007 7:28 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
seventoes
Joined: 02 Oct 2005 Posts: 79
|
Posted: Sat Apr 14, 2007 9:58 am Post subject: |
|
|
| Ohhhhh! Thanks! Makes sense now! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|