Sign in to follow this  
RaduIulianGabriel

[SDL] SDL_image.h

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 .
[code]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;
}[/code]


Thx for help.

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1313168851' post='4848321']
You can just pass the whole path to IMG_Load.
[code]IMG_Load("C:/Game/gfx/" + filename);[/code]
[/quote]


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

Share this post


Link to post
Share on other sites
Maybe this works better[code]IMG_Load(("C:/Game/gfx/" + filename).c_str());[/code]
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 this post


Link to post
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.
[code]
private String ImageLocation = "/Sprites/";
...
MaleBodyNone = getImage(this.getClass().getResource(ImageLocation + "Male/Base/NoneBody.gif"));
[/code]
This is also acceptable
[code]
MaleBodyNone = getImage(this.getClass().getResource("/Sprites/Male/Base/NoneBody.gif"));
[/code]
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 this post


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

Share this post


Link to post
Share on other sites
[quote name='Mast3r67' timestamp='1313168692' post='4848318']
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 .[/quote]

If your exe is under "C:\Game", then you should your resources like so: load_image( "./gfx/image.png" );
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 this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1313470348' post='4849705']
@0Circle0, This will not work in C or C++ ("/Sprites/" + "Male/Base/NoneBody.gif"). Is your code Java?
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this