1 /*
2 * Shotgun Debugger
3 * Copyright 2005 Game Creation Society
4 *
5 * Programmed by Matt Sarnoff
6 * http://www.contrib.andrew.cmu.edu/~msarnoff
7 * http://www.gamecreation.org
8 *
9 * bitmapfont.cpp - bitmap font routines
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 #include "bitmapfont.h"
27
28 GLuint textures[NUM_TEXTURES];
29
30 // Creates a bitmap font from a loaded texture.
31 // Must be 16 characters wide, 8 characters high.
create(int index)32 void BitmapFont::create(int index)
33 {
34 lastY = 0.0;
35 lastYScale = 1.0;
36 texIndex = index;
37 }
38
drawChar(char c)39 void BitmapFont::drawChar(char c)
40 {
41 int row = c / 16;
42 int col = c % 16;
43
44 glEnable(GL_TEXTURE_2D);
45 glBindTexture(GL_TEXTURE_2D, textures[texIndex]);
46 glBegin(GL_QUADS);
47 glTexCoord2f( col*.0625, row*.125); glVertex2f(-0.5, 1.0);
48 glTexCoord2f((col+1)*.0625, row*.125); glVertex2f(0.5, 1.0);
49 glTexCoord2f((col+1)*.0625, (row+1)*.125); glVertex2f(0.5, -1.0);
50 glTexCoord2f( col*.0625, (row+1)*.125); glVertex2f(-0.5, -1.0);
51 glEnd();
52 }
53
print(float x,float y,float xscale,float yscale,float spacing,char * text)54 void BitmapFont::print(float x, float y, float xscale, float yscale, float spacing, char *text)
55 {
56 if (x == CENTERED)
57 x = SCREEN_CENTER - ((float)strlen(text)*spacing*xscale)/2.0 + xscale/2.0;
58
59 if (y >= LAST_Y-5000.0 && y <= LAST_Y+5000.0)
60 y = lastY + (y-LAST_Y) - lastYScale - yscale;
61
62 glPushMatrix();
63 glTranslatef(x, y, 0.0);
64 glScalef(xscale, yscale, 0.0);
65 for (int i = 0; i < strlen(text); i++)
66 {
67 drawChar(text[i]);
68 glTranslatef(spacing, 0.0, 0.0);
69 }
70 glPopMatrix();
71 glDisable(GL_TEXTURE_2D);
72
73 lastY = y;
74 lastYScale = yscale;
75 }
76
printf(float x,float y,float xscale,float yscale,char * fmt,...)77 void BitmapFont::printf(float x, float y, float xscale, float yscale, char *fmt, ...)
78 {
79 char buffer[513];
80 va_list ap;
81
82 va_start(ap, fmt);
83 vsnprintf(buffer, 512, fmt, ap);
84 print(x, y, xscale, yscale, 0.75, buffer);
85 }
86
LEDprintf(float x,float y,float xscale,float yscale,char * fmt,...)87 void BitmapFont::LEDprintf(float x, float y, float xscale, float yscale, char *fmt, ...)
88 {
89 char buffer[513];
90 va_list ap;
91
92 va_start(ap, fmt);
93 vsnprintf(buffer, 512, fmt, ap);
94
95 // convert digits to LED numerals
96 char *p = buffer;
97 while (*p)
98 {
99 if (*p >= '0' && *p <= '?')
100 *p -= 32;
101
102 p++;
103 }
104
105 print(x, y, xscale, yscale, 1.0, buffer);
106 }
107
vprintf(float x,float y,float xscale,float yscale,char * fmt,va_list ap)108 void BitmapFont::vprintf(float x, float y, float xscale, float yscale, char *fmt, va_list ap)
109 {
110 char text[513];
111 vsprintf(text, fmt, ap);
112 if (x == CENTERED)
113 x = SCREEN_CENTER - ((float)strlen(text)*xscale*0.75)/2.0;
114
115 if (y >= LAST_Y-5000.0 && y <= LAST_Y+5000.0)
116 y = lastY + (y-LAST_Y) - lastYScale - yscale;
117
118 glPushMatrix();
119 glTranslatef(x, y, 0.0);
120 glScalef(xscale, yscale, 0.0);
121 for (int i = 0; i < strlen(text); i++)
122 {
123 drawChar(text[i]);
124 glTranslatef(0.75, 0.0, 0.0);
125 }
126 glPopMatrix();
127
128 lastY = y;
129 lastYScale = yscale;
130 }
131