C++ with SDL2 Framework: Loading .bmp files in same folder in XCode 5 on Mavericks

Posts: 1
Joined: 2014.02
Post: #1
I am trying to load a .bmp file located in the same folder as main.cpp etc. but I'm not sure what to input as the resource path so that it picks it up and, when I distribute it, I want it to be preferably cross platform and run smoothly.

I have tried using:
SDL_Game/hello_world.bmp (SDL_Game is the name of the project)

but it will work if I use the full path. I don't want to do this though, because then it will not work on other computers and platforms.

This is the function I use to load media:

bool loadMedia()
// Loading success flag
bool success = true;

// Load splash image
gHelloWorld = SDL_LoadBMP("hello_world.bmp");
if (gHelloWorld == NULL)
    printf("Unable to load image %s! SDL Error: %s\n", "SDL_Game/hello_world.bmp", SDL_GetError());
    success = false;

return success;

This is the error:

Unable to load image SDL_Game/hello_world.bmp!
SDL Error: Couldn't open SDL_Game/SDL_Game/hello_world.bmp
Failed to load media!

This is my XCode file setup:

SDL_Game -> SDL_Game -> hello_world.bmp, main.cpp

I am using XCode 5, SDL2.0.1, OSX 10.9 Mavericks and C++.

I believe it is possible to change code in SDLMain.m but I can't seem to find that file and believe it was removed in SDL 2.

I would like a solution as cross platform as possible that would work after I had released it as a .app file.

If there is anything I might need to amend, I will.
Quote this message in a reply
⌘-R in Chief
Posts: 1,248
Joined: 2002.05
Post: #2
SDL2 doesn't use an SDLMain.m, which is a good thing.

Since you're passing a relative path to the SDL_LoadBMP function, that path is relative to the Current Working Directory. I believe that path is still set to the folder containing the .app bundle. Since you're using Xcode, and when running in Xcode it by default puts the .app into ~/Library/Developer/Xcode/DerivedData/YourProjectName/Build/Products/Debug, that's a looooooong way from your bmp next to your main.cpp.

The simple thing to do is actually include the image in your app's Resources folder in the bundle, and then change the CWD to that Resources folder, (or a specific folder inside of it), such as:

#include <CoreFoundation/CoreFoundation.h>


const char * Platform::ResourcesDirectory()
    static char resourcesPath[4096];
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef url = CFBundleCopyResourcesDirectoryURL(mainBundle);
    CFURLGetFileSystemRepresentation(url, true, (UInt8 *)resourcesPath, 4096);
    return resourcesPath;
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Xcode4: Resource not in Documents folder? SparkyNZ 3 6,460 Apr 9, 2011 03:29 PM
Last Post: SparkyNZ
  Why won't Xcode even try compiling my .cpp files?? SparkyNZ 11 13,476 Apr 3, 2011 01:44 PM
Last Post: AnotherJake
  moving all folder project = error imaumac 2 2,922 Oct 13, 2008 08:46 AM
Last Post: imaumac
  Newbie problem XCode Include files BBBert 1 4,284 Mar 17, 2008 03:46 PM
Last Post: OneSadCookie
  Library/Preferences folder someone 5 5,769 May 17, 2007 10:31 AM
Last Post: millercdusa