Sign in to follow this  
Asheh

Sprite Sheets

Recommended Posts

Asheh    354
Ive been writing a sprite engine recently, it all works quite well apart from one issue I have run into. The "cells" are selected from a sprite sheet but the problem is the sprite alignment, because you specify each cell and it has varied width and height its next to impossible to get them to align properly.. Any advice? Regards. Ash.Eh?

Share this post


Link to post
Share on other sites
Funkymunky    1413
Yeah either redo the sprite sheet so it's a uniform width/height, or figure out how to load each individual size difference.

Or you might write an algorithm that traces the lines (assuming there's, say, a blue line surrounding each cell) and figures out the width/heights based on that.

Share this post


Link to post
Share on other sites
Toolmaker    967
I'm using an XML variant to store my texture files. I usually use 1 sheet for each sprite type. So I have 1 ship sheet, 1 for weapons, other for objects, etc.

Next, I make a XML file to store all the filenames, widths, heights, sprites per row, etc. and use PML to parse the files.

My current texture file looks like this:

<texturepackage>
<texture name="images/rocks_big.bmp" width="64" height="64" row="2" rotx="32" roty="32"/>
<texture name="images/rocks_medium.bmp" width="32" height="32" row="2" rotx="16" roty="16"/>
<texture name="images/rocks_small.bmp" width="16" height="16" row="2" rotx="8" roty="8"/>
<texture name="images/ship.bmp" width="32" height="32" row="1" rotx="32" roty="32"/>
<texture name="images/starfield.bmp" width="256" height="256" row="1"/>
<texture name="images/sun.bmp" width="128" height="128" row="1"/>
<texture name="images/vortex.bmp" width="128" height="128" row="1"/>
<texture name="images/nebula.bmp" width="128" height="128" row="1"/>
<texture name="images/bullet.bmp" width="16" height="16" row="1"/>
</texturepackage>


Next, I load and parse the file and preload all my textures. You could do a similar thing, however, you just make your sprite pack XML file look like:


<spritepack>
<spritesheet name="sheet1.bmp">
<sprite name="some name" x="10" y="10" width="64" height="64" />
<sprite name="other name" x="10" y="10" width="64" height="64" />
</spritesheet>
<spritesheet name="sheet2.bmp">
<sprite name="yet another name" x="10" y="10" width="64" height="64" />
<sprite name="name different" x="10" y="10" width="64" height="64" />
</spritesheet>
<spritepack>


Repeat all the sprite tags for all your sprites. Give them a unique name and store them somewhere in memory. When you need a sprite, you just refer to it using it's unique name(Or hash the name to speed things up).

As for the parser, I use PML to do the grunt work. It's pretty easy to use. So, to load the sprites, you should use something along the lines of this:

PML PmlSpritepack("textures.pml");

for (int nTeller = 0; nTeller < PmlSpritepack.GetTagCount(); ++nTeller)
{
// Get the <spritesheet> tag
PML PmlSheet = PmlSpritepack.GetTag(nTeller);

// Now loop through the entire list
for (int nCounter = 0; nCounter < PmlSheet.GetTagCount(); ++nCounter)
{
PML PmlTag = PmlSheet.GetTag(nCounter);
std::string strType = PmlTag.GetName();

// if strType is "sprite" we have a sprite definition
if (strType == "sprite")
{
std::string strID = PmlTag.GetParameter("name", ""); // 2nd param is default value
int x = atoi(PmlTag.GetParameter("x", "-1"));
int y = atoi(PmlTag.GetParameter("y", "-1"));
// Etc.

// Place the data into the sprite managing object or something
}
}
}




If you go with PML, include all the files into your project, and #include "PML.hpp" into the files that require it. The above code should work, although not tested.

Toolmaker

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this