fixed: SDL sprite animation

Started by
4 comments, last by Virion 16 years, 7 months ago
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]
Advertisement
no one knows how? :(
Quote:Original post by Virion
no one knows how? :(


Well, I don't have time to look over this code right now, for I have school, and was going to post a question myself, but what I can give you if you haven't looked at it already, and one programmer here is going to be happy I'm doing some free advertisement is http://lazyfoo.net/SDL_tutorials/lesson20/index.php

easier lazyfoo.net

The direct link deals specifically on animation, and as I've made games in the past I've always found those tutorials for SDL quite helpful.

Hope this helps, and Good Luck!

It seems like the example at the site you given is a little different from mine. I used timing system but it didn't. Any difference between both? Which one has more advantages?

By the way I still don't know which part of my codes got problems, please help. >_<
You do have a problem in your playAnimate function. You are creating the lastTick variable everytime you call that function.
int lastTick = 0;
which make this statement:
if ((timeStart - lastTick) > actionTime)
true all the time becouse its equal to:

[source langg = "cpp"]if ((timeStart - 0) > actionTime){ currentFrame ++; lastTick = timeStart;}


Instead you should save it to a member variable of your class inside the loop like:

m_lastTick= lastTick;

Becouse, after your function lastTick will be destroyed it's information will too.

You actiully figured out that the problem was related to that function. You should probaly have looked a little harder after the bug. Not becouse we wont help you discover it, but by figuring out your own bugs, you become a better and more produktive programmer, and youll be prepared till when you functions is aboue 100 lines long.
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
Thank you, it works now. :)

This topic is closed to new replies.

Advertisement