[SDL] grief loading/getting xpm in function with char *

Solra Bizna sbizna at tejat.net
Sat Mar 8 09:22:30 PST 2008


LoadXPMFromArray gets passed the address of the array containing the
XPM data. What you seem to be trying to do is pass it the symbol name
of the array containing the XPM data. You can't do that directly in C,
you'd need to do something non-portable like SDL_LoadObject your
executable.
If you must access the graphics this way, instead of directly, what
you should do is set up an associative array containing a mapping
between names and arrays.
Something like:

// Keep this sorted by name
static struct name_array_map {
  const char* name;
  char** array;
} name_array_map[] = {
  {"food", food_xpm},
  {"name", name_xpm},
  {"pie", pie_xpm}, // can anyone tell what's on my mind?
  {"rootbeer", rootbeer_xpm},
};

static int namcmp(const void* key, const void* base) {
  return strcmp((const char*)key, ((const struct name_array_map*)base)->name);
}

SDL_Surface* getimage(int mask, const char* name) {
  struct name_array_map* entry;
  SDL_Surface* surface;
  // snip
  entry = (struct name_array_map*)bsearch(name, name_array_map,
sizeof(name_array_map)/sizeof(*name_array_map),
sizeof(*name_array_map), namcmp);
  if(!entry) {
    SDL_SetError("No such image");
    return NULL;
  }
  surface = IMG_ReadXPMFromArray(entry->array);
  // snip
}

-:sigma.SB


More information about the SDL mailing list