Sign in to follow this  
Mizipzor

Errorcheck in SDL_LoadBMP()

Recommended Posts

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 {
		temp << "Loading texture " << filename;
		writeLog(temp.str());
		return true;
	}

	/*
	if(Bmp.find(filename) != Bmp.end()) {	// if it did load successfully, return true
		temp << "Loading texture " << filename;
		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 this post


Link to post
Share on other sites
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 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