source: git/src/glbitmapfont.cc @ 30b66b5

RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since 30b66b5 was 30b66b5, checked in by olly <olly@…>, 11 years ago

configure.ac,src/glbitmapfont.cc: Check sizeof(wxChar) at configure
time, so we can avoid a warning when sizeof(wxChar)==2 (which is the
case under mingw).

  • Property mode set to 100644
File size: 4.8 KB
Line 
1//
2//  glbitmapfont.cc
3//
4//  Draw text using glBitmap.
5//
6//  Copyright (C) 2011,2012,2013 Olly Betts
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#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include "glbitmapfont.h"
28
29#include "aventypes.h"
30#include "useful.h"
31#include "wx.h"
32
33#define CHECK_GL_ERROR(M, F) do { \
34    GLenum error_code_ = glGetError(); \
35    if (error_code_ != GL_NO_ERROR) { \
36        wxLogError(wxT(__FILE__":"STRING(__LINE__)": OpenGL error: %s " \
37                   "(call "F" in method "M")"), \
38                   wxString((const char *)gluErrorString(error_code_), \
39                            wxConvUTF8).c_str()); \
40    } \
41} while (0)
42
43bool
44BitmapFont::load(const wxString & font_file)
45{
46    if (fh) fclose(fh);
47#ifdef __WXMSW__
48    fh = _wfopen(font_file.fn_str(), L"rb");
49#else
50    fh = fopen(font_file.mb_str(), "rb");
51#endif
52
53    if (!fh) {
54        return false;
55    }
56
57    if (!gllist_base) {
58        gllist_base = glGenLists(BITMAPFONT_MAX_CHAR);
59    }
60
61    unsigned char buf[32];
62
63    for (int ch = 0; ch < BITMAPFONT_MAX_CHAR; ++ch) {
64        glNewList(gllist_base + ch, GL_COMPILE);
65        CHECK_GL_ERROR("BitmapFont::load", "glNewList");
66        int b = GETC(fh);
67        if (b == EOF) {
68            fclose(fh);
69            return false;
70        }
71        unsigned int byte_width = b;
72
73        char_width[ch] = (byte_width & 0x0f) + 2;
74        byte_width >>= 6;
75
76        int start = 0;
77        int n = 0;
78        if (byte_width) {
79            b = GETC(fh);
80            if (b == EOF) {
81                fclose(fh);
82                return false;
83            }
84            unsigned int start_and_n = b;
85            start = start_and_n >> 4;
86            n = (start_and_n & 15) + 1;
87            if (fread(buf, n * byte_width, 1, fh) != 1) {
88                fclose(fh);
89                return false;
90            }
91        }
92
93        // Even if there's nothing to display, we want to advance the
94        // raster position.
95        glBitmap(8 * byte_width, n, 0, -start, char_width[ch], 0, buf);
96        CHECK_GL_ERROR("BitmapFont::load", "glBitmap");
97        glEndList();
98        CHECK_GL_ERROR("BitmapFont::load", "glEndList");
99    }
100
101    return true;
102}
103
104inline void call_lists(GLsizei n, const GLvoid * lists)
105{
106#if SIZEOF_WXCHAR == 1
107    glCallLists(n, GL_UNSIGNED_BYTE, lists);
108#elif SIZEOF_WXCHAR == 2
109    glCallLists(n, GL_UNSIGNED_SHORT, lists);
110#elif SIZEOF_WXCHAR == 4
111    glCallLists(n, GL_UNSIGNED_INT, lists);
112#else
113# error "sizeof(wxChar) not 1, 2 or 4"
114#endif
115}
116
117void
118BitmapFont::write_glyph(wxChar ch) const
119{
120#if SIZEOF_WXCHAR > 2
121    if (ch >= 0x10000) return;
122#endif
123    if (!extra_chars) {
124        long here = ftell(fh);
125        if (here == -1 || fseek(fh, 0, SEEK_END) < 0)
126            return;
127        long data_len = ftell(fh);
128        if (data_len == -1)
129            return;
130        data_len -= here;
131        unsigned char * data = new unsigned char [data_len];
132        if (fseek(fh, here, SEEK_SET) < 0 ||
133            fread(data, data_len, 1, fh) != 1) {
134            delete data;
135            return;
136        }
137        extra_chars = new unsigned char * [0x10000 - BITMAPFONT_MAX_CHAR];
138
139        for (int i = 0; i < 0x10000 - BITMAPFONT_MAX_CHAR; ++i) {
140            if (data_len <= 0) {
141                extra_chars[i] = NULL;
142                continue;
143            }
144            extra_chars[i] = data;
145            unsigned int byte_width = *data++;
146            byte_width >>= 6;
147
148            if (byte_width) {
149                unsigned int start_and_n = *data;
150                int n = (start_and_n & 15) + 1;
151                data += n * byte_width + 1;
152                data_len -= n * byte_width + 1;
153            }
154        }
155        fclose(fh);
156        fh = NULL;
157    }
158
159    unsigned int byte_width = 0;
160    int start = 0;
161    int n = 0;
162    int width = 8;
163
164    const unsigned char * p = extra_chars[ch - BITMAPFONT_MAX_CHAR];
165    if (p) {
166        byte_width = *p++;
167        width = (byte_width & 0x0f) + 2;
168        byte_width >>= 6;
169
170        if (byte_width) {
171            unsigned int start_and_n = *p++;
172            start = start_and_n >> 4;
173            n = (start_and_n & 15) + 1;
174        }
175    }
176
177    // Even if there's nothing to display, we want to advance the
178    // raster position.
179    glBitmap(8 * byte_width, n, 0, -start, width, 0, p);
180}
181
182void
183BitmapFont::write_string(const wxChar *s, size_t len) const
184{
185    if (!gllist_base) return;
186    glListBase(gllist_base);
187#if SIZEOF_WXCHAR == 1
188    call_lists(len, s);
189#elif SIZEOF_WXCHAR == 2 || SIZEOF_WXCHAR == 4
190    while (len) {
191        size_t n;
192        for (n = 0; n < len; ++n)
193            if (int(s[n]) >= BITMAPFONT_MAX_CHAR)
194                break;
195        call_lists(n, s);
196        s += n;
197        len -= n;
198        while (len && int(s[n]) >= BITMAPFONT_MAX_CHAR) {
199            write_glyph(s[n]);
200            ++s;
201            --len;
202        }
203    }
204#else
205# error "sizeof(wxChar) not 1, 2 or 4"
206#endif
207}
Note: See TracBrowser for help on using the repository browser.