## Recommended Posts

Mizipzor    247
Ive got this function in my class where I want to load two bmp's. But I cant come up with a good way to errorcheck if the image was loaded or not. I load four files, only two of them actually exists (the other two just junknames that I came up with) to make the program do that errorcheck. I tried two ways (the older one is commented out) but neither of them seems to work.
bool Sdl_handle::LoadBmp(std::string filename) {
std::stringstream temp;	// a stringstream to hold what we want to write to the log

Bmp[filename] = SDL_LoadBMP( ("Bmp\\" + filename + ".bmp").c_str() );	// attempt to load the texture

if(Bmp.find(filename) == NULL) {	// if it did load successfully, return true
temp << "Loading texture " << filename << " FAILED" << SDL_GetError();
writeLog(temp.str());
return false;
} else {
writeLog(temp.str());
return true;
}

/*
if(Bmp.find(filename) != Bmp.end()) {	// if it did load successfully, return true
writeLog(temp.str());
return true;
} else {
temp << "Loading texture " << filename << " FAILED" << SDL_GetError();
writeLog(temp.str());
return false;
}
*/
}


Bmp is a std::map std::map <std::string, SDL_Surface*> Bmp; // holds all the texture Is there a better way to do what Im trying to do? Like check if the file exists rather than check whats in the map after Ive tried to load it?

##### Share on other sites
Perost    332
This seems to be the correct way to check if the image was loaded or not:
 if((Bmp[filename] = SDL_LoadBMP(filename.c_str())) == NULL)

And also note that Bmp.find() will return an iterator, and will never be NULL. If you want to check if a value exists in your map you can do Bmp.find(filename) != Bmp.end(), and if you want to check the value you do Bmp[filename] == NULL.

##### Share on other sites
Mizipzor    247
Thanks, now it works the way I want it to.