zlib

Started by
5 comments, last by Storyyeller 13 years, 4 months ago
What do you guys think about using zlib (http://www.zlib.net/) for sprite compression in a video game?

I think I want to use it, but I couldn't find a simple usage example program.

Advertisement
I use it, and like it. Not sure if it's the absolute best compared to other compression schemes but it works well enough. This is the article I originally read to get started with it: http://www.gamedev.net/reference/articles/article2279.asp
It seems ok. Here's a test I wrote:

I took a 300k BMP, compressed it to 100k, and wrote it out again to make sure nothing got lost.


// zlibtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"
#include "io.h"
#include "zlib.h"


//
//ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
// const Bytef *source, uLong sourceLen));



//ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
// const Bytef *source, uLong sourceLen));


int _tmain(int argc, _TCHAR* argv[])
{

FILE *fp = fopen("act0000.bmp", "rb");
int flen = filelength(fileno(fp));
char* buf = new char[flen];
_read(fileno(fp), buf, flen);
fclose(fp);


char *comp = new char[flen*3];
int destlen=flen*3;
compress((Bytef*)comp, (uLongf*)&destlen, (Bytef*)buf, flen);

int compressedSize=destlen;

char *decomp = new char[flen];
int destlen2=flen;
uncompress((Bytef*)decomp, (uLongf*)&destlen2, (Bytef*)comp, compressedSize);
FILE *fp2 = fopen("zlib0000.bmp", "wb");
_write(fileno(fp2), decomp, destlen2);
fclose(fp2);


return 0;
}

This is precisely what .png is. Well, there is 1 small difference. Png adds a step to attempt to filter image data to make it even more compressable by zlib - losslessly.
I ran a few benchmark tests on zlib.

It was able to do 140 decompresses per second of a 1214x925x32bit BMP.

Also, it was able to do 617 decompresses per second of a 126x604x32bit BMP (cropped version of previous BMP).







// zlibtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"
#include "io.h"
#include "zlib.h"
#include <time.h>


//
//ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen,
// const Bytef *source, uLong sourceLen));



//ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen,
// const Bytef *source, uLong sourceLen));


int _tmain(int argc, _TCHAR* argv[])
{

FILE *fp = fopen("kick.bmp", "rb");
int flen = filelength(fileno(fp));
char* buf = new char[flen];
_read(fileno(fp), buf, flen);
fclose(fp);


char *comp = new char[flen*3];
int destlen=flen*3;
compress((Bytef*)comp, (uLongf*)&destlen, (Bytef*)buf, flen);

int compressedSize=destlen;

char *decomp = new char[flen];
int destlen2=flen;

long numtimes = 10000;
long starttime=time(0);
for (int ctr=0; ctr<numtimes; ctr++)
uncompress((Bytef*)decomp, (uLongf*)&destlen2, (Bytef*)comp, compressedSize);
long endtime=time(0);

long fps=numtimes/(endtime-starttime);


FILE *fp2 = fopen("zlib0000.bmp", "wb");
_write(fileno(fp2), decomp, destlen2);
fclose(fp2);


return 0;
}

You don't gain much by compressing a bitmap. If I remember correctly PNG and JPEG files are already compressed with Zlib, and JPEG also adds a compression algorithm with losses to further diminish the size. PNG might also add a few tweaks to compress it I'm not too aware of compression algorithms to be honest.

It's good to obfuscate your graphic files though. You probably don't want the players to mess in your graphics folder and replace your cool hero sprites with pictures of Paris Hilton, do you? ;) It's a bit advanced for a beginner, but if you zip all your images separately and add them into an archive along with other game data, it becomes harder to mess with your data. It's far from being an uncrackable lock but not many people would go this far.
I used to store everything in compressed archives in my game using PhysFS, but I don't anymore. The problem was that there was a noticeable pause in the game while images decompressed (as much as 200ms), which was completely unacceptable. I could have used a much lower compression level or added multithreading, but I decided that it was simpler and more foolproof to just store them as plain files.

Also, I don't see any real issue with players messing with the game files. Why should you care whether they mod the game or not?
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement