Jump to content
  • Advertisement
Sign in to follow this  
Xirion

C++ Relative Paths

This topic is 2196 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Good day.

The structure of my proyect is:

  • POOA

    • data
    • src
    • vcproj

      • holy



        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
Advertisement
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?

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:
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\\[color=#ff0000]<space>tiles.png" );

Share this post


Link to post
Share on other sites
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

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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!