//	Darkstar Engine  (c) UnderWorld Interactive Studio (all coding by Svarog    2007)
//
//      Данный файл является частью Darkstar Engine. Любое использование кода файла
//      без согласия компании, является нарушением закона об авторском праве (в слу-
//      чае если не оговорено обратное).
//
//      Имя файла: blend_render_state.cpp
//      Краткое описание: Blend render states
//

#include <Darkstar/Graphic System/Render State/blend_render_state.hpp>
#include <Darkstar/Graphic System/Render State/render_state_manager.hpp>
#include <Darkstar/Kernel/logh.hpp>

using gs::BlendRenderState;
using gs::DBLEND;
using gs::DBLENDOP;
using gs::RenderStateManager;
using ker::Log;

BlendRenderState::BlendRenderState()
{
	for(unsigned int i = 0; i < render_target_num; ++i)
		blend_enable[i] = false;
	src_blend = DBLEND_ONE;
	src_blend_alpha = DBLEND_ONE;
	dst_blend = DBLEND_ZERO;
	dst_blend_alpha = DBLEND_ZERO;
	blend_op = DBLENDOP_ADD;
	blend_op_alpha = DBLENDOP_ADD;
};

BlendRenderState::~BlendRenderState()
{

};

BlendRenderState::BlendRenderState(const BlendRenderState& blend)
{
	for(unsigned int i = 0; i < render_target_num; ++i)
		blend_enable[i] = blend.getBlendEnable(i);
	src_blend = blend.getSrcBlend();
	src_blend_alpha = blend.getSrcBlendAlpha();
	dst_blend = blend.getDstBlend();
	dst_blend_alpha = blend.getDstBlendAlpha();
	blend_op = blend.getBlendOp();
	blend_op_alpha = blend.getBlendOpAlpha();
};

bool BlendRenderState::operator ==(const BlendRenderState& state) const
{
	for(unsigned int i = 0; i < render_target_num; ++i)
		if(blend_enable[i] != state.getBlendEnable(i))
			return false;
	return (src_blend == state.getSrcBlend() && src_blend_alpha == state.getSrcBlendAlpha() &&
		dst_blend == state.getDstBlend() && dst_blend_alpha == state.getDstBlendAlpha() &&
		blend_op == state.getBlendOp() && blend_op_alpha == state.getBlendOpAlpha());
};

void BlendRenderState::setBlendEnable(const unsigned int render_target_index, const bool is_enable)
{
#ifdef _NOTFINAL
	if(render_target_index >= render_target_num)
	{
		lwar << "Cannot set blend enable with index render target index greater or equal than render_target_num";
		return;
	};
#endif
	blend_enable[render_target_index] = is_enable;
};

void BlendRenderState::setBlendOp(const DBLENDOP& op)
{
	blend_op = op;
};

void BlendRenderState::setBlendOpAlpha(const DBLENDOP& op)
{
	blend_op_alpha = op;
};

void BlendRenderState::setDstBlend(const DBLEND& dstBlend)
{
	dst_blend = dstBlend;
};

void BlendRenderState::setSrcBlend(const DBLEND& srcBlend)
{
	src_blend = srcBlend;
};

void BlendRenderState::setDstBlendAlpha(const DBLEND& dstBlend)
{
	dst_blend_alpha = dstBlend;
};

void BlendRenderState::setSrcBlendAlpha(const DBLEND& srcBlend)
{
	src_blend_alpha = srcBlend;
};

const DBLEND& BlendRenderState::getDstBlend() const
{
	return dst_blend;
};

const DBLEND& BlendRenderState::getDstBlendAlpha() const
{
	return dst_blend_alpha;
};

const DBLEND& BlendRenderState::getSrcBlend() const
{
	return src_blend;
};

const DBLEND& BlendRenderState::getSrcBlendAlpha() const
{
	return src_blend_alpha;
};

const DBLENDOP& BlendRenderState::getBlendOp() const
{
	return blend_op;
};

const DBLENDOP& BlendRenderState::getBlendOpAlpha() const
{
	return blend_op_alpha;
};

bool BlendRenderState::getBlendEnable(const unsigned int render_target_index) const
{
#ifdef _NOTFINAL
	if(render_target_index >= render_target_num)
	{
		lwar << "Cannot get blend enable with index render target index greater or equal than render_target_num";
		return false;
	};
#endif
	return blend_enable[render_target_index];
};

void BlendRenderState::set()
{
	RenderStateManager::ptr()->setBlendRenderState(*this);
};
