• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Conny14156

return std::map Question[Solved]!

7 posts in this topic

Hi,

I just notice something very wierd today, when I try to return the std::map<string,ID3DResourceView*> value with help from map[std::string] I get the ID3D11ResourceView* pointer, but that only works when I return it two example I have in my code 

//While this work
return TotalTexture[textureName];


/*
This wont, Both is identical std::map<string,ID3D11ShaderResourceView*>, but the diffrent
is that this code down here insert the string instead with a null ID3D11ShaderResourceViewpointer. And I know its that line cause 
*/
ID3D11ShaderResourceView* pt;
pt = Model->Texture.Diffuse.Texture[Model->lastMaterial];	
Gfx->D3d11DevCon->PSSetShaderResources(0,1,&pt);//<---- Cause the next line here the map 
//had changed with one extra material with a null pointer. 

 

 

0

Share this post


Link to post
Share on other sites

Sorry, could you rephrase your problem, because honestly I can't seem to understand what is it about. :) Attached code doesn't make any sense too, try to put here some meaningful testcase.

1

Share this post


Link to post
Share on other sites

operator[] is a read + write operation. If the specified key doesn't exist, then it is inserted into the map.

If you want to look up an item that might be in the map, without adding it in cases where it doesn't exist, then use find instead of [].

2

Share this post


Link to post
Share on other sites

Oh, Sorry It was late at night >.< (Around 4 in the morning XD) Let me try it again.

 

What am trying to do is to avoid loading more than one of each texture file, so I made a std::map wtih the texture name as the key and the value would be a ID3D11ShaderResourceView* that contain the texture.

 

Each time I "Load" a model with materials it would check if the material would already exist inside the map, to avoid loading multipile  time of the same texture.

if the texture already had been loaded i would get it by calling RequestTexture which looks like this


ID3D11ShaderResourceView* GfxHandler::RequestTexture(std::string textureName)
{
    if(TotalTexture.find(textureName) != TotalTexture.end())
    {
        MessageBox(0,"Request Granted",textureName.c_str(),MB_OK);
        TotalTextureCounter[textureName]++;
        return TotalTexture[textureName];
    }
    else
    {
        MessageBox(0,"Request Failed",textureName.c_str(),MB_OK);
    }    
    return false;
}

 

Where the TotalTexture and TotalTexture looks like

std::map<std::string,ID3D11ShaderResourceView*> TotalTexture;
std::map<std::string,int> TotalTextureCounter;

 

The ReqeustTexture returns a shader resource which I assign and insert into a seperate class which I would call the "Model", the model class have its own map which looks like the same except the only texture that gets inserted there would be the materials that the model use, the reason why I try to do this is to avoid unnecessary looping inside the bigger map.

 

This is how I do it inside my LoadModel

Its works great, my Testing does indeed get the same adress as the loaded texture from the map


ID3D11ShaderResourceView* Testing = 0;
Gfx->LoadTextureOld(Model->Texture.Diffuse.Location,Model->Texture.Diffuse.TextureOld,Model->Texture.Diffuse.Sampler);
for(int i = 0; i < Model->Materials.size();i++)
{
	Model->Materials[i].materialName += ".jpg";
	if(Gfx->TotalTexture.find(Model->Materials[i].materialName) == Gfx->TotalTexture.end())
		{
	
		Gfx->LoadTexture(Model->Materials[i].materialName,Testing);
			Model->Texture.Diffuse.Texture.insert(Model->Texture.Diffuse.Texture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Model->Texture.Diffuse.TextureOld));
			Gfx->RequestInsertionOfTexture(Model->Materials[i].materialName,Testing);
	//		Gfx->TotalTexture.insert(Gfx->TotalTexture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Testing));
			
		}
		else
		{
			Testing = Gfx->RequestTexture(Model->Materials[i].materialName);	
	 		Model->Texture.Diffuse.Texture.insert(Model->Texture.Diffuse.Texture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Model->Texture.Diffuse.TextureOld));
			//Gfx->RequestInsertionOfTexture(Model->Materials[i].materialName,Testing);
	//		Gfx->TotalTexture.insert(Gfx->TotalTexture.end(),std::pair<std::string,ID3D11ShaderResourceView*>(Model->Materials[i].materialName,Testing));
			
		}
	}
 

 

Now when Am trying to render my object, I try to change material depending on what group it is.

So I did something like this


for(int i = 0; i < Model->Total.Group; i++)
	{
		if(Model->lastMaterial != Model->groupMaterial[i])
		{
                  //First method
                  Model->lastMaterial = Model->groupMaterial[i];
                  Gfx->D3d11DevCon->PSSetShaderResources(0,1,& Model->Texture.Diffuse.Texture[Model->lastMaterial]);//<---- //Texture
// */

//Second method Model->lastMaterial = Model->groupMaterial[i]; pt = Model->Texture.Diffuse.Texture[Model->lastMaterial]; Gfx->D3d11DevCon->PSSetShaderResources(0,1,&pt);//<---- Texture


         //*/

} Gfx->D3d11DevCon->DrawIndexed ( Model->DrawingIndexIndices.IndexCount[i], Model->DrawingIndexIndices.StartIndex[i], 0 ); }

 

In which case instead of getting the texture it somehow decided to insert the texture 

like this

1

Share this post


Link to post
Share on other sites

operator[] is a read + write operation. If the specified key doesn't exist, then it is inserted into the map.

If you want to look up an item that might be in the map, without adding it in cases where it doesn't exist, then use find instead of [].

oh, >.<

Took me so long to write my last post without noticing a new reply thanks, will check it asap to see if that is the case!

1

Share this post


Link to post
Share on other sites

Woah man, from completly unclear you went into totally detailed :) Thanks, it didn't have to be SO DETAILED, but props for this. 

 

Seems like Hodgman was able to figure out the problem from that first vague description, and its indeed what happens. Try to stay away from [] operator as its convenient but it will insert element into map if it can't find it. Its probably better to use find() and insert() / emplace() as they have single behavior (unless of course inserting new element if it couldnt find one is what you really need).

0

Share this post


Link to post
Share on other sites

is there any diffrent in performance effiency between [] and find()? seeing how my map will get pretty big and gets alot of request in short intervalls. 

if there is I have the time to try atleast to make it so that this problem wont happend again >.<

0

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  
Followers 0