PDA

View Full Version : External files in binary



roadkillguy
March 22nd, 2011, 02:23 PM
I have textures and such that I don't necessarily want to be editable by the user. Is there a way to compile external files into the final executable? Using the linker maybe?

Thanks

Some Penguin
March 22nd, 2011, 07:44 PM
You might want to mention the language you're using.

If it's in the C family, you can likely get where you want by shoving the contents into a byte array initialized in a header file.

roadkillguy
March 22nd, 2011, 08:02 PM
Good point. I'm using c++. Should I write a program to output a .h file with all the data? Could I put it in the linker args?

Some Penguin
March 23rd, 2011, 10:20 AM
If I were going to dump into a .h, I'd definitely do it via program. I've seen this done for bitmaps in really old code. The header just initializes the byte array, you include the header where you access it, and it's just another variable after that.

roadkillguy
March 23rd, 2011, 10:43 PM
Hmmm... I'm actually trying to include an image that's 512*512 at 3 bytes per pixel.. the file I generated is 4.5mb, and I can't even open it in gedit like I normally do. Should I make some sort of .dat file to include at runtime?

This is the code I currently use to write an array:


for(int i = 0;i < length;i ++)
{
fprintf(fp, "\'%c\',", (int)ambient.imageData[i]);
}

There's got to be another way.

johnl
March 24th, 2011, 04:04 AM
Hi,

You can indeed do it with ld; for example, if you have a file named 'image.png', you can do the following:



ld -r -b binary -o image.png.o image.png


The resulting .o file (image.png.o) can be linked with your other object files, and will expose the following symbols, which are pointers to the beginning and end of the blob.



extern char _binary_image_png_start[];
extern char _binary_image_png_end[];


Which you can use like so (for example, with an embedded text file, in C):


#include <stdio.h>

extern char _binary_data_start[];
extern char _binary_data_end[];

int main(int argc, char* argv[])
{
for(char* p = (char*)_binary_data_start; p < (char*)_binary_data_end; ++p) {
putchar(*p);
}

return 0;
}

roadkillguy
March 24th, 2011, 02:00 PM
That's just what I was looking for!

Is it png specific?

johnl
March 24th, 2011, 03:44 PM
That's just what I was looking for!

Is it png specific?

No, you can do it with any sort of file. The linker doesn't care.

roadkillguy
March 25th, 2011, 04:00 AM
Is it then possible to type cast that char into a FILE pointer to use with fread? I have a function that would normally read from a file, but I now want to use that data with it.