sprite sheets

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

sprite sheets

Post 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?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: sprite sheets

Post 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.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: sprite sheets

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Pickzell
Chaos Rift Junior
Chaos Rift Junior
Posts: 233
Joined: Sat May 16, 2009 10:21 am

Re: sprite sheets

Post 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:
I'm an altogether bad-natured Cupid.
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: sprite sheets

Post 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...
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
Post Reply