Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sirSolarius

Map troubles

This topic is 5335 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

Ok, so I had this idea for storing my texture values. Every object is derived from an abstract base class, called Object. So, I made a static map called "g_textures" which would (in theory) take a filename as a key, and return the OpenGL texture # of it. This way, I wouldn''t have to waste time reloading textures that were already stored in memory. The problem is that map is messing with all of my keys. Whatever texture name I put in, it gives me the first texture I stuck into the map. Code follows.
(from the Object .h)
struct ltstr
{
	bool operator()(const std::string &str1, const std::string &str2) const
  {
    return (str1 == str2);
  }
};

static std::map<std::string, CImage, ltstr> g_textures; // Map of texture names & their values

(from the Object .cpp)
std::map<std::string, CImage, ltstr> Object::g_textures;		// List of texture numbers

MD3 model texture loading
void CreateTexture(UINT &texture, LPSTR strFileName)
{
	// FORMAT= Data/Meshes/MODEL/FILE.EXT

	std::string fileName=strFileName;
	fileName=fileName.substr(12);

	if(Object::g_textures.find(fileName) != Object::g_textures.end()) // if texture is loaded

	{
		texture=(Object::g_textures[fileName]).getID();
		return;
	}
	else
	{
		CImage tex;
		tex.Load(strFileName);
		Object::g_textures[fileName]=tex;
		texture=tex.getID();
	}
}
void Object::addTexture(std::string texFile)
{
	// First, check if the texture exists

	if(g_textures.find(texFile) != g_textures.end())
	{
		textureNumber=g_textures[texFile].getID();		// if so, grab the stored texture #

		return;
	}
	else						// if not

	{
		// Make the full path for loading

		texFile.insert(0, "Data/Textures/");
		CImage newTex;
		if(!newTex.Load((char*)texFile.c_str()))		// Load the new texture

		{
			MessageBox(NULL, texFile.c_str(), "Error Loading Texture File!!   .", MB_OK);
			PostQuitMessage(0);
		}
		g_textures[texFile]=newTex;	// Add the image to the map

		textureNumber=newTex.getID();	// Set the object''s texture ID

	}
}
Yeah, so I follow my debugger and it goes like so: the first time an MD3 model is loaded, everything is perfect, and every texture runs through the loading script. If I load another model, however, "stormtrooper/body.tga" is found, "stormtrooper/armor.tga" isn''t (and it reloads), "stormtrooper/helmet.tga" is, "railgun/railgun.bmp", railgun/railgun2.bmp"... etc. up to railgun4 are reloaded and not reused. So basically, my question is: what the heck? Why are some things being found, but others not?

Share this post


Link to post
Share on other sites
Advertisement
I''m going to guess the problem is that your ltstr function is returning true when the strings are equal. std::map expect that the sorting criteria be an ordering relationship, not an equality relationship. Try changing ltstr::operator() to return (str1 < str2) and see if that works.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!