I am trying to figure out the proper and best way to do an animation..
I have a small sprite (16x32) and a texture that is 256x32 wide. Each 16 Pixels there is a new animation stored (walking left 5 anims, waiting and then walking right!).
I currently just move the sprite from left to right and change the u Coordinate by 16 each animation and reset to zero again when the 5 animations going right are done..
This works just fine and looks as it should. The thing I am not sure is if
I want to have the sprite walk right for a certain amount of time, then stop and play the waiting animation and the turn around and walk left again...
I know this isnt exactly psp dev related but I am hoping that somebody here can point me in a direction that things like that are properly done!?
thanx
Proper Way to do an animation?
Well, just play the right moving animation by setting your start u to the position of the first frame of the animation and play until you finish, then set the u to the position of the waiting animation and after this finishes, start with the left animation. So the only problem is to know the right u values for each animation frame. Then it's only a matter of correct checking for finished animations and starting another one.
However, I would highly recommend you to create a little organized type of animation system, to be more flexible and also be able to use the code for other animations. For this, you should create a structure for an animation frame that holds the coordinates of the sprite in the texture and maybe also some information about the time the sprite is to be displayed, making something like this:
struct AniFrame {
int u, v;
int delay;
}
Then the next level would be to create a structure for a complete animation sequence, which contains information on the number of frames included, the currently displayed frame, the time when the last update occured and maybe the base animation speed.
struct AniSequence {
int numframes;
int curframe;
int lastupdate;
int fps;
struct AniFrame *frames;
}
Then just create a function that plays a animation and takes the time of the current screen update and a display position and the animation structure as parameter.
void play_ani( int x, int y, int ms, struct AniSequence* ani )
In this function you'd check for the animation to be updated (ie the next frame is to be displayed after the ms exceeds the lastupdate time plus the fps time delta plus the delay time of the frame).
Then it just draws the sprite from the texture to the wanted position.
With this structure you could check on every screen update if the current frame of a animation is greater than the number of frames, meaning the animation is finished and either to be started again or another animation to be started. With this its very easy to create complex animation sequences out of a pool of available animations.
Hope this helps getting a basic idea of the concept :)
However, I would highly recommend you to create a little organized type of animation system, to be more flexible and also be able to use the code for other animations. For this, you should create a structure for an animation frame that holds the coordinates of the sprite in the texture and maybe also some information about the time the sprite is to be displayed, making something like this:
struct AniFrame {
int u, v;
int delay;
}
Then the next level would be to create a structure for a complete animation sequence, which contains information on the number of frames included, the currently displayed frame, the time when the last update occured and maybe the base animation speed.
struct AniSequence {
int numframes;
int curframe;
int lastupdate;
int fps;
struct AniFrame *frames;
}
Then just create a function that plays a animation and takes the time of the current screen update and a display position and the animation structure as parameter.
void play_ani( int x, int y, int ms, struct AniSequence* ani )
In this function you'd check for the animation to be updated (ie the next frame is to be displayed after the ms exceeds the lastupdate time plus the fps time delta plus the delay time of the frame).
Then it just draws the sprite from the texture to the wanted position.
With this structure you could check on every screen update if the current frame of a animation is greater than the number of frames, meaning the animation is finished and either to be started again or another animation to be started. With this its very easy to create complex animation sequences out of a pool of available animations.
Hope this helps getting a basic idea of the concept :)