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

Author Topic: Failing to load images and font on application run  (Read 4959 times)

0 Members and 1 Guest are viewing this topic.

karanveer41

  • Newbie
  • *
  • Posts: 3
    • View Profile
Failing to load images and font on application run
« on: October 03, 2014, 05:57:36 am »
I am working with the book 'SFML Game Development' by Artur Moreira and I am having trouble running the SFML projects that come with the book.

I followed all the instructions and used cmake and and then 'make install' to build the project files. When I click on the 01_Intro executable in the built folder, I get this error.

Failed to load image "Media/Textures/Eagle.png". Reason : Unable to open file
Failed to load font "Media/Sansation.ttf" (failed to create the font face)

Both those files are present in the Media folder right next to the executable. I don't know too much about SFML at this point to tell whats wrong. Any guesses on whats going on?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Failing to load images and font on application run
« Reply #1 on: October 03, 2014, 07:34:37 pm »
It looks like your IDE's current working directory (CWD) is set to something other than where your executable is. If you change the CWD to the folder where your executable is, it should find them okay.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

karanveer41

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Failing to load images and font on application run
« Reply #2 on: October 05, 2014, 07:52:14 pm »
There is just an executable file, I'm not using an IDE to run the application. When I double click the executable, it opens up the mac terminal and that's were I see the error message.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Failing to load images and font on application run
« Reply #3 on: October 05, 2014, 08:46:53 pm »
Oh, "mac."

I had a similar problem when I started porting my stuff.  At least on the Mac I used, the current working directory was always the user's home directory, regardless of where the actual executable file was.  As far as I know the only solutions are: 1) make a proper app/dmg for your game so it gets installed in the usual place and you can rely on that, or 2) ask Mac OS for the executable filepath on startup.

I wasn't ready to learn how every other OSs installers worked (they're massively overcomplicated on all three so I'm not exactly eager to work it out), so I went with #2.  To save you a bit of time, here's the badly written block of code I have lying around for this purpose:
//on Mac OS X the working directory defaults to your home folder
//instead of the path of the executable, so we need to fix that
#if defined __APPLE__ && defined __MACH__
//__APPLE__ is defined on all Apple computers, __MACH__ on Mach-based kernels; together they specify OS X
        #include <unistd.h>
        #include <mach-o/dyld.h>
        #include <errno.h>
        uint32_t i = 1024;
        char* s; s = new char[i];
        _NSGetExecutablePath(s, &i);
        std::string str = std::string(s);
        str = str.substr(0, str.find_last_of("/"));
        if(!chdir(str.c_str()))
                throw(std::wstring("Failed to change working directory to executable path: " + strerror(errno)));
        delete[] s;
#endif
 

Sadly, I'm pretty sure there is no cross-platform way of loading resource files from relative paths (I spent a very long time looking for such a thing).  Even the Filesystem proposal for C++ doesn't include any function that would help with this, so every program with even a single resource file will always have to call some utility function with #ifdefs to handle those files.
« Last Edit: October 05, 2014, 08:55:41 pm by Ixrec »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Failing to load images and font on application run
« Reply #4 on: October 05, 2014, 09:09:27 pm »
//on Mac OS X the working directory defaults to your home folder
//instead of the path of the executable, so we need to fix that
#if defined __APPLE__ && defined __MACH__
//__APPLE__ is defined on all Apple computers, __MACH__ on Mach-based kernels; together they specify OS X
        #include <unistd.h>
        #include <mach-o/dyld.h>
        #include <errno.h>
        uint32_t i = 1024;
        char* s; s = new char[i];
        _NSGetExecutablePath(s, &i);
        std::string str = std::string(s);
        str = str.substr(0, str.find_last_of("/"));
        if(!chdir(str.c_str()))
                throw(std::wstring("Failed to change working directory to executable path: " + strerror(errno)));
        delete[] s;
#endif
 

May I suggest using a std::unique_ptr for 's'. Currently you leak on error when you 'throw' - unique_ptr would prevent that easily.

Sadly, I'm pretty sure there is no cross-platform way of loading resource files from relative paths (I spent a very long time looking for such a thing).  Even the Filesystem proposal for C++ doesn't include any function that would help with this, so every program with even a single resource file will always have to call some utility function with #ifdefs to handle those files.
Right. There is no such thing.
On Linux you can read the /proc/self/exe symlink. On Windows you can call GetModuleFileNameEx(). You already covered OS X.

On a related note; at least with the GNU ld and the gold linker, one can use the -rpath option along with $ORIGIN to refer to libraries relative to the applications location. Thus, if you put your resources into a library then the linker can take care of locating them relative to the executables location which can simplify things sometimes :)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Failing to load images and font on application run
« Reply #5 on: October 05, 2014, 09:27:32 pm »
May I suggest using a std::unique_ptr for 's'. Currently you leak on error when you 'throw' - unique_ptr would prevent that easily.
That's what I meant by "badly written".  If I ever get a chance to work on this code again I'd fix things like that immediately.

Quote
On a related note; at least with the GNU ld and the gold linker, one can use the -rpath option along with $ORIGIN to refer to libraries relative to the applications location. Thus, if you put your resources into a library then the linker can take care of locating them relative to the executables location which can simplify things sometimes :)

Since I wanted my program to work with resources created by other (non-technical) people, this is unfortunately not a practical option.
« Last Edit: October 05, 2014, 09:30:09 pm by Ixrec »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Failing to load images and font on application run
« Reply #6 on: October 05, 2014, 09:39:59 pm »
As far as I know the only solutions are: 1) make a proper app/dmg for your game so it gets installed in the usual place and you can rely on that, or 2) ask Mac OS for the executable filepath on startup.

You should read the getting started tutorial for OS X, it provides everything you need to create a .app with everything inside and the appropriate "get path" function...
SFML / OS X developer

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Failing to load images and font on application run
« Reply #7 on: October 05, 2014, 09:46:36 pm »
As far as I know the only solutions are: 1) make a proper app/dmg for your game so it gets installed in the usual place and you can rely on that, or 2) ask Mac OS for the executable filepath on startup.

You should read the getting started tutorial for OS X, it provides everything you need to create a .app with everything inside and the appropriate "get path" function...

I never had access to a real Mac, and the version of OS X I could get running in a VM couldn't seem to run any version of XCode (I still don't know why), so the tutorial didn't really apply.

If I have enough money lying around to buy a real Mac next time I want to port something, then I'll try doing it the proper way.

karanveer41

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Failing to load images and font on application run
« Reply #8 on: October 06, 2014, 06:16:31 am »
Okay, I found a way to make it work. Instead of navigating to the folder using the finder, I opened up the terminal and navigated with that and ran the executable with ./ command and it worked.