Spacefight – Packing all the resources into one file – Part 10

While the project is in development phase it is very convenient to have all the assets laying around in folders. But when it’s time to distribute your game it doesn’t look as professional when everything is lying all over the place. I wanted to put all resources into one compressed file. I spent some time looking up how other people have solved this issue. It needed to be a solution that is supported through a variety of platforms.



I stumbled upon PhysicsFS, which had the solution to my problem. Many games seem to use PhysFS and it reads zip, 7z, tar.gz, tar.bz2 files and returns a pointer to a SDL_RWops object. You can apparently use the PhysFS lib to even more things, like adding files and data into a compressed file.

PhysFS also includes some helper functions to SDL which are very handy. I compressed all my assets into one 7z file and tried to access resources from it. The PhysFS helper returns a pointer to a SDL_RWops object. This means I had to rewrite all the resource loading functions. Instead IMG_Load I had to use IMG_Load_RW and using the same approach when loading font and sound.

I finally got it working and I was able to read from the resource file and find the correct asset. But it went very very slow. The game would halt every time it loaded a new resource.

After accessing my external knowledge (google) I found out that 7Z uses solid compression as default. This is an efficient way of compressing data, but every time a single file needs to be accessed, it needs to go through all files in the archive. Passing -ms=off to 7za made a massive difference.

Now it reads in the resources very fast and transparent during the game run time. However in the next game I probably introduce a short loading screen.

This is the function I wrote in order to access the data

One thing to keep in mind is that PHYSFSRWOPS _openRead only works this null ended c-strings. Which means a char array needs to be passed and the last character of the char array needs to be the NULL sign ‘\0’. C++’s string function has a method called .c_str() which converts the string to a char array.

Here are the source distribution for physfs:

The files physfsrwops.c and physfsrwops.h are located the extras/ directory. Copied these to my project and renamed the extension of physrwops.c to .cpp to make my Makefile happier.

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.