SDL_Surfaces in map?

Hi everyone. I’m pretty new to both C++ and SDL and i’m trying to make simple game engine. So,what i want to do is make C++ map object and store SDL surfaces in it.

When i use something like this:

Code:

SDL_Surface *load_image( std::string filename )
{
blablabla
return image;
}

sdlsurfaces[“surfacename”] = *load_image(“fsaasfgsa”);

I get an error in last line showing me that i cant load image in that map object. My map object is type of <string,SDL_Surface>.

Any ideas how i can do this propertly?

Cheers, Igor

:smiley:

You didn’t show us the error, nor did you show us the type. This is just a
guess, you can get better responses by including more information with your
posts.

You probably shouldn’t be dereferencing the pointer returned by load_image
when storing it in the map. The map should be of type std::map<std::string,
SDL_Surface*> (or possibly a smart pointer such as boost::shared_ptr or
std::tr1::shared_ptr).

HTH,

– BrianOn 21 March 2010 19:20, igorpan wrote:

Hi everyone. I’m pretty new to both C++ and SDL and i’m trying to make
simple game engine. So,what i want to do is make C++ map object and store
SDL surfaces in it.

[quote=“igorpan”]Hi everyone. I’m pretty new to both C++ and SDL and i’m trying to make simple game engine.
[/code]

If you are new to c++ maybe it’s better to start with something easier than game programming

anyway here http://www.lazyfoo.net/SDL_tutorials/index.php you can find good tutorials about SDL

MandarX------------------------
http://mandarx.xoom.it/index.php?lang=eng

Hi everyone. I’m pretty new to both C++ and SDL and i’m trying to make simple game engine. So,what i want to do is make C++ map object and store SDL surfaces in it.

When i use something like this:

Code:

SDL_Surface *load_image( std::string filename )
{
? ? ? blablabla
? ? ? return image;
}

sdlsurfaces[“surfacename”] = *load_image(“fsaasfgsa”);

I get an error in last line showing me that i cant load image in that map object. My map object is type of .

Any ideas how i can do this propertly?

Cheers, Igor

I did this in a game I wrote with SDL.

Assuming you’ve got the following two containers:

std::map<const char*, SDL_Surface*> image_cache;
std::vector<const char*> image_list;

where image_list is a list of the image names

typedef std::vector<const char*>::const_iterator Iter;
for (Iter it = image_list.begin(); it != image_list.end(); ++it) {
std::string path = datadir + it;
SDL_Surface
temp = IMG_Load(path.c_str());
if (temp == NULL) {
free_images();
std::ostringstream s;
s << "Unable to load image file: " << it << ‘\n’;
throw s.str();
}
SDL_Surface
image = SDL_DisplayFormatAlpha(temp);
image_cache[*it] = image;
SDL_FreeSurface(temp);
}
}

free_images is a function which will free any images already loaded in
case something goes wrong and path is the full path to the image.

The source for the game “Classic Invaders” I used this in is here
http://www.noquarterarcade.com/system/files/Source.zip

Todd SteinackleOn Sun, Mar 21, 2010 at 3:20 PM, igorpan wrote:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Todd Steinackle

Yeah,i’m trying to do very similar thing [Wink]