| 1 |
/* |
| 2 |
* FTGLDemo - advanced demo for FTGL, the OpenGL font library |
| 3 |
* |
| 4 |
* Copyright (c) 2001-2004 Henry Maddocks <[email protected]> |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 7 |
* a copy of this software and associated documentation files (the |
| 8 |
* "Software"), to deal in the Software without restriction, including |
| 9 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 11 |
* permit persons to whom the Software is furnished to do so, subject to |
| 12 |
* the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be |
| 15 |
* included in all copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "config.h" |
| 27 |
|
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#if defined HAVE_GL_GLUT_H |
| 33 |
# include <GL/glut.h> |
| 34 |
#elif defined HAVE_GLUT_GLUT_H |
| 35 |
# include <GLUT/glut.h> |
| 36 |
#else |
| 37 |
# error GLUT headers not present |
| 38 |
#endif |
| 39 |
|
| 40 |
#include <FTGL/ftgl.h> |
| 41 |
|
| 42 |
#include "tb.h" |
| 43 |
|
| 44 |
// YOU'LL PROBABLY WANT TO CHANGE THESE |
| 45 |
#if defined FONT_FILE |
| 46 |
char const *defaultFonts[] = { FONT_FILE }; |
| 47 |
const int NumDefaultFonts = 1; |
| 48 |
#elif defined __APPLE_CC__ |
| 49 |
char const *defaultFonts[] = { "/System/Library/Fonts/Helvetica.dfont", |
| 50 |
"/System/Library/Fonts/Geneva.dfont" }; |
| 51 |
const int NumDefaultFonts = 2; |
| 52 |
#elif defined _WIN32 |
| 53 |
char const *defaultFonts[] = { "C:\\WINNT\\Fonts\\arial.ttf" }; |
| 54 |
const int NumDefaultFonts = 1; |
| 55 |
#else |
| 56 |
// Put your font files here if configure did not find any. |
| 57 |
char const *defaultFonts[] = { }; |
| 58 |
const int NumDefaultFonts = 0; |
| 59 |
#endif |
| 60 |
|
| 61 |
/* Set this to 1 to build a Mac os app (ignore the command line args). */ |
| 62 |
#ifndef IGNORE_ARGV |
| 63 |
# define IGNORE_ARGV 0 |
| 64 |
#endif /* IGNORE_ARGV */ |
| 65 |
|
| 66 |
#define EDITING 1 |
| 67 |
#define INTERACTIVE 2 |
| 68 |
|
| 69 |
#define FTGL_BITMAP 0 |
| 70 |
#define FTGL_PIXMAP 1 |
| 71 |
#define FTGL_OUTLINE 2 |
| 72 |
#define FTGL_POLYGON 3 |
| 73 |
#define FTGL_EXTRUDE 4 |
| 74 |
#define FTGL_TEXTURE 5 |
| 75 |
const int NumStyles = 6; |
| 76 |
|
| 77 |
char const * const *fontfiles; |
| 78 |
int current_font = FTGL_EXTRUDE; |
| 79 |
|
| 80 |
GLint w_win = 640, h_win = 480; |
| 81 |
int mode = INTERACTIVE; |
| 82 |
int carat = 0; |
| 83 |
|
| 84 |
FTSimpleLayout simpleLayout; |
| 85 |
FTLayout *layouts[] = { &simpleLayout, NULL }; |
| 86 |
int currentLayout = 0; |
| 87 |
const int NumLayouts = 2; |
| 88 |
|
| 89 |
const float InitialLineLength = 300.0f; |
| 90 |
|
| 91 |
const float OX = -100; |
| 92 |
const float OY = 200; |
| 93 |
|
| 94 |
//wchar_t myString[16] = { 0x6FB3, 0x9580}; |
| 95 |
char myString[4096]; |
| 96 |
|
| 97 |
int totalFonts; |
| 98 |
static FTFont** fonts; |
| 99 |
static FTPixmapFont* infoFont; |
| 100 |
|
| 101 |
void SetCamera(void); |
| 102 |
|
| 103 |
inline int GetStyle() |
| 104 |
{ |
| 105 |
return current_font % NumStyles; |
| 106 |
} |
| 107 |
|
| 108 |
inline int GetFace() |
| 109 |
{ |
| 110 |
return current_font / NumStyles; |
| 111 |
} |
| 112 |
|
| 113 |
void setUpLighting() |
| 114 |
{ |
| 115 |
// Set up lighting. |
| 116 |
float light1_ambient[4] = { 1.0, 1.0, 1.0, 1.0 }; |
| 117 |
float light1_diffuse[4] = { 1.0, 0.9, 0.9, 1.0 }; |
| 118 |
float light1_specular[4] = { 1.0, 0.7, 0.7, 1.0 }; |
| 119 |
float light1_position[4] = { -1.0, 1.0, 1.0, 0.0 }; |
| 120 |
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); |
| 121 |
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); |
| 122 |
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular); |
| 123 |
glLightfv(GL_LIGHT1, GL_POSITION, light1_position); |
| 124 |
glEnable(GL_LIGHT1); |
| 125 |
|
| 126 |
float light2_ambient[4] = { 0.2, 0.2, 0.2, 1.0 }; |
| 127 |
float light2_diffuse[4] = { 0.9, 0.9, 0.9, 1.0 }; |
| 128 |
float light2_specular[4] = { 0.7, 0.7, 0.7, 1.0 }; |
| 129 |
float light2_position[4] = { 1.0, -1.0, -1.0, 0.0 }; |
| 130 |
glLightfv(GL_LIGHT2, GL_AMBIENT, light2_ambient); |
| 131 |
glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse); |
| 132 |
glLightfv(GL_LIGHT2, GL_SPECULAR, light2_specular); |
| 133 |
glLightfv(GL_LIGHT2, GL_POSITION, light2_position); |
| 134 |
//glEnable(GL_LIGHT2); |
| 135 |
|
| 136 |
float front_emission[4] = { 0.3, 0.2, 0.1, 0.0 }; |
| 137 |
float front_ambient[4] = { 0.2, 0.2, 0.2, 0.0 }; |
| 138 |
float front_diffuse[4] = { 0.95, 0.95, 0.8, 0.0 }; |
| 139 |
float front_specular[4] = { 0.6, 0.6, 0.6, 0.0 }; |
| 140 |
glMaterialfv(GL_FRONT, GL_EMISSION, front_emission); |
| 141 |
glMaterialfv(GL_FRONT, GL_AMBIENT, front_ambient); |
| 142 |
glMaterialfv(GL_FRONT, GL_DIFFUSE, front_diffuse); |
| 143 |
glMaterialfv(GL_FRONT, GL_SPECULAR, front_specular); |
| 144 |
glMaterialf(GL_FRONT, GL_SHININESS, 16.0); |
| 145 |
glColor4fv(front_diffuse); |
| 146 |
|
| 147 |
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); |
| 148 |
glEnable(GL_CULL_FACE); |
| 149 |
glColorMaterial(GL_FRONT, GL_DIFFUSE); |
| 150 |
glEnable(GL_COLOR_MATERIAL); |
| 151 |
|
| 152 |
glEnable(GL_LIGHTING); |
| 153 |
glShadeModel(GL_SMOOTH); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
void setUpFonts(int numFontFiles) |
| 158 |
{ |
| 159 |
// The total number of fonts is styles * faces |
| 160 |
totalFonts = numFontFiles*NumStyles; |
| 161 |
|
| 162 |
// Allocate an array to hold all fonts |
| 163 |
fonts = new FTFont *[totalFonts]; |
| 164 |
|
| 165 |
// Instantiate and configure named fonts |
| 166 |
for(int i = 0; i < numFontFiles; i++) |
| 167 |
{ |
| 168 |
fonts[i*NumStyles + FTGL_BITMAP] = new FTBitmapFont(fontfiles[i]); |
| 169 |
fonts[i*NumStyles + FTGL_PIXMAP] = new FTPixmapFont(fontfiles[i]); |
| 170 |
fonts[i*NumStyles + FTGL_OUTLINE] = new FTOutlineFont(fontfiles[i]); |
| 171 |
fonts[i*NumStyles + FTGL_POLYGON] = new FTPolygonFont(fontfiles[i]); |
| 172 |
fonts[i*NumStyles + FTGL_EXTRUDE] = new FTExtrudeFont(fontfiles[i]); |
| 173 |
fonts[i*NumStyles + FTGL_TEXTURE] = new FTTextureFont(fontfiles[i]); |
| 174 |
|
| 175 |
for(int x = 0; x < NumStyles; ++x) |
| 176 |
{ |
| 177 |
int j = i * NumStyles + x; |
| 178 |
|
| 179 |
if(fonts[j]->Error()) |
| 180 |
{ |
| 181 |
fprintf(stderr, "Failed to open font %s\n", fontfiles[i]); |
| 182 |
exit(1); |
| 183 |
} |
| 184 |
|
| 185 |
if(!fonts[j]->FaceSize(24)) |
| 186 |
{ |
| 187 |
fprintf(stderr, "Failed to set size\n"); |
| 188 |
exit(1); |
| 189 |
} |
| 190 |
|
| 191 |
fonts[j]->Depth(20); |
| 192 |
fonts[j]->CharMap(ft_encoding_unicode); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
infoFont = new FTPixmapFont(fontfiles[0]); |
| 197 |
|
| 198 |
if(infoFont->Error()) |
| 199 |
{ |
| 200 |
fprintf(stderr, "Failed to open font %s\n", fontfiles[0]); |
| 201 |
exit(1); |
| 202 |
} |
| 203 |
|
| 204 |
infoFont->FaceSize(18); |
| 205 |
|
| 206 |
strcpy(myString, "OpenGL is a powerful software interface for graphics " |
| 207 |
"hardware that allows graphics programmers to produce high-quality " |
| 208 |
"color images of 3D objects. abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL" |
| 209 |
"MNOPQRSTUVWXYZ0123456789"); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
void renderFontmetrics() |
| 214 |
{ |
| 215 |
FTBBox bbox; |
| 216 |
float x1, y1, z1, x2, y2, z2; |
| 217 |
|
| 218 |
// If there is a layout, use it to compute the bbox, otherwise query as |
| 219 |
// a string. |
| 220 |
if(layouts[currentLayout]) |
| 221 |
bbox = layouts[currentLayout]->BBox(myString); |
| 222 |
else |
| 223 |
bbox = fonts[current_font]->BBox(myString); |
| 224 |
|
| 225 |
x1 = bbox.Lower().Xf(); y1 = bbox.Lower().Yf(); z1 = bbox.Lower().Zf(); |
| 226 |
x2 = bbox.Upper().Xf(); y2 = bbox.Upper().Yf(); z2 = bbox.Upper().Zf(); |
| 227 |
|
| 228 |
// Draw the bounding box |
| 229 |
glDisable(GL_LIGHTING); |
| 230 |
glDisable(GL_TEXTURE_2D); |
| 231 |
glEnable(GL_LINE_SMOOTH); |
| 232 |
glEnable(GL_BLEND); |
| 233 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE); // GL_ONE_MINUS_SRC_ALPHA |
| 234 |
|
| 235 |
glColor3f(0.0, 1.0, 0.0); |
| 236 |
// Draw the front face |
| 237 |
glBegin(GL_LINE_LOOP); |
| 238 |
glVertex3f(x1, y1, z1); |
| 239 |
glVertex3f(x1, y2, z1); |
| 240 |
glVertex3f(x2, y2, z1); |
| 241 |
glVertex3f(x2, y1, z1); |
| 242 |
glEnd(); |
| 243 |
// Draw the back face |
| 244 |
#pragma GCC diagnostic push |
| 245 |
#pragma GCC diagnostic ignored "-Wfloat-equal" |
| 246 |
bool zd = z1 != z2; |
| 247 |
#pragma GCC diagnostic pop |
| 248 |
if((GetStyle() == FTGL_EXTRUDE) && zd) |
| 249 |
{ |
| 250 |
glBegin(GL_LINE_LOOP); |
| 251 |
glVertex3f(x1, y1, z2); |
| 252 |
glVertex3f(x1, y2, z2); |
| 253 |
glVertex3f(x2, y2, z2); |
| 254 |
glVertex3f(x2, y1, z2); |
| 255 |
glEnd(); |
| 256 |
// Join the faces |
| 257 |
glBegin(GL_LINES); |
| 258 |
glVertex3f(x1, y1, z1); |
| 259 |
glVertex3f(x1, y1, z2); |
| 260 |
|
| 261 |
glVertex3f(x1, y2, z1); |
| 262 |
glVertex3f(x1, y2, z2); |
| 263 |
|
| 264 |
glVertex3f(x2, y2, z1); |
| 265 |
glVertex3f(x2, y2, z2); |
| 266 |
|
| 267 |
glVertex3f(x2, y1, z1); |
| 268 |
glVertex3f(x2, y1, z2); |
| 269 |
glEnd(); |
| 270 |
} |
| 271 |
|
| 272 |
// Render layout-specific metrics |
| 273 |
if(!layouts[currentLayout]) |
| 274 |
{ |
| 275 |
// There is no layout. Draw the baseline, Ascender and Descender |
| 276 |
glBegin(GL_LINES); |
| 277 |
glColor3f(0.0, 0.0, 1.0); |
| 278 |
glVertex3f(0.0, 0.0, 0.0); |
| 279 |
glVertex3f(fonts[current_font]->Advance(myString), 0.0, 0.0); |
| 280 |
glVertex3f(0.0, fonts[current_font]->Ascender(), 0.0); |
| 281 |
glVertex3f(0.0, fonts[current_font]->Descender(), 0.0); |
| 282 |
glEnd(); |
| 283 |
} |
| 284 |
else if (layouts[currentLayout] |
| 285 |
&& (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout]))) |
| 286 |
{ |
| 287 |
float lineWidth = ((FTSimpleLayout *)layouts[currentLayout])->GetLineLength(); |
| 288 |
|
| 289 |
// The layout is a SimpleLayout. Render guides that mark the edges |
| 290 |
// of the wrap region. |
| 291 |
glColor3f(0.5, 1.0, 1.0); |
| 292 |
glBegin(GL_LINES); |
| 293 |
glVertex3f(0, 10000, 0); |
| 294 |
glVertex3f(0, -10000, 0); |
| 295 |
glVertex3f(lineWidth, 10000, 0); |
| 296 |
glVertex3f(lineWidth, -10000, 0); |
| 297 |
glEnd(); |
| 298 |
} |
| 299 |
|
| 300 |
// Draw the origin |
| 301 |
glColor3f(1.0, 0.0, 0.0); |
| 302 |
glPointSize(5.0); |
| 303 |
glBegin(GL_POINTS); |
| 304 |
glVertex3f(0.0, 0.0, 0.0); |
| 305 |
glEnd(); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
void renderFontInfo() |
| 310 |
{ |
| 311 |
glMatrixMode(GL_PROJECTION); |
| 312 |
glLoadIdentity(); |
| 313 |
gluOrtho2D(0, w_win, 0, h_win); |
| 314 |
glMatrixMode(GL_MODELVIEW); |
| 315 |
glLoadIdentity(); |
| 316 |
|
| 317 |
// draw mode |
| 318 |
glColor3f(1.0, 1.0, 1.0); |
| 319 |
glRasterPos2f(20.0f , h_win - (20.0f + infoFont->Ascender())); |
| 320 |
|
| 321 |
switch(mode) |
| 322 |
{ |
| 323 |
case EDITING: |
| 324 |
infoFont->Render("Edit Mode"); |
| 325 |
break; |
| 326 |
case INTERACTIVE: |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
// draw font type |
| 331 |
glRasterPos2i(20 , 20); |
| 332 |
switch(GetStyle()) |
| 333 |
{ |
| 334 |
case FTGL_BITMAP: |
| 335 |
infoFont->Render("Bitmap Font"); |
| 336 |
break; |
| 337 |
case FTGL_PIXMAP: |
| 338 |
infoFont->Render("Pixmap Font"); |
| 339 |
break; |
| 340 |
case FTGL_OUTLINE: |
| 341 |
infoFont->Render("Outline Font"); |
| 342 |
break; |
| 343 |
case FTGL_POLYGON: |
| 344 |
infoFont->Render("Polygon Font"); |
| 345 |
break; |
| 346 |
case FTGL_EXTRUDE: |
| 347 |
infoFont->Render("Extruded Font"); |
| 348 |
break; |
| 349 |
case FTGL_TEXTURE: |
| 350 |
infoFont->Render("Texture Font"); |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
glRasterPos2f(20.0f , 20.0f + infoFont->Ascender() - infoFont->Descender()); |
| 355 |
infoFont->Render(fontfiles[GetFace()]); |
| 356 |
|
| 357 |
// If the current layout is a SimpleLayout, output the alignemnt mode |
| 358 |
if(layouts[currentLayout] |
| 359 |
&& (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout]))) |
| 360 |
{ |
| 361 |
glRasterPos2f(20.0f , 20.0f + 2*(infoFont->Ascender() - infoFont->Descender())); |
| 362 |
// Output the alignment mode of the layout |
| 363 |
switch (((FTSimpleLayout *)layouts[currentLayout])->GetAlignment()) |
| 364 |
{ |
| 365 |
case FTGL::ALIGN_LEFT: |
| 366 |
infoFont->Render("Align Left"); |
| 367 |
break; |
| 368 |
case FTGL::ALIGN_RIGHT: |
| 369 |
infoFont->Render("Align Right"); |
| 370 |
break; |
| 371 |
case FTGL::ALIGN_CENTER: |
| 372 |
infoFont->Render("Align Center"); |
| 373 |
break; |
| 374 |
case FTGL::ALIGN_JUSTIFY: |
| 375 |
infoFont->Render("Align Justified"); |
| 376 |
break; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
void do_display (void) |
| 383 |
{ |
| 384 |
switch(GetStyle()) |
| 385 |
{ |
| 386 |
case FTGL_BITMAP: |
| 387 |
case FTGL_PIXMAP: |
| 388 |
case FTGL_OUTLINE: |
| 389 |
break; |
| 390 |
case FTGL_POLYGON: |
| 391 |
glDisable(GL_BLEND); |
| 392 |
setUpLighting(); |
| 393 |
break; |
| 394 |
case FTGL_EXTRUDE: |
| 395 |
glEnable(GL_DEPTH_TEST); |
| 396 |
glDisable(GL_BLEND); |
| 397 |
setUpLighting(); |
| 398 |
break; |
| 399 |
case FTGL_TEXTURE: |
| 400 |
glEnable(GL_TEXTURE_2D); |
| 401 |
glDisable(GL_DEPTH_TEST); |
| 402 |
setUpLighting(); |
| 403 |
glNormal3f(0.0, 0.0, 1.0); |
| 404 |
break; |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
glColor3f(1.0, 1.0, 1.0); |
| 409 |
// If you do want to switch the color of bitmaps rendered with glBitmap, |
| 410 |
// you will need to explicitly call glRasterPos (or its ilk) to lock |
| 411 |
// in a changed current color. |
| 412 |
|
| 413 |
// If there is an active layout use it to render the font |
| 414 |
if (layouts[currentLayout]) |
| 415 |
{ |
| 416 |
layouts[currentLayout]->Render(myString); |
| 417 |
} |
| 418 |
else |
| 419 |
{ |
| 420 |
fonts[current_font]->Render(myString); |
| 421 |
} |
| 422 |
|
| 423 |
renderFontmetrics(); |
| 424 |
renderFontInfo(); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
void display(void) |
| 429 |
{ |
| 430 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 431 |
|
| 432 |
SetCamera(); |
| 433 |
|
| 434 |
switch(GetStyle()) |
| 435 |
{ |
| 436 |
case FTGL_BITMAP: |
| 437 |
case FTGL_PIXMAP: |
| 438 |
glRasterPos2i((long)(w_win / 2 + OX), (long)(h_win / 2 + OY)); |
| 439 |
glTranslatef(w_win / 2 + OX, h_win / 2 + OY, 0.0); |
| 440 |
break; |
| 441 |
case FTGL_OUTLINE: |
| 442 |
case FTGL_POLYGON: |
| 443 |
case FTGL_EXTRUDE: |
| 444 |
case FTGL_TEXTURE: |
| 445 |
glTranslatef(OX, OY, 0); |
| 446 |
tbMatrix(); |
| 447 |
break; |
| 448 |
} |
| 449 |
|
| 450 |
glPushMatrix(); |
| 451 |
|
| 452 |
do_display(); |
| 453 |
|
| 454 |
glPopMatrix(); |
| 455 |
|
| 456 |
glutSwapBuffers(); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
void myinit(int numFontFiles) |
| 461 |
{ |
| 462 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 463 |
glClearColor(0.13, 0.17, 0.32, 0.0); |
| 464 |
glColor3f(1.0, 1.0, 1.0); |
| 465 |
|
| 466 |
glEnable(GL_CULL_FACE); |
| 467 |
glFrontFace(GL_CCW); |
| 468 |
|
| 469 |
glEnable(GL_DEPTH_TEST); |
| 470 |
|
| 471 |
glEnable(GL_POLYGON_OFFSET_LINE); |
| 472 |
glPolygonOffset(1.0, 1.0); // ???? |
| 473 |
|
| 474 |
SetCamera(); |
| 475 |
|
| 476 |
tbInit(GLUT_LEFT_BUTTON); |
| 477 |
tbAnimate(GL_FALSE); |
| 478 |
|
| 479 |
setUpFonts(numFontFiles); |
| 480 |
|
| 481 |
// Configure the SimpleLayout |
| 482 |
simpleLayout.SetLineLength(InitialLineLength); |
| 483 |
simpleLayout.SetFont(fonts[current_font]); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
void parsekey(unsigned char key, int, int) |
| 488 |
{ |
| 489 |
switch (key) |
| 490 |
{ |
| 491 |
case 27: |
| 492 |
exit(0); |
| 493 |
break; |
| 494 |
case 13: |
| 495 |
if(mode == EDITING) |
| 496 |
{ |
| 497 |
mode = INTERACTIVE; |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
mode = EDITING; |
| 502 |
carat = 0; |
| 503 |
} |
| 504 |
break; |
| 505 |
case '\t': |
| 506 |
// If current layout is a SimpleLayout, change its alignment properties |
| 507 |
if(layouts[currentLayout] |
| 508 |
&& (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout]))) |
| 509 |
{ |
| 510 |
FTSimpleLayout *l = (FTSimpleLayout *)layouts[currentLayout]; |
| 511 |
// Decrement the layout |
| 512 |
switch (l->GetAlignment()) |
| 513 |
{ |
| 514 |
case FTGL::ALIGN_LEFT: |
| 515 |
l->SetAlignment(FTGL::ALIGN_RIGHT); |
| 516 |
break; |
| 517 |
case FTGL::ALIGN_RIGHT: |
| 518 |
l->SetAlignment(FTGL::ALIGN_CENTER); |
| 519 |
break; |
| 520 |
case FTGL::ALIGN_CENTER: |
| 521 |
l->SetAlignment(FTGL::ALIGN_JUSTIFY); |
| 522 |
break; |
| 523 |
case FTGL::ALIGN_JUSTIFY: |
| 524 |
l->SetAlignment(FTGL::ALIGN_LEFT); |
| 525 |
break; |
| 526 |
} |
| 527 |
} |
| 528 |
break; |
| 529 |
default: |
| 530 |
if(mode == INTERACTIVE) |
| 531 |
{ |
| 532 |
myString[0] = key; |
| 533 |
myString[1] = 0; |
| 534 |
} |
| 535 |
else |
| 536 |
{ |
| 537 |
myString[carat] = key; |
| 538 |
myString[carat + 1] = 0; |
| 539 |
carat = carat > 2000 ? 2000 : carat + 1; |
| 540 |
} |
| 541 |
break; |
| 542 |
} |
| 543 |
|
| 544 |
glutPostRedisplay(); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
void parseSpecialKey(int key, int, int) |
| 549 |
{ |
| 550 |
FTSimpleLayout *l = NULL; |
| 551 |
unsigned int s; |
| 552 |
|
| 553 |
// If the currentLayout is a SimpleLayout store a pointer in l |
| 554 |
if(layouts[currentLayout] |
| 555 |
&& (dynamic_cast <FTSimpleLayout *>(layouts[currentLayout]))) |
| 556 |
{ |
| 557 |
l = (FTSimpleLayout *)layouts[currentLayout]; |
| 558 |
} |
| 559 |
|
| 560 |
switch (key) |
| 561 |
{ |
| 562 |
case GLUT_KEY_UP: |
| 563 |
current_font = (GetFace()*NumStyles + (current_font + 1)%NumStyles)%totalFonts; |
| 564 |
break; |
| 565 |
case GLUT_KEY_DOWN: |
| 566 |
current_font = (GetFace()*NumStyles + (current_font + NumStyles - 1)%NumStyles)%totalFonts; |
| 567 |
break; |
| 568 |
case GLUT_KEY_LEFT: |
| 569 |
s = fonts[current_font]->FaceSize(); |
| 570 |
if (s >= 2) |
| 571 |
fonts[current_font]->FaceSize(s - 1); |
| 572 |
break; |
| 573 |
case GLUT_KEY_RIGHT: |
| 574 |
fonts[current_font]->FaceSize(fonts[current_font]->FaceSize() + 1); |
| 575 |
break; |
| 576 |
case GLUT_KEY_PAGE_UP: |
| 577 |
current_font = (current_font + NumStyles)%totalFonts; |
| 578 |
break; |
| 579 |
case GLUT_KEY_PAGE_DOWN: |
| 580 |
current_font = (current_font + totalFonts - NumStyles)%totalFonts; |
| 581 |
break; |
| 582 |
case GLUT_KEY_HOME: |
| 583 |
currentLayout = (currentLayout + 1)%NumLayouts; |
| 584 |
break; |
| 585 |
case GLUT_KEY_END: |
| 586 |
currentLayout = (currentLayout + NumLayouts - 1)%NumLayouts; |
| 587 |
break; |
| 588 |
case GLUT_KEY_F1: |
| 589 |
case GLUT_KEY_F10: |
| 590 |
// If the current layout is simple decrement its line length |
| 591 |
if (l) l->SetLineLength(l->GetLineLength() - 10.0f); |
| 592 |
break; |
| 593 |
case GLUT_KEY_F2: |
| 594 |
case GLUT_KEY_F11: |
| 595 |
// If the current layout is simple increment its line length |
| 596 |
if (l) l->SetLineLength(l->GetLineLength() + 10.0f); |
| 597 |
break; |
| 598 |
} |
| 599 |
|
| 600 |
// If the current layout is a SimpleLayout, update its font. |
| 601 |
if(l) |
| 602 |
{ |
| 603 |
l->SetFont(fonts[current_font]); |
| 604 |
} |
| 605 |
|
| 606 |
glutPostRedisplay(); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
void motion(int x, int y) |
| 611 |
{ |
| 612 |
tbMotion(x, y); |
| 613 |
} |
| 614 |
|
| 615 |
void mouse(int button, int state, int x, int y) |
| 616 |
{ |
| 617 |
tbMouse(button, state, x, y); |
| 618 |
} |
| 619 |
|
| 620 |
void myReshape(int w, int h) |
| 621 |
{ |
| 622 |
glMatrixMode (GL_MODELVIEW); |
| 623 |
glViewport (0, 0, w, h); |
| 624 |
glLoadIdentity(); |
| 625 |
|
| 626 |
w_win = w; |
| 627 |
h_win = h; |
| 628 |
SetCamera(); |
| 629 |
|
| 630 |
tbReshape(w_win, h_win); |
| 631 |
} |
| 632 |
|
| 633 |
void SetCamera(void) |
| 634 |
{ |
| 635 |
switch(GetStyle()) |
| 636 |
{ |
| 637 |
case FTGL_BITMAP: |
| 638 |
case FTGL_PIXMAP: |
| 639 |
glMatrixMode(GL_PROJECTION); |
| 640 |
glLoadIdentity(); |
| 641 |
gluOrtho2D(0, w_win, 0, h_win); |
| 642 |
glMatrixMode(GL_MODELVIEW); |
| 643 |
glLoadIdentity(); |
| 644 |
break; |
| 645 |
case FTGL_OUTLINE: |
| 646 |
case FTGL_POLYGON: |
| 647 |
case FTGL_EXTRUDE: |
| 648 |
case FTGL_TEXTURE: |
| 649 |
glMatrixMode (GL_PROJECTION); |
| 650 |
glLoadIdentity (); |
| 651 |
gluPerspective(90, (float)w_win / (float)h_win, 1, 1000); |
| 652 |
glMatrixMode(GL_MODELVIEW); |
| 653 |
glLoadIdentity(); |
| 654 |
gluLookAt(0.0, 0.0, (float)h_win / 2.0f, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); |
| 655 |
break; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
int main(int argc, char *argv[]) |
| 661 |
{ |
| 662 |
int numFontFiles; |
| 663 |
|
| 664 |
if((argc >= 2) && !IGNORE_ARGV) |
| 665 |
{ |
| 666 |
fontfiles = (char const * const *)argv + 1; |
| 667 |
numFontFiles = argc - 1; |
| 668 |
} |
| 669 |
else |
| 670 |
{ |
| 671 |
fontfiles = defaultFonts; |
| 672 |
numFontFiles = NumDefaultFonts; |
| 673 |
} |
| 674 |
|
| 675 |
if(!fontfiles[0]) |
| 676 |
{ |
| 677 |
fprintf(stderr, "At least one font file must be specified on the command line\n"); |
| 678 |
exit(1); |
| 679 |
} |
| 680 |
|
| 681 |
glutInit(&argc, argv); |
| 682 |
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE | GLUT_MULTISAMPLE); |
| 683 |
glutInitWindowPosition(50, 50); |
| 684 |
glutInitWindowSize(w_win, h_win); |
| 685 |
glutCreateWindow("FTGL TEST"); |
| 686 |
glutDisplayFunc(display); |
| 687 |
glutKeyboardFunc(parsekey); |
| 688 |
glutSpecialFunc(parseSpecialKey); |
| 689 |
glutMouseFunc(mouse); |
| 690 |
glutMotionFunc(motion); |
| 691 |
glutReshapeFunc(myReshape); |
| 692 |
glutIdleFunc(display); |
| 693 |
|
| 694 |
myinit(numFontFiles); |
| 695 |
|
| 696 |
glutMainLoop(); |
| 697 |
|
| 698 |
return 0; |
| 699 |
} |
| 700 |
|