Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

Parameter to static compare function

This topic is 3177 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

Heya,I am wondering if there is a way to pass a parameter to a compare function that Im using with std::sort,I couldn't find a way so I made a ugly static member and using it.
bool DARenderer::sortParticleSystems( boost::shared_ptr<STE::ParticleSystem> p1,boost::shared_ptr<STE::ParticleSystem> p2)
{	
	float distanceToCam1 = STMath::Distance(s_CameraPos,p1->m_Center);
	float distanceToCam2 = STMath::Distance(s_CameraPos,p2->m_Center);

	return distanceToCam1 > distanceToCam2;
	
}	

Here is the call
//update static ugly :/
s_CameraPos = m_Camera->getPosition();
//sort the particle system based on distance to camera
std::sort(world->getMap()->m_Smokes.begin(),world->getMap()->m_Smokes.end(),sortParticleSystems);
	
	for(unsigned int i=0; i<world->getMap()->m_Smokes.size(); ++i)
	{
//		draw systems
	}

How can I pass camera position to sortParticleSystems without making the camera static or am I stuck with this?

Share this post


Link to post
Share on other sites
Advertisement
I would pass the shared pointers by constant reference (just to avoid some unnecessary incrementing and decrementing of the reference count).

Also, boost::bind (not compiled or tested):

// Somewhere...
typedef boost::shared_ptr<STE::ParticleSystem> ParticleSystemPtr;

bool DARenderer::sortParticleSystems(
const ParticleSystemPtr& p1, const ParticleSystemPtr& p2, const Vector3& cameraPos)
{
float distanceToCam1 = STMath::Distance(cameraPos,p1->m_Center);
float distanceToCam2 = STMath::Distance(cameraPos,p2->m_Center);
return distanceToCam1 > distanceToCam2;
}

// ...

std::sort(
world->getMap()->m_Smokes.begin(),
world->getMap()->m_Smokes.end(),
boost::bind(&sortParticleSystems, _1, _2, m_Camera->getPosition());
);



[Edited by - jyk on December 6, 2009 2:14:09 PM]

Share this post


Link to post
Share on other sites
Or if boost is unavailable, a functor:


struct DistanceToCameraCompare {
const Vector3& cameraPos;

DistanceToCameraCompare( const Vector3& cam )
: cameraPos( cam ) {
}

template<typename T>
bool operator() ( const T& p1, const T& p2 ) const {
float distanceToCam1 = STMath::Distance( cameraPos, p1->m_Center );
float distanceToCam2 = STMath::Distance( cameraPos, p2->m_Center );

return distanceToCam1 > distanceToCam2;
}
};

std::sort(world->getMap()->m_Smokes.begin(),world->getMap()->m_Smokes.end(), DistanceToCameraCompare( s_CameraPos ) );




Untested code.

Share this post


Link to post
Share on other sites
Quote:
boost::shared_ptr<STE::ParticleSystem> p1


Try to always pass shared_ptr by (const) reference. It avoids a lot of overhead.

Share this post


Link to post
Share on other sites
Yes I will have to go through my code and change them.At the moment they are 99% passed by value.

Off topic screenshot since I got this working :)



The particle systems are sorted and drawn with ID3DXSprite with back to front ordering.I also disable Z writes.Looks much better while they are in motion.

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!