[6199d17] | 1 | // |
---|
| 2 | // fnt.cc |
---|
| 3 | // |
---|
| 4 | // Draw text using texture mapped fonts. |
---|
| 5 | // |
---|
[ecc16fc] | 6 | // Copyright (C) 2003,2004,2006,2010 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 | #include "fnt.h" |
---|
| 28 | |
---|
| 29 | #include <stdio.h> |
---|
| 30 | |
---|
| 31 | static bool isSwapped = false; |
---|
| 32 | |
---|
[5b13e4c] | 33 | inline unsigned char fnt_readByte(FILE *fd) { |
---|
[1eeb55a] | 34 | return (unsigned char)getc(fd); |
---|
| 35 | } |
---|
| 36 | |
---|
[5b13e4c] | 37 | inline unsigned short fnt_readShort(FILE *fd) { |
---|
[1eeb55a] | 38 | unsigned short x; |
---|
[deeebad] | 39 | if (isSwapped) { |
---|
| 40 | x = getc(fd) | (getc(fd) << 8); |
---|
| 41 | } else { |
---|
| 42 | x = (getc(fd) << 8) | getc(fd); |
---|
| 43 | } |
---|
[1eeb55a] | 44 | return x; |
---|
| 45 | } |
---|
| 46 | |
---|
[5b13e4c] | 47 | inline unsigned int fnt_readInt(FILE *fd) { |
---|
[1eeb55a] | 48 | unsigned int x; |
---|
[deeebad] | 49 | if (isSwapped) { |
---|
| 50 | x = getc(fd) | (getc(fd) << 8) | (getc(fd) << 16) | (getc(fd) << 24); |
---|
| 51 | } else { |
---|
| 52 | x = (getc(fd) << 24) | (getc(fd) << 16) | (getc(fd) << 8) | getc(fd); |
---|
| 53 | } |
---|
[1eeb55a] | 54 | return x; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | #define FNT_BYTE_FORMAT 0 |
---|
| 58 | #define FNT_BITMAP_FORMAT 1 |
---|
| 59 | |
---|
| 60 | bool |
---|
| 61 | fntTexFont::load(const char *fname) |
---|
| 62 | { |
---|
| 63 | FILE *fd; |
---|
| 64 | |
---|
| 65 | if ((fd = fopen(fname, "rb")) == NULL) { |
---|
[bd551c0] | 66 | fprintf(stderr, "Failed to open '%s' for reading.\n", fname); |
---|
[1eeb55a] | 67 | return false; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | unsigned char magic[4]; |
---|
| 71 | |
---|
[deeebad] | 72 | if (fread(&magic, 4, 1, fd) != 1) { |
---|
[bd551c0] | 73 | fprintf(stderr, "'%s' an empty file!\n", fname); |
---|
[1eeb55a] | 74 | return false; |
---|
| 75 | } |
---|
| 76 | |
---|
[deeebad] | 77 | if (memcmp(magic, "\xfftxf", 4) != 0) { |
---|
[bd551c0] | 78 | fprintf(stderr, "'%s' is not a 'txf' font file.\n", fname); |
---|
[1eeb55a] | 79 | return false; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | isSwapped = false; |
---|
[5b13e4c] | 83 | int endianness = fnt_readInt(fd); |
---|
[1eeb55a] | 84 | |
---|
| 85 | isSwapped = (endianness != 0x12345678); |
---|
| 86 | |
---|
[5b13e4c] | 87 | int format = fnt_readInt(fd); |
---|
| 88 | int tex_width = fnt_readInt(fd); |
---|
| 89 | int tex_height = fnt_readInt(fd); |
---|
[c57bd20] | 90 | fnt_size = fnt_readInt(fd); |
---|
| 91 | fnt_size += fnt_size >> 2; |
---|
[5b13e4c] | 92 | /* int unknown = */ fnt_readInt(fd); |
---|
| 93 | int num_glyphs = fnt_readInt(fd); |
---|
[1eeb55a] | 94 | list_base = glGenLists(256 - 32) - 32; |
---|
| 95 | |
---|
| 96 | int i, j; |
---|
| 97 | |
---|
| 98 | // Skip the glyph info first so we can set up the texture, then create |
---|
| 99 | // display lists from it using the glyph info |
---|
| 100 | int fpos = ftell(fd); |
---|
| 101 | fseek(fd, num_glyphs * 12, SEEK_CUR); |
---|
| 102 | // Load the image part of the file |
---|
| 103 | int ntexels = tex_width * tex_height; |
---|
| 104 | |
---|
[885f73f] | 105 | unsigned char *teximage = new unsigned char[ntexels]; |
---|
[1eeb55a] | 106 | |
---|
| 107 | switch (format) { |
---|
| 108 | case FNT_BYTE_FORMAT: { |
---|
[885f73f] | 109 | if ((int)fread(teximage, 1, ntexels, fd) != ntexels) { |
---|
| 110 | delete [] teximage; |
---|
[bd551c0] | 111 | fprintf(stderr, "Premature EOF in '%s'.\n", fname); |
---|
[1eeb55a] | 112 | return false; |
---|
| 113 | } |
---|
| 114 | break; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | case FNT_BITMAP_FORMAT: { |
---|
| 118 | int stride = (tex_width + 7) >> 3; |
---|
| 119 | |
---|
| 120 | unsigned char *texbitmap = new unsigned char[stride * tex_height]; |
---|
| 121 | |
---|
| 122 | if ((int)fread(texbitmap, 1, stride * tex_height, fd) |
---|
| 123 | != stride * tex_height) { |
---|
[885f73f] | 124 | delete [] texbitmap; |
---|
| 125 | delete [] teximage; |
---|
[bd551c0] | 126 | fprintf(stderr, "Premature EOF in '%s'.\n", fname); |
---|
[1eeb55a] | 127 | return false; |
---|
| 128 | } |
---|
| 129 | |
---|
[885f73f] | 130 | memset((void*)teximage, 0, ntexels); |
---|
[1eeb55a] | 131 | |
---|
[5b13e4c] | 132 | for (i = 0; i < tex_height; ++i) { |
---|
| 133 | for (j = 0; j < tex_width; ++j) { |
---|
[1eeb55a] | 134 | if (texbitmap[i * stride + (j >> 3)] & (1 << (j & 7))) { |
---|
[5f4a155] | 135 | teximage[i * tex_width + j] = 0xff; |
---|
[1eeb55a] | 136 | } |
---|
[5b13e4c] | 137 | } |
---|
| 138 | } |
---|
[1eeb55a] | 139 | |
---|
| 140 | delete [] texbitmap; |
---|
| 141 | break; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | default: |
---|
[885f73f] | 145 | delete [] teximage; |
---|
[bd551c0] | 146 | fprintf(stderr, "Unrecognised format type in '%s'.\n", fname); |
---|
[1eeb55a] | 147 | return false; |
---|
| 148 | } |
---|
| 149 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
---|
| 150 | |
---|
| 151 | glGenTextures(1, & texture); |
---|
| 152 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
| 153 | |
---|
| 154 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
---|
| 155 | |
---|
| 156 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); |
---|
| 157 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); |
---|
[5f4a155] | 158 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tex_width, tex_height, 0 /* Border */, |
---|
| 159 | GL_ALPHA, GL_UNSIGNED_BYTE, (GLvoid *)teximage); |
---|
[1eeb55a] | 160 | delete [] teximage; |
---|
| 161 | |
---|
[096e56c] | 162 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
[1dda087] | 163 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
| 164 | |
---|
[1eeb55a] | 165 | fseek(fd, fpos, SEEK_SET); |
---|
| 166 | |
---|
[aa048c3] | 167 | for (i = 0; i < FNT_MAXCHAR; ++i) widths[i] = -1; |
---|
[1eeb55a] | 168 | |
---|
| 169 | // Load the glyph array |
---|
| 170 | |
---|
| 171 | float W = 1.0f / (float)tex_width; |
---|
| 172 | float H = 1.0f / (float)tex_height; |
---|
| 173 | unsigned char max_w = 0; |
---|
| 174 | for (i = 0; i < num_glyphs; ++i) { |
---|
[5b13e4c] | 175 | unsigned short ch = fnt_readShort(fd); |
---|
| 176 | unsigned char w = fnt_readByte(fd); |
---|
[1eeb55a] | 177 | if (w > max_w) max_w = w; |
---|
[5b13e4c] | 178 | unsigned char h = fnt_readByte(fd); |
---|
| 179 | int vtx_left = (signed char)fnt_readByte(fd); |
---|
| 180 | int vtx_bot = (signed char)fnt_readByte(fd); |
---|
[ecc16fc] | 181 | signed char step = fnt_readByte(fd); |
---|
[5b13e4c] | 182 | /* signed char unknown =*/ fnt_readByte(fd); |
---|
| 183 | short x = fnt_readShort(fd); |
---|
| 184 | short y = fnt_readShort(fd); |
---|
[1eeb55a] | 185 | |
---|
[aa048c3] | 186 | if (ch < 32 || ch >= FNT_MAXCHAR) continue; |
---|
[5b13e4c] | 187 | |
---|
[1eeb55a] | 188 | float tex_left = x * W; |
---|
| 189 | float tex_right = (x + w) * W; |
---|
| 190 | float tex_bot = y * H; |
---|
| 191 | float tex_top = (y + h) * H; |
---|
| 192 | int vtx_right = vtx_left + w; |
---|
| 193 | int vtx_top = vtx_bot + h; |
---|
[5b13e4c] | 194 | glNewList(list_base + ch, GL_COMPILE); |
---|
| 195 | if (w != 0 && h != 0) { |
---|
| 196 | glBegin(GL_QUADS); |
---|
| 197 | glTexCoord2f(tex_left, tex_bot); |
---|
| 198 | glVertex2i(vtx_left, vtx_bot); |
---|
| 199 | glTexCoord2f(tex_right, tex_bot); |
---|
| 200 | glVertex2i(vtx_right, vtx_bot); |
---|
| 201 | glTexCoord2f(tex_right, tex_top); |
---|
| 202 | glVertex2i(vtx_right, vtx_top); |
---|
| 203 | glTexCoord2f(tex_left, tex_top); |
---|
| 204 | glVertex2i(vtx_left, vtx_top); |
---|
| 205 | glEnd(); |
---|
| 206 | } |
---|
[ecc16fc] | 207 | widths[ch] = step; |
---|
[5b13e4c] | 208 | glTranslated(widths[ch], 0, 0); |
---|
[1eeb55a] | 209 | glEndList(); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | if (widths[(int)' '] == -1) { |
---|
| 213 | glNewList(list_base + ' ', GL_COMPILE); |
---|
[0e21a58] | 214 | widths[(int)' '] = widths[(int)'n']; |
---|
[5b13e4c] | 215 | glTranslated(widths[(int)' '], 0, 0); |
---|
[1eeb55a] | 216 | glEndList(); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | fclose(fd); |
---|
| 220 | return true; |
---|
| 221 | } |
---|