Sunday, January 29, 2012

Game data in a zip file

For our platform game, we wanted to minimize the number of files needed to play the game. The best would be to distribute just a single exe file, but getting all SDL libraries statically linked seems to be a challenge. This post is about the game data files (sprites, sounds and level data), and how to make the game access them directly from within a zip file.

Our current approach uses zziplib and SDL RWops. Zziplib makes it easy for the program to access files inside a zip archive. As a bonus, the library looks for the file outside the archive first, so one can keep the game data as separate, regular files while programming and testing. For distributing the game, one simply packs the data files in a zip archive, the program will still find them. SDL RWops is a mechanism for passing data to SDL functions, when loading images or sound clips. The data may be stored in memory, in a file or in some other way.

The zziplib homepage gives an example program for getting a RWops structure from zziplib. I made a few modifications to it, which I want to share here. The code is not yet pretty, it is at the works-for-me stage.  In particular, the mode argument is handled sloppily. The original example file was released under the zlib license, so the same applies for my modified versions.
SDL_rwops_zzip.c  SDL_rwops_zzip.h

First, mingw does not like the names _zzip_read() and _zzip_write(), it reports a name conflict. I guess this is a mingw problem, but it is easy to get around by changing the names, which are used only locally in this file.
SDL_rwops_zzip.c:23:12: error: conflicting types for 'read'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/../../../../include/io.h:452:37: note: previous declaration of 'read' was here

Second, zziplib supports obfuscating the zip archive, which makes it slightly harder to see the files inside it. It would be nice to use this feature together with the RWops. In the c file below, I have simply combined the obfuscation example from the zzlib site with the RWops functionality. A nice feature: obfuscation is applied only on files found in a file with the extension .dat, unpacked files and files in a .zip archive are assumed not to be obfuscated. I also added the function openZzipFile(), which opens a file with the same obfuscation mechanism but does not turn it into a RWops structure.
Newer Older Home
Related Posts Plugin for WordPress, Blogger...