• 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
Crusable77

C++ Help

11 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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.  

2

Share this post


Link to post
Share on other sites

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;
		}
	}
}
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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);
	}
}
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.
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