How to compile data into the executable?

Started by
3 comments, last by irreversible 16 years, 5 months ago
How can I include a PNG file in my executable, and how do I access it in my sourcecode? I'm programming in C++, using mvs and codeblocks. I have searched around a bit, and found out about .rc files. But there doesn't seem to exist any good tutorials that deals with different platforms, since its implementation seems very compilator specific. The filetypes available also seems very pre-set. All tutorials found only handle ICO or gui-elements, and I can't find any information about defining my own, or how to actually use the included resources. Maybe these rc-files isn't the way to go? In the end, I plan to include ALL my own datafiles, sounds, and images into my executable somehow, at least for smaller games. thanks for any help! I'm totally lost..
Advertisement
You can convert your data file into an array of chars in C source code. This will then be put into memory at runtime, you can load it with your PNG library if it supports reading from memory. You could also convert the data into the format you want first, then you don't need a PNG library.

However this will be fairly inefficient as there is no way of freeing these data. Another option is to make your .exe file into a .zip file (this can be done easily, because .zip files can have an arbitrary prefix) and use a zip library to read the files out of your own executable. This is a mostly portable approach (it works on most types of executable files on different platforms).

Another option (this time not portable) is to use a win32 PE resource section - you can add resources using winres.exe or something - your tools may also provide other options. You have to use (typically) win32 APIs to get at them.

Other options exist. It all depends what you want.

Mark

Thanks for answering!

Well, I am already using the first option. I have converted some very small pics to characters and loaded them directly into opengl, but I stopped just there as I figured other options might exist.

The zipfile solution is kind of messy. Isn't there another solution to just add the files as they are without invoking some external algoritms? In theory it seems easy to just have pointers to data that I easily can access during runtime, located in the exe.

I searched around with winres.exe as reference, and the results was a bit overwhelming. Anyone care to give me some pointers at some more specific instructions and/or tools?

Any feedback is appreciated.
.rc files allow you to put any arbitrary file into your executable. There's something like a "FILE" resource or something along those lines (it's been awhile). However, file resources are a completely Windows specific thing. Something similar exists on the Mac, but it's been even longer since I did that, so I don't remember what it was. No idea about Linux. I'm not aware of a cross-platform mechanism other than the cumbersome "turn the file into an array of bytes in C-format".

Would you consider having 2 files, an executable and a data file? Then you are pretty much free to make up whatever format you want for the data file, and it will work cross-platform.

Geoff
To add the resource to the exe externally, use the UpdateResource() function (MSDN!). Take note of the detail pertaining to Windows Vista which says you can't update a resource which type isn't already existent in the PE file.

Use FindResource() and LoadResource() to load the resource within the exe.

This topic is closed to new replies.

Advertisement