Original Post
Hi, I am making a 2D game and am using SDL for the graphic side. I want to make my sprite animate, so I created a class for it. I want it to animate from let's say frame 1 to frame 4, but it stopped at frame 2. My code is as follow:
class sprite
{
SDL_Rect spriteInfo[255];
SDL_Rect clipInfo[255];
SDL_Surface* spriteImage;
SDL_Surface* realSprite;
public:
SDL_Surface* loadSprite(const char* spriteFile,int alphaR,int alphaG,int alphaB)
{
spriteImage = SDL_LoadBMP(spriteFile);
realSprite = SDL_DisplayFormat(spriteImage);
Uint32 colorkey = SDL_MapRGB(realSprite->format,alphaR,alphaG,alphaB);
SDL_SetColorKey(realSprite,SDL_RLEACCEL | SDL_SRCCOLORKEY,colorkey);
SDL_FreeSurface(spriteImage);
return realSprite;
}
void setFrame(int frameNumber,int x,int y,int width,int height)
{
clipInfo[frameNumber].x = x;
clipInfo[frameNumber].y = y;
clipInfo[frameNumber].w = width;
clipInfo[frameNumber].h = height;
}
void displayFrame(int frameNumber,int posX,int posY)
{
spriteInfo[frameNumber].x = posX;
spriteInfo[frameNumber].y = posY;
SDL_BlitSurface(realSprite,&clipInfo[frameNumber],screen,&spriteInfo[frameNumber]);
}
void playAnimation(int posX,int posY,int startFrame,int endFrame,int actionTime)
{
int currentFrame;
currentFrame = startFrame;
Uint32 timeStart = 0;
int lastTick = 0;
timeStart = SDL_GetTicks();
if ((timeStart - lastTick) > actionTime)
{
currentFrame ++;
lastTick = timeStart;
}
spriteInfo[currentFrame].x = posX;
spriteInfo[currentFrame].y = posY;
SDL_BlitSurface(realSprite,&clipInfo[currentFrame],screen,&spriteInfo[currentFrame]);
}
}; I think probably I've did a mistake in playAnimation function, but I don't know how to make it work properly. Please help me, thanks. [Edited by - Virion on October 3, 2007 9:13:31 PM]