Game Archives

Started by
3 comments, last by SiCrane 16 years ago
Hello again everyone, I have a question that I thought would have been answered by now, but maybe not many people are quite so far into their game as I am. Up until this point my game has been using hard-coded folder paths to access things like graphics and sound. The structure looks something like: Game folder -> Data folder -> Sound folder, Graphics folder, Saves folder, Maps foler, etc. Those folders are often broken down even further to specifics. Anyway, I'm trying to devise an system like what most games use, an archiving system. I want everything in one or multiple large archives that my game can decompress quickly at run time. My question is, how can create a good archive and access it quickly? Should I use a preconceived format and adopt that, or should I create my own format (like I've done for everything else so far)? How do games so quickly depress models, graphics, sound, etc. from one large archive and load them into memory? Any help is appreciates, as always. Thanks.
Advertisement
I personally use just a one zip file where all game data is stored, and for unzipping I use zlib: http://www.zlib.net/

Using it is quite simple:
#include <unzip.h>// Open zip archiveHandle=unzOpen(ZipFileName);// Find file from zip and open itunzLocateFile(Handle,FileName,false);unzOpenCurrentFile(Handle);// Find file sizeunz_file_info FileInfo;unzGetCurrentFileInfo(Handle, &FileInfo, NULL,0, NULL,0, NULL,0);int FileSize=FileInfo.uncompressed_size;// Allocate memory and read fileunsigned char *Buffer=new unsigned char[FileSize];unzReadCurrentFile(ZipHandle,Buffer,FileSize);unzCloseCurrentFile(ZipHandle);


And basically that's it, just removed some error handling for clarity.

The only thing that has given me problems in this is that whenever you read something from the disk you'll get memory pointer - not file pointer. For example I tried to use few image loading libraries, but they accepted only file pointer or file name. They were not able to read that memory buffer where I have that file data stored. So this limits choice of libraries a bit.

Edit: Missing source tag
Edit2: Wrong url :)
To Jænis' problem, it is a common practice to "inflate" archived files to a temporary folder so you don't need to re-inflate them if you remove them from memory after a first load.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
Thanks, zlib looks like the way to go. Simple, cross-platform, and patent free. I'm gonna go look into implementing now, thanks for the sample code too.
You might want to give PhysFS a try. It has direct support for accessing archive files such as zips as well as neat things like override folders.

This topic is closed to new replies.

Advertisement