I’m trying to get SDL setup in Mac OS X lion with Xcode. So far I seem to have everything working except for loading images. (Using SDL_image) I’m using the following code, which will work when compiling the code via the command line and not making a .app, but doesn’t work in this Xcode setup.
SDL_Surface* load_image( std::string filename )
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormatAlpha( loadedImage );
SDL_FreeSurface( loadedImage );
} else {
std::cout << "Error loading image: " << filename << " Error:"<< std::endl << SDL_GetError() << std::endl;
}
return optimizedImage;
}
When calling this function I get back a null pointer and the console says this:
Error loading image: puppy.png Error:
Couldn't open puppy.png
I’ve looked in the bundle, and the puppy.png is put in the Resources folder as expected. I’ve also tried calling load_image(“../Resources/puppy.png”) or moving the puppy.png into the MacOS folder, but neither worked.
A pathname to a file is relative to the current working directory, which is unpredictable in programs launched from Xcode or the Finder (in a shell, it’s based on wherever you last ran
cd).Unless a function’s documentation says that it searches the application bundle for a file name, you can’t really assume that it will. Instead, look at the
NSBundleclass and use those methods to automatically find the full path to a file that is located in a place such as Resources; then pass that full path to the image loader in this case.