[solved] getting the directory of my executable on win XP

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

[solved] getting the directory of my executable on win XP

Post by short »

Basically, I was hoping to sort of keep my files sorted instead of having them all in the same folder as my executable, but referencing files in sub folders relative to my executable has proven difficult.

Code: Select all

// DEFINES
#define IMAGE_BACKGROUND "\\content\\images\\background.bmp"
#define FONT_MAIN "\\content\\fonts\\sai.ttf"
I read supposedly args[0] or something is supposed to be my path? Anyone care to elaborate a little more? :worship:

Code: Select all

int main(int argc, char* args[])
{
Last edited by short on Sun May 03, 2009 3:44 am, edited 2 times in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Spikey
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 98
Joined: Sat Dec 13, 2008 6:39 am
Programming Language of Choice: C++
Location: Ottawa, Canada
Contact:

Re: another question, images in folders relative to executable

Post by Spikey »

Just defining a path and then calling it should work. I'm not sure what you're trying to do with the main args.

#define IMAGE_BACKGROUND "\\content\\images\\background.bmp"
....
LoadImage ( IMAGE_BACKGROUND );

Always worked for me. IMAGE_BACKGROUND path will always use the executable's current path before hand.

[EDIT]
Also note, depending on the function parameters, you might even have to pass a Unicode path, ie:
#define IMAGE_BACKGROUND L"\\content\\images\\background.bmp"
or
#define IMAGE_BACKGROUND TEXT("\\content\\images\\background.bmp")

hmm, I just thought now, I'm not sure L or TEXT are windows dependent or not.... :S
In that case I believe there is a way in the project properties to make it use unicode instead.

[EDIT 2]
ya I just found your other post, where you mentioned using SDL. I wouldn't have gone so in depth if I knew that. I haven't used SDL much, oh well, I hope it's still applicable, oh and Welcome, nice to see more game development enthusiasts.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: another question, images in folders relative to executable

Post by short »

unfortunately, it doesn't work. I tried loading the image with my constant, but the only way it works is by hard coding the entire directory. Which, is obviously not what I want. I am just trying to find the directory the executable is in, and then go off to my sub folders, like I "think" I saw in the "Adventures of Game Development" videos.

As for args[0], when my program gets called the "working directory" is supposedly passed as args[0], but is not necessarily the path I am looking for.

edit: I added the following code:

Code: Select all

string s = args[0];
and the value of s after that line is the entire path to my executable. =)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Spikey
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 98
Joined: Sat Dec 13, 2008 6:39 am
Programming Language of Choice: C++
Location: Ottawa, Canada
Contact:

Re: another question, images in folders relative to executable

Post by Spikey »

When you debug your program IMAGE_BACKGROUND will display as "\\content\\images\\background.bmp", but when the program compiles it actually includes the entire path. Something like this: "C:\Game\content\images\background.bmp", but you will never see that while debugging.

I think you are trying to do something like this:
http://www.indigorose.com/forums/showthread.php?t=8113
and http://elysianshadows.com/phpBB3/viewto ... art=999999
But that's something to do when you use something like Lua to help load files. You don't have to use lua, it's one of those things that helps teams and large projects in the long run.

If you still want the current directory in a string, another way is with fstream.h, that handles reading and writing files, but usually don't use that for graphics. Could for like save files, high scores, configuration, or something like that. But still you wouldn't really need the working directory in a string.
http://www.velocityreviews.com/forums/t ... ctory.html

EDIT
Sometimes when functions ask for wide chars, you will need forward slashes instead of back slashes.
#define IMAGE_BACKGROUND "content/images/background.bmp"

hm also, did you try something like
"content\\images\\background.bmp"
without the first to two slashes.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: another question, images in folders relative to executable

Post by short »

I actually got it to work, I'll post the code for anyone interested =)

Code: Select all

// DEFINES
#define IMAGE_BACKGROUND "\\content\\images\\background.bmp"
#define FONT_MAIN "\\content\\fonts\\sai.ttf"
#define EXECUTABLE_NAME_STRING_LENGTH 13 // how many characters are in the executable name


/* GLOBAL VARIABLES */
	 string globalString;

//load files function, notice it uses the two functions from below to get the file names

Code: Select all

bool load_Files(char* args[])
{
	 //Load the images 
	 background = load_image( getStringFileName(args, IMAGE_BACKGROUND)); 

	 //Open the font 
	 font = TTF_OpenFont( getFontFileName(args, FONT_MAIN), FONT_SIZE ); 

	 // Check for error loading font.
	 if( font == NULL ) 
	 { 
		  return false;
	 } 
	 return true;
}
//two called functions I use

Code: Select all

string getStringFileName(char* args[], string s)
{
	 globalString = args[0];
	 globalString.erase(globalString.length()-(EXECUTABLE_NAME_STRING_LENGTH+6)); //the +1 is for the / in the directory we add in, 6 for the debug folder.
	 globalString.append("Schmetris");
	 globalString.append(s);

	 return globalString;
}

const char * getFontFileName(char * args[], string s)
{
	 globalString = args[0];
	 globalString.erase(globalString.length()-(EXECUTABLE_NAME_STRING_LENGTH+6)); //the +1 is for the / in the directory we add in, 6 for the debug folder.
	 globalString.append("Schmetris");
	 globalString.append(s);
	 return (const char *) globalString.c_str();
}
edit: Thank you to Spikey for the help =)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: [solved] getting the directory of my executable on win XP

Post by fantastico »

Uhm, looks pretty complicated. First thing I noticed in your initial post is that "\\content\\images\\background.bmp" is not a relative path, at least it wouldn't be in linux and I'm pretty sure it neither is in windows. If you want a relative path leave out the leading backslashes or write it like that: ".\\content\\etc" (the dot stands for the current working directory).

edit: Just noticed Spikey already wrote that :)
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: [solved] getting the directory of my executable on win XP

Post by MarauderIIC »

On a technical note without really looking at the code, windows supports forward slashes and backslashes and linux supports forward slashes only pretty much, so forward slashes are "safer"
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: [solved] getting the directory of my executable on win XP

Post by short »

Code: Select all

// DEFINES
#define IMAGE_BACKGROUND ".\\content\\images\\background.png"
#define IMAGE_BLUEBLOCK ".\\content\\images\\blueblock.png"
#define FONT_MAIN ".\\content\\fonts\\sai.ttf"
Worked. Thanks, I can get rid of those functions now. I had no idea the . represented the current working path, thanks :lol:
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply