Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ Relative Paths


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Xirion   Members   -  Reputation: 243

Like
0Likes
Like

Posted 19 September 2012 - 04:03 PM

Good day.

The structure of my proyect is:
  • POOA
  • data
  • src
  • vcproj
  • holy
[/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, 19 September 2012 - 04:06 PM.


Sponsor:

#2 swiftcoder   Senior Moderators   -  Reputation: 9637

Like
0Likes
Like

Posted 19 September 2012 - 04:10 PM

You need to use a path relative to the generated executable, not the project itself. What directory does your executable end up in when you build?

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#3 Xirion   Members   -  Reputation: 243

Like
0Likes
Like

Posted 19 September 2012 - 04:16 PM

vcproj/holy/debug

i tried using: "..\\..\\data/tiles.png" and it seems to work now, but is this correct or is there a better way?

#4 Servant of the Lord   Crossbones+   -  Reputation: 17239

Like
1Likes
Like

Posted 19 September 2012 - 04:23 PM

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:
load_image( "..\\data\\ tiles.png" );

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".
load_image( "..\\data\\<space>tiles.png" );

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#5 Servant of the Lord   Crossbones+   -  Reputation: 17239

Like
4Likes
Like

Posted 19 September 2012 - 04:30 PM

Each '..' moves up one folder. So if your path is:
"POOA/vcproj/holy/debug/game.exe"

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

You need:
"../../../data/tiles.png"

(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 "../data/tiles.png"

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, 19 September 2012 - 04:31 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#6 greenvertex   Members   -  Reputation: 510

Like
3Likes
Like

Posted 19 September 2012 - 04:45 PM

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.

#7 Trienco   Crossbones+   -  Reputation: 2055

Like
1Likes
Like

Posted 19 September 2012 - 10:24 PM

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, 19 September 2012 - 10:25 PM.

f@dzhttp://festini.device-zero.de

#8 Xirion   Members   -  Reputation: 243

Like
0Likes
Like

Posted 20 September 2012 - 07:23 AM

Thank you all for the tips, i'm sure i'll be using them from now on.
@Servant of the lord: the space was because if i don't use that space my letter "t" was erased.

#9 Xirion   Members   -  Reputation: 243

Like
0Likes
Like

Posted 20 September 2012 - 07:24 AM

another question though, how do i mark this topic as SOLVED?

#10 rip-off   Moderators   -  Reputation: 7691

Like
2Likes
Like

Posted 20 September 2012 - 08:03 AM

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 Forum FAQ in fact specifically says not to do so.

#11 Xirion   Members   -  Reputation: 243

Like
0Likes
Like

Posted 20 September 2012 - 09:20 AM

Thank you, it's good to know.

#12 Servant of the Lord   Crossbones+   -  Reputation: 17239

Like
0Likes
Like

Posted 20 September 2012 - 12:03 PM

@Servant of the lord: the space was because if i don't use that space my letter "t" was erased.

' ' 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.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#13 Serapth   Crossbones+   -  Reputation: 5182

Like
1Likes
Like

Posted 20 September 2012 - 12:17 PM

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, 20 September 2012 - 12:19 PM.


#14 Xirion   Members   -  Reputation: 243

Like
0Likes
Like

Posted 21 September 2012 - 07:42 AM

sorry I do not think I expressed myself well, i meant that the letter "t" dessapeared but not in my code but in the post.
sorry for the confusion




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS