[a1614eb] | 1 | // |
---|
| 2 | // glbitmapfont.h |
---|
| 3 | // |
---|
| 4 | // Draw text using glBitmap. |
---|
| 5 | // |
---|
[ee63994] | 6 | // Copyright (C) 2011,2013 Olly Betts |
---|
[a1614eb] | 7 | // |
---|
| 8 | // This program is free software; you can redistribute it and/or modify |
---|
| 9 | // it under the terms of the GNU General Public License as published by |
---|
| 10 | // the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | // (at your option) any later version. |
---|
| 12 | // |
---|
| 13 | // This program is distributed in the hope that it will be useful, |
---|
| 14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | // GNU General Public License for more details. |
---|
| 17 | // |
---|
| 18 | // You should have received a copy of the GNU General Public License |
---|
| 19 | // along with this program; if not, write to the Free Software |
---|
| 20 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 21 | // |
---|
| 22 | |
---|
| 23 | #ifndef GLBITMAPFONT_H_INCLUDED |
---|
| 24 | #define GLBITMAPFONT_H_INCLUDED |
---|
| 25 | |
---|
| 26 | #include "wx.h" |
---|
| 27 | |
---|
[ee63994] | 28 | #ifdef HAVE_GL_GL_H |
---|
| 29 | # include <GL/gl.h> |
---|
| 30 | #elif defined HAVE_OPENGL_GL_H |
---|
| 31 | # include <OpenGL/gl.h> |
---|
[a1614eb] | 32 | #endif |
---|
| 33 | |
---|
| 34 | class BitmapFont { |
---|
| 35 | enum { |
---|
[36b4cd7] | 36 | // The highest character point to generate an OpenGL list for. |
---|
[a1614eb] | 37 | // |
---|
[36b4cd7] | 38 | // We can't generate a GL list for every Unicode character, so we |
---|
| 39 | // generate them for the first BITMAPFONT_MAX_CHAR characters and then |
---|
| 40 | // use glBitmap directly to draw other characters if they are needed. |
---|
| 41 | // |
---|
| 42 | // FIXME: We could perhaps even store strings consisting of only the |
---|
| 43 | // first 256 points as ISO8859-1. |
---|
[a1614eb] | 44 | BITMAPFONT_MAX_CHAR = 256 |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | int gllist_base; |
---|
| 48 | |
---|
[36b4cd7] | 49 | mutable unsigned char ** extra_chars; |
---|
| 50 | |
---|
| 51 | mutable FILE * fh; |
---|
| 52 | |
---|
| 53 | unsigned char char_width[BITMAPFONT_MAX_CHAR]; |
---|
| 54 | |
---|
| 55 | void write_glyph(wxChar ch) const; |
---|
[a1614eb] | 56 | |
---|
| 57 | public: |
---|
| 58 | |
---|
[36b4cd7] | 59 | BitmapFont() : gllist_base(0), extra_chars(0), fh(0) { } |
---|
[a1614eb] | 60 | |
---|
| 61 | ~BitmapFont() { |
---|
| 62 | if (gllist_base) |
---|
| 63 | glDeleteLists(gllist_base, BITMAPFONT_MAX_CHAR); |
---|
[36b4cd7] | 64 | if (fh) |
---|
| 65 | fclose(fh); |
---|
[a1614eb] | 66 | } |
---|
| 67 | |
---|
| 68 | bool load(const wxString & font_file); |
---|
| 69 | |
---|
| 70 | // Hard-code for now. |
---|
| 71 | int get_font_size() const { return 16; } |
---|
| 72 | |
---|
| 73 | void get_text_extent(const wxChar *s, size_t len, int *width, int *height) const { |
---|
| 74 | if (width) { |
---|
| 75 | int total_width = 0; |
---|
| 76 | while (len--) { |
---|
[6f9b20d] | 77 | int ch = *s++; |
---|
[a1614eb] | 78 | if (ch < BITMAPFONT_MAX_CHAR) |
---|
| 79 | total_width += char_width[ch]; |
---|
[d4d6909] | 80 | else |
---|
| 81 | total_width += 16; // FIXME: look up the width... |
---|
[a1614eb] | 82 | } |
---|
| 83 | *width = total_width; |
---|
| 84 | } |
---|
| 85 | if (height) { |
---|
| 86 | *height = get_font_size() + 1; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | void write_string(const wxChar *s, size_t len) const; |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | #endif |
---|