/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/drivers/wintel32/graphics/ftgl/ftgl_font_manager.cpp

*/

#include "core.h"
#include "ftgl_font_manager.hpp"
#include "lib/ftgl/FTlibrary.h"
#include "lib/ftgl/FTGLExtrdFont.h"
#include "lib/ftgl/FTGLOutlineFont.h"
#include "lib/ftgl/FTGLPolygonFont.h"
#include "lib/ftgl/FTGLTextureFont.h"
#include "lib/ftgl/FTGLPixmapFont.h"
#include "lib/ftgl/FTGLBitmapFont.h"

class CDeviceFTGLFont : public CSGXFont {
public:
	virtual void drawFont(const sgxString &name, const sgxVector2 &pos) const;
	FTFont	*m_pFTFont;
};


CSGXFontManager *CDeviceFTGLFontManager::create()
{
	m_pSingleton = new CDeviceFTGLFontManager;
	if (m_pSingleton) {
		m_pSingleton->initialize();
	}
	return m_pSingleton;
}

CSGXFontManager *CDeviceFTGLFontManager::get()
{
	if (!m_pSingleton) {
		return create();
	}
	return m_pSingleton;
}

CDeviceFTGLFontManager::CDeviceFTGLFontManager() : CSGXFontManager()
{
}

CDeviceFTGLFontManager::~CDeviceFTGLFontManager()
{
}

void
CDeviceFTGLFontManager::initialize()
{
}

CSGXFont *	
CDeviceFTGLFontManager::registerFont(const sgxString &name, const sgxString &filename, const CSGXFontParams &params)
{
	FTFont *pFT;

	switch(params.m_iFontType) {
	case	CSGXFontParams::tFontTypes::tBitmapFontType:
		pFT = new FTGLBitmapFont(filename.c_str());
		break;

	case	CSGXFontParams::tFontTypes::tOutlineFontType:
		pFT = new FTGLOutlineFont(filename.c_str());
		break;

	case	CSGXFontParams::tFontTypes::tPolygonFontType:
		pFT = new FTGLPolygonFont(filename.c_str());
		break;

	case	CSGXFontParams::tFontTypes::tExtrudedFontType:
		pFT = new FTGLExtrdFont(filename.c_str());
		break;

	case	CSGXFontParams::tFontTypes::tPixmapFontType:
		pFT = new FTGLPixmapFont(filename.c_str());
		break;

	case	CSGXFontParams::tFontTypes::tTextureFontType:
		pFT = new FTGLTextureFont(filename.c_str());
		break;

	default:
		return NULL;
	}

	if (pFT && !pFT->Error()) {
		
		pFT->FaceSize(params.m_iTypeSize);
		pFT->Depth(params.m_iTypeDepth);
		pFT->CharMap(ft_encoding_unicode);

		CDeviceFTGLFont *pFont = new CDeviceFTGLFont;
		if (!pFont) {
			delete pFT;
			return NULL;
		}

		pFont->m_pFTFont = pFT;

		return m_Fonts.insert(RegisteredFontMap::value_type(name, pFont)).second ? pFont : NULL;
	}

	return NULL;
}

void
CDeviceFTGLFont::drawFont(const sgxString &name, const sgxVector2 &pos) const
{
	sgxColorRGBA col;
	
	CSGXGraphicsEngine::get()->GetColor(col);

	glColor4f(col.r, col.g, col.b, col.a);
	glRasterPos2f(pos.x, pos.y);

	m_pFTFont->Render(name.c_str());
}

			
