Changeset 3736 in orxonox.OLD for orxonox/branches/textEngine/src/lib/graphics/font
- Timestamp:
- Apr 6, 2005, 8:35:06 PM (20 years ago)
- Location:
- orxonox/branches/textEngine/src/lib/graphics/font
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc
r3735 r3736 15 15 *** 16 16 * This file is extended to the needs of the orxonox-project. * 17 * the Copyright of the original file is below this copyright*17 * Originally it came from the glfont.c-example from SDL_ttf. * 18 18 * *** 19 19 … … 23 23 adding them to orxonox. This is really important, because we do not 24 24 want to defend anyone. 25 */26 27 /*28 glfont: An example of using the SDL_ttf library with OpenGL.29 Copyright (C) 1997-2004 Sam Lantinga30 31 This library is free software; you can redistribute it and/or32 modify it under the terms of the GNU Library General Public33 License as published by the Free Software Foundation; either34 version 2 of the License, or (at your option) any later version.35 36 This library is distributed in the hope that it will be useful,37 but WITHOUT ANY WARRANTY; without even the implied warranty of38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU39 Library General Public License for more details.40 41 You should have received a copy of the GNU Library General Public42 License along with this library; if not, write to the Free43 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA44 45 The SDL_GL_* functions in this file are available in the public domain.46 47 Sam Lantinga48 slouken@libsdl.org49 25 */ 50 26 … … 76 52 delete this->currentText; 77 53 54 if (this->glyphArray) 55 { 56 for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) 57 delete this->glyphArray[i]; 58 delete []this->glyphArray; 59 } 60 78 61 if (this->font) 79 62 TTF_CloseFont(this->font); … … 128 111 this->font = NULL; 129 112 this->fontFile = NULL; 113 this->glyphArray = NULL; 130 114 131 115 this->currentText = new Text; … … 369 353 /** 370 354 \param character The character to get info about. 371 \returns a Glyph struct of a character. 355 \returns a Glyph struct of a character. This Glyph is a pointer, 356 and MUST be deleted by the user.. 372 357 373 358 This only works for horizontal fonts. see … … 375 360 for more info about vertical Fonts 376 361 */ 377 Glyph GLFont::getGlyphMetrics(Uint16 character)378 { 379 Glyph rg;380 rg .character = character;381 TTF_GlyphMetrics(this->font, rg .character,382 &rg .minX, &rg.maxX,383 &rg .minY, &rg.maxY,384 &rg .advance);385 rg .height = rg.maxY - rg.minY;386 rg .width = rg.maxX - rg.minX;387 rg .bearingX = (rg.advance - rg.width) / 2;388 rg .bearingY = rg.maxY;362 Glyph* GLFont::getGlyphMetrics(Uint16 character) 363 { 364 Glyph* rg = new Glyph; 365 rg->character = character; 366 TTF_GlyphMetrics(this->font, rg->character, 367 &rg->minX, &rg->maxX, 368 &rg->minY, &rg->maxY, 369 &rg->advance); 370 rg->height = rg->maxY - rg->minY; 371 rg->width = rg->maxX - rg->minX; 372 rg->bearingX = (rg->advance - rg->width) / 2; 373 rg->bearingY = rg->maxY; 389 374 return rg; 390 375 } … … 451 436 * 97-122: a-z 452 437 */ 438 int numberOfGlyphs = 90; 439 this->initGlyphs(33, numberOfGlyphs); 440 int rectSize = this->findOptimalFastTextureSize(); 441 442 PRINTF(1)("rectSize = %d\n", rectSize); 443 453 444 SDL_Color tmpColor; 454 445 tmpColor.r = tmpColor.g = tmpColor.b = 0; 455 446 SDL_Surface* tmpSurf = SDL_CreateRGBSurface( 456 447 SDL_SWSURFACE, 457 1000, 300,448 rectSize, rectSize, 458 449 32, 459 450 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ … … 473 464 tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h; 474 465 SDL_SetClipRect(tmpSurf, &tmpRect); 466 int maxLineHeight = 0; 475 467 for ( int i = 33; i <= 122; i++ ) 476 468 { 477 469 SDL_Surface* glyph = NULL; 478 470 SDL_Surface* glyph2 = NULL; 479 Glyph tmpGlyph; 480 481 tmpGlyph = getGlyphMetrics(i); 482 if (tmpRect.x+tmpGlyph.width > tmpSurf->w) 471 Glyph* tmpGlyph; 472 473 tmpGlyph = this->glyphArray[i]; 474 if (tmpGlyph->height > maxLineHeight) 475 maxLineHeight = tmpGlyph->height; 476 477 if (tmpRect.x+tmpGlyph->width > tmpSurf->w) 483 478 { 484 479 tmpRect.x = 0; 485 tmpRect.y = tmpRect.y +tmpGlyph.height+1;480 tmpRect.y = tmpRect.y + maxLineHeight; 486 481 printf("x:%d, y:%d\n", tmpRect.x, tmpRect.y); 487 482 } 488 if (tmpRect.y +tmpGlyph .height > tmpSurf->h)483 if (tmpRect.y +tmpGlyph->height > tmpSurf->h) 489 484 { 490 485 PRINTF(1)("Protection, so font cannot write over the boundraries error\n"); … … 513 508 SDL_BlitSurface(glyph, NULL, tmpSurf, &tmpRect); 514 509 SDL_SaveBMP(tmpSurf, outname); 515 tmpRect.x += tmpGlyph.width+1; 510 tmpRect.x += tmpGlyph->width+1; 511 delete tmpGlyph; 516 512 } 517 513 } … … 601 597 602 598 return texture; 599 } 600 601 /** 602 \brief stores Glyph Metrics in an Array. 603 \param from The Glyph to start from. 604 \param count The number of Glyphs to start From. 605 */ 606 void GLFont::initGlyphs(Uint16 from, Uint16 count) 607 { 608 /* initialize the Array, and set all its entries to NULL 609 * only if the Glyph-array has not been initialized 610 */ 611 if (!this->glyphArray) 612 { 613 this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR]; 614 for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) 615 this->glyphArray[i] = NULL; 616 } 617 618 Uint16 lastGlyph = from + count; 619 620 for (int i = from; i <= lastGlyph; i++) 621 { 622 // setting up all the Glyphs we like. 623 glyphArray[i] = getGlyphMetrics(i); 624 } 625 return; 626 } 627 628 /** 629 \returns the optimal size to use as the texture size 630 631 \todo: this algorithm can be a lot more faster, althought it does 632 not really matter within the init-context, and 128 glyphs. 633 */ 634 int GLFont::findOptimalFastTextureSize(void) 635 { 636 int i; 637 int x,y; // the counters 638 int maxLineHeight; 639 int size = 32; // starting Value, we have to start somewhere 640 bool sizeOK = false; 641 Glyph* tmpGlyph; 642 643 while (!sizeOK) 644 { 645 x = 0; y = 0; 646 maxLineHeight = 0; 647 for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) 648 { 649 if(tmpGlyph = this->glyphArray[i]) 650 { 651 // getting the height of the highest Glyph in the Line. 652 if (tmpGlyph->height > maxLineHeight) 653 maxLineHeight = tmpGlyph->height; 654 655 if (x + tmpGlyph->width > size) 656 { 657 x = 0; 658 y = y + maxLineHeight; 659 maxLineHeight = 0; 660 } 661 if (y + maxLineHeight + 1 > size) 662 break; 663 x += tmpGlyph->width + 1; 664 665 } 666 } 667 if (i == FONT_HIGHEST_KNOWN_CHAR) 668 sizeOK = true; 669 else 670 size *= 2; 671 } 672 return size; 673 603 674 } 604 675 -
orxonox/branches/textEngine/src/lib/graphics/font/glfont.h
r3735 r3736 25 25 #define FONT_NUM_COLORS 256 //!< The number of colors. 26 26 27 #define FONT_HIGHEST_KNOWN_CHAR 128 //!< The highest character known to the textEngine. 27 28 28 29 … … 88 89 89 90 // information about the Font 90 TTF_Font* font; //!< The font we use for this. 91 char* fontFile; //!< The fontfile from whitch the font was loaded. 92 unsigned int fontSize; //!< The size of the font in pixels. each Font has one size. 91 TTF_Font* font; //!< The font we use for this. 92 char* fontFile; //!< The fontfile from whitch the font was loaded. 93 unsigned int fontSize; //!< The size of the font in pixels. each Font has one size. 94 95 Glyph** glyphArray; //!< An Array of all the Glyphs stored in the Array of Glyphs. 93 96 94 97 //! Represents one textElement. … … 112 115 int getMaxAscent(void); 113 116 int getMaxDescent(void); 114 Glyph getGlyphMetrics(Uint16 character);117 Glyph* getGlyphMetrics(Uint16 character); 115 118 116 119 static bool ttfInitialized; … … 124 127 GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord); 125 128 129 void initGlyphs(Uint16 from, Uint16 count); 130 int findOptimalFastTextureSize(void); 126 131 static int powerOfTwo(int input); 127 132
Note: See TracChangeset
for help on using the changeset viewer.