Recent

Author Topic: MyImage.LoadFromFile: "unable to open file" (wrong folder in compiled file)  (Read 6314 times)

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
I'm having a problem with MyImage.LoadFromFile function. There are two variants of the code:

variant 1:
MyImage.LoadFromFile('help.jpg');

variant 2:
MyImage.LoadFromFile('/home/eugene/HELENA-130606-1/help.jpg');
('/home/eugene/HELENA-130606-1/' is a project folder, containing help.jpg file)

Both variants work perfectly when game is launched from the Lazarus GUI (debugged). BUT when I launch a pre-compiled game, variant 1 replies "unable to open file help.jpg", while variant 2 works as intended. By trial and error I've found that variant 1 looks for '/home/eugene/help.jpg' which is my home folder (when I copy the file there it works fine).
I use Lazarus 1.0.8 and Debian Squeeze.

I want that the game to be folder-independent (i.e. use the variant 1) and requiring only that help.jpg would be in the same folder with the compiled game... How can I set the working folder to the compiled file folder?

The complete source&game (if that helps...):
https://sourceforge.net/projects/projecthelena/
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Bart

  • Hero Member
  • *****
  • Posts: 5465
    • Bart en Mariska's Webstek
variant 1:
MyImage.LoadFromFile('help.jpg');

This will look for the file 'help.jpg' in the current directory, which would probably the directory you were in when launching the app.

Take a look at the GetAppConfigDir() function, there you can store things the app needs.

Bart

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
in the current directory, which would probably the directory you were in when launching the app.
Yes, it is intended to do so. But it does so only when i launch the program from Lazarus GUI (i.e. compile it with F9). But, when I launch the compiled file it considers the "current directory" not the directory where the compiled file is, but my home directory.
Quote
GetAppConfigDir()
Returned "/etc/Project Helena/"...
But it was a good hint:
MyImage.LoadFromFile(ExtractFilePath(application.ExeName)+'help.jpg'); worked perfectly. Thanks!
(found it here: http://forum.lazarus.freepascal.org/index.php?topic=12070.0)
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

Bart

  • Hero Member
  • *****
  • Posts: 5465
    • Bart en Mariska's Webstek
But it was a good hint:
MyImage.LoadFromFile(ExtractFilePath(application.ExeName)+'help.jpg'); worked perfectly. Thanks!

Well, this might work for now.
But (you are on Linux), it is not th eproper way to do so.
First of all Application.ExeName is only guaranteed to work on MS Windows and DOS platform.
It works on most Linux distro's, but you cannot rely on that.
And if someone created a hardlink to your app, it will point to the wrong location alltogether.

In the wiki there is a page about Multi Platform Programming, it has a section on this topic. Please read it.

bart

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Code: [Select]
MyImage.LoadFromFile('help.jpg');This should really work aswell, if help.jpg is in same folder as executable. In Windows you can ensure it works, by changing the work directory from application shortcut link, or running it directly from browsed folder. I don't know how you do it on *NIX. If you browse to the path in console window and run it, it should work right? So it's just about learning how to use the OS tricks.

 

TinyPortal © 2005-2018