[6199d17] | 1 | // |
---|
| 2 | // fnt.h |
---|
| 3 | // |
---|
| 4 | // Draw text using texture mapped fonts. |
---|
| 5 | // |
---|
[0e21a58] | 6 | // Copyright (C) 2003,2004,2006 Olly Betts |
---|
[6199d17] | 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 | // |
---|
[1eeb55a] | 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 | |
---|
[5627cbb] | 36 | #include "wx.h" // For wxChar |
---|
| 37 | |
---|
[1eeb55a] | 38 | #include <string.h> |
---|
| 39 | |
---|
[aa048c3] | 40 | #define FNT_MAXCHAR 256 |
---|
[1eeb55a] | 41 | |
---|
| 42 | class fntTexFont { |
---|
| 43 | private: |
---|
| 44 | GLuint texture; |
---|
| 45 | |
---|
| 46 | int fnt_size; |
---|
| 47 | |
---|
| 48 | int list_base; |
---|
| 49 | |
---|
| 50 | /* Nominal baseline widths */ |
---|
[aa048c3] | 51 | int widths[FNT_MAXCHAR]; |
---|
[1eeb55a] | 52 | |
---|
| 53 | public: |
---|
| 54 | |
---|
| 55 | fntTexFont() : texture(0), fnt_size(0), list_base(0) |
---|
| 56 | { |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | ~fntTexFont() |
---|
| 60 | { |
---|
| 61 | if (list_base != 0) glDeleteLists(list_base + 32, 256 - 32); |
---|
| 62 | if (texture != 0) glDeleteTextures(1, &texture); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | bool load(const char *fname); |
---|
| 66 | |
---|
| 67 | bool hasGlyph(char c) const { return widths[(unsigned char)c] >= 0; } |
---|
| 68 | |
---|
| 69 | int getFontSize() const { return fnt_size; } |
---|
| 70 | |
---|
[5627cbb] | 71 | void getTextExtent(const wxChar *s, int *width, int *height) const { |
---|
[1eeb55a] | 72 | if (width) { |
---|
| 73 | int w = -1; |
---|
[0e21a58] | 74 | while (*s) w += widths[(unsigned char)*s++]; |
---|
[1eeb55a] | 75 | if (w < 0) w = 0; |
---|
| 76 | *width = w; |
---|
| 77 | } |
---|
[0e21a58] | 78 | if (height) *height = fnt_size + 1; |
---|
[1eeb55a] | 79 | } |
---|
| 80 | |
---|
[5627cbb] | 81 | void puts(int x, int y, const wxChar *s) const { |
---|
[6b017c3] | 82 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
[1eeb55a] | 83 | glMatrixMode(GL_MODELVIEW); |
---|
| 84 | glPushMatrix(); |
---|
| 85 | glLoadIdentity(); |
---|
| 86 | glTranslated(x, y, 0); |
---|
| 87 | glListBase(list_base); |
---|
[5627cbb] | 88 | if (sizeof(wxChar) == 1) { |
---|
| 89 | glCallLists(strlen((const char *)s), GL_UNSIGNED_BYTE, s); |
---|
| 90 | } else if (sizeof(wxChar) == 2) { |
---|
| 91 | size_t len = 0; |
---|
| 92 | while (s[len]) ++len; |
---|
| 93 | glCallLists(len, GL_UNSIGNED_SHORT, s); |
---|
| 94 | } else if (sizeof(wxChar) == 4) { |
---|
| 95 | size_t len = 0; |
---|
| 96 | while (s[len]) ++len; |
---|
| 97 | glCallLists(len, GL_UNSIGNED_INT, s); |
---|
| 98 | } |
---|
[1eeb55a] | 99 | glPopMatrix(); |
---|
| 100 | } |
---|
| 101 | #if 0 |
---|
| 102 | void putch(int x, int y, char c) { |
---|
| 103 | glMatrixMode(GL_MODELVIEW); |
---|
| 104 | glPushMatrix(); |
---|
| 105 | glLoadIdentity(); |
---|
| 106 | glTranslated(x, y, 0); |
---|
| 107 | glCallList(list_base + (unsigned char)c); |
---|
| 108 | glPopMatrix(); |
---|
| 109 | } |
---|
| 110 | #endif |
---|
| 111 | |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | #endif |
---|