load file with source file and output exe

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
thenewkgb
Posts: 59
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

load file with source file and output exe

Post by thenewkgb »

Hello.
I checked how the examples found files and also played around with <filesystem> and io::IFileSystem and argv[0]. When running my code in an IDE but also being able to test it for release, not both paths are the same.

The only way I could load a file developing it and sending it off to a potential player, I finally had to check both paths and either one will find the file.

Code: Select all

const irr::io::path media_folder_exe = "media/";
const irr::io::path media_folder_vs2022 = "../../bin/media/";

// load texture
// first try in 2022
irr::video::ITexture* img_square = driver->getTexture(media_folder_vs2022 + "water.jpg");
if (img_square == 0) std::cout << "Checked " << media_folder_vs2022.c_str() << ". image not found. Checking bin folder...";
// try with executable
if(img_square == 0) img_square = driver->getTexture(media_folder_exe + "water.jpg");
if(img_square == 0) std::cout << "Checked " << media_folder_exe.c_str() << ". Media. still not found.";
I saw Irrlicht outputs the exes into bin/Win, and also gives us the examples in examples/06.2DGraphics.

If Irrlicht checks for the media folder - from both exe and main.cpp - ../../media are both true!
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: load file with source file and output exe

Post by CuteAlien »

Yes, kinda the reason why Irrlicht has that folder structure I think ;-)
In svn trunk we switched to a tool function called getExampleMediaPath.

But basically paths always depend on the working directory you are in at the moment you call a file-function. Independent of library or programming language - that's an OS thing. In Windows for example users can even set the path a program starts at in desktop links.

The trick is usually - in your development environment just make sure your data is found. Doesn't matter how. If you are using Visual Studio you can for example also set the working directory in Configuration Properties of your project (Debugging - Working Directory). Or same trick with Irrlicht - use a path relative to the project files and relative to executable (works mostly).

Once you deliver to users things get more complicated. Simplest - assume working directory is where your .exe is and use relative paths to that. Won't always be correct (like when users set another working directory in links), but usually is and when not you can blame the user (hehe). This is used a lot when distributing apps simply as zip. You can also make it a bit safer by finding your .exe first on program start. On windows GetModuleFileName can be used for that, on Linux people tend to use start-scripts.

Other option is to use an installer and ensure your media is installed in folder you know. Most installer tools allow to write that path in the registry which you can read out on program start. Many games do that. Or download the media automatically from the web and save it to a certain folder. On Windows maybe a sub-folder of local appdate (which you can find with getSpecialFolder((CSIDL_LOCAL_APPDATA)). And config data often written to a sub-folder of "My Documents " which you can find with getSpecialFolder(CSIDL_MYDOCUMENTS) (again only on Windows, Linux has differnt folders which sometimes even depend on the distribution).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thenewkgb
Posts: 59
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: load file with source file and output exe

Post by thenewkgb »

I'm fairly new to Visual Studio. I managed first to compile with VS Code. Then I was stuck between Studio or Code for projects.

I see the working directory now, but a lot of the macros are solution/project/studio specific. I'll have to play around with that setting.

For my inspiration I browsed images of CD directories years ago (some Steam) and see they are simple. On the CD usually the exe is front and foremost and the folders hold the data. As long as you shortcut to the exe it should be good.

Registry stuff will be for a later date. I've always been told, "Don't mess with the registry until you absolutely know what you are doing!!!"
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: load file with source file and output exe

Post by CuteAlien »

Yeah, worry about it later. For a start just make sure your media-path is variable or function and not hardcoded all over the place. Then you can do things like: mediaPath + "some_folder/files" and fixing it later means only modifying a single place.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
thenewkgb
Posts: 59
Joined: Thu Jan 25, 2024 6:54 pm
Location: UK

Re: load file with source file and output exe

Post by thenewkgb »

Hmm. Magic constants. I also like to initialise objects then see a lot of people not doing so. Thank Bjarne. I still don't understand pointers enough to keep everything safe.

The working directory worked (magic it is). I targeted the bin folder and now both x64 and 2022 find the file.

To note, I didn't need to change to Debug mode.
Post Reply