iDevGames Forums
C++ with SDL2 Framework: Loading .bmp files in same folder in XCode 5 on Mavericks - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Tools & Technology (/forum-10.html)
+--- Thread: C++ with SDL2 Framework: Loading .bmp files in same folder in XCode 5 on Mavericks (/thread-10904.html)



C++ with SDL2 Framework: Loading .bmp files in same folder in XCode 5 on Mavericks - exitcode - Feb 23, 2014 06:07 AM

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:
hello_world.bmp
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:

Code:
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:

Code:
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.


RE: C++ with SDL2 Framework: Loading .bmp files in same folder in XCode 5 on Mavericks - SethWillits - Feb 23, 2014 11:15 PM

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:


Code:
#include <CoreFoundation/CoreFoundation.h>

chdir(ResourcesDirectory());

const char * Platform::ResourcesDirectory()
{
    static char resourcesPath[4096];
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef url = CFBundleCopyResourcesDirectoryURL(mainBundle);
    CFURLGetFileSystemRepresentation(url, true, (UInt8 *)resourcesPath, 4096);
    CFRelease(url);
    return resourcesPath;
}