Help! Program Paths!

Linux specific forum
Num3
PureBasic Expert
PureBasic Expert
Posts: 2810
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Help! Program Paths!

Post by Num3 »

I need help!

Under windows my game seeks the current executable directory for game media, but under linux it doesn't.

I don't wan't to bloat the executable with all the game media included, so i resort to external media packs.

If i run using PBCOMPILER game.pb it loads all media, but if i create a stand alone executable it doesn't...

I know this has to do with the paths you should store things, but i'm a linux newbie and can't get it rigth.

1. Can one define a path for the game directory? Something like Path=/usr/mygame/

2. If not, can anyone tell me how to solve this problem with code?
I've been using a smart routine by Freak, but that does not work under Knoppix, it crashes the game :(


Maybe on next releases Fred can include the CurrentPath() to both windows and linux, so we can solve this!

Thanks in advance...
Saboteur
Enthusiast
Enthusiast
Posts: 271
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Post by Saboteur »

It seems like program is executed from /home/user directory.
Executing the program by command line, it works ok, but executing from desktop, the result is different.

Try:

Code: Select all

examinedirectory(0,".","*")

while(nextdirectoryentry())
  file.s+directoryentryname()+chr(10)
wend
messagerequester("Info","Files:"+chr(10)+file)

end
From command line, you must get files from local directory, but from konqueror/nautilus... what do you get? Your /home files?

Somebody asked for a method to read the output from other programs, and that could be a solution. You could execute "pwd" or another utility and capture the exit string in purebasic programs.

Another solution is to write a hide config file in home directory (like .myconfigfile), where you write the true path to your program. And you allways can access to that file ( ./.myconfigfile ).
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

Another solution is to write a hide config file in home directory (like .myconfigfile), where you write the true path to your program. And you allways can access to that file ( ./.myconfigfile ).
You can access the users home dir from any path by using the "~" character. So I would access the above mentioned config file from your app like so:
"~/.myconfigfile"
-Beach
Saboteur
Enthusiast
Enthusiast
Posts: 271
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Post by Saboteur »

Beach wrote: You can access the users home dir from any path by using the "~" character. So I would access the above mentioned config file from your app like so:
"~/.myconfigfile"
I tried thad, it works by command line, but not in purebasic program. I only get an empty string. :( Fred!! is a bug?
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

DOHH! Sorry, I should have tested that first... :oops:
-Beach
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It's not a bug, from what I know, the '~' shortcut is shell dependant (here you use probably bash). You have to use the correct environment variable to get the user home directory (seems to be PWD).
Num3
PureBasic Expert
PureBasic Expert
Posts: 2810
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

PWD does not report to the executable path, seems to vary from profile to profile....

_ (underscore) is the variable that reports the executable path and name e.g. (/usr/bin/myexe)

But has i have told there seems to be diferences on how to get this info from Linux core to Linux core :(
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

PWD returns the current directory, doesn't it?
HOME if for the user directory.

All distributions i used so far had that environment variable present.
quidquid Latine dictum sit altum videtur
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You're right indeed, PWD is the for current working dir and HOME for the user home directory. My mistake.
Num3
PureBasic Expert
PureBasic Expert
Posts: 2810
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

I just hardcoded the game path to the game....

/usr/games/mygame/

Not very elegant, but it works!
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You said my routine crashes. Did you use this one?

Code: Select all

*home = getenv_("HOME")
If *home
  Home$ = PeekS(*home)
Else
  Home$ = ""
EndIf
Because that works much better than the one i posted earlier.
You can then set an environment variable to your program's directory,
which the user can change if he wants.

Timo
quidquid Latine dictum sit altum videtur
Num3
PureBasic Expert
PureBasic Expert
Posts: 2810
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

I'll take a look to this linux API call!

From what i've read about it's exactly what i need :P

Does anyone have an offline API manual for linux !?
Kinda of like win32 help ?
BasicGuy
User
User
Posts: 70
Joined: Sat Apr 26, 2003 4:37 am
Location: US

Post by BasicGuy »

I'm a long time (over 9 years now) Linux user and SysAdmin, so please don't take my suggestions in the wrong spirit... 8^)

"but that does not work under Knoppix..."

First, I'd suggest installing a "real" Linux distro, like Red Hat or SuSE, on your computer... Knoppix is nice but is intended more for it's ability to create a friendly desktop than a developers enviroment. It has issues, especially when trying to compile/link source code. The newer mainstream distros have the ability to provide multiboot capabilities at startup. I generally use two HD's, the C drive for M$ and the E drive for Linux.
SuSE has a very nice install suite, is very simple to use and normally the distro I recommend to noobs.

"...but i'm a linux newbie and can't get it right. Does anyone have an offline API manual for linux !?"
As you're new to Linux, I suggest getting a comprehensive book on the subject. Linux is not like M$ and if you realistically expect to accomplish much of anything, you need to know just how different is it. This applies especially to advanced programming techniques like program compilation and linking.

Or, you'll find just about everything you need to know in the 'man' pages. These are documents that describe, in varying detail, the programs that comprise a Linux distro. For instance, you might type at the commandline; 'man bash <return>' or even 'man man' to learn about man pages in general. Under X-Windows, Gnome or the KDE, try typing 'xman' (a graphical man page viewer). If it's installed, it will allow you to access the man pages quite easily.

I would also suggest reading "Learning the BASH shell" from O'Reilly. BASH is generally the default shell enviroment in most Linux'es and quite easy to use. Most problems noobs have can be solved by shell scripting. The problem you describe is solvable by adding your development directory to the $PATH variable (like in M$). If you need it to work dynamically, a simple bash shell script can set the variables for you then invoke the compiler.
Hope this might prove helpful. 8)
Num3
PureBasic Expert
PureBasic Expert
Posts: 2810
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

You're very helpfull BasicGuy, and i took good notes on your considerations...

I've worked around the problem has described above.

The main reason why i use a shake and bake linux distro is because most end users will also use this kind of distro at the begining.

Right know i'm using Mepis Linux (Gentoo), and took me just 5 minutes to install and setup purebasic.

All my cross-platform code worked like a charm :D

It's obvious i'm not experienced working with linux, so most of my stuff is try and fail. Linux reminds me of Amiga OS architecture, so i'm a bit confortable using it.

The GetWorkingPath() is a long time request of mine, and it would be cool it was built in to purebasic to avoid head haches!

Anyway, i'm very pleased you're onboard, you're experience will sure be welcome!
BasicGuy
User
User
Posts: 70
Joined: Sat Apr 26, 2003 4:37 am
Location: US

Post by BasicGuy »

Cool! Glad you got it work out! :wink:
Post Reply