c++ xcode cross-platform game development

Discussion in 'Mac Programming' started by Ultimatenero, Sep 3, 2012.

  1. Ultimatenero macrumors newbie

    Sep 3, 2012
    Hi everyone,

    I've got a ansi-compliant c++ games project using "takes a deep breath" openGL, SDL, cAudio, SDL_image, SDL_ttf which I started developing on Windows and I'm investigating how to get things to work on Mac. I'm on XCode 4.3.2 and I'm on mac osx 10.7.4.
    I'm linking my application with the Cocoa framework, am using gcc 4.2 and I am using SDLMain.m and SDLMain.h directly in my project which you can get from the sdl-devel package.
    So I've managed to get to the same point in my application on Mac where I was at at windows. The application compiles, links and runs and I can start receiving debug messages from cout etc.

    Let's come to the problem. I have created the xcodeproj inside my root where my source code resides:

    -- xcodeproj
    -- all source files
    Data files are one level deeper for instance:
    -- enemies/enemy.xml

    I then configured xcode to build to my project root / xcodebuild

    So I end up with xcodebuild / debug / myapp

    When running, my logs tell me that the application can't find my data files such as enemies / enemy.xml

    I've heard about bundles and the resources folder and I also saw this little piece of code on a different forum:

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    [[NSFileManager defaultManager] changeCurrentDirectoryPath:resourcePath];

    I put this in my SDLMain and it allows me to run my app either from Xcode or from the app directly in the xcodebuild / debug / myapp directory. The above mentioned code should give you the original forum post that is useful to understand how mac's working directories work. My whole app runs fine if I manually copy all of my resource files to the directory where my app resides.

    There's fundamentally two issues I am facing with xcode:

    Why do I need to add my files to a special build phase in my target? When running my app, why does it not find enemies/enemy.xml? Why can't I add a directory to xcode, and it will automatically copy all the files in that directory to the build directory? I can only select individual files, so everytime I would add another data file, like enemy2.xml, I would have to remember adding it into my copy files build phase. This gets worse, because for each directory like enemies/enemy.xml or weapons/weapon.xml I would have an additional "copy file" build phase.
    What is a good approach to cross-platform applications using sdl when they hit the mac platform?

    The second issue for me on mac is that I don't fully understand how to deploy my application so that all of my dylib files and frameworks are bundled together and ready to be turned into an (i'm assuming) dmg or pkg file, ready for installation on someone else's system. I'm curious to know how mac people do this and I hope you can point me in the right direction.

    Thanks for any insights you might provide
  2. holmesf, Sep 3, 2012
    Last edited: Sep 3, 2012

    holmesf macrumors 6502a

    Sep 30, 2001
    Add your resources folder to your project but tell XCode to create folder references instead of creating groups. When your project is built the directory structure will be preserved so that your files will be found correctly, and new files added to your resources folder will automatically be added to your project. When you add files as groups the default action is to flatten the directory structure, which is going to screw up your paths. The line of code you listed will change the current working directory to the app's resource directory, which resides inside the application bundle. This is probably what you want. In Mac OS X you don't put application resources in the same directory as the application, but rather you bundle them together with the executable.

    Edit: you will probably get more helpful responses if you ask fewer questions at a time.
  3. Ultimatenero thread starter macrumors newbie

    Sep 3, 2012

    Thanks for your answer. That was just the info I needed.

    I'm still chipping away and I've done the following for everyone's reference:

    I read :


    I followed these steps and hurray, I no longer have a unix file but an app bundle.

    I am adding my data as references like you suggested, and they get copied into my bundle automatically. Great!

    When I run from Xcode everything is fine and the application launches.

    To get it to run standalone I tried the following:

    I added SDL.framework and my other dependencies to the copy files build phase.

    I opened the bundle and they appear under frameworks. So that's good too.

    When double clicking my app under my build directory, I don't get any message. The program starts, and finishes with no crash, nothing.

    When I right-click my app, and browse into it and go under Contents/MacOS and double-click the unix executable file, the program works fine. So why would double-clicking the .app not work? Any ideas?
  4. Ultimatenero thread starter macrumors newbie

    Sep 3, 2012
    I never replaced :

    [self setupWorkingDirectory:gFinderLaunch];

    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    [[NSFileManager defaultManager] changeCurrentDirectoryPath:resourcePath];

    I had both codes in there, and ultimately the gfinderLaunch path was still being used...

    I didn't spot the second "setupWorkingDirectory" call in SDLMain.m.

    Works perfectly now. Hope this helps to anyone else who is starting out on these issues

Share This Page