//	Darkstar Engine  (c) UnderWorld Interactive Studio (all coding by Svarog    2007-2008)
//
//      Данный файл является частью Darkstar Engine. Любое использование кода файла
//      без согласия компании, является нарушением закона об авторском праве (в слу-
//      чае если не оговорено обратное).
//
//      Имя файла: render_state_manager_d3d9.hpp
//      Краткое описание: manager for render state blocks in DirectX 9
//

#ifdef GD_D3D9

#ifndef DSE_GS_RENDER_STATE_MANAGER_D3D9_HPP
#define DSE_GS_RENDER_STATE_MANAGER_D3D9_HPP

#include <Darkstar/common.hpp>
#include <Darkstar/Kernel/Misc/singleton.hpp>
#include <Darkstar/Graphic System/Render State/common_render_state.hpp>
#include <Darkstar/Graphic System/Render State/blend_render_state.hpp>
#include <Darkstar/Graphic System/Render State/colorwrite_render_state.hpp>
#include <Darkstar/Graphic System/Render State/cull_render_state.hpp>
#include <Darkstar/Graphic System/Render State/depth_render_state.hpp>
#include <Darkstar/Graphic System/Render State/fill_render_state.hpp>
#include <Darkstar/Graphic System/Render State/sampler_render_state.hpp>
#include <Darkstar/Graphic System/Direct3D/direct3d_include.hpp>

namespace gs
{
	class RenderStateManager : public ker::SingletonHolder<RenderStateManager>
	{
	private:
		// current values
		shared_ptr<BlendRenderState> cur_blend_state;
		shared_ptr<DepthRenderState> cur_depth_state;
		shared_ptr<ColorWriteRenderState> cur_color_write_state;
		shared_ptr<FillModeRenderState> cur_fill_mode_state;
		shared_ptr<SamplerRenderState> cur_sampler_state[sampler_num];
		shared_ptr<CullModeRenderState> cur_cull_state;

		// new values
		shared_ptr<BlendRenderState> new_blend_state;
		shared_ptr<DepthRenderState> new_depth_state;
		shared_ptr<ColorWriteRenderState> new_color_write_state;
		shared_ptr<FillModeRenderState> new_fill_mode_state;
		shared_ptr<SamplerRenderState> new_sampler_state[sampler_num];
		shared_ptr<CullModeRenderState> new_cull_state;

		void _setAllBlendState(const shared_ptr<BlendRenderState>& state);
		void _setAllDepthState(const shared_ptr<DepthRenderState>& state);
		void _setAllColorWriteState(const shared_ptr<ColorWriteRenderState>& state);
		void _setAllFillModeState(const shared_ptr<FillModeRenderState>& state);
		void _setAllSamplerState(const unsigned int index, const shared_ptr<SamplerRenderState>& state);
		void _setAllCullModeState(const shared_ptr<CullModeRenderState>& state);
	public:
		RenderStateManager();
		~RenderStateManager();

		// set
		void setFillModeRenderState(const FillModeRenderState& fill_render_state);
		void setCullModeRenderState(const CullModeRenderState& cull_render_state);
		// only one flag will be use (with index 0)
		void setColorWriteRenderState(const ColorWriteRenderState& color_write_render_state);
		// only one blend_enable will be use (with index 0)
		void setBlendRenderState(const BlendRenderState& blend_render_state);
		void setDepthRenderState(const DepthRenderState& depth_render_state);
		void setSamplerRenderState(const SamplerRenderState& sampler_render_state, const unsigned int sampler_index, const SAMPLER_TYPE& type);

		const shared_ptr<FillModeRenderState>& getCurFillModeRenderState() const;
		const shared_ptr<CullModeRenderState>& getCurCullModeRenderState() const;
		const shared_ptr<ColorWriteRenderState>& getCurColorWriteRenderState() const;
		const shared_ptr<BlendRenderState>& getCurBlendRenderState() const;
		const shared_ptr<DepthRenderState>& getCurDepthRenderState() const;
		const shared_ptr<SamplerRenderState>& getCurSamplerRenderState(const unsigned int sampler_index, const SAMPLER_TYPE& type) const;

		void flush();
	};
};

#endif

#endif