[SDL] Embedded Images

David Olofson david at olofson.net
Tue Oct 10 10:12:41 PDT 2006


On Tuesday 10 October 2006 18:44, Simon Roby wrote:
> On 10/10/06, Gabriele Greco <gabriele.greco at darts.it> wrote:
> > David Olofson wrote:
> > > Hints:
> > >       SDL_RWops
> > >       IMG_Load_RW()
> > >
> > >
> > There is still the problem to link the image in your source code.
> > It's an easy task if it's an XPM (you can include it with
> > #include), but not that easy if you want to use a binary format.
> > There is a portable way to do this without external programs that
> > convert images in source files? 
> 
> Convert your image files into plain C char arrays (not talking about
> XPMs here), and use what David mentionned to load them as SDL
> surfaces during execution. It's really easy to do, and can be
> included in your compilation process.
> 
> Quick pseudocode for converting a binary file to an array:
> ---
> open binary.png for reading
> open array.h for writing
> write to array.h: "static const char binary[] = {"
> while not at end of file of binary.png
>     get one byte from binary.png
>     write to array.h: (string)byte + ","
> end while
> write to array.h: "};"
> ---
> 
> This is fully portable, and you do not lose any aspect of the
> original image file, it's really embedded "as-is" inside your
> program.

Well, yes, this is the easy, solid and *mostly* portable way. There is 
the issue I mentioned in the last mail, with cross compilers and 
custom build tools like this, but that's usually no major 
showstopper. (Just make sure you actually have a native compiler as 
well, and use whatever means your build tools provide for compiling 
the tool for the host, rather than the target platform.)

Another problem, if you have lots of data, is that all of it will be 
loaded right away, before the application starts. That could be very 
annoying, at least when running directly from CDs or other slow 
media.


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'




More information about the SDL mailing list