Categories
Game Development General Linux Game Development

Relative Path to Game Directory on Gnu/Linux

One thing I discovered when trying to run my Pong clone is that I had to change to the same directory as the binary in order to run it. I should be able to just type “sdl-pong” from the command line and have it run without caring which directory is my current working directory. Now, I didn’t hardcode it to use the exact path name for files, so I could move the entire game directory and it could still run just fine. I haven’t made that mistake since I was actively using QBasic almost 10 years ago. B-)

I don’t want to require the user to need to use the command line interface to run the game, so I need to solve this problem. No matter where the user is currently running the game from, the game should run. So what can be done?

Well, luckily, this problem has been solved. I checked, and sure enough, the games I currently have installed do almost the same thing.

The games are launched using a shell script. When you type “darwinia” or “dhlore” or “drod-jtrh-demo”, the thing that you are running is a shell script. In some cases, the path is hardcoded, but the best way is to use the script to find the actual path to the game binary, then run it.

How does one find the path? Sam Lantinga, of Loki Software and libsdl fame, had created a script called FindPath(). Details can be found in this mailing list post.

I don’t know why something so simple and easily solved isn’t more readily available to new developers. I just checked, and “Programming Linux Games” does not seem to use such a script for Penguin Warrior, the example game provided. “Linux Game Programming” mentions path names, but only when talking about the difference between Windows separators (“\”) and everyone else’s separators (“/”).

It took me some time before I found the script online, but now that I know what to look for, it is easy to find.

Unfortunately, I found out that it may be a problem. At least, it isn’t recognized as THE way to do handle the relative path problem. It seems that while the Filesystem Hierarchy Standard mentions using /usr/local/games for your installation directory, there seems to be some contention. Do you use /usr/local/games, or do you use /opt? What files should belong where? Is it even ok to worry about relative pathnames, or should your game assume that the files will be in the standard locations? If you distribute binary-only games, is it safe to ignore some documents because they assume that everything should be distributed as source?

After spending only a couple of days on this topic, I no longer wonder why hardly anyone makes games for Gnu/Linux. Even if you decide to do the actual development of the game, just packaging it up and distributing it is a big hassle. I believe Apple provides developer resources that specify details such as these, as does Microsoft. Until there is a definitive answer as to how to do something, one solution seems as good as another.

Of course, I could do worse than to follow the example of Introversion or Loki.