What's the best way to store sprite animations?

Started by
25 comments, last by Raveler 18 years, 11 months ago
Alright guys, I am making a 2d tile-based game using SDL for my C++ programming class, and I am currently designing the CSprite base class. So what is the best way to store animations for a sprite? I was thinking of storing it as text file:
WalkNorth:
	(0,0)(0,1)(0,2)(0,3)(0,4)(0,5)
WalkSouth:
	(1,0)(1,1)(1,2)(1,3)(1,4)(1,5)
WalkEast:
	(2,0)(2,1)(2,2)(2,3)(2,4)(2,5)
StandLeft:
	(2,0)(2,1)(2,2)(2,3)(2,4)(2,5)
Each coordinate pair represents a tile in the tileset where 0,0 is the first tile. What do you think? I've got till May 17 to get the game done, so the quickest method would be good to know.
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
Advertisement
A better way is to store the tiles number instead of its (x,y) coordinate in the tileset.
For instance, what if you wish to change tileset, but use the same animation? All creatures may have "walk", but they use diffrent art.

This would not work with your approach if the tilesets dont look exactly the same. For instance, the tileset for "squirrle" is only 32x32 big per tile and all tiles fit on one single row in the image. While the "knight" tile is 256x256, and the tileset image has 4 rows and 3 columns or something.

Instead just save the tiles number, and calculate the position with:
x = (int)(number / num_rows_of_this_tileset);
y = number % num_rows_of_this_tileset;
(atleast i think it was like this)

And your animations look like this:
walk = 0,1,2,3,2,0,1;
Then it can be used with any tileset.


Another thing may be to save the number of millisecs that each frame should be visible. For instance if you have some spell casting animation where the character holds his arms up, you may want to display that frame for a longer period of time than the rest of the frames.


Hope this was of any help.

Shields up! Rrrrred alert!
Thanks peter_b, but I think I wasn't clear enough.

I don't have to calculate any coordinates, my CTileSet class does that already.
int x = 45;int y = 100;int tsRow = 0;int tsCol = 0;CTileSet tileset("graphics\tileset1.bmp");tileset.BlitTile(tsCol, tsRow, x, y);


That blits the the tile in the first row and first column in the tileset onto the screen at 45 and 100.

But I was considering what you said about having a specified time period for each frame, the other option would to type the same frame multiple times (kinda ugly though).

Example with miliseconds per frame:
WalkNorth:         (0,0)15,         (0,1)2,         (0,2)4,         (0,3)9,         (0,4)5,         (0,5)5,


(number next to coord pair is the miliseconds per frame)

How 'bout that?
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
I have mine like this:
//THIS IS JAVAclass animationData {   int dmx; //CHANGE IN MAP POSITION AFTER ANIMATION IS DONE   int dmy;   int[] x,y,t,s; //Map_x, Map_y, Pixel_x, Pixel_y, Time in ms, Sprite cache key}
For Example:

dmx = 0;
dmy = -1;
x = {0,2,4,6,8,10,12,14,16,18};
y = {0,1,2,3,4,5,6,7,8,9};
t = {10,10,10,10,10,10,10,10,10};
s = {1,2,3,4,5,4,3,2,1};

Makes the animation go 'south' with 9 frames of animation, and then moves the character y-1 after the animation is done...

Your needs are probably different though...
BRING BACK THE BLACK (or at least something darker)
Ooh, I like your approach h_o_p_s. ([lol] -inside joke)

But I don't wan't to hardcode the animations, nor use a binary format, I want as much flexibility as possible.

How's this for a text file:

// Guy.txtWalkSouth {	dmx =  0;	dmy =  1; 	// tile coordinates (not cartesian)	x   = {0,1,2};	y   = {0,0,0};	t   = {5,5,5};}StandSouth {	dmx =  0;	dmy =  0;	x   = {3,4};	y   = {0,0};	t   = {7,8};}


Not much different from the source I know, but it looks cool!

One question though, how will this individual frame timing system affect non-fixed game loop frame timing?

oh, h_o_p_s, it looks like you put 1 too many numbers (10 as opposed to 9) in your x and y array, or was this intentional?
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
It is intentional. The first in the xy is the position when the Animation is not animated. This way I can define an animation and turn it on and off and still have control over the graphics.
BRING BACK THE BLACK (or at least something darker)
Why not have rectangles representing the dimensions and position of the frame?
With a rectangular array the size of the animation, you can quite easily store the information needed for animation. Say something like this:
RECT* frames = new RECT[numberOfFrames];if(nextFrameHorizontal){  for(int i = 0; i < numberOfFrames;i++)  {    frames.top = topOfFrameY    frames.left = topOfFrameX *i;    frames.right = frames.left + frameWidth;    frames.bottom = frames.top + frameHeight;  }}else if(nextFrameVertical){  for(int i = 0; i < numberOfFrames;i++)  {    frames.top = topOfFrameY*i;    frames.left = topOfFrameX ;    frames.right = frames.left + frameWidth;    frames.bottom = frames.top + frameHeight;  }}


There are a few potential problems you may have with this approach, mainly that the frames need to be next to each other either vertically or horizontaly for this to work.The size of each frame must also be the same.
Thanks barakus, but that kind of limits flexibility.

I want to be able to splice lots of different frames together to make new and crazy animations (like in Chrono Trigger, when you dance).

Also keep in mind that I am simply asking for a method of which to store animation data, not tile coordinates data or blitting, I've already got that down with my tileset class.

I'm also doing pixel by pixel movement and collision, so I'll replace the 'change in map position' variables with 'change in pixels' variables.

// Guy.txtWalkSouth {	dpx =  0;	dpy =  15; 	// pixel coordinates (not cartesian)	r   = {0,1,2};  // tileset row	c   = {0,0,0};  // tileset column	t   = {5,5,5};}StandSouth {	dpx =  0;	dpy =  0;	r   = {3,4}; // has an idle, subtle breathing, frame	c   = {0,0};	t   = {7,8};}


Oh, and I understand that the milliseconds per frame system would not complicate my non-fixed game loop speed. (milliseconds is constant, fps isn't, duh)

I'm pretty pleased with this approach, but I'd still like to hear some other ideas before I get too far in my code. [grin]

EDIT: Changed x and y in the text file to r and c. It just makes more sense for my use.

[Edited by - The Forgotten Mindset on April 28, 2005 12:01:05 PM]
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
*bumpetty bump*

Any more takers?
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
My suggestion, if you're just wondering about storing data, would be to use a simple XML format and TinyXML(link: http://www.grinninglizard.com/tinyxml/) to input it. XML is easy to read, TinyXML is really easy to use. It took me about 20 minutes to get it up and running with a basic format(for storing my characters). that would be my suggestion. If you don't want to take the time, what you have seems to be fine in my opinion.

NOTE: I'm not the best at this. As of yet, I haven't completed a full and working 2D tile engine(*tear*) I've tried like hell, though. I keep on getting too bogged down with small features that I really shouldn't be thinking about. So yeah, I'm not the best reference for this, but there's my 2 odd yen.

This topic is closed to new replies.

Advertisement