/*
	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.
*/

//=====================================================================================================================
/// @brief		This class displays a textured background that scrolls as the camera moves.
//=====================================================================================================================

#include "Precompiled.h"
#include "Background.h"
#include "Game\GameCore.h"
#include "Graphics\Display.h"
#include "Graphics\Camera2D.h"

using namespace Game;
using namespace std;

//=====================================================================================================================
// Constants
//=====================================================================================================================

/// Area of the game world over which the background area is shown

const Vec2f Background::BACKGROUND_TEX_AREA = Vec2f( 1024 , 1024 );

//=====================================================================================================================
/// @brief		Constructor: creates the background
/// @param		texture_name - name of the texture to use for the background
/// @pre		The game must exist
/// @pre		The game must have a texture resource manager
/// @pre		The game must have a pixel shader resource manager
/// @pre		The game must have a vertex shader resource manager
//=====================================================================================================================

Background::Background( const string & texture_name )
{
	// Get the game:
	
	GameCore * game = GameCore::GetSingleton();

	// Make sure ok:
	
	ENSURE( game->GetTextureManager()		, "The game must have a texture resource manager"		);
	ENSURE( game->GetVertexShaderManager()	, "The game must have a pixel shader resource manager"	);
	ENSURE( game->GetVertexShaderManager()	, "The game must have a vertex shader resource manager"	);

	// Load all resources
	
	m_texture		= game->GetTextureManager()->GetTexture(texture_name);
	m_vertex_shader = game->GetVertexShaderManager()->GetVertexShader("Shaders\\textured.vsh.bin");
	m_pixel_shader	= game->GetPixelShaderManager()->GetPixelShader("Shaders\\textured.psh.bin");
	
	// Vertex format:
	
	{
		const VertexFormat::ElementUsage usages[] = 
		{
			VertexFormat::VF_USAGE_POSITION	,
			VertexFormat::VF_USAGE_COLOR	,
			VertexFormat::VF_USAGE_TEXCOORD			
		};
		
		const VertexFormat::ElementType types[] = 
		{
			VertexFormat::VF_TYPE_FLOAT4	,
			VertexFormat::VF_TYPE_FLOAT4	,
			VertexFormat::VF_TYPE_FLOAT2			
		};
		
		m_vertex_format = VertexFormat( game->GetDisplay() , 3 , usages , types );
	}	
	
	/// Uniforms:
	
	if ( m_vertex_shader.IsValid() ) m_u_transform	= m_vertex_shader.GetUniform("Transform");
	if ( m_pixel_shader.IsValid()  ) m_u_texture	= m_pixel_shader.GetUniform("Texture");
	
	/// Setup vertices:
	
	m_vertices[0].m_pos = Vec4f( -1.0f , +1.0f , 0.99f );
	m_vertices[1].m_pos = Vec4f( +1.0f , +1.0f , 0.99f );
	m_vertices[2].m_pos = Vec4f( -1.0f , -1.0f , 0.99f );
	m_vertices[3].m_pos = Vec4f( +1.0f , -1.0f , 0.99f );
	
	m_vertices[0].m_col = Vec4f::One();
	m_vertices[1].m_col = Vec4f::One();
	m_vertices[2].m_col = Vec4f::One();
	m_vertices[3].m_col = Vec4f::One();

	m_vertices[0].m_tex = Vec2f(0,0);
	m_vertices[1].m_tex = Vec2f(1,0);
	m_vertices[2].m_tex = Vec2f(0,1);
	m_vertices[3].m_tex = Vec2f(1,1);
	
	// See if all valid:
	
	if 
	( 
		m_texture.IsValid()			&& 
		m_vertex_shader.IsValid()	&& 
		m_pixel_shader.IsValid()	&& 
		m_u_transform.IsValid()		&&
		m_u_texture.IsValid()
	)
	{
		m_valid_graphics_resources = true;
	}
	else
	{
		m_valid_graphics_resources = false;
	}
}			

//=====================================================================================================================
/// @brief		Annouces the type name for this object.
/// @returns	Type name of this object
//=====================================================================================================================

const char * Background::GetTypeName() const
{
	return "Background";
}

//=====================================================================================================================
/// @brief		Draw function: draws the background
/// @param		c - camera to view from
/// @pre		Camera must not be null
/// @pre		Game must exist
/// @pre		Game must have a valid camera
//=====================================================================================================================

void Background::OnDraw( Camera2D * c )
{
	// Make sure ok:
	
	ENSURE( c , "Camera must not be null" );
	
	// Make sure ok:
	
	ENSURE( GameCore::GetSingleton() , "Game must exist" );
	
	// Abort if invalid resources:
	
	if ( m_valid_graphics_resources == false ) return;
	
	// Get the display:
	
	Display * d = GameCore::GetSingleton()->GetDisplay();

	// Get current viewing area half extents:
	
	Vec2f a = c->GetViewArea() * 0.5f;
	
	// Get camera position:
	
	Vec2f p = c->GetPosition();
	
	// Set the texure coordinates according to camera position
	
	m_vertices[0].m_tex = Vec2f( ( p.x - a.x ) / BACKGROUND_TEX_AREA.x , ( p.y + a.y ) / BACKGROUND_TEX_AREA.y );
	m_vertices[1].m_tex = Vec2f( ( p.x + a.x ) / BACKGROUND_TEX_AREA.x , ( p.y + a.y ) / BACKGROUND_TEX_AREA.y );
	m_vertices[2].m_tex = Vec2f( ( p.x - a.x ) / BACKGROUND_TEX_AREA.x , ( p.y - a.y ) / BACKGROUND_TEX_AREA.y );
	m_vertices[3].m_tex = Vec2f( ( p.x + a.x ) / BACKGROUND_TEX_AREA.x , ( p.y - a.y ) / BACKGROUND_TEX_AREA.y );
	
	// Apply everything:
	
	m_vertex_format.Apply();
	m_pixel_shader.Apply();
	m_vertex_shader.Apply();
	
	// Set texture and transform:
	
	m_u_texture.BindTexture(m_texture);
	m_u_transform.SetMatrix(Matrix4f::Identity());
	
	// Draw:
	
	d->DrawUserPrimitives( Display::PT_TRIANGLE_STRIP , m_vertices , 2 );
}
