#ifndef RENDERDEVICE_H
#define RENDERDEVICE_H

#include <stdint.h>
#include "render/render_shared.h"

class VertexBuffer;
class IndexBuffer;
class VertexFormat;
class RenderTarget;

enum TextureSurfaceType
{
	TST_COLOR = 1 << 0,
	TST_DEPTH = 1 << 1,
	TST_STENCIL = 1 << 2,
};

enum PrimitiveType
{
	PT_POINTS,
	PT_LINES,
	PT_TRIANGLES
};

class RenderDevice
{
public:
	RenderDevice();
	~RenderDevice();

	VertexBuffer* CreateVertexBuffer(void* data, size_t size, bool isStream = false);
	IndexBuffer* CreateIndexBuffer(void* data, size_t size, bool isStream = false);

	void SetVerticesBuffer(VertexBuffer* buffer);
	void SetIndicesBuffer(IndexBuffer* buffer);

	void SetDepthTest(bool enable);
	void SetDepthWrite(bool enable);

	//void setVertexFormat(VertexFormat* format);

	void SetBlending(bool value);
	void SetBlendingFunction(BlendFactor srcFactor, BlendFactor destFactor);

	void SetReadRenderTarget(RenderTarget* renderTarget);
	void SetWriteRenderTarget(RenderTarget* renderTarget);

	void SetBackfaceCull(bool value);

	void SetViewport(int x, int y, int w, int h);

	// glBlitFramebuffer
	void BlitRenderTarget(int srcX, int srcY, int srcWidth, int srcHeight,
						 int destX, int destY, int destWidth, int destHeight,
						 TextureSurfaceType surfaceType);

	// drawing
	void DrawArrays(PrimitiveType primType, uint32_t startOf, size_t verticesCount);
	void DrawElements(PrimitiveType primType, size_t elementsCount, bool is16bitIndices);

private:
	VertexBuffer* m_activeVB;
	IndexBuffer* m_activeIB;

	RenderTarget* m_activeReadRT;
	RenderTarget* m_activeWriteRT;

	bool m_blending;
	BlendFactor m_srcBlendFactor;
	BlendFactor m_destBlendFactor;

	bool m_backfaceCull;
};

extern RenderDevice* g_renderDevice;

#endif // !RENDERDEVICE_H
