Ejemplo n.º 1
0
///===========================================================================
CParticleSystem *CParticleSystemShape::instanciatePS(CScene &scene, NLMISC::CContiguousBlockAllocator *blockAllocator /*= NULL*/)
{
	if (_Sharing && _SharedSystem != NULL) // is sharing enabled, and is a system already instanciated
	{
		return _SharedSystem;
	}

	// avoid prb with concurrent thread (may happen if an instance group containing ps is loaded in background)
	s_PSSMutex.enter();


	#ifdef PS_FAST_ALLOC
		nlassert(PSBlockAllocator == NULL);
		if (blockAllocator)
		{
			// set new allocator for particle system memory
			PSBlockAllocator = blockAllocator;
			blockAllocator->init(_NumBytesWanted); // if size wanted is already known, set it
		}
	#endif

	//NLMISC::TTicks start = NLMISC::CTime::getPerformanceTime();
	// copy the datas
	CParticleSystem *myInstance = NULL;

	// serialize from the memory stream
	if (!_ParticleSystemProto.isReading()) // we must be sure that we are reading the stream
	{
		_ParticleSystemProto.invert();
	}

	_ParticleSystemProto.resetPtrTable();
	_ParticleSystemProto.seek(0, NLMISC::IStream::begin);

//	NLMISC::TTicks start = NLMISC::CTime::getPerformanceTime();
	_ParticleSystemProto.serialPtr(myInstance); // instanciate the system
/*	NLMISC::TTicks end = NLMISC::CTime::getPerformanceTime();
	nlinfo("instanciation time = %.2f", (float) (1000 * NLMISC::CTime::ticksToSecond(end - start)));
*/

	myInstance->setScene(&scene);

	if (_CachedTex.empty() && scene.getDriver())
	{
		//nlinfo("flushing texs");
		// load && cache textures
		myInstance->enumTexs(_CachedTex, *scene.getDriver());
		for(uint k = 0; k < _CachedTex.size(); ++k)
		{
			if (_CachedTex[k])
			{
				_CachedTex[k]->setTextureCategory(CPSTextureCategory::get());
				scene.getDriver()->setupTexture (*(ITexture *)_CachedTex[k]);
			}
		}
	}
	else
	{
		/*
		for(uint k = 0; k < _CachedTex.size(); ++k)
		{
			nlinfo(_CachedTex[k]->getShareName().c_str());
		}
		*/
	}

	// tmp

	if (_Sharing)
	{
		_SharedSystem = myInstance; // set this as the first shared instance
	}

	#ifdef PS_FAST_ALLOC
		if (blockAllocator)
		{
			_NumBytesWanted = blockAllocator->getNumAllocatedBytes(); // now we know the number of wanted bytes, subsequent alloc can be much faster
			PSBlockAllocator = NULL;
		}
	#endif

	s_PSSMutex.leave();

	/*NLMISC::TTicks end = NLMISC::CTime::getPerformanceTime();
	nlinfo("instanciation time = %.2f", (float) (1000 * NLMISC::CTime::ticksToSecond(end - start)));	*/
	return myInstance;
}