Handmade Hero»Forums»Code
Oliver Marsh
177 posts / 1 project
A budding game developer and programmer
Attach assets to exe

In a handmade chat I remember Casey say you can attach your assets to the end of your exe to turn your program into a single file.

The only way I can work out how do this is append the assets to the end of the exe, but instead of having the meta data (like asset offsets etc.) straight after where the original exe ended, putting it at the end. So I can get the file size of my bundled exe when I run my program & move backward from the end of the file.

Is this how you would do it without knowing the size of your original exe prior to building it?

Hope I explained it satisfactory 😅

Mārtiņš Možeiko
2235 posts / 1 project
Attach assets to exe
Edited by Mārtiņš Možeiko on

That is one way how to do that. Sometimes strict corporate anti-virus software may not like that, because technically those kind of exe files have invalid format.

"Proper" way is to put them in resources.

Create whatever.rc file and put all your assets there in following format:

1 RCDATA "image.png"
2 RCDATA "sounds.wav"
3 RCDATA ...

Then in your source code to load asset you do:

void LoadAsset(int id)
{
    HRSRC res = FindResource(GetModuleHandle(0), MAKEINTRESOURCE(id), RT_RCDATA);
    HGLOBAL handle = LoadResource(0, res);
    void* data = LockResource(handle);
    DWORD size = SizeofResource(0, res);
    // ... use data & size
}

And compile .rc file with rc.exe and add .res file it produces to your linker.

You can even share those 1/2/3 numbers with your c code by having them as #define's in some .h file and including it in .rc and .c files.

And if you choose to use clang compiler, then there is even better way - which works on Windows and Linux the same, using .incbin inline assembly directive: https://gist.github.com/mmozeiko/ed9655cf50341553d282 This way you can have all your assets referenced from source code.

Regardless of which option you choose, don't forget that exe/dll files on Windows have 2GB size limitation.

Oliver Marsh
177 posts / 1 project
A budding game developer and programmer
Attach assets to exe

Thanks Martin, that helps a lot. Oh didn't realise the 2gb limit which might make it not worth it.