vrijdag 21 augustus 2009

Easy Compression

Thanks to the pretty detailed graphics (sprites of 48x48 with 6 frames of animations drawn from 8 sides) I went over the normal DS RAM limit (4 MB). This isn’t a huge problem, since the DSi has 16 MB ram (4 times more!). However I like to try and keep everything as small as possible. And I was never satisfied with the way I handled the graphics (I was also too lazy to really change anything, until I hit this limit).

I applied a technique that I also use for my voxels. Although recently, I noticed this is also the way John Carmack did store the graphics for Doom.

Basically what I do is, throw out all the transparent pixels of the bitmap (and with transparent I mean the color the game doesn’t draw). This (especially for sprites, which always seem to have a lot of transparent pixels) decreases the size a lot.

I just scan the bitmap map, and store only the pixels that aren’t transparent. I do this using ‘slabs’ or lines. When I find a pixel that isn’t transparent I create a new slab or line. The line will grow (in width) until we hit another transparent pixel.

Here's a sprite animation from the pirate theme world -

At the same time I store all non-transparent pixels in a array.

When I want to draw such a picture in the game. I just loop through the lines and draw them. This way I can figure out where all the non-transparent pixels go on the screen. It’s also faster than my normal approach (since I use to scan over every pixel in the 48x48 bitmap, and then decide if it needed to be drawn).

This is kind of cool, because normally compression means you need to decompress. Which makes things slower instead of faster!