Sign in to follow this  
Xirion

C++ Relative Paths

Recommended Posts

Good day.

The structure of my proyect is:[list]
[*]POOA
[list]
[*]data
[*]src
[*]vcproj
[list]
[*]holy
[/list]
[/list]
[/list]
So my Visual Studio solution is in the folder "holy" and i need to use an image wich is in
the "data" folder so i try to use something like ..\\data\\ tiles.png but it's no use, how can i
accomplish this

SDL_surface *tile_sheet;
tile_sheet = load_image( "..\\data\\ tiles.png" );

Im using C++ and SDL Edited by Xirion

Share this post


Link to post
Share on other sites
You can replace the \\ with /, and your paths will work fine on Windows as well as Linux/Mac (That's not the cause of your problem though). Consistency is good.

Your path in the first post is:
[i]load_image( "..\\data\\ tiles.png" );[/i]

If you copied and pasted that from your actual code, then the problem might be that you have a space between "data\\" and "tiles.png".
[i]load_image( "..\\data\\[b][color=#ff0000]<space>[/color][/b]tiles.png" );[/i]

Share this post


Link to post
Share on other sites
Each '..' moves up one folder. So if your path is:
[i]"POOA/vcproj/holy/debug/game.exe"[/i]

And you want to get to:
[i]"POOA/data/tiles.png"[/i]

You need:
[i]"../../../data/tiles.png"[/i]

(one '..' for each of: "vcproj/holy/debug")

However, more practically, your game isn't always going to be in the debug folder. If you release your games to other people, you'll more likely not want it in a folder labeled "debug".
So, you should tell Visual Studio to output the generated executable into a folder more to your liking. I suggest a 'bin' folder, right next to the 'data' and 'src' folders, as it is commonly named 'bin' (for 'binaries', which is what .exes and .DLLs are).

Once you tell Visual Studio to always put the executable in your 'bin' folder, each time you compile your project it will end up there. Then your path would just be [i]"../data/tiles.png"[/i]

I don't use Visual Studio personally, so I can't tell you how to redirect the exe to a different folder, but I'm sure it can do it (most IDEs can). Edited by Servant of the Lord

Share this post


Link to post
Share on other sites
Under the properties for your VStudio project: Configuration Properties->General->Output Directory is where your project will ultimately compile to. Then just make sure all your assets exist in that directory and you can use relative paths as you were.

Share this post


Link to post
Share on other sites
Basically it doesn't matter where your binary ends up, as long as you start it from the debugger. The only thing that matters is what you configured as your working directory. Once you want to run it manually, you have to make sure the binary is located in that same directory.

Considering that your binary will most likely be in POOA when you're done, you may want to change the debugger settings to use that as your working directory (something like $(ProjectDir)/..) and stick with "data/tiles.png" as your path. Edited by Trienco

Share this post


Link to post
Share on other sites
You don't.

We don't use the notion of "solved" topics here, someone might always chime in a little later with an extra information. Just because the OP thinks they have an answer, doesn't mean that it is the best answer.

The [url="http://www.gamedev.net/index.php?app=forums&module=forums&section=rules&f=31"]Forum FAQ[/url] in fact specifically says not to do so.

Share this post


Link to post
Share on other sites
[quote name='Xirion' timestamp='1348147380' post='4982014']
@Servant of the lord: the space was because if i don't use that space my letter "t" was erased.
[/quote]
' ' is an escape code for tab, in the same way '\n' is an escape code for a new line.
If you are using '\\' that shouldn't happen, and if you are using '/' it definitely won't happen.

Share this post


Link to post
Share on other sites
You hit on something very important here, and this is ultimately the source of your problems.

In C++, literal string values use escape codes to present special characters.

For example \n is used in place of a newline, so:

"Hello\nWorld"

When evaluated becomes:
Hello
World

While you escape the escape character ( \ ) using predictably enough, the escape character, so if you wanted to say:

Hello\World

You would go:
"Hello\\World"

'\ ' is such an escape code ( it is how you encode a TAB ).

This is why you should use "/" for your path values, such as:

c:/my/path/somewhere

This has the advantage of being portable to every other non-DOS based OS under the sun as well.


Edit: Ironically \ + t is how you encode a tab on this site aswell apparently. Edited by Serapth

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this