Click here to Skip to main content
15,897,704 members
Articles / Mobile Apps / iPhone

FreeType on OpenGL ES (iPhone)

Rate me:
Please Sign up or sign in to vote.
4.75/5 (10 votes)
24 Nov 2010CPOL4 min read 87K   3K   27  
Faster, smarter and better looking fonts rendered with OpenGL ES
#ifndef FTBITMAPFONT_H_
#define FTBITMAPFONT_H_

class FTBitmapChar;
class FontAtlas;


class FTBitmapFont
{
public:
	FTBitmapFont(FontAtlas* pFontAtlas);
	~FTBitmapFont();
	void Load(const string& fileName);
	void DrawVertical(int x, int y, const string& text, int color, float alpha = 1.0f);
	// draws the string at position x, y (given in screen coordinates)
	int DrawString(int x, int y, const string& text, int color, float alpha = 1.0f) 
	{
		return DrawString(x, y, text.c_str(), color, alpha);
	}	
	int DrawStringShadow(int x, int y, const string& text, int color, int backColor) 
	{
		return DrawStringShadow(x, y, text.c_str(), color, backColor);
	}	
	int DrawString(int x, int y, const char* text, int color, float alpha = 1.0f);
	int DrawStringShadow(int x, int y, const char* text, int color, int backColor);
	int GetWidth(const char* text);
	int GetLineHeight() const
	{
		return m_lineHeight;
	}
	void SetLineHeight(int lineHeight)
	{
		m_lineHeight = lineHeight;
	}
	void Release();
	void Init();
	int GetCharWidth(const string& text, int index);
	void AddChar(int charCode, FTBitmapChar* pFTBitmapChar)
	{
		m_mapBitmapChar[charCode] = pFTBitmapChar;
	}
	void SetFTFace(struct FT_FaceRec_* pFTFace)
	{
		m_pFTFace = pFTFace;
	}
	void FinishCreating();
	FTBitmapChar* GetChar(int charCode)
	{
		fmap<int, FTBitmapChar*>::const_iterator iter;
		iter = m_mapBitmapChar.find(charCode);
		if (iter != m_mapBitmapChar.end()) 
			return iter->second;
		return NULL;
	}
private:
	void ReleaseFace();
	int m_lineHeight;
	fmap<int, FTBitmapChar*> m_mapBitmapChar;
	FontAtlas* m_pFontAtlas;
	struct FT_FaceRec_* m_pFTFace;
};

#endif /*FTBITMAPFONT_H_*/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Astronautz
Spain Spain
After working in the software industry for many years, I've started my own games company that specialises in strategy games for mobile platforms.

Comments and Discussions