Question

I'm having an odd problem with accessing files through SDL2 on Mac. I am using XCode 4 on OS X Mountain Lion, and I am using SDL2 as a framework. I am NOT compiling SDL2 from source. I have added the framework to the bundle, as well as the files I need loaded. The program builds and runs fine from XCode, but files do not load outside of the editor (I.E. if I double-click the standalone .app the files don't load). Here is what my .app package looks like:

MyApp.app/
    Contents/
        Frameworks/
            SDL2.framework/
        MacOS/
            MyApp
        Resources/
            Configure.txt
            MyBMP.bmp
        Info.plist
        PkgInfo

In my C code, I have attempted this:

SDL_LoadBMP("MyApp.app/Contents/Resources/MyBMP.bmp");

As well as just this:

SDL_LoadBMP("MyBMP.bmp");

And virtually everything inbetween. I've also tried accessing the text file through:

FILE* data = fopen("MyApp.app/Contents/Resources/Configure.txt", "r");

and

FILE* data = fopen("Configure.txt", "r");

to no success. Within the XCode editor, only the long absolute paths work, while nothing I've tried has worked in the standalone .app.

Are others experiencing this issue? I had the files loading when I was using SDL 1.2, but for some reason nothing seems to load with SDL2. Does SDL2 do something weird with the active directory during initialization?

-------------EDIT 1-------------- My most recent attempt to figure out what's going on used this code:

#include "SDL2/SDL.h"
#include "stdio.h"
#include "stdlib.h"

int main (int argc, char** argv){
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
         return EXIT_FAILURE;

    FILE* test = fopen("Test.txt", "w");

    if (!test) 
        return EXIT_FAILURE;

    fprintf(test, "Let's see where this ends up...");
    fclose(test);
    return EXIT_SUCCESS;

    //The rest of my code, which shouldn't ever come into play...
}

When run from the XCode 4 editor, this works as expected. In the Debug folder, right next to my .app file, there is a Test.txt file containing the phrase "Let's see where this ends up...". However, when run by clicking the standalone app, the program ends immediately and there is no text file to be found. I've checked the logs, and it simply says the program exited with code 1. Through more thorough analysis, it seems that fopen() is failing. Does anyone have an idea as to what may be going on?

Was it helpful?

Solution

You can set the working directory to the Resources directory within the app bundle using:

#include "CoreFoundation/CoreFoundation.h"

char path[PATH_MAX];
CFURLRef res = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
CFURLGetFileSystemRepresentation(res, TRUE, (UInt8 *)path, PATH_MAX)
CFRelease(res);
chdir(path);

which you might want to wrap between #ifdef __APPLE__ and #endif for cross platform compatibility.

OTHER TIPS

Download the SFML library for ResourcePath.hpp or get it from https://github.com/Malaxiz/Third/blob/network/Fifth/ResourcePath.hpp https://github.com/Malaxiz/Third/blob/network/Fifth/ResourcePath.mm

#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#include "ResourcePath.hpp"
#endif

void CGame::_initRelativePaths() {
// ----------------------------------------------------------------------------
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
    {
        // error!
    }
    CFRelease(resourcesURL);

    chdir(path);
    #endif
    // ---------------------------------------------------------------------------    -
}

Run the function in init.

Reference: https://github.com/Malaxiz/Third/blob/network/Fifth/CGame.cpp#L191

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top