Embedding image file in source, and use them from there

Hi Everyone,

I am currently using SDL to read a small bmp file and use it ( as a default
icon )

I would like to store the image somehow in the source code, to get rid of
the file dependency.
And therefore I guess use SDL_CreateSurfaceFrom(void* pixels,…) instead of
SDL_LoadBMP(char* filename).

Although I dont really want to duplicate the SDL_LoadBMP code, I d like to
know the easiest way to do that, just for few well known images…

Has anyone already done it ?

Thanks a lot for your help.

Take a look at the file SDL_rwops.h, it defines a simple little class with a
virtual table (ok, in C) for reading and writing to an abstract
source/destination. SDL_LoadBMP is really a macro that points to a load
function that takes one of these structures as an argument. Something like
this:

unsigned char g_startOfBitmapMemoryArray[] =
{
#include “mybitmap.inl”
};

SDL_LoadBMP_RW( SDL_RWFromConstMem( g_startOfBitmapMemoryArray,
sizeof(g_startOfBitmapMemoryArray), 1 );

To easily create the .inl file, use binary2.py at
http://www.tilander.org/jim/pythoncookbook.shtml

Cheers,
JimOn 1/23/06, Asmodehn Shade wrote:

Hi Everyone,

I am currently using SDL to read a small bmp file and use it ( as a
default icon )

I would like to store the image somehow in the source code, to get rid of
the file dependency.
And therefore I guess use SDL_CreateSurfaceFrom(void* pixels,…) instead
of SDL_LoadBMP(char* filename).

Although I dont really want to duplicate the SDL_LoadBMP code, I d like to
know the easiest way to do that, just for few well known images…

Has anyone already done it ?

Thanks a lot for your help.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

maybe you should try .xpm files
look under the hood, and you will see
( open them in your text editor )

@Stanislaw_Morgun> On 1/23/06, Asmodehn Shade wrote:

I would like to store the image somehow in the source code


Grypa? Damy rad?! Sprawd? jak jej zapobiega?, a je?li ju? za p??no
…jak leczy? - grypa.wp.pl
http://klik.wp.pl/?adr=www.grypa.wp.pl&sid=636

I would like to store the image somehow in the source code

Is the goal in doing this to make everything available from one binary,
or is it just to make it so you don’t have to do file i/o and allocation
to get at the data?

I’d be inclined to avoid this tactic, but you COULD write a small
program to convert a file into a C array:

/* This code hasn’t even been compiled, let alone debugged. */
int main(int argc, char **argv)
{
int ch;

printf("unsigned char myfile[] = {\n");
while ((ch = fgetc()) != EOF)
	printf("0x%X,\n", ch);
printf("};\n\n");

return 0;

}

…then run it as “convertdata < myfile.bmp > myfile.c”

Obviously, that can be cleaned up a little.

However, if you don’t mind jumping through some hoops and really just
wanted all your data to be contained in one file with your binary, you
can save on ugliness and static memory usage by putting a bunch of files
in a zipfile and attaching it to the end of the binary…this works on
at least Windows and Linux, maybe Mac OS, too. Zipfiles store their
table of contents at the end of the file, so this is how self-extracting
.EXE files get away with it.

So now the pipeline looks like:

  • Build your program in myprogram
  • Build your data in mydata.zip
  • cat myprogram mydata.zip > finalprogramfile

Verify it works:

  • unzip -v finalprogramfile

Now run the program and have it open itself as a zipfile at runtime to
access the data.

“have it open itself as a zipfile at runtime” is left as an exercise to
the reader. This may be overkill, depending on your needs.

–ryan.

Mmm interesting what you said about zipfiles…

But my goal is not to make everything available from one binary…
See, I am writing a library to simplify using SDL (I know it s already
simple, but still…) . As an example one call to my lib and you can create
a window with basic event handling.
To achieve this kind of goal you need some default behaviour and default
data.

At the moment my library actually depends on only one little image file :
the icon for the window.
I dont want the build to mess around with this file, and I dont want users
to have to put the file in the correct place before using the library…

Thats why embedding the file in the source code looks the good solution to
me.
I was able to get the raw code of the whole file but using it is not really
simple.

I think I am gonna have a look at the RWOps to use them to read my data from
memory…

Thanks a lot all for your help.

2006/1/25, Ryan C. Gordon :>

I would like to store the image somehow in the source code

Is the goal in doing this to make everything available from one binary,
or is it just to make it so you don’t have to do file i/o and allocation
to get at the data?

I’d be inclined to avoid this tactic, but you COULD write a small
program to convert a file into a C array:

/* This code hasn’t even been compiled, let alone debugged. */
int main(int argc, char **argv)
{
int ch;

    printf("unsigned char myfile[] = {\n");
    while ((ch = fgetc()) != EOF)
            printf("0x%X,\n", ch);
    printf("};\n\n");

    return 0;

}

…then run it as “convertdata < myfile.bmp > myfile.c”

Obviously, that can be cleaned up a little.

However, if you don’t mind jumping through some hoops and really just
wanted all your data to be contained in one file with your binary, you
can save on ugliness and static memory usage by putting a bunch of files
in a zipfile and attaching it to the end of the binary…this works on
at least Windows and Linux, maybe Mac OS, too. Zipfiles store their
table of contents at the end of the file, so this is how self-extracting
.EXE files get away with it.

So now the pipeline looks like:

  • Build your program in myprogram
  • Build your data in mydata.zip
  • cat myprogram mydata.zip > finalprogramfile

Verify it works:

  • unzip -v finalprogramfile

Now run the program and have it open itself as a zipfile at runtime to
access the data.

“have it open itself as a zipfile at runtime” is left as an exercise to
the reader. This may be overkill, depending on your needs.

–ryan.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl