Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was looking at some sprite sheets at sprite sheet database. I noticed that some of the sprite sheets had several different objects placed together in one file. Some of the sprites, such as movement animations were placed next to each other so that they were of the same height. However, sometimes next to them are larger sprite objects that take up a different amount of space.

These sprite sheets have no apparent information such as cell width and height. It seems like you would have to clip them through trial and error. Is there some sort of algorithm to figure out how to clip them without external sprite grid information?

http://sdb.drshnaps.com/display.php?object=7604

share|improve this question
    
"Is there some sort of algorithm to figure out how to clip them without external sprite grid information?" Why not just make some external sprite grid information? It would probably take a lot more time to solve programmatically than just having some meta data that you author by hand. –  Tetrad Nov 25 '10 at 6:47
add comment

2 Answers

up vote 3 down vote accepted

Almost all sprite sheets ripped from old games that you will find online contain images that have been rebuilt from the original, smaller (usually 8x8 or 16x16 pixels) tiles. Below is an image of a tile editor without the color paletted adjusted to illustrate what i'm saying.

Tile editor

So, to use those graphics in your game, you should have the sprite origins and sizes stored in some way, for performance's sake. But, if you still want to get the sizes programatically, do something along this pseudocode:

var current_line = 0
var current_column = 0

while (IsEmptyLine(lines[current_line]))
{
    current_line++
}

var top_line = current_line

while (!IsEmptyLine(lines[current_line]))
{
    current_line++
}

var bottom_line = current_line

while (IsEmptyColumn(columns[current_column]))
{
    current_column++
}

var left_column = current_column

while (!IsEmptyColumn(columns[current_column]))
{
    current_column++
}

var right_column = current_column

// rectangle(x, y, width, height)
rectangle bounds = new rectangle(left_column, top_line, right_column - left_column, bottom_line - top_line)

IsEmptyLine/Column determines if a line or column is entirely made of fully transparent pixels. Also keep in mind that this is a generic code, and won't work for special cases, like non-vertical sprite tiling.

share|improve this answer
add comment

You will most likely have to either (a) create a mapping of the sprite sheet or (b) rearrange the sprites based on your application's mapping convention using some sort of graphical editing application (such as Adobe Photoshop or GIMP); most modern graphical editing applications support adding guidelines and rulers which make both (a) and (b) much easier to do.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.