Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ Help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 18 March 2013 - 05:12 AM

Hello, I am making a game in C++ and SDL and I am having some iterator/vector trouble(I think). I am loading all my images into a vector of Sprites, which I made so I could associate a std::string with SDL_Surface*. However When I try to get the image I get This error:

 
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Sprite' (or there is no acceptable conversion)

 

 

Here is the code:

 

 
SDL_Surface* MediaRepository::GetSurface(std::string SurfaceName){
 
mIter = find(mImages.begin(), mImages.end(), SurfaceName);
 
if(mIter != mImages.end()){
 
return mIter->GetSurface();
}
 
return NULL;
}

 

mImages is the vector of Sprites and GetSurface returns a SDL_Surface*. Thank you in advance for any help.


"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3812

Like
4Likes
Like

Posted 18 March 2013 - 05:34 AM

std::find returns the first iterator it for which *it == SurfaceName is true. Unless your Sprite class has an operator == which takes an std::string as a second argument, that cannot work. I would not advise adding such an operator by the way.

Either use std::find_if with an appropriate functor/lambda expression or use an std::map<std::string, Sprite>.

#3 Serapth   Crossbones+   -  Reputation: 5266

Like
2Likes
Like

Posted 18 March 2013 - 07:37 AM

std::find returns the first iterator it for which *it == SurfaceName is true. Unless your Sprite class has an operator == which takes an std::string as a second argument, that cannot work. I would not advise adding such an operator by the way.

Either use std::find_if with an appropriate functor/lambda expression or use an std::map<std::string, Sprite>.

 

I would squarely suggest doing the latter.  If you are indexing a datatype by string... thats a map.  



#4 Jutaris   Members   -  Reputation: 473

Like
0Likes
Like

Posted 18 March 2013 - 08:14 AM

To be clear, you should use the map INSTEAD of the vector.


My Site -- My Music  -- My Ramblings -- My Game

 Basic Flying Rules:Try to stay in the middle of the air. Do not go near the edges of it. The edges of the air can be recognized by the appearance of ground, buildings, sea, trees and interstellar space. It is much more difficult to fly there.

Come join us for some friendly game dev discussions over in XNA Chat!


#5 Wrathnut   Members   -  Reputation: 361

Like
0Likes
Like

Posted 18 March 2013 - 11:07 AM

For my C++/SDL 2d game I am doing the same sort of thing where I have a manager that keeps a vector list of gracphics objects and can render them according to an ID or a string name. For you to compare string values you have to do a

.compare(value)==0

 

I store an name, ID and an image type in my graphics object. The ID or name are referencing the owner of the image. The type refers to a standard image type. This can easily be associated with an animation list so that current images from a running animation can be pulled from the library.

 

Here is one of my routines for rendering images

 

void gFXManager::renderImage(SDL_Surface *screen, string name, int type, int x, int y){
	
	for(int i=0; i < this->gFXList.size(); i++){
		if(this->gFXList[i]->getName().compare(name)==0 && this->gFXList[i]->getType()==type){
			//Render the image to the surface
			this->gFXList[i]->drawImage(screen, x, y);
			break;
		}
	}
}


#6 Serapth   Crossbones+   -  Reputation: 5266

Like
0Likes
Like

Posted 18 March 2013 - 11:47 AM

For my C++/SDL 2d game I am doing the same sort of thing where I have a manager that keeps a vector list of gracphics objects and can render them according to an ID or a string name. For you to compare string values you have to do a

.compare(value)==0

 

I store an name, ID and an image type in my graphics object. The ID or name are referencing the owner of the image. The type refers to a standard image type. This can easily be associated with an animation list so that current images from a running animation can be pulled from the library.

 

Here is one of my routines for rendering images

 

void gFXManager::renderImage(SDL_Surface *screen, string name, int type, int x, int y){
	
	for(int i=0; i < this->gFXList.size(); i++){
		if(this->gFXList[i]->getName().compare(name)==0 && this->gFXList[i]->getType()==type){
			//Render the image to the surface
			this->gFXList[i]->drawImage(screen, x, y);
			break;
		}
	}
}

 

This code should almost certainly use a map in place of a vector, especially if you have a lot of items.  There are two reasons for this, first the vector will simplify your code and as a result make it less error prone and easier to maintain.  You would eliminate the loop completely.  Second, as complexity grows the search will certainly be faster on a map than a raw iteration over elements.

 

Small quibbles, you should use an iterator.  Also, generally prefer pre-increment operators ( ++i  ) over post-increment ( i++ )  unless you have a reason to do otherwise.  In a for loop, they will be functionally identical, but the use of a post increment can result in a variable being creating each iteration, depending on your compiler.  I think most are smart enough to deal with this, but it's a low hanging fruit thing to get in the habit of changing.



#7 Wrathnut   Members   -  Reputation: 361

Like
0Likes
Like

Posted 18 March 2013 - 03:03 PM

I might beg to differ a bit. But it is only a minor quible. My system has the flexibility to look up versus name or ID and as simple name value pair isn't quite what I desire. I am comparing several things in my search not just a name. That and I am not particlulary fond of the syntax for iterators. But that is preference as far as I am concerned.

 

If it works and gives you the performance you need then use it.  A map or a vector is just the vehicle to get the job done.

 

BitMaster answered the question as to why it was not working I was just showing him how he can use his current system by just comparing a string value.

 

I would like to ask how my imlementation is more error prone. I am assuming you meant to say, "There are two reasons for this, first the map will simplify your code and as a result make it less error prone and easier to maintain."

 

I am not try to start an argument but I am always trying to learn new things or techniques.



#8 Serapth   Crossbones+   -  Reputation: 5266

Like
0Likes
Like

Posted 18 March 2013 - 05:07 PM

I would like to ask how my imlementation is more error prone. I am assuming you meant to say, "There are two reasons for this, first the map will simplify your code and as a result make it less error prone and easier to maintain."

 

Basically it boils down to any time you are writing code to make your datatype do what another data type does better out of the box, that additional code is more prone to be buggy.  It's an extension of the logic, the STL datatypes have been viewed by thousands of developers and tested by millions, so unless you have a very good reason, you shouldn't roll your own.  You are essentially adapting a vector to be a datatype it isn't meant to be.  ( Not that your code is wrong or bad, it's short and simple enough that it really doesn't matter, but in a more complex system it becomes more important ).

 

In your particular case, if you want to have two keys, one of string and one of int say, you would make your map of type std::map<std::pair<int,string>,Sprite>;  I know the syntax looks unwieldy at first ( like iterators ), but this is the way of the future with C++ ( but thank god for auto! ). There are behind the scenes advantages to each type as well, and if you have two keys, the map will certainly perform better once you hit a very small threshold ( maybe 12 items or so... ).  Now if you require certain traits, like guaranteed order or compactness, vector may be a better choice.



#9 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 18 March 2013 - 05:23 PM

With a map the function would look something like this, and it would not get slower as the list gets larger. I think it's an improvement. You can define comparison or hash functions, so there shouldn't be any issue over flexibility.

void gFXManager::renderImage(SDL_Surface *screen, string name, int type, int x, int y){
	
	auto it = gFXMap.find(KeyType(name, type));
		
	if (it != gFXMap.end()) {

		//Render the image to the surface
		it->second->drawImage(screen, x, y);
	}
}


#10 Wrathnut   Members   -  Reputation: 361

Like
0Likes
Like

Posted 18 March 2013 - 08:29 PM

Serapth I actually did look into using maps when I was first designing my systems apparently I didn't look hard enough at maps. I am genuinely surprised that it doesn't get slower as the list gets larger I will have to look into it a bit more.

 

And Thank you EwClay for the example.



#11 Serapth   Crossbones+   -  Reputation: 5266

Like
0Likes
Like

Posted 18 March 2013 - 09:43 PM

Oh, it still gets slower as it grows, it just does it at a lower pace.

#12 EWClay   Members   -  Reputation: 659

Like
0Likes
Like

Posted 19 March 2013 - 03:09 AM

Oh, it still gets slower as it grows, it just does it at a lower pace.


Tree based maps are O(logN), which is pretty slow growth. But hash maps are constant time, allowing for the occasional resize.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS