• Advertisement
Sign in to follow this  

Errorcheck in SDL_LoadBMP()

This topic is 4618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement