Page 1 of 1

sprite sheets

Posted: Tue Oct 13, 2009 1:18 am
by short
I am curious to know what you all think about method's for implementing spritesheet's. I am working on implementing sprite sheet's into my "engine" and I was doing some research about the best ways of storing coordinates in sprites.

First of all, there are different kinds of spritesheet's. Well, sort of. What I was thinking is if you have a "character" or "person" or something that itself has lots of different animations but are all the same size, you could just take the size of the image, and calculate out how many frames there are based on your bpp value. This is assuming of course that you always put the run animations (same number of frames for running for every sprite) in the same spot in every sprite sheet. Etc... This to me seems very, limited. I was also thinking for things like environmental sprite sheet's this would be impossible. Especially if you had different sized sprites in your sprite sheet.

I was also reading you could have a sort-of meta file for each sprite sheet. I think, that this would be the best approach. Parse the header file in binary mode and you could store everything you need about the sprite sheet. Thing is, you have to generate this "meta" file for each sprite sheet. Is this normally how it is done? Separate files that contain the data for sprite sheet's?

I've also been following TruFun's videos, and I saw him using xml files for storing maps and it got me thinking I could use this "xml" file format for the metadata I need.

Being honest, I've never dealt with xml files, so I don't know anything specific about them, but maybe somebody here does.

I won't have anytime to work on this until the weekend, but I am curious to know everyone's thoughts. How do you guys handle spritesheet's? Is xml a good format for storing the metadata?

Re: sprite sheets

Posted: Tue Oct 13, 2009 3:31 am
by MadPumpkin
Well I don't know about using XML, I've never actually used for anything useful really. Just WoW addons.

The first method you described is most commonly used. I believe I have heard of the second. However, I don't see what you gain from doing this as apposed to the first described, unless you have a much bigger game and need it done a specific way for every different object.

I have a friend that uses both. For each size of character (e.g. usually 32x64 px, bosses/special NPCs are usually 64x64+ px) he uses a different meta file. But for regular characters (32x64) he doesn't use a meta file.

Re: sprite sheets

Posted: Tue Oct 13, 2009 2:03 pm
by MarauderIIC
short wrote:How do you guys handle spritesheets?
enum Directions { NORTH, EAST, SOUTH, WEST };

sheet x = sprite width * current frame in current animation
sheet y = sprite height * current direction + current special animation #
Will put all special animations on their own row. Metadata says how many frames per special animation.

total walk frames = sheet width / sprite width
total animations = sheet height / sprite height

Sprite size for players is fixed.
Sprite size for other things requires metadata, but this can be reused (for instance, sparkly mana potion & sparkly health potion might share general potion metadata).

World objects are divided into tiles of a fixed size (for instance, a tree might consist of five tiles). The world supports changing a tile to another tile on a timed basis.

This is simple and easy to maintain. If you have a spritesheet where objects within the sheet aren't a constant size, editing the spritesheet becomes dangerous, because you have to ripple changes through to metadata.

I think you can store whatever you want in a png header, if you don't want separate files, but this eliminates the possibility of reuse.

Re: sprite sheets

Posted: Tue Oct 13, 2009 2:20 pm
by Pickzell
short wrote:I am curious to know what you all think about method's for implementing spritesheet's.
http://lazyfoo.net/SDL_tutorials/lesson06/index.php has never failed me


XML tutorial thing:

Re: sprite sheets

Posted: Tue Oct 13, 2009 6:58 pm
by Netwatcher
This is my method.
here the sprite size is fixed, but I guess you can use an array to define the specified non-constant sizes then run it then SpriteNum 32 will have the corresponding index value of 32 in the size array(probably two-dimensional for the lenght and the width).

Code: Select all


//Sprite Size
int SpriteWidth=36;        
int SpriteHeight=36;
 
//SpriteSheet Size
int ImageWidth=1028;   
int ImageHeight=780;
int SpirteNum;//The number # of the sprite in the SpriteSheet beginning from left to right
//then you do something like so

RECT SpriteRect;

LPRECT FindSpriteRectToExtractFromSpriteSheet(int SpriteWidth,int SpriteHeight,int ImageWidth,int ImageHeight,int SpriteNum)
SpriteRect.bottom= SpriteHeight * ((int) ((SpriteWidth*SpriteNum) / ImageWidth));
SpriteRect.right = (SpriteWidth * TileNum) % ImageWidth;
SpriteRect.top= spriteRect.bottom - SpriteHeight;
SpriteRect.left = SpriteRect.right - SpriteWidth;
return &SpriteRect;
That's how I approach it ;) .

Now, if I were to set it up in the game code really fast(co's I'm lazy) I would do

Code: Select all

int running[3]={25,26,27,28};
int SpriteStep =0;

Game_Loop()
{
/*...shit starts...*/
/*Player moved*/
//Move forward in the animation
SpriteStep=(SpriteStep+1)%(sizeof(running)/sizeof(int));

/*...shit happens...*/
 /*A madeup drawing function*/

//Now we actually move the Rect forward to the next sprite
DrawImage(SpriteSheetTexture,SpritesLocation,
//I'm here, look at me! FindSpriteRectToExtractFromSpriteSheet(SpriteWidth,SpriteHeight,ImageWidth,ImageHeight,running[SpriteStep])
,SpritesColor);
//Just ending the DrawImage call...


/*...shit ends...*/
 }                            
Please correct me on any mistakes I've made, it's 2AM and I'ma feeling sleepy...