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

//=====================================================================================================================
/// @brief		Type of resource manager that looks after loading pixel shaders.
//=====================================================================================================================

#include "Precompiled.h"
#include "Resources\BitmapFontManager.h"
#include "Resources\VertexShaderManager.h"
#include "Resources\PixelShaderManager.h"
#include "Resources\TextureManager.h"
#include "Graphics\Display.h"
#include "Graphics\BitmapFont.h"
#include "Graphics\VertexFormat.h"

using namespace Game;

//=====================================================================================================================
/// @brief		Constructor. Creates the bitmap font manager
/// @param		display - display used by the game
/// @param		vs_mgr - A vertex shader manager that can be used to load vertex shaders
/// @param		ps_mgr - A vertex shader manager that can be used to load pixel shaders
/// @param		tex_mgr - A texture manager that can be used to load textures
/// @pre		Display cannot be null
/// @pre		The display must have a valid d3d device
/// @pre		Vertex shader manager cannot be null
/// @pre		Pixel shader manager cannot be null
/// @pre		Texture manager cannot be null
//=====================================================================================================================

BitmapFontManager::BitmapFontManager
(
	Display *				display,
	VertexShaderManager *	vs_mgr ,  
	PixelShaderManager *	ps_mgr ,
	TextureManager *		tex_mgr
)
: ResourceManager(0) , m_render_buffer_stack( sizeof(BitmapFontVertex) * RENDER_BUFFER_SIZE )
{
	// Debug checks:
	
	ENSURE( display != 0			, "Display cannot be null"						)
	ENSURE( display->GetDevice()	, "The display must have a valid d3d device"	)
	ENSURE( vs_mgr	!= 0			, "Vertex shader manager cannot be null"		)
	ENSURE( ps_mgr	!= 0			, "Pixel shader manager cannot be null"			)
	ENSURE( tex_mgr	!= 0			, "Texture manager cannot be null"				)
	
	// Save:
	
	m_d3d_device			= display->GetDevice();
	m_texture_manager		= tex_mgr;
	m_vertex_shader_manager	= vs_mgr;
	m_pixel_shader_manager	= ps_mgr;

	// Add a reference to everything:
	
	m_d3d_device->AddRef();
	vs_mgr->AddReference();
	ps_mgr->AddReference();
	tex_mgr->AddReference();
	
	// Need to create our vertex format:
	
	VertexFormat::ElementUsage usages[] = 
	{
		VertexFormat::VF_USAGE_POSITION	,
		VertexFormat::VF_USAGE_COLOR	,
		VertexFormat::VF_USAGE_TEXCOORD
	};
	
	VertexFormat::ElementType types[] = 
	{
		VertexFormat::VF_TYPE_FLOAT3,
		VertexFormat::VF_TYPE_FLOAT4,
		VertexFormat::VF_TYPE_FLOAT2
	};	
	
	// Make our vertex declaration:
	
	m_vertex_format = new VertexFormat(display,3,usages,types);
}

//=====================================================================================================================
/// @brief		Destructor. Destroys the pixel shader manager.
//=====================================================================================================================

BitmapFontManager::~BitmapFontManager()
{
	// Cleanup all references
		
	m_vertex_shader_manager->RemoveReference();
	m_pixel_shader_manager->RemoveReference();
	m_texture_manager->RemoveReference();
	m_d3d_device->Release();
	
	// Delete vertex format:
	
	delete m_vertex_format;
}

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

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

//=====================================================================================================================
/// @brief		Retrieves the given bitmap font
/// @param		name - name of the font file to get. this is an xml file containing the font description.
/// @returns	The requested font, or an invalid font on failure
/// @pre		The given font name must not be null
//=====================================================================================================================

BitmapFont BitmapFontManager::GetBitmapFont( const char * name )
{
	// Make sure ok:

	ENSURE( name != 0 , "The given font name must not be null" )
	
	// Return the font:
	
	return BitmapFont( GetResource(name) );
}

//=====================================================================================================================
/// @brief		Retrieves the given bitmap font
/// @param		name - name of the font file to get. this is an xml file containing the font description.
/// @returns	The requested font, or an invalid font on failure
//=====================================================================================================================

BitmapFont BitmapFontManager::GetBitmapFont( const std::string & name )
{	
	// Return the font:
	
	return BitmapFont( GetResource(name.c_str()) );
}