Monday, November 13, 2017

c++ - How to find resources location regardless of working directory?


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.



Answer



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");.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...