Sign in to follow this  
roccog

[SDL/C++] Sprite Animation

Recommended Posts

Hi! I want to animate my sprite by using some frame... i think 4 frames for each direction... What kind of function i have to write in order to do this? I think there are no sdl functions for this... so i think i have to write down the entire function, but i don't know how... How can i load each image for frame? and how i have to use it then? I need to set speed and time? Can u help me, please? I'm a newbie... i searched on google, but i found nothing about this... no examples... :( [Edited by - roccog on November 30, 2005 3:24:24 PM]

Share this post


Link to post
Share on other sites
Here's how I would do it:

1. Create a small structure that has the following items in it:
* how many frames the animation has
* a list of the frames in the order that they will be played

2. Put the structure into your sprite structure. Also, put a pointer to the image in the sprite structure as well.

3. Write a simple function that sets what animation it will have (you will define the animations externally).

4. Write a function that does the animation itself. For this part, it would simply loop through the frames that the current animation feeds it. When it reaches the last one, it will go back to the first one. Here's some sample code of this:


struct sAnimation
{
int frames[4]; // the frame references themselves
int numFrames; // this will always equal 4 in your case
int FPS; // how many frames per second this will run
};

class cSprite
{
public:
void Animate (); // the animation function itself
//...
private:
int currentFrame; // what frame in the animation we're on
int oldTime; // the last time it went
sAnimation animation;

};

void cSprite::Animate ()
{
int frameIndex; // this keeps track of what frame it is for this function only


oldTime += timeDifference; // time difference is how long it took the last frame to process

if (oldTime >= (1000/FPS))
{
frameIndex = animation.frames[currentFrame]; // this is an array of integers that contains the actual frame references to the animation
currentFrame++; // increment it;
if (currentFrame >= numFrames) currentFrame = 0;
oldTime = 0; // reset the timing variable
}
}

Share this post


Link to post
Share on other sites
i knew how to load a bmp with sdl...
but i don't know how to use the function posted by Oberon_Command...

i write this for the commands:

keystate = SDL_GetKeyState(NULL);

if (keystate[SDLK_LEFT] ) {
sprite1.x -= 4;
dir = 1; /* direzione: sinistra */
}
if (keystate[SDLK_RIGHT] ) {
sprite1.x += 4;
dir = 2; /* direzione: destra */
static diag2 = 2;
}
if (keystate[SDLK_UP] ) {
sprite1.y -= 4;
dir = 3; /* direzione: alto */
}
if (keystate[SDLK_DOWN] ) {
sprite1.y += 4;
dir = 4; /* direzione: basso */
static diag4 = 4;
}
if (keystate[SDLK_a] ) {
sprite1.y -= 4;
dir = 4; /* direzione: basso */
static diag4 = 4;
}


and my sprite struct is:

typedef struct Sprite { /* sprite dinamico */
SDL_Surface *img; /* immagine dello sprite */
int x, y; /* coordinate dello sprite in 2d! */
int col_width, col_height, col_x_offset, col_y_offset;
} Sprite;


if u want i can post the entire code i wrote for my game...

i think i have to load all the frame like this:

/* ## load sprite, uso temp come swap... ## */
temp = SDL_LoadBMP("sprite.bmp");
sprite1.img = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
...
...

but i don't know what i have to do to use this in the animation array...


(sorry for my english, i'm italian...)

Share this post


Link to post
Share on other sites
Awhile ago I wrote a wrote a C++ class encapsulating sprites and basic sprite animation in SDL. You can download it here. It still needs a little bit of work, but it also comes with an example app. I wrote it on linux, so you might need to make a few minor changes to get it working on another OS, but it should give you a basic idea of one way to do sprite animation in SDL.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this