Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Load resources in app bundle  (Read 3382 times)

0 Members and 1 Guest are viewing this topic.

Dooskington

  • Newbie
  • *
  • Posts: 7
    • View Profile
Load resources in app bundle
« on: September 20, 2015, 08:05:38 pm »
I compile and bundle my SFML application into a .app, and then I run the app by typing open Orbs.app.
What I cannot figure out, however, is the working directory so that I can load a font. I copy a font to the Resources folder of the app during compilation, but the app can't find it. I thought that the default working directory of an SFML app was the resources folder?

Here is my folder structure:

Orbs.app
    Contents
        Mac OSX
            main (main binary)
        Resources
            SFML stuff
            geometos.ttf
            icon.icns
    info.plist

I want to be able to load the geometos.ttf font, but font.loadFromFile("geometos.ttf") cannot find the font. What do I have to do?

Dooskington

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Load resources in app bundle
« Reply #1 on: September 20, 2015, 09:13:37 pm »
I figured it out. What a pain.

Remember to build your project with the CoreFoundation framework and to include "CoreFoundation/CoreFoundation.h".
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[512];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8*)path, 512))
    {
        // error!
    }
    CFRelease(resourcesURL);
    strncat(path, "/geometos.ttf", 512); // The final path
 

I know that this is a Mac OSX specific solution, only when developing .app bundles, but there should be some sort of reference to this in the SFML docs. I'm not talking about an explanation, because there is enough documentation through Apple, but this would have been a lot easier for me to figure out it the tutorials told me "When developing a .app bundle, the working directory will have to be determined pragmatically.", and then a link to the App Bundle Reference.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Load resources in app bundle
« Reply #2 on: September 20, 2015, 10:31:12 pm »
What about the solution provided in the official tutorial?
Quote
Header & source files: the project comes with a basic example in main.cpp and the helper function std::string resourcePath(void); in ResourcePath.hpp and ResourcePath.mm. The purpose of this function, as illustrated in the provided example, is to provide a convenient way to access the Resources folder of your application bundle.
Please note that this function only works on Mac OS X. If you are planning to make your application work on other operating systems, you should implement your own version of this function on the operating systems in question.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Dooskington

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Load resources in app bundle
« Reply #3 on: September 20, 2015, 10:48:38 pm »
What about the solution provided in the official tutorial?
Quote
Header & source files: the project comes with a basic example in main.cpp and the helper function std::string resourcePath(void); in ResourcePath.hpp and ResourcePath.mm. The purpose of this function, as illustrated in the provided example, is to provide a convenient way to access the Resources folder of your application bundle.
Please note that this function only works on Mac OS X. If you are planning to make your application work on other operating systems, you should implement your own version of this function on the operating systems in question.

I didn't see that, now I feel stupid.

However I'm not using XCode. Where would I get that ResourcePath.hpp file?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Load resources in app bundle
« Reply #4 on: September 20, 2015, 10:58:14 pm »
It's part of the Xcode template.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/