Sign in to follow this  

zlib

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this