/*
	Lan Wars.
	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 SPRITE_RENDERER_H
#define SPRITE_RENDERER_H

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

#include "Graphics/Sprite.h"
#include "Graphics/PixelShader.h"
#include "Graphics/VertexShader.h"
#include "Graphics/VertexFormat.h"
#include "Graphics/ShaderUniform.h"

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

namespace Game
{	
	//=================================================================================================================
	//=================================================================================================================

	class Window;
	class Camera3D;

	//=================================================================================================================
	/// @brief		This class accepts a bunch of sprites to be drawn and renders them later on when requested to.
	///				It renders sprites in batches for efficiency.
	//=================================================================================================================

	class SpriteRenderer : public ObjectBase
	{
		public:

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

				/// Default constructor: creates the renderer

				SpriteRenderer( Display * display , VertexShaderManager * vsmgr , PixelShaderManager * psmgr );

				/// Destructor. Frees the sprite renderer.

				~SpriteRenderer();

				/// Adds the specified sprite to the list of sprites to be rendered

				void AddSprite( const Sprite & sprite );

				/// Draws all of the sprites within the sprite renderer

				void DrawAll( Camera3D * camera );
				
				/// Returns the name of this type

				virtual const char * GetTypeName() const { return "SpriteRenderer"; }				
				
		private:
		
			//=========================================================================================================
			// Structs
			//=========================================================================================================
			
				/// Represents a sprite vertex
				
				struct SpriteVertex 
				{
					/// This is the position of the vertex
					
					Vec3f m_pos;
					
					/// This is the texture coordinate of the vertex
					
					Vec2f m_tex;
					
					/// This is the color of the vertex
					
					Color m_col;
				};
				
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				#ifdef USE_DEBUG_CONSOLE
			
					/// Console command. Turns on/off z-testing for sprites
					
					static bool ConCmd_R_SpriteZTest( bool arg , bool arg_given );
					
				#endif
		
			//=========================================================================================================
			// Variables
			//=========================================================================================================

				/// This is a sorted queue of sprites to be drawn for the next frame

				std::priority_queue<Sprite> m_sprites;

				/// This is a reference to the graphics device

				IDirect3DDevice9 * m_device;
				
				/// Pixel shader used by the buffered point renderer

				PixelShader m_ps;

				/// Vertex shader used by the buffered point renderer

				VertexShader m_vs;

				/// Vertex format used by the buffered point renderer

				VertexFormat m_vf;

				/// Shader uniform for the transform variable on the vertex shader

				ShaderUniform m_u_transform;

				/// True if this renderer has valid resources

				bool m_valid_resources;
				
				#ifdef USE_DEBUG_CONSOLE
				
					/// Debug only: if true then do no depth testing for sprites
					
					static bool s_sprite_depth_test;
					
				#endif
	};
};

#endif
