[SDL] loading xpm from memory

Ryan C. Gordon icculus at icculus.org
Tue Sep 26 21:51:58 PDT 2006

> 1) At some files, i got this error: colour parse error , but these files
> should be correct, cuz imageviewer programs (i use eeyes and display) can
> display it. Some files of them are here: http://12E7041.ath.cx/xpm/ . What
> can be the problem?

I haven't checked, but they're likely broken, and display/eeyes are 
filling in best guesses for the missing color values.

> 2) I want to load a lots of (e. g. 40- 50) xpms, and refresh them a lots
> of times. Certainly after 5- 6 refresh i got back this error: couldn' t
> open xpm file. I bet this is cuz of the lots of open file. Is there any
> way to load xpm files from memory? What is the method to store them in the
> memory, and second time to load from there?

First, is there a good reason to reload them? Are they changing? Save 
the SDL_Surfaces from IMG_Load* instead, if the data isn't actually 

Some notes on the code:

> void ikonkirak(SDL_Surface *surface, SDL_Surface *ikonimage, char *nev,
>                int x, int y)

Should "SDL_Surface *ikonimage" actually be "SDL_Surface **ikonimage"?

Right now you're allocating a surface in this function, and leaking it, 
since ikonimage is a temporary variable. If your intention was to return 
the SDL_Surface to the calling function, you aren't doing that here.

> {
>  SDL_RWops *rwop;
> // rwop = SDL_RWFromFile("/usr/share/pixmaps/pstree16.xpm", "rb");
>  rwop = SDL_RWFromFile(nev, "rb");

Use SDL_RWFromConstMem() if you want it from a buffer in RAM instead of 
a file.

>  ikonimage = IMG_LoadXPM_RW(rwop);

Alternately, you can use IMG_ReadXPMFromArray() instead of a RWOPS, but 
this only works with XPM files...you probably should make this generic 
so you can use something other than XPM in the future, and should use 
IMG_Load_RW() and let SDL_image figure out what type of image format you 
are feeding it.

Also, if you meant to return the SDL_Surface to the caller, you should 
have done: "*ikonimage =" ... actually, you'd need "*ikonimage" 
everywhere in this function.

You'll also want to free the RWOPS, which is why you're running out of 
file handles:


>  if (!ikonimage)
>  {
>   printf("Hiba!: %s\n", IMG_GetError());
>  } else
>  {
>   SDL_Rect dest = {x, y, 16, 16};
>   SDL_BlitSurface(ikonimage, NULL, surface, &dest);
>  }
> }

(And if you don't want to return the surface to the caller, you'll want 
to use SDL_FreeSurface() when you are done with it!)

But if these XPMs aren't changing, you really should save the surfaces 
instead of reloading them every time.


More information about the SDL mailing list