/*
	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 RENDER_TARGET_H
#define RENDER_TARGET_H

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

#include "Graphics\Display.h"
#include "Graphics\DisplayEvictable.h"

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

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

	class Dispplay;
	
	//=================================================================================================================
	/// @brief		Represents a offscreen 2D render target surface.
	/// @remarks	Render target data is volatile and will be lost after a device reset. 
	//=================================================================================================================

	class RenderTarget : public DisplayEvictable
	{
		public:	
		
			//=========================================================================================================
			// Enums
			//=========================================================================================================
			
				/// Format for a render target:
				
				enum Format
				{
					/// 24 bit color with one unused byte for padding
				
					FORMAT_X8R8G8B8 = D3DFMT_X8R8G8B8,
					
					/// 32 bit color with alpha component
				
					FORMAT_A8R8G8B8 = D3DFMT_A8R8G8B8,
					
					// 32-bit Floating point format. Red component is only stored.
					
					FORMAT_FLOAT = D3DFMT_R32F
				};

			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Creates the render target with the given size and format
				
				RenderTarget
				( 
					Display * display												, 
					const Vec2u & size												, 
					Format format													, 
					Display::AntialiasingLevel aa_level = Display::ANTIALIAS_NONE	,
					bool clamp_to_max_texure_size = true
				);
				
				/// Destructor: releases the render target
				
				virtual ~RenderTarget();
				
				/// Tells the object to announce it's type name: this is the type name without the Game:: namespace

				virtual const char * GetTypeName() const;				
			
				/// Releases the render target before a device reset: not to be called by anything other than the display!

				virtual void ReleaseEvictableResources();
				
				/// Recreates the render target after a device reset: not to be called by anything other than the display!
				
				virtual void RecreateEvictableResources();
				
				/// Copies the contents of the render target into a texture that can be used: must be called before using ApplyTexture()
				
				void UpdateTexture();
				
				/// Applys the render target texture to the given sampler unit. Be sure to
				
				void ApplyTexture( unsigned sampler_unit );
				
				//=====================================================================================================
				/// @brief		Gets the surface used by the render target
				/// @returns	The surface for the render target
				/// @pre		The render target must be valid
				//=====================================================================================================
				
				inline IDirect3DSurface9 * GetSurface()
				{
					// Make sure ok:
					
					ENSURE( IsValid() , "The render target must first be valid" )
					
					// Return the surface
									
					return m_surface;
				}
				
				//=====================================================================================================
				/// @brief		Gets the size of the render target
				/// @returns	Size of the render target
				/// @pre		The render target must first be valid
				//=====================================================================================================
				
				inline Vec2u GetSize()
				{ 
					// Make sure ok:
					
					ENSURE( IsValid() , "The render target must first be valid" )
					
					// Return the size{:
				
					return m_size;
				}
				
				//=====================================================================================================
				/// @brief		Gets the surface format of the render target
				/// @returns	Format of the render target
				/// @pre		The render target must first be valid
				//=====================================================================================================
				
				inline Format GetFormat()
				{ 
					// Make sure ok:
					
					ENSURE( IsValid() , "The render target must first be valid" )
					
					// Return the size:
				
					return m_surface_format;
				}
				
				//=====================================================================================================
				/// @brief		Gets the level of anti-aliasing used by the render target
				/// @returns	Level of anti-aliasing used by the render target
				/// @pre		The render target must first be valid
				//=====================================================================================================
				
				inline Display::AntialiasingLevel GetAALevel()
				{ 
					// Make sure ok:
					
					ENSURE( IsValid() , "The render target must first be valid" )
					
					// Return the size:
				
					return m_aa_level;
				}							

		private:
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Determines the maximum anti-aliasing level supported by the settings for the render target
				
				Display::AntialiasingLevel GetMaxAALevel();

			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Format the render target uses
				
				Format m_surface_format;
				
				/// Size of the render target
				
				Vec2u m_size;
				
				/// Anti-aliasing format used by the render target
				
				Display::AntialiasingLevel m_aa_level;
				
				/// The actual render target surface itself
				
				IDirect3DSurface9 * m_surface;
				
				/// Texture used by the render target to copy RT data into
				
				IDirect3DTexture9 * m_texture;
	};

};

#endif 