SDL2 Mac File Handling

I am having a strange problem accessing files via SDL2 on Mac. I am using Xcode 4 in OS X Mountain Lion and I am using SDL2 as the frame. I am NOT compiling SDL2 from the source. I added the framework to the set, as well as the files that I need to download. The program builds and works fine with Xcode, but the files do not load outside the editor (I.E., if I double-click the standalone .app file, the files do not load). This 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 tried to do this:

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

Just like this:

 SDL_LoadBMP("MyBMP.bmp"); 

And almost everything in between. I also tried to access the text file via:

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

and

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

not successful. Only long absolute paths work in the Xcode editor, while nothing I tried worked in standalone .app.

Do you have other problems? I downloaded files when I used SDL 1.2, but for some reason SDL2 doesn’t load anything. Does SDL2 do something weird with the active directory during initialization?

------------- EDIT 1 -------------- My last attempt to find out what is happening with 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 see where this ends up..."); fclose(test); return EXIT_SUCCESS; //The rest of my code, which shouldn't ever come into play... } 

When launched from the Xcode 4 editor, this works as expected. In the Debug folder, next to my .app file, there is a Test.txt file containing the phrase "Let me see where it ends ...". However, when you start by clicking on the stand-alone application, the program immediately ends and the text file is not found. I checked the logs and it just says that the program exited with code 1. Thanks to a more thorough analysis, it seems that fopen() is failing. Does anyone have an idea of ​​what might be happening?

+4
source share
2 answers

You can install the working directory in the "Resources" directory in the application 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 can wrap between #ifdef __APPLE__ and #endif for cross platform compatibility.

+3
source

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.

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

0
source

Source: https://habr.com/ru/post/1501234/


All Articles