1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class Font
{
public:
	Font()
	{
		if (FT_Init_FreeType(&ftLibrary))
		{
			printf("Could not initialize FreeType library\n");
			return;
		}
		glGenBuffers(1,&iVerts);
	}

	bool Load(std::string sFont, unsigned int Size = 18.0f)
	{
		if (FT_New_Face(ftLibrary,sFont.c_str(),0,&ftFace))
		{
			printf("Could not open font: %s\n",sFont.c_str());
			return true;
		}
		iSize = Size;
		FT_Set_Pixel_Sizes(ftFace,0,(int)iSize);

		FT_GlyphSlot gGlyph = ftFace->glyph;

		//Generating the texture atlas.
		//Rather than some amazing rectangular packing method, I'm just going
		//to have one long strip of letters with the height being that of the texture size.
		unsigned int width = 0;
		unsigned int height = 0;

		for (int i = 32; i < 128; i++)
		{
			if (FT_Load_Char(ftFace,i,FT_LOAD_RENDER))
			{
				printf("Error rendering letter %c for font %s.\n",i,sFont.c_str());
			}

			width += gGlyph->bitmap.width;
			height = std::max(height,(unsigned int)gGlyph->bitmap.rows);
		}

		//Generate the openGL texture
		glActiveTexture(GL_TEXTURE0);

		//if I texture exists then delete it.
		iTexture ? glDeleteBuffers(1,&iTexture):0;

		glGenTextures(1,&iTexture);
		glBindTexture(GL_TEXTURE_2D,iTexture);
		glPixelStorei(GL_UNPACK_ALIGNMENT,1);
		glTexImage2D(GL_TEXTURE_2D,0,GL_ALPHA,width,height,0,GL_ALPHA,GL_UNSIGNED_BYTE,0);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

		//load the glyphs and set the glyph data
		int x = 0;
		for (int i = 32; i < 128; i++)
		{
			if (FT_Load_Char(ftFace,i,FT_LOAD_RENDER))
			{
				//if it cant load the character
				continue;
			}
			//load the glyph map into the texture
			glTexSubImage2D(GL_TEXTURE_2D,0,x,0,
				gGlyph->bitmap.width,
				gGlyph->bitmap.rows,
				GL_ALPHA,
				GL_UNSIGNED_BYTE,
				gGlyph->bitmap.buffer);

			//move the "pen" down the strip
			x += gGlyph->bitmap.width;


			chars[i].ax = (float)(gGlyph->advance.x >> 6);
			chars[i].ay = (float)(gGlyph->advance.y >> 6);

			chars[i].bw = (float)gGlyph->bitmap.width;
			chars[i].bh = (float)gGlyph->bitmap.rows;

			chars[i].bl = (float)gGlyph->bitmap_left;
			chars[i].bt = (float)gGlyph->bitmap_top;

			chars[i].tx = (float)x/width;
		}


		//glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_UNSIGNED_INT,0);

		printf("Loaded font: %s\n",sFont.c_str());
		return true;
	}

	void Draw(std::string sString,Vector2f vPos = Vector2f(0,0),Vector2f vScale = Vector2f(1,1))
	{
		struct pPoint
		{
			pPoint()
			{
				x = y = s = t = 0;
			}
			pPoint(float a,float b,float c,float d)
			{
				x = a;
				y = b;
				s = c;
				t = d;
			}

			float x,y;
			float s,t;
		};

		pPoint* cCoordinates = new pPoint[6*sString.length()];

		int n = 0;

		float x,y;
		x = vPos.x();
		y = vPos.y();
		for (const char *p = sString.c_str(); *p; p++)
		{

			float x2 = x + chars[*p].bl * vScale.x();
			float y2 = -y - chars[*p].bt * vScale.y();
			float w = chars[*p].bw * vScale.x();
			float h = chars[*p].bh * vScale.y();

			x =+ chars[*p].ax * vScale.x();
			y =+ chars[*p].ay * vScale.y();

			//skip characters with no pixels
			//still advances though
			if (!w || !h)
			{
				continue;
			}

			//triangle one
			cCoordinates[n++] = pPoint( x2     , -y2      , chars[*p].tx					 , 0);
			cCoordinates[n++] = pPoint( x2 + w , -y2      , chars[*p].tx + chars[*p].bw / w  , 0);
			cCoordinates[n++] = pPoint( x2     , -y2-h    , chars[*p].tx					 , chars[*p].bh / h);

			cCoordinates[n++] = pPoint( x2 + w , -y2      , chars[*p].tx + chars[*p].bw / w  , 0);
			cCoordinates[n++] = pPoint( x2     , -y2-h    , chars[*p].tx					 , chars[*p].bh / h);
			cCoordinates[n++] = pPoint( x2 + w , -y2-h    , chars[*p].tx + chars[*p].bw / w  , chars[*p].bh / h);

		}

		glBindBuffer(GL_ARRAY_BUFFER,iVerts);
		glBindTexture(GL_TEXTURE_2D,iTexture);

		//Vertices
		glEnableClientState(GL_VERTEX_ARRAY);
		glVertexPointer(2,GL_FLOAT,sizeof(pPoint),(void*)0);
		//TexCoord 0
		glClientActiveTexture(GL_TEXTURE0);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glTexCoordPointer(2,GL_FLOAT,sizeof(pPoint),(void*)8);

		glCullFace(GL_NONE);

		glBufferData(GL_ARRAY_BUFFER,6*sString.length(),cCoordinates,GL_DYNAMIC_DRAW);
		glDrawArrays(GL_TRIANGLES,0,n);

		glCullFace(GL_BACK);

		glBindBuffer(GL_ARRAY_BUFFER,0);
		glBindBuffer(GL_TEXTURE_2D,0);
		glDisableClientState(GL_VERTEX_ARRAY);
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);

	}

	~Font()
	{
		glDeleteBuffers(1,&iVerts);
		glDeleteBuffers(1,&iTexture);
	}

private:
	unsigned int iSize;

	//openGL texture atlas
	unsigned int iTexture;

	//openGL geometry buffer;
	unsigned int iVerts;

	FT_Library ftLibrary;
	FT_Face ftFace;

	struct Character
	{
		float ax,ay;//Advance
		float bw,bh;//bitmap size
		float bl,bt;//bitmap left and top
		float tx;
	} chars[128];

};