/*
	Trampoline Sim.
	Copyright (c) 2009, 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 VERTEX_FORMAT_H
#define VERTEX_FORMAT_H

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

struct ID3DXBaseMesh;

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

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================
	
	class Display;
	
	//=================================================================================================================
	/// @brief		Represents a Direct3D9 vertex format that can be used to draw primitives
	//=================================================================================================================
	
	class VertexFormat
	{
		public:

			//=========================================================================================================
			// Enums
			//=========================================================================================================

				/// Represents what each element in a vertex format is used for

				enum ElementUsage
				{
					/// This element is a position
				
					VF_USAGE_POSITION ,	
					
					/// This element is a color
					
					VF_USAGE_COLOR ,
					
					/// This element is a texture coordinate
					
					VF_USAGE_TEXCOORD ,
					
					/// This element is a normal
					
					VF_USAGE_NORMAL	,
					
					/// This element is a binormal
					
					VF_USAGE_BINORMAL ,	
					
					/// This element is a tangent
					
					VF_USAGE_TANGENT 
				};

				/// Represents the type of each element in a vertex format

				enum ElementType
				{
					/// Single float number
				
					VF_TYPE_FLOAT1 , 
					
					/// Two floats
					
					VF_TYPE_FLOAT2 , 
					
					/// Three floats
					
					VF_TYPE_FLOAT3 , 
					
					/// Four floats
					
					VF_TYPE_FLOAT4 ,
					
					/// Four byte color value
					
					VF_TYPE_COLOR
				};

			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Creates a default null vertex format
				
				VertexFormat();
				
				/// Copy constructor: creates this vertex declaration from another one
				
				VertexFormat( const VertexFormat & other );				
				
				/// Creates a format from the format contained in a mesh
				
				VertexFormat( Display	* display , ID3DXBaseMesh * mesh );

				/// Creates the vertex format from the given element types and counts

				VertexFormat
				( 
					Display	*				display			, 
					unsigned char			num_elements	,
					const ElementUsage *	usages			,
					const ElementType	*	types 				
				);
				
				/// Creates the vertex format from the format contained in a mesh
				
				VertexFormat( ID3DXMesh * mesh );
				
				/// Assignment operator: assigns the other vertex declaration to this one
				
				VertexFormat & operator = ( const VertexFormat & other );
				
				/// Destroys the vertex format
				
				~VertexFormat();
				
				/// Tells if this vertex format is valid

				inline bool IsValid() const { return m_is_valid; }

				/// Gets the Direct3D representation of the format: returns null if invalid

				inline IDirect3DVertexDeclaration9 * GetDeclaration(){ return m_vertex_declaration; }
				
				/// Returns the size of each vertex element with this format
				
				inline unsigned GetVertexSize(){ return m_vertex_size; }

				/// Applies the vertex format as the current with direct3d

				void Apply();
				
				//=====================================================================================================
				/// @brief		Tells if this is a default null vertex declaration
				/// @returns	True if this is a null vertex declaration
				//=====================================================================================================
				
				inline bool IsNull() const
				{
					return m_vertex_declaration == 0 || m_d3d_device == 0;
				}

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

				/// The Direct3D vertex declaration for this format

				IDirect3DVertexDeclaration9 * m_vertex_declaration;

				/// The actual Direct3D device used by the vertex declaration

				IDirect3DDevice9 * m_d3d_device;

				/// Is this format valid ?

				bool m_is_valid;
				
				/// Size of each individual vertex with this format
				
				unsigned m_vertex_size;
	};

};

#endif 