• Advertisement
Sign in to follow this  

std::map and d3dtexture

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

i am trying to make a resource manager so i dont load a texture twice if it is used by two sprites. Is this the way to use std::map?
#ifndef RESOURCEMANAGER_H_HEADER_INCLUDED_BDC27945
#define RESOURCEMANAGER_H_HEADER_INCLUDED_BDC27945
#include "D3DCommon.h"
#include <map>
#include <string>

//##ModelId=423DCD8D0000
class ResourceManager : public D3DCommon
{
public:
	IDirect3DTexture9 *getTexture(char *filename);
private:
	std::map <std::string, IDirect3DTexture9*> textures;

};



#endif /* RESOURCEMANAGER_H_HEADER_INCLUDED_BDC27945 */

#include "ResourceManager.h"

IDirect3DTexture9 *ResourceManager::getTexture(char *filenamee){
  IDirect3DTexture9 *tmp;
	// if it doesnt exist create it
	if( textures.find(filename) == textures.end()){
  	if( D3DXCreateTextureFromFile( p_d3ddevice, filename, &tmp ) != D3D_OK ){
	  	MessageBox(hWnd, filename, "Could not load texture", 0);
		  return NULL;
	  }
		textures[filename] = tmp;
	// otherways return the one we have
	}else{
		return textures[filename];
	}
	return tmp;
}


Share this post


Link to post
Share on other sites
Advertisement
That looks reasonable. However, if speed is important, use a map iterator to store the result of your find() call, and dereference that in the else clause to save having to look up the texture a second time.

Share this post


Link to post
Share on other sites
does the char *filename automatically made into std::string there or do i have to do something? it doesnt work very well, just wondering if the problem is in here or my d3d code...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement