Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

877232 Posts in 32852 Topics- by 24294 Members - Latest Member: RopeDrink

May 18, 2013, 10:43:10 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Embedding files into an application
Pages: [1]
Print
Author Topic: Embedding files into an application  (Read 497 times)
Claw
Level 4
****



View Profile WWW Email
« on: August 19, 2012, 02:42:30 AM »

I'm currently trying to find a way to embed files (images at the moment) into an application so they are built into the executable. This is for C++ and I'm loading the data into OpenGL for rendering.

I've tried converting a PNG to a header file with ImageMagick but it bloats the exe a lot and doesn't actually work - pixel data doesn't seem to be stored properly and it ignores alpha.

Has anyone got any advice? Things like gamemaker package images and sounds etc into the .exe as far as I know, so it should be possible without bloating everything.

Here's a picture of one of my failed attempts:

Logged

rivon
Level 10
*****



View Profile
« Reply #1 on: August 19, 2012, 03:15:39 AM »

GIMP can export images as C header files.
Logged
rostok
Level 0
*



View Profile WWW Email
« Reply #2 on: August 19, 2012, 03:40:22 AM »

i would pack all the necessary media files into some archive, say 7z. without compression of course since pngs are already compressed. this needs some extra libs included but afaik simplest unzip source shouldn't get your exe any larger (hope you are not writing an 64k compo). than i would just attach the archive to the compiled exe's end (copy /b run.exe + data.zip final.exe on windows). final thing is to know where the data.zip starts. easiest is to get filelength of run.exe and attach it as binary long int to the end of file.exe.

some simple batches should do the trick and you will get the possibility to work with a file/directory structure instead some memory pointers to textures. this way making change will be much easier.
Logged
Polly
Level 2
**


View Profile
« Reply #3 on: August 19, 2012, 03:46:33 AM »

You basically have 3 options when it comes to embedding data into a exe.

- Provide the data in your C++ source ( as rivon mentioned ).
- Use a resource file with your C++ projectš
- Append the data to your exe ( as rostok mentioned )˛

šIn case your IDE doesn't have a Resource Editor, you could use something like ResEdit.
˛In case you don't want to break your digital signature, you need to pull off some tricks.
Logged
Claw
Level 4
****



View Profile WWW Email
« Reply #4 on: August 19, 2012, 04:03:49 AM »

i would pack all the necessary media files into some archive, say 7z. without compression of course since pngs are already compressed. this needs some extra libs included but afaik simplest unzip source shouldn't get your exe any larger (hope you are not writing an 64k compo). than i would just attach the archive to the compiled exe's end (copy /b run.exe + data.zip final.exe on windows). final thing is to know where the data.zip starts. easiest is to get filelength of run.exe and attach it as binary long int to the end of file.exe.

some simple batches should do the trick and you will get the possibility to work with a file/directory structure instead some memory pointers to textures. this way making change will be much easier.

This sounds interesting, how would I access the files in the .zip from my program? This kinda stuff is entirely new to me.

You basically have 3 options when it comes to embedding data into a exe.

- Provide the data in your C++ source ( as rivon mentioned ).
- Use a resource file with your C++ projectš
- Append the data to your exe ( as rostok mentioned )˛

šIn case your IDE doesn't have a Resource Editor, you could use something like ResEdit.
˛In case you don't want to break your digital signature, you need to pull off some tricks.

The best solution sounds like appending the data to the exe or storing it in some kind of resource file/archive, but I'd like to keep it cross platform so want to avoid using any windows specific resource stuff (but again I have no idea how it works or if there's a cross platform version of microsoft's RCs ).
Logged

SuperDisk
Level 1
*



View Profile Email
« Reply #5 on: August 19, 2012, 06:36:01 AM »

I think rostok's right on it.

Do like he says to shove the zip / tar / whatever into the EXE, then in your code, all you have to do is just open the EXE as an archive file. You could actually just rename the EXE to .ZIP and it will function as a ZIP.
Logged
d
Level 0
***



View Profile WWW Email
« Reply #6 on: August 19, 2012, 10:18:53 AM »

˛In case you don't want to break your digital signature, you need to pull off some tricks.

The fact that this is possible is completely insane (though we are talking about X.509, so insane is the norm), and I'd be wary of relying on it in case it gets exploited by some malware and Microsoft decides to "fix" it.
Logged
rostok
Level 0
*



View Profile WWW Email
« Reply #7 on: August 19, 2012, 10:20:59 AM »

@claw:
accessing archive is pretty straightforward. if it is small enough just load all your exe into memory buffer, read the offset from the last 4 bytes and buffer+offset will give you memory pointer to the beginning of an archive. then you should just pass this pointer to some function from archive library that supports opening archive from memory. i wont recommend you any libraries since last time i coded in c++ was sth like 10 years ago.

alternatively (however i don't really like this way) you can create temporary file with archive and just open it (any archive library supports opening archive by file name).

note that archive option is pretty good for debugging as in my opinion data should be kept outside of code. so you can store all assets in archive on disk and access it directly from folder instead of reading exe and so on. when ready for release compile you just change line that opens archive from file into code that gets it from exe.

reading exe as zip without offset as SuperDisk says may work but not necessarily. it all depends on library you will use. some will seek for archive header and some will just check first couple of bytes (and will get MZ instead of PK for zip).

one more thing. you may also use mixed approach. zip assets into archive and add it into exe as resource or buffer in .h file.
Logged
d
Level 0
***



View Profile WWW Email
« Reply #8 on: August 19, 2012, 10:23:53 AM »

zip assets into archive and add it into exe as resource or buffer in .h file.

This is your best bet.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic