[SDL] SDL_image.h

Started by
6 comments, last by BrianJensen 12 years, 8 months ago
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.
Advertisement
You can just pass the whole path to IMG_Load.
IMG_Load("C:/Game/gfx/" + filename);

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

Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.

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

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 .


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\").

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

Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.

This topic is closed to new replies.

Advertisement