1 | // |
---|
2 | // fnt.h |
---|
3 | // |
---|
4 | // Draw text using texture mapped fonts. |
---|
5 | // |
---|
6 | // Copyright (C) 2003,2004 Olly Betts |
---|
7 | // |
---|
8 | // Based on code from PLIB - http://plib.sourceforge.net |
---|
9 | // Copyright (C) 1998,2002 Steve Baker |
---|
10 | // Relicensed under the GNU GPL as permitted by the GNU LGPL |
---|
11 | // |
---|
12 | // This program is free software; you can redistribute it and/or modify |
---|
13 | // it under the terms of the GNU General Public License as published by |
---|
14 | // the Free Software Foundation; either version 2 of the License, or |
---|
15 | // (at your option) any later version. |
---|
16 | // |
---|
17 | // This program is distributed in the hope that it will be useful, |
---|
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | // GNU General Public License for more details. |
---|
21 | // |
---|
22 | // You should have received a copy of the GNU General Public License |
---|
23 | // along with this program; if not, write to the Free Software |
---|
24 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
25 | // |
---|
26 | |
---|
27 | #ifndef _FNT_H_ |
---|
28 | #define _FNT_H_ 1 |
---|
29 | |
---|
30 | #ifdef __APPLE__ |
---|
31 | # include <OpenGL/gl.h> |
---|
32 | #else |
---|
33 | # include <GL/gl.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <string.h> |
---|
37 | |
---|
38 | #define FNT_MAXCHAR 256 |
---|
39 | |
---|
40 | class fntTexFont { |
---|
41 | private: |
---|
42 | GLuint texture; |
---|
43 | |
---|
44 | int fnt_size; |
---|
45 | |
---|
46 | int list_base; |
---|
47 | |
---|
48 | /* Nominal baseline widths */ |
---|
49 | int widths[FNT_MAXCHAR]; |
---|
50 | |
---|
51 | public: |
---|
52 | |
---|
53 | fntTexFont() : texture(0), fnt_size(0), list_base(0) |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | ~fntTexFont() |
---|
58 | { |
---|
59 | if (list_base != 0) glDeleteLists(list_base + 32, 256 - 32); |
---|
60 | if (texture != 0) glDeleteTextures(1, &texture); |
---|
61 | } |
---|
62 | |
---|
63 | bool load(const char *fname); |
---|
64 | |
---|
65 | bool hasGlyph(char c) const { return widths[(unsigned char)c] >= 0; } |
---|
66 | |
---|
67 | int getFontSize() const { return fnt_size; } |
---|
68 | |
---|
69 | void getTextExtent(const char *s, int *width, int *height) const { |
---|
70 | if (width) { |
---|
71 | int w = -1; |
---|
72 | while (*s) w += widths[(unsigned char)*s++] + 1; |
---|
73 | if (w < 0) w = 0; |
---|
74 | *width = w; |
---|
75 | } |
---|
76 | if (height) *height = fnt_size + 2; |
---|
77 | } |
---|
78 | |
---|
79 | void puts(int x, int y, const char *s) const { |
---|
80 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
81 | glMatrixMode(GL_MODELVIEW); |
---|
82 | glPushMatrix(); |
---|
83 | glLoadIdentity(); |
---|
84 | glTranslated(x, y, 0); |
---|
85 | glListBase(list_base); |
---|
86 | glCallLists(strlen(s), GL_UNSIGNED_BYTE, s); |
---|
87 | glPopMatrix(); |
---|
88 | } |
---|
89 | #if 0 |
---|
90 | void putch(int x, int y, char c) { |
---|
91 | glMatrixMode(GL_MODELVIEW); |
---|
92 | glPushMatrix(); |
---|
93 | glLoadIdentity(); |
---|
94 | glTranslated(x, y, 0); |
---|
95 | glCallList(list_base + (unsigned char)c); |
---|
96 | glPopMatrix(); |
---|
97 | } |
---|
98 | #endif |
---|
99 | |
---|
100 | }; |
---|
101 | |
---|
102 | #endif |
---|