Suppose that you don't use an engine like "Unity3D" that has some built-in ways to deal with spritesheets, how would you deal with the "spritesheet problem"? As it is known, spritesheets are better than loading separate .png files for animation purposes (Considering that a character has movement, atack, defense, death, etc, animations). Most of the people, I guess, would take the 0x, 0y pixel colorkey and make it transparent for the whole image AND cut manually all animations and store them in a collection of some sort.
The key point here is automation. If a spritesheet has irregular sprites (For example, the first one is a rectangle of 30 by 25 pixels, and the second one is irregularly far from the first sprite image), one cannot implement a function to cut all next sprites based on the rectangle of the first, because all sprites would have parts missing, etc.
Manually storing every rectangle position in the sprite sheet seems to be a great option for a general game, but the same does not apply to an engine. I'm developing an engine on Pygame/Python, and, therefore, I want a clever way to separate/cut the inner sprite rectangles and return them as a list.
The solution? Looping pixel per pixel and applying some logic based on the colorkey. How would you do that? Would you bother to implement such a function? What do you think about it? For the sake of the topic, here's my method for cutting based on the first rectangle's position and size (It does not work if the spritesheet is irregular):
def getInnerSprites(self, xOffset, yOffset, innerRectWidth, innerRectHeight, innerRectQuantity):
"""
If the grict is a sprite sheet, it returns a list of sprites based on the first offsets and the width and the height of the sprite rect inside the sprite sheet.
"""
animation = []
if self.isSpriteSheet == True:
for i in range(xOffset, innerRectWidth*innerRectQuantity, innerRectWidth):
print i
animation.append(self.getSubSurface((i,yOffset,innerRectWidth,innerRectHeight)))
else:
print "The Grict must be a sprite sheet in order to be animated."
return animation
I'll try to implement the "getInnerSpritesByPixel()" method. Spritesheets are a key thing in a complex game like a MMORPG, where almost every item has its own animation. Such method is more than necessary.