#include "fontmanager.h"
#include "devicestatemanager.h"
#include "buffermanager.h"

FontManager::FontManager(DeviceStateManager*device)
{
	m_Device = device;

	m_VDecl = NULL;
	m_VB = NULL;
	m_FX = NULL;

	D3DVERTEXELEMENT9 decl[] =
    {
        { 0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITIONT, 0 }, 
		{ 0, 16, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, 
		{ 0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, 
        D3DDECL_END()
    };
	m_Device->GetDevice()->CreateVertexDeclaration( decl, &m_VDecl );

	m_VB = m_Device->GetBufferManager()->CreateDynamicVB(2048);
	m_FX = m_Device->GetShaderManager()->CreateFX("Shaders\\Menu.fx");
}

FontManager::~FontManager(void)
{
	m_Fonts.Clear();
	SAFERELEASE(m_VB);
	SAFERELEASE(m_FX);
	SAFERELEASE(m_VDecl);
}

void FontManager::Restore(void)
{
/*	ListElement<Font> *el = m_Fonts.GetHead();
	while(el)
	{
		el->GetInfo()->Restore();
		el = el->GetNext();
	}*/
}

void FontManager::Invalidate(void)
{
/*	ListElement<Font> *el = m_Fonts.GetHead();
	while(el)
	{
		el->GetInfo()->Invalidate();
		el = el->GetNext();
	}*/
}

Font* FontManager::Create(FontParam &fntp)
{
	FOREACH(efnt,m_Fonts,Font)
	{
		if(*(efnt->GetInfo()->GetParam()) == fntp)
		{
			efnt->GetInfo()->IncRefCount();
			return efnt->GetInfo();
		}
	}
	Font* fnt = new Font(this,fntp);
	m_Fonts.AddTail(fnt,(DWORD)fnt);
	return fnt;
}

void FontManager::Release(Font*fnt)
{
	ListElement<Font> *el=m_Fonts.GetElement((DWORD)fnt);
	if(el)
	{
		el->Unlink(); //font already delete
		delete el;
		//el->Delete();
	}
}