| 1 |
#include <iostream> |
| 2 |
#include <stdlib.h> // exit() |
| 3 |
|
| 4 |
#if defined HAVE_GL_GLUT_H |
| 5 |
# include <GL/glut.h> |
| 6 |
#elif defined HAVE_GLUT_GLUT_H |
| 7 |
# include <GLUT/glut.h> |
| 8 |
#else |
| 9 |
# error GLUT headers not present |
| 10 |
#endif |
| 11 |
|
| 12 |
#include "FTGL/ftgl.h" |
| 13 |
|
| 14 |
//#include "mmgr.h" |
| 15 |
|
| 16 |
static FTFont* fonts[5]; |
| 17 |
static int width; |
| 18 |
static int height; |
| 19 |
|
| 20 |
|
| 21 |
// YOU'LL PROBABLY WANT TO CHANGE THESE |
| 22 |
#ifdef __linux__ |
| 23 |
# define DEFAULT_FONT "/usr/share/fonts/truetype/arial.ttf" |
| 24 |
#endif |
| 25 |
#ifdef __APPLE_CC__ |
| 26 |
# define DEFAULT_FONT "/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf" |
| 27 |
#endif |
| 28 |
#ifdef _WIN32 |
| 29 |
# define DEFAULT_FONT "C:\\WINNT\\Fonts\\arial.ttf" |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
void |
| 34 |
my_init(const char* font_filename) |
| 35 |
{ |
| 36 |
glClearColor(0.0, 0.0, 0.0, 0.0); |
| 37 |
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 38 |
|
| 39 |
fonts[0] = new FTOutlineFont(font_filename); |
| 40 |
fonts[1] = new FTPolygonFont(font_filename); |
| 41 |
fonts[2] = new FTTextureFont(font_filename); |
| 42 |
fonts[3] = new FTBitmapFont(font_filename); |
| 43 |
fonts[4] = new FTPixmapFont(font_filename); |
| 44 |
for (int i=0; i< 5; i++) |
| 45 |
{ |
| 46 |
if (fonts[i]->Error()) |
| 47 |
{ |
| 48 |
std::cerr << "ERROR: Unable to open file " << font_filename << std::endl; |
| 49 |
} |
| 50 |
else |
| 51 |
{ |
| 52 |
std::cout << "Reading font " << i << " from " << font_filename << std::endl; |
| 53 |
|
| 54 |
int point_size = 48; |
| 55 |
if (!fonts[i]->FaceSize(point_size)) |
| 56 |
{ |
| 57 |
std::cerr << "ERROR: Unable to set font face size " << point_size << std::endl; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
do_ortho() |
| 65 |
{ |
| 66 |
int w; |
| 67 |
int h; |
| 68 |
GLdouble size; |
| 69 |
GLdouble aspect; |
| 70 |
|
| 71 |
w = width; |
| 72 |
h = height; |
| 73 |
aspect = (GLdouble)w / (GLdouble)h; |
| 74 |
|
| 75 |
// Use the whole window. |
| 76 |
glViewport(0, 0, w, h); |
| 77 |
|
| 78 |
// We are going to do some 2-D orthographic drawing. |
| 79 |
glMatrixMode(GL_PROJECTION); |
| 80 |
glLoadIdentity(); |
| 81 |
size = (GLdouble)((w >= h) ? w : h) / 2.0; |
| 82 |
if (w <= h) { |
| 83 |
aspect = (GLdouble)h/(GLdouble)w; |
| 84 |
glOrtho(-size, size, -size*aspect, size*aspect, |
| 85 |
-100000.0, 100000.0); |
| 86 |
} |
| 87 |
else { |
| 88 |
aspect = (GLdouble)w/(GLdouble)h; |
| 89 |
glOrtho(-size*aspect, size*aspect, -size, size, |
| 90 |
-100000.0, 100000.0); |
| 91 |
} |
| 92 |
|
| 93 |
// Make the world and window coordinates coincide so that 1.0 in |
| 94 |
// model space equals one pixel in window space. |
| 95 |
glScaled(aspect, aspect, 1.0); |
| 96 |
|
| 97 |
// Now determine where to draw things. |
| 98 |
glMatrixMode(GL_MODELVIEW); |
| 99 |
glLoadIdentity(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
void |
| 104 |
my_reshape(int w, int h) |
| 105 |
{ |
| 106 |
width = w; |
| 107 |
height = h; |
| 108 |
|
| 109 |
do_ortho(); |
| 110 |
} |
| 111 |
|
| 112 |
void |
| 113 |
my_handle_key(unsigned char key, int x, int y) |
| 114 |
{ |
| 115 |
switch (key) { |
| 116 |
case 'q': // Esc or 'q' Quits the program. |
| 117 |
case 27: |
| 118 |
{ |
| 119 |
for (int i=0; i<5; i++) { |
| 120 |
if (fonts[i]) { |
| 121 |
delete fonts[i]; |
| 122 |
fonts[i] = 0; |
| 123 |
} |
| 124 |
} |
| 125 |
exit(1); |
| 126 |
} |
| 127 |
break; |
| 128 |
default: |
| 129 |
break; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void |
| 134 |
draw_scene() |
| 135 |
{ |
| 136 |
/* Set up some strings with the characters to draw. */ |
| 137 |
unsigned int count = 0; |
| 138 |
char string[8][256]; |
| 139 |
int i; |
| 140 |
for (i=1; i < 32; i++) { /* Skip zero - it's the null terminator! */ |
| 141 |
string[0][count] = i; |
| 142 |
count++; |
| 143 |
} |
| 144 |
|
| 145 |
string[0][count] = '\0'; |
| 146 |
|
| 147 |
count = 0; |
| 148 |
for (i=32; i < 64; i++) { |
| 149 |
string[1][count] = i; |
| 150 |
count++; |
| 151 |
} |
| 152 |
string[1][count] = '\0'; |
| 153 |
|
| 154 |
count = 0; |
| 155 |
for (i=64; i < 96; i++) { |
| 156 |
string[2][count] = i; |
| 157 |
count++; |
| 158 |
} |
| 159 |
string[2][count] = '\0'; |
| 160 |
|
| 161 |
count = 0; |
| 162 |
for (i=96; i < 128; i++) { |
| 163 |
string[3][count] = i; |
| 164 |
count++; |
| 165 |
} |
| 166 |
string[3][count] = '\0'; |
| 167 |
|
| 168 |
count = 0; |
| 169 |
for (i=128; i < 160; i++) { |
| 170 |
string[4][count] = i; |
| 171 |
count++; |
| 172 |
} |
| 173 |
string[4][count] = '\0'; |
| 174 |
|
| 175 |
count = 0; |
| 176 |
for (i=160; i < 192; i++) { |
| 177 |
string[5][count] = i; |
| 178 |
count++; |
| 179 |
} |
| 180 |
string[5][count] = '\0'; |
| 181 |
|
| 182 |
count = 0; |
| 183 |
for (i=192; i < 224; i++) { |
| 184 |
string[6][count] = i; |
| 185 |
count++; |
| 186 |
} |
| 187 |
string[6][count] = '\0'; |
| 188 |
|
| 189 |
count = 0; |
| 190 |
for (i=224; i < 256; i++) { |
| 191 |
string[7][count] = i; |
| 192 |
count++; |
| 193 |
} |
| 194 |
string[7][count] = '\0'; |
| 195 |
|
| 196 |
|
| 197 |
glColor3f(1.0, 1.0, 1.0); |
| 198 |
|
| 199 |
for (int font = 0; font < 5; font++) { |
| 200 |
GLfloat x = -250.0; |
| 201 |
GLfloat y; |
| 202 |
GLfloat yild = 20.0; |
| 203 |
for (int j=0; j<4; j++) { |
| 204 |
y = 275.0-font*120.0-j*yild; |
| 205 |
if (font >= 3) { |
| 206 |
glRasterPos2f(x, y); |
| 207 |
fonts[font]->Render(string[j]); |
| 208 |
} |
| 209 |
else { |
| 210 |
if (font == 2) { |
| 211 |
glEnable(GL_TEXTURE_2D); |
| 212 |
glEnable(GL_BLEND); |
| 213 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 214 |
} |
| 215 |
glPushMatrix(); { |
| 216 |
glTranslatef(x, y, 0.0); |
| 217 |
fonts[font]->Render(string[j]); |
| 218 |
} glPopMatrix(); |
| 219 |
if (font == 2) { |
| 220 |
glDisable(GL_TEXTURE_2D); |
| 221 |
glDisable(GL_BLEND); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
void |
| 230 |
my_display(void) |
| 231 |
{ |
| 232 |
glClear(GL_COLOR_BUFFER_BIT); |
| 233 |
|
| 234 |
draw_scene(); |
| 235 |
|
| 236 |
glutSwapBuffers(); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
void |
| 241 |
my_idle() |
| 242 |
{ |
| 243 |
glutPostRedisplay(); |
| 244 |
} |
| 245 |
|
| 246 |
int |
| 247 |
file_exists(const char * fontFilePath) |
| 248 |
{ |
| 249 |
FILE * fp = fopen(fontFilePath, "r"); |
| 250 |
|
| 251 |
if (fp == NULL) |
| 252 |
{ |
| 253 |
// That fopen failed does _not_ definitely mean the file isn't there |
| 254 |
// but for now this is ok |
| 255 |
return 0; |
| 256 |
} |
| 257 |
fclose(fp); |
| 258 |
return 1; |
| 259 |
} |
| 260 |
|
| 261 |
void |
| 262 |
usage(const char * program) |
| 263 |
{ |
| 264 |
std::cerr << "Usage: " << program << " <fontFilePath.ttf>\n" << std::endl; |
| 265 |
} |
| 266 |
|
| 267 |
int |
| 268 |
main(int argc, char **argv) |
| 269 |
{ |
| 270 |
char * fontFilePath; |
| 271 |
|
| 272 |
glutInitWindowSize(600, 600); |
| 273 |
glutInit(&argc, argv); |
| 274 |
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); |
| 275 |
|
| 276 |
glutCreateWindow("FTGL demo"); |
| 277 |
|
| 278 |
if (argc >= 2) |
| 279 |
{ |
| 280 |
if (!file_exists(argv[ 1 ])) |
| 281 |
{ |
| 282 |
usage(argv[ 0 ]); |
| 283 |
std::cerr << "Couldn't open file '" << argv[ 1 ] << "'" << std::endl; |
| 284 |
exit(-1); |
| 285 |
} |
| 286 |
fontFilePath = argv[ 1 ]; |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
// try a default font |
| 291 |
fontFilePath = DEFAULT_FONT; |
| 292 |
|
| 293 |
if (!file_exists(fontFilePath)) |
| 294 |
{ |
| 295 |
usage(argv[ 0 ]); |
| 296 |
std::cerr << "Couldn't open default file '" << fontFilePath << "'" << std::endl; |
| 297 |
exit(-1); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
my_init(fontFilePath); |
| 302 |
|
| 303 |
glutDisplayFunc(my_display); |
| 304 |
glutReshapeFunc(my_reshape); |
| 305 |
glutIdleFunc(my_idle); |
| 306 |
glutKeyboardFunc(my_handle_key); |
| 307 |
|
| 308 |
glutMainLoop(); |
| 309 |
|
| 310 |
for(int x = 0; x < 5; ++x) |
| 311 |
{ |
| 312 |
delete fonts[x]; |
| 313 |
} |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|