Click here to Skip to main content
15,896,118 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 86.9K   3K   27  
Faster, smarter and better looking fonts rendered with OpenGL ES
#ifndef FTBITMAPCHAR_H_
#define FTBITMAPCHAR_H_

struct FT_GlyphRec_;

const int verticesPerQuad = 4;
const int indicesPerQuad = 6;
const int compVertPos = 2;
const int compVertTex = 2;

const int WHITE = 0xffffff;
const int BLACK = 0;

// each FTBitmapChar represents a character from the fnt file
// reads paramaters from fnt file and creates textured quad   
class FTBitmapChar
{
public:
//	enum { numVertices = 4 } Enum;
	FTBitmapChar();
	FTBitmapChar(int charCode)
	{
		m_charCode = charCode;
	}
	~FTBitmapChar();
	void Render(int x, int y) const;
	void SetXY(int x, int y) 
	{
		m_x = x;
		m_y = y;
	}
	int GetX2() const
	{
		return m_x + m_width; 
	}
	int GetY2() const
	{
		return m_x + m_width; 
	}
	int GetXAdvance() const
	{
		return m_xAdvance;
	}
	void SetXAdvance(int xAdvance)
	{
		m_xAdvance = xAdvance;
	}
	int GetHeight() const
	{
		return m_height;
	}
	int GetTotalHeight() const
	{
		return m_yOffset + m_height;
	}
	int GetWidth() const
	{
		return m_width;
	}
	void SetSize(int width, int height)
	{
		m_width = width;
		m_height = height;
	}
	void SetOffsets(int xOffset, int yOffset)
	{
		m_xOffset = xOffset;
		m_yOffset = yOffset;
	}
	int GetYOffset() const
	{
		return m_yOffset;
	}
	void ReduceYOffset(int amount)
	{
		m_yOffset -= amount;
	}
	void AddKerning(FTBitmapChar* pBitmapChar, int amount);
	bool IsLoaded() const
	{
		return m_x >= 0;
	}
	int GetNumPixels() const
	{
		return m_width*m_height;
	}
	int GetCharCode() const
	{
		return m_charCode;
	}
	void SetCharCode(int charCode)
	{
		m_charCode = charCode;
	}
	void SetGlyph(struct FT_GlyphRec_* pGlyph)
	{
		m_pGlyph = pGlyph;
	}
	void DrawToBitmap(unsigned char* pData, 
		int texWidth, int texHeight);
	bool IsEmpty() const
	{
		return m_width == 0 || m_height == 0;
	}
	void ReleaseGlyph();
	void InitTexCoords(int texWidth, int texHeight);
private:
	int m_x, m_y;
	int m_width;
	int m_height;
	int m_xOffset;
	int m_yOffset;
	int m_xAdvance;
	int m_charCode;	// for debugging
	float m_texCoords[verticesPerQuad*compVertTex];
	struct FT_GlyphRec_* m_pGlyph;
};

#endif /*FTBITMAPCHAR_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