#include "dxstdafx.h"
#include "PostProcFXChain.h"
#include "ScaledPPEffect.h"

#pragma once

//------------------------------------------------------------------------------------------
// Constructor
// Prepares the filename list
//------------------------------------------------------------------------------------------
PostProcFXChain::PostProcFXChain()
{
	// clear the list to be sure its empty
	m_lstFXChain.clear();
}


//--------------------------------------------------------------------------------------
// Destructor
//--------------------------------------------------------------------------------------
PostProcFXChain::~PostProcFXChain()
{
	// Release the effects
	list<ScaledPPEffect*>::const_iterator EffectIterator;
	for(EffectIterator=m_lstFXChain.begin(); EffectIterator!=m_lstFXChain.end(); ++EffectIterator)
	{
		if(*EffectIterator)
			delete(*EffectIterator);
	}
	// Final clean on the list
	m_lstFXChain.clear();
}


//--------------------------------------------------------------------------------------
// Add a PP effect at nIndex
//--------------------------------------------------------------------------------------
ScaledPPEffect * PostProcFXChain::InsertEffect(PPEffectsCommon * pEffect, const int nIndex, const float fScaleX, const float fScaleY)
{
	// Encapsulate our pEffect with a scaler
	ScaledPPEffect * pNewScaledEffect = new ScaledPPEffect(pEffect, fScaleX, fScaleY);	// Create a new ScaledPPEffect structure

	// And insert the new scaled effect in the list
	int i=0;
	list<ScaledPPEffect*>::iterator EffectIterator = m_lstFXChain.begin();
	while(i!=nIndex)
	{
		EffectIterator++;
		i++;
	}
	m_lstFXChain.insert(EffectIterator, pNewScaledEffect);
	
	// finally we returns the new scaled effect
	return pNewScaledEffect;	
}
	

//--------------------------------------------------------------------------------------
// Remove the PP effect at nIndex
//--------------------------------------------------------------------------------------
void PostProcFXChain::RemoveEffect(const int nIndex)
{
	// Going to the element we'll erase
	int i=0;
	list<ScaledPPEffect*>::iterator EffectIterator = m_lstFXChain.begin();
	while(i!=nIndex)
	{
		EffectIterator++;
		i++;
	}
	
	// Remove the list block
	delete (*EffectIterator);
	m_lstFXChain.erase(EffectIterator);	
}


//--------------------------------------------------------------------------------------
// Empty the PP effects list
//--------------------------------------------------------------------------------------
void PostProcFXChain::EmptyPPEffectList()
{
	list<ScaledPPEffect*>::const_iterator EffectIterator;
	for(EffectIterator=m_lstFXChain.begin(); EffectIterator!=m_lstFXChain.end(); ++EffectIterator)
	{
		if(*EffectIterator)
			delete(*EffectIterator);
	}
	// Final clean on the list
	m_lstFXChain.clear();
}


//--------------------------------------------------------------------------------------
// Retrieve the element at nIndex
//--------------------------------------------------------------------------------------
ScaledPPEffect * PostProcFXChain::GetEffect(const int nIndex)
{
	// Is nIndx invalid?
	if( nIndex<0 || nIndex>(int)(m_lstFXChain.size()-1) ) return NULL;

	// We search the element
	int i=nIndex;
	list<ScaledPPEffect*>::iterator EffectIterator;
	EffectIterator=m_lstFXChain.begin();
	while(i>0)
	{
		EffectIterator++;
		i--;
	}

	// And return it
	return (*EffectIterator);
}

