Simple Animation Question
I've finally got my threads working correctly and now I am on to animation for my game. Basically for every animation I have a Animation Struct.
typedef struct
{
int numberOfFrames;
int frameLength;
int currentFrame;
int intoFrame;
} Animation;
typedef struct
{
GLuint * texID;
int width;
int height;
Animation animation;
} Sprite;
Then from here I go to declare a sprite, then in each loop I handle which frame the animation is at with this.
Animation handleAnimation( Animation animation, int timeElapsed )
{
animation.intoFrame += timeElapsed;
while( animation.intoFrame > animation.frameLength)
{
animation.currentFrame++;
if(animation.currentFrame >= animation.numberOfFrames)
animation.currentFrame = 0;
animation.intoFrame -= animation.frameLength;
}
return animation;
}
In all essence it works perfectly, I'm just wondering if there is a better way to do this? better as in faster.
typedef struct
{
int numberOfFrames;
int frameLength;
int currentFrame;
int intoFrame;
} Animation;
typedef struct
{
GLuint * texID;
int width;
int height;
Animation animation;
} Sprite;
Then from here I go to declare a sprite, then in each loop I handle which frame the animation is at with this.
Animation handleAnimation( Animation animation, int timeElapsed )
{
animation.intoFrame += timeElapsed;
while( animation.intoFrame > animation.frameLength)
{
animation.currentFrame++;
if(animation.currentFrame >= animation.numberOfFrames)
animation.currentFrame = 0;
animation.intoFrame -= animation.frameLength;
}
return animation;
}
In all essence it works perfectly, I'm just wondering if there is a better way to do this? better as in faster.
Well, you could use one texture and use seperate portions of it. For example if you did it vertically and had 2 frames you would access the first at (0,0) (0,.5) (1,.5) (1,0) for left-top, left-bottom, right-bottom, top-bottom
Code:
void handleAnimation(Animation* animation, float timeElapsed)
{
animation->intoFrame += timeElapsed;
if( animation->intoFrame > animation->frameLength)
{
animation->currentFrame++;
if(animation->currentFrame >= animation->numberOfFrames)
animation->currentFrame = 0;
animation->intoFrame = 0;
}
}
Instead of:
you could just use:
EDIT: also, there is no need to use a while loop to find out which frame to draw, just do something like:
Code:
animation.currentFrame++;
if(animation.currentFrame >= animation.numberOfFrames)
animation.currentFrame = 0;you could just use:
Code:
animation.currentFrame = (animation.currentFrame+1) % animation.numberOfFrames;EDIT: also, there is no need to use a while loop to find out which frame to draw, just do something like:
Code:
void handleAnimation( Animation& animation, int timeElapsed )
{
animation.intoFrame += timeElapsed;
int framesElapsed = animation.intoFrame/animation.frameLength;
animation.currentFrame = (animation.currentFrame+framesElapsed) % animation.numberOfFrames;
animation.intoFrame -= framesElapsed*animation.frameLength;
}Code:
animation.currentFrame = (animation.currentFrame+1) % animation.numberOfFrames;Wouldnt the branch execute quicker than the divide? Thou we are talking millionths of a second.
James Wrote:modulo is faster than branching, in fact, branching is one of the worse things you can do in programmingCode:
animation.currentFrame = (animation.currentFrame+1) % animation.numberOfFrames;
Wouldnt the branch execute quicker than the divide? Thou we are talking millionths of a second.
But the branch difference is only 5-10 instructions apart so 99% of the time all the code would be in the processor so no memory fetch would result (where there's substantial slowdown)
However I am not up with the current G3/G4/G5 architectures. I am talking from 68K/603/604 experience.
However I am not up with the current G3/G4/G5 architectures. I am talking from 68K/603/604 experience.
while it is only 5-10 instructions apart, there is still the possibility of wrongly predicting the branch, thus causing you to have to flush your pipes. The G5 has a much longer pipe now. while it is only minor details, branching is much slowing than computation, most of the time
From the MPC7450 (PowerPC G4) User Manual it states most integer instructions are only 1 cycle and a bad branch prediction has a penalty of 7 cycles. I will also assume jumps are only 1 cycle.
This would incur 3 cycles on a good branch prediction or 10 cycles on a bad prediction.
This would incur 2 cycles making you right skyhawk. Of course this is the case if everything was in L1 cache and loaded into registers beforehand. If it wasn't, then you would see the cycle counts skyrocket but the skyhawk's example would still win.
Sorry to doubt you skyhawk, however for any other doubters, there's the proof.
Code:
animation.currentFrame++;
if(animation.currentFrame >= animation.numberOfFrames)
animation.currentFrame = 0;This would incur 3 cycles on a good branch prediction or 10 cycles on a bad prediction.
Code:
animation.currentFrame = (animation.currentFrame+1) % animation.numberOfFrames;This would incur 2 cycles making you right skyhawk. Of course this is the case if everything was in L1 cache and loaded into registers beforehand. If it wasn't, then you would see the cycle counts skyrocket but the skyhawk's example would still win.
Sorry to doubt you skyhawk, however for any other doubters, there's the proof.
This is the kind of optimisation without any point. The time spent in that is miniscule compared to the time spent in rendering the aforementioned animation, no matter how simple that rendering is.
That is true, but now that I know beforehand of this, any future games would use the new code. However I would think that compilers are also smart enough to include this code automatically.
This is the kind of change no compiler can make to date, because it would have to perform some quite complex semantical analysis of the code to understand what you want to do.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| simple question (private) | imaumac | 9 | 3,397 |
Oct 21, 2008 09:37 PM Last Post: imaumac |
|
| simple Question about movement | bonanza | 3 | 2,709 |
Oct 17, 2007 11:12 AM Last Post: bonanza |
|
| Really simple #pragma question | WhatMeWorry | 9 | 3,690 |
Dec 13, 2006 10:15 PM Last Post: Nick |
|
| Simple CarbonEvent Question | Abyssal | 3 | 2,761 |
Jan 1, 2006 09:53 PM Last Post: radiance |
|
| NSTimer / animation framerate question | MonitorFlickers | 5 | 4,526 |
Dec 4, 2005 01:10 PM Last Post: MonitorFlickers |
|

