Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed resources into a single executable?

If you've ever used the tool Game Maker, it's a bit like that. I want to be able to take all my sounds, images, and everything else of the like and embed them into a single C++ executable. Game Maker would have a built-in editor, and would have the images embedded into the .gmk file, and when you'd open it it would read the images, and display them in the game. I'm thinking he had the images saved not as images, but as pure data stored in the .gmk file and interpreted by the editor or by some interpreter written into the .exe. How would I go about making something similar?

like image 972
Keelx Avatar asked May 14 '11 23:05

Keelx


2 Answers

The windows resource system works like this, so if you make a WinAPI or MFC application, you can use this. Also, Qt provides the same functionality, but in a platform independent way. They just write the files in raw binary format into a byte array in a normal C++ file, so they get compiled as data into the exe. Then they provide functions for accessing these data blocks like normal files, although I don't know how they really work. Probably a special implementation of their file class which just accesses those byte array variables.

like image 120
Christian Rau Avatar answered Oct 06 '22 04:10

Christian Rau


For images only, a very simple approach is to use the XPM format.

This format is a valid C/C++ header, so you can include it directly into a C++ source file and use it directly.

The main issue with this approach is that XPM is not a compressed format, so uses a lot of storage. In consequence, in practice I only seen this used for icons and small graphical objects, but in principle you could do more.

The other cool thing about XPM is that it's human readable - again great for designing small and simple icons.

To generalize this idea to other formats, what you could do is to create a compile chain that:

  1. Encodes the target file as ASCII (Uuencode or such)
  2. Turns that into a single named C String in a source file.
  3. Create a header just declaring the name
  4. Define a function recovering the binary form from the string
like image 39
Keith Avatar answered Oct 06 '22 04:10

Keith