Page 1 of 1

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

Posted: Sat May 02, 2009 9:13 pm
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[])
{

Re: another question, images in folders relative to executable

Posted: Sat May 02, 2009 11:17 pm
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.

Re: another question, images in folders relative to executable

Posted: Sun May 03, 2009 12:42 am
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. =)

Re: another question, images in folders relative to executable

Posted: Sun May 03, 2009 2:16 am
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.

Re: another question, images in folders relative to executable

Posted: Sun May 03, 2009 3:39 am
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 =)

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

Posted: Sun May 03, 2009 6:00 am
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 :)

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

Posted: Sun May 03, 2009 6:20 pm
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"

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

Posted: Sun May 03, 2009 9:20 pm
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: