#include "dxstdafx.h"
#include "ScaledPPEffect.h"
#include "PPEffectsCommon.h"
#include "EffectParam.h"

#pragma once


//------------------------------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------------------------------
ScaledPPEffect::ScaledPPEffect(PPEffectsCommon * pEffect, const float fScaleX, const float fScaleY)
{
	// Copying the parameters values
	m_pEffect = pEffect;
	m_vScaler.x = fScaleX;
	m_vScaler.y = fScaleY;

	// Instancing the scaled effect parameters values	
	m_vParamValues = NULL;
	if(pEffect->GetNbAvailableParams())
	{
		m_vParamValues = new FLOAT*[pEffect->GetNbAvailableParams()];
		ZeroMemory(m_vParamValues, pEffect->GetNbAvailableParams()*sizeof(FLOAT*));	// Always setup data to 0
		
		// Affecting them the default values
		for(int i=0; i<pEffect->GetNbAvailableParams(); ++i)
		{
			EffectParam * pParam = m_pEffect->getParameter(i);
			if(pParam)
			{
				m_vParamValues[i] = new FLOAT[pParam->GetParamSize()];
				memcpy(m_vParamValues[i], pParam->GetParamDefault(), pParam->GetParamSize()*sizeof(FLOAT));
			}
		}
	}
}


//------------------------------------------------------------------------------------------------
// Copy constructor
//------------------------------------------------------------------------------------------------
ScaledPPEffect::ScaledPPEffect(const ScaledPPEffect * pSrcScaledEffect)
{
	// Always setup pointers to null at init
	m_pEffect = NULL;
	m_vParamValues = NULL;

	*this = *pSrcScaledEffect;
}


//------------------------------------------------------------------------------------------------
// Destructor
//------------------------------------------------------------------------------------------------
ScaledPPEffect::~ScaledPPEffect()
{
	// Releasing the instance parameters
	if(m_vParamValues && m_pEffect)
	{
		for(int i=0; i<m_pEffect->GetNbAvailableParams(); ++i)
			delete [] m_vParamValues[i];
		delete [] m_vParamValues;
	}
}


//------------------------------------------------------------------------------------------------
// Override the = operator (in case we try to setup an instance with bigger one)
//------------------------------------------------------------------------------------------------
ScaledPPEffect& ScaledPPEffect::operator = (const ScaledPPEffect& SrcScaledEffect)
{
	// If not auto-affecting
	if(this != &SrcScaledEffect)
	{
		// Releasing the instance parameters now (because it's dependent of m_pEffect)
		if(m_vParamValues && m_pEffect)
		{
			for(int i=0; i<m_pEffect->GetNbAvailableParams(); ++i)
				delete [] m_vParamValues[i];
			delete [] m_vParamValues;
		}

		// Copying the effect and scaler members
		m_pEffect = ((ScaledPPEffect&)SrcScaledEffect).GetEffect();
		m_vScaler = ((ScaledPPEffect&)SrcScaledEffect).GetScaler();

		// Reaffecting the parameter(s) value(s)
		m_vParamValues = NULL;
		if(m_pEffect->GetNbAvailableParams())
		{
			m_vParamValues = new FLOAT*[m_pEffect->GetNbAvailableParams()];
			ZeroMemory(m_vParamValues, m_pEffect->GetNbAvailableParams()*sizeof(FLOAT*));	// Always setup data to 0

			// Affecting them the default values
			for(int i=0; i<m_pEffect->GetNbAvailableParams(); ++i)
			{
				EffectParam * pParam = m_pEffect->getParameter(i);
				if(pParam)
				{
					m_vParamValues[i] = new FLOAT[pParam->GetParamSize()];
					memcpy(m_vParamValues[i], ((ScaledPPEffect&)SrcScaledEffect).GetParamValues(i), pParam->GetParamSize()*sizeof(FLOAT));
				}
			}
		}
	}

	// Return the new object
	return (*this);
}