#pragma once

#include <Shas.h>

#define D3DFVF_SCREENMASK (D3DFVF_XYZRHW | D3DFVF_TEX2)

struct IRenderable
{
	virtual void Render(IDirect3DDevice9* lpd3dd) = 0;
	virtual void BeginRender(IDirect3DDevice9* lpd3dd) = 0;
};


class ScreenMask
	: public IRenderable
{
public:
	ScreenMask(bool ab_Virtual = false);
	~ScreenMask();
public:

	typedef struct
	{
		float x,y,z;
	} TPoint3D;
	typedef struct
	{
		float x,y;
	} TPoint2D;
	typedef struct ScreenMaskVertex
	{
		ScreenMaskVertex()
		{
			rhw = 1.0f;;
		}

		TPoint3D pos;
		float rhw;
		TPoint2D UV[2];
	} TScreenMaskVertex;

	std::vector<TScreenMaskVertex> vertex;
	LPDIRECT3DVERTEXBUFFER9	screenMaskVB;
	LPDIRECT3DINDEXBUFFER9	screenMaskIB;
	float x,y,z,width,height;
	bool ScreenCoords;

	bool mb_Changed;
	bool mb_Bias;
	bool mb_Virtual;
public:

	void SetBias(bool ab_State);
	void SetZ(float inz);
	void SetSize(float inw, float inh);
	void SetPos(float inx, float iny);
	void SetTextureCoords(float a, float b, float c, float d, int channel=0);
	void SetFrame(int x, int y, int maxX, int maxY, int channel=0);
	virtual void BeginRender(IDirect3DDevice9* lpd3dd);
	virtual void Render(IDirect3DDevice9* lpd3dd);
	void Remove();
	void Create(IDirect3DDevice9* lpd3dd,float inx,float iny,float inwidth, float inheight, bool useScreenCoords=false);
	void ReCreate(IDirect3DDevice9* lpd3dd);

	std::vector<TScreenMaskVertex> get_VertexList();
};

class BatchScreenMask
	: public IRenderable
{
public:
	BatchScreenMask(IDirect3DDevice9* lpd3dd, int ai_MaxVertex);
	~BatchScreenMask();

	virtual void BeginRender(IDirect3DDevice9* lpd3dd);
	virtual void Render(IDirect3DDevice9* lpd3dd);

	void AddScreenMaskData(ScreenMask& ar_ScreenMask, IDirect3DDevice9* lpd3dd, bool ab_ResetFirst, bool ab_AllowRebuild);

private:
	std::vector<ScreenMask::TScreenMaskVertex> vertex;
	LPDIRECT3DVERTEXBUFFER9	screenMaskVB;
	LPDIRECT3DINDEXBUFFER9	screenMaskIB;

	int mi_MaxVerticies;
};
