/*
	Trig Wars.
	Copyright (c) 2008, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef PRIMITIVES_RENDERER_H
#define PRIMITIVES_RENDERER_H

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================
	
	class Display;
	class Window;
	class PixelShader;
	class PixelShaderManager;
	class VertexShader;
	class VertexShaderManager;
	class VertexFormat;
	class ShaderUniform;
	class Camera2D;
	class Texture;

	//=================================================================================================================
	/// @brief		Class that handles the rendering of basic primitives. Uses the colored pixel
	///				and vertex shaders. Also allows for drawing of sprites etc. 
	/// @remarks	This class is not optimised for speed and should only be used for debug purposes
	//=================================================================================================================
	
	class PrimitivesRenderer : public ObjectBase
	{
		public:

			//=========================================================================================================
			// Functions
			//=========================================================================================================

				/// Creates the primitives renderer

				PrimitivesRenderer
				( 
					Display *				display					, 
					VertexShaderManager *	vertex_shader_manager	,
					PixelShaderManager *	pixel_shader_manager
				);
				
				/// Destroys the primitives renderer
				
				~PrimitivesRenderer(){ ReleaseResources(); }

				/// Draws a 2D quadrilateral: takes 4 2D points and assumes points are in either clockwise or anticlockwise order.

				void DrawQuad
				( 
					Camera2D * camera	, 
					const Vec2f & p1	, 
					const Vec2f & p2	, 
					const Vec2f & p3	, 
					const Vec2f & p4	, 
					int color 
					
				) const;
				
				/// Draws a rectangular sprite:

				void DrawSprite
				( 
					Camera2D * camera	,
					const Vec2f & pos	, 
					const Vec2f & size	, 
					int color			, 
					Texture & texture 
					
				) const;

				/// Draws an outline of a 2D quadrilateral: takes 4 2D points and assumes points are in either clockwise or anticlockwise order.

				void DrawQuadOutline
				( 
					Camera2D * camera	,  
					const Vec2f & p1	, 
					const Vec2f & p2	, 
					const Vec2f & p3	, 
					const Vec2f & p4	, 
					int color 
					
				) const;

				/// Draws a 2d circle: takes the position and radius of the circle as well as the color

				void DrawCircle( Camera2D * camera,  const Vec2f & p, float radius , int color ) const;
				
				/// Draws a 2d line: takes the position of the two points as well as the color

				void DrawLine( Camera2D * camera, const Vec2f & p1 , const Vec2f & p2 , int color ) const;
				
				/// Draws a 2d point: takes the position of the point as well as the color

				void DrawPoint( Camera2D * camera, const Vec2f & p , int color ) const;				

				/// Tells if this is correctly setup

				inline bool IsValid() const { return m_is_valid; }

				/// Gets the window used by this primitives renderer: 

				inline Window * GetWindow(){ return m_window; }
				
				/// Returns the type name for this object

				virtual const char * GetTypeName() const;				
				
		private:			
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Copying is not allowed
				
				PrimitivesRenderer( PrimitivesRenderer& );		
				
				/// Copying is not allowed
				
				PrimitivesRenderer & operator = ( PrimitivesRenderer& );
				
				/// Frees all resources used by the primitives renderer
				
				void ReleaseResources();

			//=========================================================================================================
			// Variables
			//=========================================================================================================		

				/// Direct3D device used by the primitives renderer

				IDirect3DDevice9 * m_d3d_device;
				
				/// Window the renderer belongs to
				
				Window * m_window;
								
				/// Pixel shader used for the colored shader
				
				VertexShader * m_colored_vs;
				
				/// Pixel shader used for the textured shader
				
				VertexShader * m_textured_vs;
				
				/// Pixel shader used for the colored shader
				
				PixelShader	* m_colored_ps;
				
				/// Pixel shader used for the textured shader
				
				PixelShader	* m_textured_ps;
				
				/// Vertex format used for the colored shader
				
				VertexFormat * m_colored_format;
				
				/// Vertex format used for the textured shader
				
				VertexFormat * m_textured_format;
				
				/// Shader uniform on the colored shader: transform for an object
				
				ShaderUniform * m_u_colored_transform;
				
				/// Shader uniform on the textured shader: transform for an object
				
				ShaderUniform * m_u_textured_transform;
				
				/// Shader uniform on the textured shader: texture for an object
				
				ShaderUniform * m_u_textured_texture;
				
				/// True if the renderer is valid
				
				bool m_is_valid;
	};

};

#endif 