Jump to content
  • Advertisement
Sign in to follow this  

std::map and d3dtexture

This topic is 4954 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?
#include "D3DCommon.h"
#include <map>
#include <string>

class ResourceManager : public D3DCommon
	IDirect3DTexture9 *getTexture(char *filename);
	std::map <std::string, IDirect3DTexture9*> textures;



#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
		return textures[filename];
	return tmp;

Share this post

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

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!