1
\$\begingroup\$

I programmed a game in c++. Now I want to pack the finished product. Game runs fine from within the compiler. But how can i link the assets(like images, music etc) of the game to the exe ? When I run the executable, it says it is not able to load any assets and terminates immediately. How can I bind the assets with the executable ?

\$\endgroup\$
  • \$\begingroup\$ What OS are you on? Each system has a different method of packaging things. For example, on MacOS, you usually have an application bundle, and resources go in You.app/Contents/Resources/. But on Windows or Linux, it will be different. \$\endgroup\$ – user1118321 May 24 '15 at 14:49
  • \$\begingroup\$ I am on Windows. \$\endgroup\$ – arandomguy May 24 '15 at 16:53
  • \$\begingroup\$ Well I finally figured it out. Before asking this question I had researched on google came to know that to make a program installable, a software like InnoSetup can be used. So I learned how to use it. But even then the application could not load the assets. It still showed that it is unable to load assets, and terminated. That is why I asked the question. Now I know how to properly use InnoSetup. I was not creating the destinaton folder for the assets properly and that was causing the problem. \$\endgroup\$ – arandomguy May 24 '15 at 17:55
3
\$\begingroup\$

There's no general way to do this, especially not cross-platform.

Do you really have to pack your ressources into the executable file? I'd consider deploying a small setup program instead, which would unpack the ressources next to the executable file, which is essentially what you have when running from your IDE/when debugging.

Just because you've added some file to your project doesn't mean it's actually compiled into your executabe (an impression MSVC might give you for example; there are different "build actions" for files added to the project).

Also consider making your executable bigger and reading ressources from it might slow down your program, since overpedantic antivirus solutions might cause repeated scans.

So if you really insist on packing everything into your executable, I'd do something like this:

  • Compile a list of all required assets.
  • Write a second tool to "pack" the assets:
    • Possibly compress and append the ressources to the final executable file, starting with some kind of index file containing all the offsets/sizes-
    • Write the starting offset of the ressources at the end of the resulting file.

Your actual program can then access ressources this way:

  • Open the executable in readonly mode (random access will be blocked).
  • Read the starting offset from the end of the file.
  • Move to the offset and read the index file.
  • Load your assets according to the index file.

Please note that this approach has its flaws, e.g. you won't be able to sign your final binary (since that would once again change the layout). There are workarounds, but it would overcomplicate things here I guess.

Of course you can use platform specific loading of embedded resources if you want to, but this won't be a cross-platform solution and requires further information regarding your goals.

TL;DR: Is there really some reason to do so? If you want easy deployment, use a setup programm/installer (like NSIS or InnoSetup). If you want to compress/pack ressources, do so in a separate file.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.