# [SDL] SDL_image.h

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

## Recommended Posts

Hello.

What should I modify in my image loading function to be able to load from a specified path. If the .exe is located in Game folder, I'like to load from gfx folder, which is located in C:\Game\gfx .
SDL_Surface *load_image(std::string filename) { SDL_Surface *loadedImage = NULL; SDL_Surface *optimizedImage = NULL; loadedImage = IMG_Load(filename.c_str()); //I was thinking at this line if (loadedImage != NULL) { optimizedImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface (loadedImage); if (optimizedImage != NULL) { SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF)); } } return optimizedImage; }

Thx for help.

##### Share on other sites
You can just pass the whole path to IMG_Load.
IMG_Load("C:/Game/gfx/" + filename);

##### Share on other sites

You can just pass the whole path to IMG_Load.
IMG_Load("C:/Game/gfx/" + filename);

Are you sure that I shoud use the + . And what about .c_str() after filename? Is not working like you said

##### Share on other sites
Maybe this works betterIMG_Load(("C:/Game/gfx/" + filename).c_str());
My point is that you can pass the full (or relative) path to IMG_Load and not just the name of the file.

You might not want to hardcode the path because that will not work if the game is located at a different location than C:/Game. Using relative paths you have the problem that they depend on the working directory so you will have to take that into account.

##### Share on other sites
You can use + it will work fine. When loading my images from a folder I use it all the time. An example from my sprite creator which loads everything from folders.
 private String ImageLocation = "/Sprites/"; ... MaleBodyNone = getImage(this.getClass().getResource(ImageLocation + "Male/Base/NoneBody.gif")); 
This is also acceptable
 MaleBodyNone = getImage(this.getClass().getResource("/Sprites/Male/Base/NoneBody.gif")); 
Both do the same thing.

How I have it I can just change the variable "ImageLocation" instead of changing 30 lines if I decide to change the folder where the sprites are loaded from.

##### Share on other sites
@0Circle0, This will not work in C or C++ ("/Sprites/" + "Male/Base/NoneBody.gif"). Is your code Java?

##### Share on other sites

Hello.

What should I modify in my image loading function to be able to load from a specified path. If the .exe is located in Game folder, I'like to load from gfx folder, which is located in C:\Game\gfx .

When running your game within the IDE, be sure the working directory is set to the path you expect it to be. For instance, under Visual Studio, the working directory would be the project directory ("C:\Game\msvc10\").

##### Share on other sites

@0Circle0, This will not work in C or C++ ("/Sprites/" + "Male/Base/NoneBody.gif"). Is your code Java?

The example is Java yes, perhaps that one line was a bad example removed.

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002142
×