Start of July - Technical MonthHi all!
A year ago I showed that I'd been working on a tool to pack individual sprite images together into a sprite sheet. Well, this weekend I finally got around to making the game able to use these packed images successfully!
Here's the tool in action. I reads a series of commands from a file and then runs through them all. Sprite packing is the main thing that it does. The process for sprite packing is:
- Load all of the sprites definition XML files in a single folder, and load all the images that they use. These images can contain multiple frames of a single animation.
- Crop every animation frame as much as possible.
- Remove duplicate frames.
- Pack all the frames into the smallest size image possible.
This means I can lazily create animations that contain a large amount of empty space, or duplicate frames, and I know it won't be wasteful on memory in the final build of the game.
Getting the packed sprites and other data to work properly in the game took a while and involved fixing various bugs both on the game side and the sprite packing side. At one point it was a
glorious glitchy mess, but eventually the game was looking perfectly normal.
I added some debug info for my resource loading system so I could test out the effects of the sprite packing.
This is the game running on raw unpacked data. Note in the top left, 573 textures loaded using 11.6MB.
And this is running on the packed data. 57 textures loaded - that's a good reduction! But now it uses 12.6MB, so the memory usage for the images has actually gone up.
I kinda expected this would happen - I'm limiting the packed sprite images to being power-of-two sizes (128, 256, 512 etc) so it's quite easy for the image to be way bigger than it needs to be. If the sprites *nearly* fit into a 256x256 image, but not quite, then it'll double the size to a 512x256 image instead, resulting in a lot of wasted space.
This is ok though. When the game is in its later stages I can be smarter about which sprites are packed into the same sprite sheets and make much better use of the space.
However I did poke around in the current images to see if there was anything obvious that is worth fixing. And I found this:
This is a horribly wasteful 1024x512 image! My packing algorithm was obviously doing a bad job at packing sprite sheets containing very wide sprites.
A quick bit of tinkering with the algorithm, specifically to do with how it chooses the size of image to try packing the sprites into, made this particular case way better. 1024x128.
And the improvements put the game's image memory usage down to 10.3. Hurrah!