2

I'm trying to make an SDL application in Xcode, but I'm having trouble loading images. I'm using this template, because I couldn't get it to work when made from scratch.

Whenever I try to load an image with SDL_LoadBMP however, it returns NULL unless I give the absolute path. (/Users/Cole/code...) I looked in the exported .app file, and it does have the image I want to load in Contents/Resources/, and I've tried every combination I can think of to get at those (../Resources/image.bmp, ect.) but I can't seem to get it working.

Does anyone have a solution? I'm running Mac OS 10.7 with Xcode 4, so I can't use the templates that is within the SDL download.

Also, I tried using SDL_ttf, but I get this error:

warning: Unable to read symbols for @executable_path/../Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf (file not found).
warning: Unable to read symbols from "SDL_ttf" (not yet mapped into memory).

There does not happen to be a Frameworks folder where it's looking, but somehow it finds the regular SDL framework just fine.

Community
  • 1
  • 1
Cole
  • 720
  • 1
  • 8
  • 18
  • 1
    @bottleboot Yeah, see the answer below. I didn't accept it at the time because I didn't know how to make it work, but now I realize that it's exactly what I needed. You need to include his code in a .m or .mm file though and pass it to c++ code. – Cole Aug 01 '13 at 23:57
  • oh cool, thank you very much! So even if I want to code for cross platform, this is the way to go? – bottleboot Aug 02 '13 at 09:02

2 Answers2

1

You can get the path to your the Resources directory containing your file with

NSString *path = [[NSBundle mainBundle] resourcePath];

or alternatively (in theory more clean as it can access localized files) you can get the full file name with

NSString *file = [[NSBundle mainBundle] pathForResource:@"image.bmp" ofType:nil];

You'll need to pass the C string to SDL_LoadBMP, so either of the two:

SDL_LoadBMP([[path stringByAppendingString: @"/image.bmp"] UTF8String]);

SDL_LoadBMP([file UTF8String]);
Simon Urbanek
  • 13,842
  • 45
  • 45
0

I had the same problem and found a way without using any objective-c.

In xcode click on your target then go onto the build phase section

Then in the top bar click: Editor -> Add Build Phase -> Add Copy Files Build Phase

Now change the destination of the newly created phase to "Products Directory" and then add any subpaths if needed.

All you need to do now is add your image onto the list below and it should work!

Xcode steps

user1628311
  • 166
  • 1
  • 3
  • 12