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

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

#include "Resources\BitmapFontManager.h"
#include "Resources\BitmapFontResourceData.h"
#include "Graphics\BitmapFontSettings.h"

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

namespace Game
{
    //=================================================================================================================
    /// @brief		Class that stores a bitmap font and displays strings in this font type. Expects font image to 
    ///				have 16 columns and 16 rows of characters, ordered by the layout of the codepage in use. 
    ///				Character spacings are precomptuted in xml files and loaded when the font is created.
    ///				These spacings are created by a tool that was written for another project.
    /// @remarks	This is adapted from previously written C#/XNA code. The adaptions make use of the resource 
    ///				management system of the DX framework since DX does not manage loadead resources automatically.
    ///				This class is a wrapper around an actual font resource, and does the 'work' of the font - i.e 
    ///				rendering.
	//=================================================================================================================

    class BitmapFont
    {
		public:	        

			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				//=====================================================================================================
				/// @brief		Creates a default invalid font
				//=====================================================================================================
				
				BitmapFont(){}
   
				//=====================================================================================================
				/// @brief		Creates the font from the given managed font resource. This is not intended to be 
				///				called directly. Use BitmapFontManager::GetFont() instead to retrieve the desired font.
				/// @param		res - a managed resource handle to an actual bitmap font resource
				//=====================================================================================================
				
				BitmapFont( const BitmapFontManager::ManagedResource & res ) : m_resource(res){}
				
				//=====================================================================================================
				/// @brief		Copy constructor. Creates this font from another.
				/// @param		other - other font to create from
				//=====================================================================================================
				
				BitmapFont( const BitmapFont & other ) : m_resource(other.m_resource){}
				
				//=====================================================================================================
				/// @brief		Assignment operator. Assigns the other font to this font.
				/// @param		other - other font to assign to this one
				/// @returns	This font
				//=====================================================================================================
				
				BitmapFont & operator = ( const BitmapFont & other )
				{
					// Copy resource handle:
					
					m_resource = other.m_resource;
					
					// Return us:
					
					return * this;
				}

				//=====================================================================================================
				/// @brief		Retrieves the loaded settings for this particular bitmap font. These are stored in the 
				///				XML file describing the font.
				/// @returns	Settings for this font
				/// @pre		The resource must be valid
				//=====================================================================================================
				
				inline const BitmapFontSettings & GetSettings()
				{ 
					// Make sure ok:
					
					ENSURE( m_resource.IsValid() , "The resource must be valid" )
				
					// Return the settings:
				
					return m_resource.GetResource()->GetData()->m_settings; 
				}
				
				/// Tells if this is a valid resource: do not attempt to use if not valid
				
				inline bool IsValid(){ return m_resource.IsValid(); }

				/// Draws a string at the specified location with the given transform matrix.

				void Draw( const char * text , const Vec3f & position , const Matrix4f & transform );
				
				/// Draws a string at the specified location with the given transform matrix.

				inline void Draw( const std::string & text , const Vec3f & position , const Matrix4f & transform )
				{
					Draw(text.c_str(),position,transform);
				}

				/// Draws a string centered horizontally over the given position with the given transform matrix.

				void DrawCentered ( const char * text, const Vec3f & position , const Matrix4f & transform );
				
				/// Draws a string centered horizontally over the given position with the given transform matrix.

				inline void DrawCentered ( const std::string & text , const Vec3f & position , const Matrix4f & transform )
				{
					DrawCentered( text.c_str() , position , transform );
				}				

				/// Draws a string right aligned to the given position with the given transform matrix.

				void DrawRightAligned( const char * text, const Vec3f & position , const Matrix4f & transform );
				
				/// Draws a string right aligned to the given position with the given transform matrix.

				inline void DrawRightAligned( const std::string & text, const Vec3f & position , const Matrix4f & transform )
				{
					DrawRightAligned( text.c_str() , position , transform );
				}				

				/// Gets the dimensions for a given string with the current font settings. 

				Vec2f GetSize( const char * text );
				
				/// Gets the dimensions for a given string with the current font settings. 

				inline Vec2f GetSize( const std::string & text )
				{
					return GetSize(text.c_str());
				}				
        
        private:
        
			//=========================================================================================================
			// Functions
			//=========================================================================================================			

				/// Setups the font for drawing. 

				bool DrawSetup( const Matrix4f & transform );

				/// Puts the specified character into the rendering buffer at the given character offset in the buffer.

				void PutBufferCharacter( unsigned char c , const Vec3f & position );
        
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Our managed resource handle to the actual font data itself
				
				BitmapFontManager::ManagedResource m_resource;
    };
}
    
#endif
