[SDL] Embedded Images

Simon Roby simon.roby at gmail.com
Tue Oct 10 09:44:15 PDT 2006


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.
-- 
- SR




More information about the SDL mailing list