3
\$\begingroup\$

I am working on a resources loader for my personal C++/OpenGL ES engine. My resources are in a resources folder separated trough different sub-folders (shaders, textures, etc).

I am currently giving a path to this folder when I want to load a file. My code looks like this:

texture.load("../../resources/textures/dirt.png");

The problem is - in addition to not be gracious - it depends of the current working directory and of the final binary location when I execute it.

How can I store the complete path to the resource when I call the load function to be able to load it regardless of working directory? I would like to be able to use the following line:

resourcesManager.loadTexture("dirt.png");

Regardless of where I am compiling nor executing my game, as long as I told to the texture manager where is the resources folder.


I tried to find a solution using __FILE__ macro, because I can get the complete path to the compiling file with it, but it would be the path to the source file, not the path to the binary.

I'm trying to make it cross-plateform, so I would prefer a solution not working through an external editor or IDE.

\$\endgroup\$
  • 1
    \$\begingroup\$ what language, API, platform? \$\endgroup\$ – Fuzzy Logic Sep 25 '15 at 4:00
  • \$\begingroup\$ Please clarify your question for other readers so they know what you mean by path to binary, given our discussion in the comments of my answer. It would also be helpful to know what editor you are using in this case, to be able to provide a more specific answer. \$\endgroup\$ – Fuzzy Logic Sep 25 '15 at 6:11
  • \$\begingroup\$ @FuzzyLogic It is done. For editors, I use emacs on Linux and Visual Studio 2015 on Windows, but I'm trying to make my engine works on both, so I wouldn't like a solution using one of them. \$\endgroup\$ – Aracthor Sep 25 '15 at 6:18
  • \$\begingroup\$ Is this really game-development specific? It sounds like a problem you might have with any C++ application. \$\endgroup\$ – Anko Sep 30 '15 at 14:44
  • \$\begingroup\$ stackoverflow.com/questions/1023306/… \$\endgroup\$ – winduptoy Jun 5 '19 at 20:20
3
\$\begingroup\$

You can create a separate variable called dataPath which gets initialized on applications launch and stores your data/resources path root. If you plan that resources can be located in different locations - just pick the right one and store it. What you get in dataPath is, for example:

C:\Program Files\MyGameStudio\MyGame\bin\

sdcard\0\game\game\resources\

or just anything else.

After that it is simple resourcesManager.loadTexture(dataPath + "textures/dirt.png");.

\$\endgroup\$
  • \$\begingroup\$ That is what I planned to do, but how to find this dataPath from scratch? \$\endgroup\$ – Aracthor Sep 25 '15 at 6:50
  • \$\begingroup\$ All my resources are located either relative to exe path (find exe path from args[0]) or on SDCard (use folder I put them to). What seems to be the problem in your case? \$\endgroup\$ – Kromster Sep 25 '15 at 6:52
  • \$\begingroup\$ @KromStem I am worrying about what would happen if the executable itself would be moved from its folder. Is that an error itself to worry about it ? \$\endgroup\$ – Aracthor Sep 25 '15 at 6:54
  • \$\begingroup\$ This is quite unlikely and most games don't account for that. But if this is likely in your case, you can write the data path after the installation into registry/ini and read it from there. \$\endgroup\$ – Kromster Sep 25 '15 at 7:02
  • \$\begingroup\$ I think I spent to much time on a console development environment to think like a common gamer would do... That's why I was wondering about it. Thanks for your help. \$\endgroup\$ – Aracthor Sep 25 '15 at 7:04
1
\$\begingroup\$

In C++, you can get the path of the binary with the argv[0] parameter that is passed to the main() function.

You will need to define main() as...

int main(int argc, char* argv[])

You will want to store argv[0] somewhere, so you can reference it outside of main().

Then you can prepend it to your relative paths to build the full path to your resources.

\$\endgroup\$
  • \$\begingroup\$ This works if I execute a binary from any folder. But I would like more: to work wherever the executable is created. \$\endgroup\$ – Aracthor Sep 25 '15 at 5:27
  • \$\begingroup\$ Do you mean you want it relative to your project's build directory? You will need to either use preprocessor defines (simple but could break if your project structure changes) or else create a build script that packages your binary project for you before it is executed (more robust). Most IDE's have some type of post-build event where you can add your script, or even script directly in the event. \$\endgroup\$ – Fuzzy Logic Sep 25 '15 at 5:35
  • \$\begingroup\$ so yes, I'm more interested by the second option that you quoted. The first one was what I was thinking with __FILE__, but my code structure shall probably change many times with refactoring. \$\endgroup\$ – Aracthor Sep 25 '15 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.