Dovahkiin
Dovahkiin

Reputation: 1056

How come my SFML game only works when ran in Terminal?

Ok, this question needs more explanation.

I've written a game in c++ using SFML. So when I finished the game (I'm using xcode), I went to Product > Archive and saved my game's executable in a folder. I put all the resources in the same folder as the executable. When I double click the executable, I get an error like this:

Last login: Wed Dec 17 19:38:15 on ttys001 My-Macbook:~ My_Name$ /Users/My_Name/Dropbox/SFML/DodgeBall/Builds/DodgeBall\ Build\ 12-17/DodgeBall ; exit; Failed to load font "sansation.ttf" (failed to create the font face) Game Crashed: class 'ResourceLoader' was unable to load file logout

This part:

Game Crashed: class 'ResourceLoader' was unable to load file

Is output from my game itself. The rest I don't understand.

But when I go into terminal and navigate to my game's executable, I can do this:

./DodgeBall

And my game runs fine. How come this is? Is there a way to make it so I can double-click the executable and it will run?

I'm using a 2010-ish macbook, xcode 6, OSX Mavericks, and SFML 2.1.

Upvotes: 0

Views: 322

Answers (1)

Mooing Duck
Mooing Duck

Reputation: 66912

When you double click an executable, the Operating System GUI runs your executable as a new process, and sets the "current directory" of that process to some arbitrary directory. In Windows it's C:/Windows/System32/ or something. Then, your code tells the operating system to open sansation.ttf, and it checks the current directory, and a couple other directories, and then fails to find the file and gives up.

When you "navigate to my game's executable" in the console, and launch the program that way, the console runs your application as a new process, and sets it's "current directory" to the same as that of the console, which is the game executable's directory. When you try to open the ttf file, the operating system checks the current directory, finds the file, and continues happily.

The best solution is to add a function that changes the process' current directory to wherever the application is. I'm unfamiliar with the Mac OS, but I found this: how to change the working directory to the location of the program. Alternatively, you can make an installer that creates some sort of system variable that stores the location of the tff file. (In windows this might be setting the PATH or making registry entries)

Upvotes: 1

Related Questions