1 | //*******************************************************************
|
---|
2 | //glfont2.h -- Header for glfont2.cpp
|
---|
3 | //Copyright (c) 1998-2002 Brad Fish
|
---|
4 | //See glfont.html for terms of use
|
---|
5 | //May 14, 2002
|
---|
6 | //*******************************************************************
|
---|
7 |
|
---|
8 | #ifndef GLFONT2_H
|
---|
9 | #define GLFONT2_H
|
---|
10 |
|
---|
11 | //*******************************************************************
|
---|
12 | //GLFont Interface
|
---|
13 | //*******************************************************************
|
---|
14 |
|
---|
15 | //glFont namespace
|
---|
16 | namespace glfont
|
---|
17 | {
|
---|
18 | class GLFont;
|
---|
19 | }
|
---|
20 |
|
---|
21 | //glFont class
|
---|
22 | class glfont::GLFont
|
---|
23 | {
|
---|
24 | private:
|
---|
25 |
|
---|
26 | //glFont character structure
|
---|
27 | typedef struct
|
---|
28 | {
|
---|
29 | float dx, dy;
|
---|
30 | float tx1, ty1;
|
---|
31 | float tx2, ty2;
|
---|
32 | } GLFontChar;
|
---|
33 |
|
---|
34 | //glFont header structure
|
---|
35 | struct
|
---|
36 | {
|
---|
37 | int tex;
|
---|
38 | int tex_width, tex_height;
|
---|
39 | int start_char, end_char;
|
---|
40 | GLFontChar *chars;
|
---|
41 | } header;
|
---|
42 |
|
---|
43 | public:
|
---|
44 |
|
---|
45 | //Constructor
|
---|
46 | GLFont ();
|
---|
47 |
|
---|
48 | //Destructor
|
---|
49 | ~GLFont ();
|
---|
50 |
|
---|
51 | public:
|
---|
52 |
|
---|
53 | //Creates the glFont
|
---|
54 | bool Create (const char *file_name, int tex);
|
---|
55 | bool Create (const std::string &file_name, int tex);
|
---|
56 |
|
---|
57 | //Destroys the glFont
|
---|
58 | void Destroy (void);
|
---|
59 |
|
---|
60 | //Texture size retrieval methods
|
---|
61 | void GetTexSize (std::pair<int, int> *size);
|
---|
62 | int GetTexWidth (void);
|
---|
63 | int GetTexHeight (void);
|
---|
64 |
|
---|
65 | //Character interval retrieval methods
|
---|
66 | void GetCharInterval (std::pair<int, int> *interval);
|
---|
67 | int GetStartChar (void);
|
---|
68 | int GetEndChar (void);
|
---|
69 |
|
---|
70 | //Character size retrieval methods
|
---|
71 | void GetCharSize (int c, std::pair<int, int> *size);
|
---|
72 | int GetCharWidth (int c);
|
---|
73 | int GetCharHeight (int c);
|
---|
74 |
|
---|
75 | //Calculates the size in pixels of a character array
|
---|
76 | template<class T> void GetStringSize (const T *text,
|
---|
77 | std::pair<int, int> *size)
|
---|
78 | {
|
---|
79 | const T *i;
|
---|
80 | GLFontChar *glfont_char;
|
---|
81 | float width;
|
---|
82 |
|
---|
83 | //Height is the same for now...might change in future
|
---|
84 | size->second = (int)(header.chars[header.start_char]->dy *
|
---|
85 | header.tex_height);
|
---|
86 |
|
---|
87 | //Calculate width of string
|
---|
88 | width = 0.0F;
|
---|
89 | for (i = text; *i != (T)'\0'; i++)
|
---|
90 | {
|
---|
91 | //Make sure character is in range
|
---|
92 | if (*i < header.start_char || *i > header.end_char)
|
---|
93 | continue;
|
---|
94 |
|
---|
95 | //Get pointer to glFont character
|
---|
96 | glfont_char = &header.chars[*i - header.start_char];
|
---|
97 |
|
---|
98 | //Get width and height
|
---|
99 | width += glfont_char->dx * header.tex_width;
|
---|
100 | }
|
---|
101 |
|
---|
102 | //Save width
|
---|
103 | size->first = (int)width;
|
---|
104 | }
|
---|
105 |
|
---|
106 | //Template function to calculate size of a std::basic_string
|
---|
107 | template<class T> void GetStringSize (
|
---|
108 | const std::basic_string<T> &text, std::pair<int, int> *size)
|
---|
109 | {
|
---|
110 | unsigned int i;
|
---|
111 | T *c;
|
---|
112 | GLFontChar *glfont_char;
|
---|
113 | float width;
|
---|
114 |
|
---|
115 | //Height is the same for now...might change in future
|
---|
116 | size->second = (int)(header.chars[header.start_char]->dy *
|
---|
117 | header.tex_height);
|
---|
118 |
|
---|
119 | //Calculate width of string
|
---|
120 | width = 0.0F;
|
---|
121 | for (i = 0; i < text.size(); i++)
|
---|
122 | {
|
---|
123 | //Make sure character is in range
|
---|
124 | c = text[i];
|
---|
125 | if (c < header.start_char || c > header.end_char)
|
---|
126 | continue;
|
---|
127 |
|
---|
128 | //Get pointer to glFont character
|
---|
129 | glfont_char = &header.chars[c - header.start_char];
|
---|
130 |
|
---|
131 | //Get width and height
|
---|
132 | width += glfont_char->dx * header.tex_width;
|
---|
133 | }
|
---|
134 |
|
---|
135 | //Save width
|
---|
136 | size->first = (int)width;
|
---|
137 | }
|
---|
138 |
|
---|
139 | //Begins text output with this font
|
---|
140 | void Begin (void);
|
---|
141 |
|
---|
142 | //Template function to output a character array
|
---|
143 | template<class T> void DrawString (const T *text, float x,
|
---|
144 | float y)
|
---|
145 | {
|
---|
146 | const T *i;
|
---|
147 | GLFontChar *glfont_char;
|
---|
148 | float width, height;
|
---|
149 |
|
---|
150 | //Begin rendering quads
|
---|
151 | glBegin(GL_QUADS);
|
---|
152 |
|
---|
153 | //Loop through characters
|
---|
154 | for (i = text; *i != (T)'\0'; i++)
|
---|
155 | {
|
---|
156 | //Make sure character is in range
|
---|
157 | if (*i < header.start_char || *i > header.end_char)
|
---|
158 | continue;
|
---|
159 |
|
---|
160 | //Get pointer to glFont character
|
---|
161 | glfont_char = &header.chars[*i - header.start_char];
|
---|
162 |
|
---|
163 | //Get width and height
|
---|
164 | width = glfont_char->dx * header.tex_width;
|
---|
165 | height = glfont_char->dy * header.tex_height;
|
---|
166 |
|
---|
167 | //Specify vertices and texture coordinates
|
---|
168 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
169 | glVertex3f(x, y, 0.0F);
|
---|
170 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
171 | glVertex3f(x, y - height, 0.0F);
|
---|
172 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
173 | glVertex3f(x + width, y - height, 0.0F);
|
---|
174 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
175 | glVertex3f(x + width, y, 0.0F);
|
---|
176 |
|
---|
177 | //Move to next character
|
---|
178 | x += width;
|
---|
179 | }
|
---|
180 |
|
---|
181 | //Stop rendering quads
|
---|
182 | glEnd();
|
---|
183 | }
|
---|
184 |
|
---|
185 | //Template function to draw a std::basic_string
|
---|
186 | template<class T> void DrawString (
|
---|
187 | const std::basic_string<T> &text, float x, float y)
|
---|
188 | {
|
---|
189 | unsigned int i;
|
---|
190 | T c;
|
---|
191 | GLFontChar *glfont_char;
|
---|
192 | float width, height;
|
---|
193 |
|
---|
194 | //Begin rendering quads
|
---|
195 | glBegin(GL_QUADS);
|
---|
196 |
|
---|
197 | //Loop through characters
|
---|
198 | for (i = 0; i < text.size(); i++)
|
---|
199 | {
|
---|
200 | //Make sure character is in range
|
---|
201 | c = text[i];
|
---|
202 | if (c < header.start_char || c > header.end_char)
|
---|
203 | continue;
|
---|
204 |
|
---|
205 | //Get pointer to glFont character
|
---|
206 | glfont_char = &header.chars[c - header.start_char];
|
---|
207 |
|
---|
208 | //Get width and height
|
---|
209 | width = glfont_char->dx * header.tex_width;
|
---|
210 | height = glfont_char->dy * header.tex_height;
|
---|
211 |
|
---|
212 | //Specify vertices and texture coordinates
|
---|
213 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
214 | glVertex3f(x, y, 0.0F);
|
---|
215 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
216 | glVertex3f(x, y - height, 0.0F);
|
---|
217 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
218 | glVertex3f(x + width, y - height, 0.0F);
|
---|
219 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
220 | glVertex3f(x + width, y, 0.0F);
|
---|
221 |
|
---|
222 | //Move to next character
|
---|
223 | x += width;
|
---|
224 | }
|
---|
225 |
|
---|
226 | //Stop rendering quads
|
---|
227 | glEnd();
|
---|
228 | }
|
---|
229 |
|
---|
230 | //Template function to output a scaled character array
|
---|
231 | template<class T> void DrawString (const T *text, float scalar,
|
---|
232 | float x, float y)
|
---|
233 | {
|
---|
234 | const T *i;
|
---|
235 | GLFontChar *glfont_char;
|
---|
236 | float width, height;
|
---|
237 |
|
---|
238 | //Begin rendering quads
|
---|
239 | glBegin(GL_QUADS);
|
---|
240 |
|
---|
241 | //Loop through characters
|
---|
242 | for (i = text; *i != (T)'\0'; i++)
|
---|
243 | {
|
---|
244 | //Make sure character is in range
|
---|
245 | if (*i < header.start_char || *i > header.end_char)
|
---|
246 | continue;
|
---|
247 |
|
---|
248 | //Get pointer to glFont character
|
---|
249 | glfont_char = &header.chars[*i - header.start_char];
|
---|
250 |
|
---|
251 | //Get width and height
|
---|
252 | width = (glfont_char->dx * header.tex_width) * scalar;
|
---|
253 | height = (glfont_char->dy * header.tex_height) * scalar;
|
---|
254 |
|
---|
255 | //Specify vertices and texture coordinates
|
---|
256 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
257 | glVertex3f(x, y, 0.0F);
|
---|
258 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
259 | glVertex3f(x, y - height, 0.0F);
|
---|
260 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
261 | glVertex3f(x + width, y - height, 0.0F);
|
---|
262 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
263 | glVertex3f(x + width, y, 0.0F);
|
---|
264 |
|
---|
265 | //Move to next character
|
---|
266 | x += width;
|
---|
267 | }
|
---|
268 |
|
---|
269 | //Stop rendering quads
|
---|
270 | glEnd();
|
---|
271 | }
|
---|
272 |
|
---|
273 | //Template function to output a scaled std::basic_string
|
---|
274 | template<class T> void DrawString (
|
---|
275 | const std::basic_string<T> &text, float scalar, float x,
|
---|
276 | float y)
|
---|
277 | {
|
---|
278 | unsigned int i;
|
---|
279 | T c;
|
---|
280 | GLFontChar *glfont_char;
|
---|
281 | float width, height;
|
---|
282 |
|
---|
283 | //Begin rendering quads
|
---|
284 | glBegin(GL_QUADS);
|
---|
285 |
|
---|
286 | //Loop through characters
|
---|
287 | for (i = 0; i < text.size(); i++)
|
---|
288 | {
|
---|
289 | //Make sure character is in range
|
---|
290 | c = text[i];
|
---|
291 | if (c < header.start_char || c > header.end_char)
|
---|
292 | continue;
|
---|
293 |
|
---|
294 | //Get pointer to glFont character
|
---|
295 | glfont_char = &header.chars[c - header.start_char];
|
---|
296 |
|
---|
297 | //Get width and height
|
---|
298 | width = (glfont_char->dx * header.tex_width) * scalar;
|
---|
299 | height = (glfont_char->dy * header.tex_height) * scalar;
|
---|
300 |
|
---|
301 | //Specify vertices and texture coordinates
|
---|
302 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
303 | glVertex3f(x, y, 0.0F);
|
---|
304 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
305 | glVertex3f(x, y - height, 0.0F);
|
---|
306 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
307 | glVertex3f(x + width, y - height, 0.0F);
|
---|
308 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
309 | glVertex3f(x + width, y, 0.0F);
|
---|
310 |
|
---|
311 | //Move to next character
|
---|
312 | x += width;
|
---|
313 | }
|
---|
314 |
|
---|
315 | //Stop rendering quads
|
---|
316 | glEnd();
|
---|
317 | }
|
---|
318 |
|
---|
319 | //Template function to output a colored character array
|
---|
320 | template<class T> void DrawString (const T *text, float x,
|
---|
321 | float y, const float *top_color,
|
---|
322 | const float *bottom_color)
|
---|
323 | {
|
---|
324 | const T *i;
|
---|
325 | GLFontChar *glfont_char;
|
---|
326 | float width, height;
|
---|
327 |
|
---|
328 | //Begin rendering quads
|
---|
329 | glBegin(GL_QUADS);
|
---|
330 |
|
---|
331 | //Loop through characters
|
---|
332 | for (i = text; *i != '\0'; i++)
|
---|
333 | {
|
---|
334 | //Make sure character is in range
|
---|
335 | if (*i < header.start_char || *i > header.end_char)
|
---|
336 | continue;
|
---|
337 |
|
---|
338 | //Get pointer to glFont character
|
---|
339 | glfont_char = &header.chars[*i - header.start_char];
|
---|
340 |
|
---|
341 | //Get width and height
|
---|
342 | width = glfont_char->dx * header.tex_width;
|
---|
343 | height = glfont_char->dy * header.tex_height;
|
---|
344 |
|
---|
345 | //Specify colors, vertices, and texture coordinates
|
---|
346 | glColor3fv(top_color);
|
---|
347 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
348 | glVertex3f(x, y, 0.0F);
|
---|
349 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
350 | glVertex3f(x + width, y, 0.0F);
|
---|
351 | glColor3fv(bottom_color);
|
---|
352 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
353 | glVertex3f(x + width, y - height, 0.0F);
|
---|
354 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
355 | glVertex3f(x, y - height, 0.0F);
|
---|
356 |
|
---|
357 | //Move to next character
|
---|
358 | x += width;
|
---|
359 | }
|
---|
360 |
|
---|
361 | //Stop rendering quads
|
---|
362 | glEnd();
|
---|
363 | }
|
---|
364 |
|
---|
365 | //Template function to output a colored std::basic_string
|
---|
366 | template<class T> void DrawString (
|
---|
367 | const std::basic_string<T> &text, float x, float y,
|
---|
368 | const float *top_color, const float *bottom_color)
|
---|
369 | {
|
---|
370 | unsigned int i;
|
---|
371 | T c;
|
---|
372 | GLFontChar *glfont_char;
|
---|
373 | float width, height;
|
---|
374 |
|
---|
375 | //Begin rendering quads
|
---|
376 | glBegin(GL_QUADS);
|
---|
377 |
|
---|
378 | //Loop through characters
|
---|
379 | for (i = 0; i < text.size(); i++)
|
---|
380 | {
|
---|
381 | //Make sure character is in range
|
---|
382 | c = text[i];
|
---|
383 | if (c < header.start_char || c > header.end_char)
|
---|
384 | continue;
|
---|
385 |
|
---|
386 | //Get pointer to glFont character
|
---|
387 | glfont_char = &header.chars[c - header.start_char];
|
---|
388 |
|
---|
389 | //Get width and height
|
---|
390 | width = glfont_char->dx * header.tex_width;
|
---|
391 | height = glfont_char->dy * header.tex_height;
|
---|
392 |
|
---|
393 | //Specify colors, vertices, and texture coordinates
|
---|
394 | glColor3fv(top_color);
|
---|
395 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
396 | glVertex3f(x, y, 0.0F);
|
---|
397 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
398 | glVertex3f(x + width, y, 0.0F);
|
---|
399 | glColor3fv(bottom_color);
|
---|
400 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
401 | glVertex3f(x + width, y - height, 0.0F);
|
---|
402 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
403 | glVertex3f(x, y - height, 0.0F);
|
---|
404 |
|
---|
405 | //Move to next character
|
---|
406 | x += width;
|
---|
407 | }
|
---|
408 |
|
---|
409 | //Stop rendering quads
|
---|
410 | glEnd();
|
---|
411 | }
|
---|
412 |
|
---|
413 | //Template function to output a scaled, colored character array
|
---|
414 | template<class T> void DrawString (const T *text, float scalar,
|
---|
415 | float x, float y, const float *top_color,
|
---|
416 | const float *bottom_color)
|
---|
417 | {
|
---|
418 | const T *i;
|
---|
419 | GLFontChar *glfont_char;
|
---|
420 | float width, height;
|
---|
421 |
|
---|
422 | //Begin rendering quads
|
---|
423 | glBegin(GL_QUADS);
|
---|
424 |
|
---|
425 | //Loop through characters
|
---|
426 | for (i = text; *i != '\0'; i++)
|
---|
427 | {
|
---|
428 | //Make sure character is in range
|
---|
429 | if (*i < header.start_char || *i > header.end_char)
|
---|
430 | continue;
|
---|
431 |
|
---|
432 | //Get pointer to glFont character
|
---|
433 | glfont_char = &header.chars[*i - header.start_char];
|
---|
434 |
|
---|
435 | //Get width and height
|
---|
436 | width = (glfont_char->dx * header.tex_width) * scalar;
|
---|
437 | height = (glfont_char->dy * header.tex_height) * scalar;
|
---|
438 |
|
---|
439 | //Specify colors, vertices, and texture coordinates
|
---|
440 | glColor3fv(top_color);
|
---|
441 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
442 | glVertex3f(x, y, 0.0F);
|
---|
443 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
444 | glVertex3f(x + width, y, 0.0F);
|
---|
445 | glColor3fv(bottom_color);
|
---|
446 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
447 | glVertex3f(x + width, y - height, 0.0F);
|
---|
448 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
449 | glVertex3f(x, y - height, 0.0F);
|
---|
450 |
|
---|
451 | //Move to next character
|
---|
452 | x += width;
|
---|
453 | }
|
---|
454 |
|
---|
455 | //Stop rendering quads
|
---|
456 | glEnd();
|
---|
457 | }
|
---|
458 |
|
---|
459 | //Template function to output a scaled, colored std::basic_string
|
---|
460 | template<class T> void DrawString (
|
---|
461 | const std::basic_string<T> &text, float scalar, float x,
|
---|
462 | float y, const float *top_color, const float *bottom_color)
|
---|
463 | {
|
---|
464 | unsigned int i;
|
---|
465 | T c;
|
---|
466 | GLFontChar *glfont_char;
|
---|
467 | float width, height;
|
---|
468 |
|
---|
469 | //Begin rendering quads
|
---|
470 | glBegin(GL_QUADS);
|
---|
471 |
|
---|
472 | //Loop through characters
|
---|
473 | for (i = 0; i < text.size(); i++)
|
---|
474 | {
|
---|
475 | //Make sure character is in range
|
---|
476 | c = text[i];
|
---|
477 | if (c < header.start_char || c > header.end_char)
|
---|
478 | continue;
|
---|
479 |
|
---|
480 | //Get pointer to glFont character
|
---|
481 | glfont_char = &header.chars[c - header.start_char];
|
---|
482 |
|
---|
483 | //Get width and height
|
---|
484 | width = (glfont_char->dx * header.tex_width) * scalar;
|
---|
485 | height = (glfont_char->dy * header.tex_height) * scalar;
|
---|
486 |
|
---|
487 | //Specify colors, vertices, and texture coordinates
|
---|
488 | glColor3fv(top_color);
|
---|
489 | glTexCoord2f(glfont_char->tx1, glfont_char->ty1);
|
---|
490 | glVertex3f(x, y, 0.0F);
|
---|
491 | glTexCoord2f(glfont_char->tx2, glfont_char->ty1);
|
---|
492 | glVertex3f(x + width, y, 0.0F);
|
---|
493 | glColor3fv(bottom_color);
|
---|
494 | glTexCoord2f(glfont_char->tx2, glfont_char->ty2);
|
---|
495 | glVertex3f(x + width, y - height, 0.0F);
|
---|
496 | glTexCoord2f(glfont_char->tx1, glfont_char->ty2);
|
---|
497 | glVertex3f(x, y - height, 0.0F);
|
---|
498 |
|
---|
499 | //Move to next character
|
---|
500 | x += width;
|
---|
501 | }
|
---|
502 |
|
---|
503 | //Stop rendering quads
|
---|
504 | glEnd();
|
---|
505 | }
|
---|
506 | };
|
---|
507 |
|
---|
508 | //*******************************************************************
|
---|
509 |
|
---|
510 | #endif
|
---|
511 |
|
---|
512 | //End of file
|
---|
513 |
|
---|