Jump to content

  • Log In with Google      Sign In   
  • Create Account

Mercile55

Member Since 20 Jan 2013
Offline Last Active Jan 25 2013 02:09 PM

#5024529 Killing AI

Posted by Mercile55 on 22 January 2013 - 06:20 PM

Without iterators:

for(std::size_t i = 0; i < v.size(); ++i)
	{
		if(v[i].dead)
		{
			
			std::swap(v[i], v.back());
			v.pop_back();
			--i;
		}
	}

Remember that modification of the for-loop index inside the loop is one bad programming practice. It usually leads to infinite loops and computer hang ups.




#5024072 Killing AI

Posted by Mercile55 on 21 January 2013 - 05:13 PM

I knew I should've tested the snippets before posting them. laugh.png

So the correct method to 'swap and pop' is:
std::vector<Zombie> zombies;typedef d::vector<Zombie>::iterator ZombieIterator; for(ZombieIterator it = zombies.begin(); it != zombies.end();){	if(it->dead)	{		//Swap the value of the current zombie with the zombie at the back of the container.		std::swap(*it, zombies.back());		//Pop the back of the container.		zombies.pop_back();	}	else	{		++it;	}}
Looking over my previous version, it'd also skip whatever elements were at the end before being swapped. rolleyes.gif

But the correct choice in most cases is the third solution I posted, using the heavily optimized and standardized std::remove_if() algorithm.
This isn't right man, try this when all Zombies are dead, and you'll end up with a crash, as when you pop_back the very first element, the iterator becomes invalid. It will be ok, as long as there's at least one element left though.
It's always good idea to make sure what does and what doesn't invalidate your iterator, and avoid any such cases if possible. It saves time in the future.

remove_if() does the trick for most cases, although it may not be as fast, due to additional calls overhead.


#5023726 Parallizing Antialising and other graphic concepts

Posted by Mercile55 on 20 January 2013 - 08:02 PM

Not necessarily Graphics processing, but image processing in general, i.e. image comparison, effects, i.e. extrusion, edge detection, emboss, take you pick. All of those are best suited for parallel processing. Face detection is one nice pick, and with OpenCV not so hard to do.




#5023707 FASM Opengl Triangle

Posted by Mercile55 on 20 January 2013 - 07:20 PM

why would you want to do it in FASM ? it's really not the best idea if you think that attached example is complicated.

I suggest C/C++/C# instead.

 

btw: an example>http://www.dbfinteractive.com/forum/index.php?topic=3040.0




#5023704 Inputs

Posted by Mercile55 on 20 January 2013 - 07:05 PM

The usual input processing is buffered and asynchronous, where you get a collection of all input events that happened since your last reading, and the process them doing appropriate actions. Maybe if your loop is reading only one at a time, and reacting to it, it works synchronously, and until it gets turned off, it wont switch to another one ?

make sure that any input reading is asynchronous, and independent of drawing.




#5023698 Killing AI

Posted by Mercile55 on 20 January 2013 - 06:54 PM

The problem is that you're modifying the very collection that you're iterating through. Every time you delete something from a collection that you're going through, the size will change, but not in the loop, so original size (i.e. 10) will now be one less, (i.e. 9), and when you get to the last original index (i.e. 10), you'll access random location, and usually crash the app.

Save indexes of zombies to delete in another collection, and delete them in one consistent loop after the loop you're doing now.




PARTNERS