Changeset 5336 in orxonox.OLD for trunk/src/lib/graphics
- Timestamp:
- Oct 9, 2005, 12:54:09 PM (19 years ago)
- Location:
- trunk/src/lib/graphics
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/text_engine.cc
r5306 r5336 29 29 #include <stdio.h> 30 30 #include <string.h> 31 32 #ifdef HAVE_SDL_IMAGE_H 33 #include <SDL_image.h> 34 #else 35 #include <SDL/SDL_image.h> 36 #endif 37 #include "font.xpm" 38 31 39 32 40 #include "graphics_engine.h" … … 333 341 w = powerOfTwo(surface->w); 334 342 h = powerOfTwo(surface->h); 335 if (texCoord )343 if (texCoord != NULL) 336 344 { 337 345 texCoord->minU = 0.0f; … … 391 399 GL_UNSIGNED_BYTE, 392 400 image->pixels); 393 SDL_FreeSurface(image); /* No longer needed */401 SDL_FreeSurface(image); /* No longer needed the data */ 394 402 395 403 return texture; … … 416 424 //////////// 417 425 /** 418 * 426 * constructs a Font 419 427 * @param fontFile the File to load the font from 420 428 * @param fontSize the Size of the Font in Pixels 421 * @param r Red value of the Font. 422 * @param g Green value of the Font. 423 * @param b Blue value of the Font. 424 */ 429 */ 425 430 Font::Font(const char* fontFile, unsigned int fontSize) 426 431 { … … 433 438 this->setSize(fontSize); 434 439 435 this->loadFont(fontFile); 440 if (fontFile != NULL) 441 this->loadFont(fontFile); 436 442 437 443 this->setStyle("c");//TTF_STYLE_NORMAL); … … 439 445 this->fastTextureID = this->createFastTexture(); 440 446 } 447 448 /** 449 * constructs a Font 450 * @param fontFile the File to load the font from 451 * @param fontSize the Size of the Font in Pixels 452 */ 453 Font::Font(char** xpmArray) 454 { 455 this->setClassID(CL_FONT, "Font"); 456 // setting default values. 457 this->font = NULL; 458 this->glyphArray = NULL; 459 this->fastTextureID = 0; 460 461 // this->setSize(fontSize); 462 463 if (xpmArray != NULL) 464 this->loadFontFromXPMArray(xpmArray); 465 } 466 441 467 442 468 /** 443 469 * destructs a font 444 470 * this releases the memory a font uses to be opened. 445 */ 471 * deletes the glLists, and the TTF-handler, if present. 472 */ 446 473 Font::~Font() 447 474 { 448 // deleting the List of all Texts475 // deleting the Lists of all Texts 449 476 450 477 // deleting all Glyphs 451 if (this->glyphArray )478 if (this->glyphArray != NULL) 452 479 { 453 480 for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) 454 delete this->glyphArray[i]; 481 { 482 if (this->glyphArray[i] != NULL) 483 { 484 glDeleteLists(this->glyphArray[i]->displayList, 1); 485 delete this->glyphArray[i]; 486 } 487 } 455 488 delete[] this->glyphArray; 456 489 } … … 461 494 } 462 495 463 /** 464 * sets The Font. 496 497 /** 498 * sets The Font. 465 499 * @param fontFile The file containing the font. 466 500 * @returns true if loaded, false if something went wrong, or if a font was loaded before. … … 489 523 490 524 /** 525 * loads a font From an XPM-array. 526 * @param xpmArray the array of the XPM to load the font from. 527 */ 528 bool Font::loadFontFromXPMArray(char** xpmArray) 529 { 530 if (xpmArray == NULL) 531 return false; 532 533 // loading to a texture. 534 SDL_Surface* image = IMG_ReadXPMFromArray(xpmArray); 535 if(image == NULL) 536 { 537 PRINTF(1)("loading from XPM-array failed: %s\n", IMG_GetError()); 538 return false; 539 } 540 TexCoord texCoord; 541 this->fastTextureID = Text::loadTexture(image, &texCoord); 542 543 // initializing the Glyphs. 544 if (!this->glyphArray) 545 { 546 float cx,cy; 547 this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR]; 548 for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) 549 { 550 this->glyphArray[i] = new Glyph; 551 this->glyphArray[i]->displayList = glGenLists(1); 552 cx=(float)(i%16)/16.0f; // X Position Of Current Character 553 cy=(float)(i/16)/16.0f; // Y Position Of Current Character 554 glNewList(this->glyphArray[i]->displayList, GL_COMPILE); // Start Building A List 555 glBegin(GL_QUADS); // Use A Quad For Each Character 556 glTexCoord2f(cx,1.0f-cy-0.0625f); // Texture Coord (Bottom Left) 557 glVertex2d(0,16); // Vertex Coord (Bottom Left) 558 glTexCoord2f(cx+0.0625f,1.0f-cy-0.0625f); // Texture Coord (Bottom Right) 559 glVertex2i(16,16); // Vertex Coord (Bottom Right) 560 glTexCoord2f(cx+0.0625f,1.0f-cy-0.001f); // Texture Coord (Top Right) 561 glVertex2i(16,0); // Vertex Coord (Top Right) 562 glTexCoord2f(cx,1.0f-cy-0.001f); // Texture Coord (Top Left) 563 glVertex2i(0,0); // Vertex Coord (Top Left) 564 glEnd(); // Done Building Our Quad (Character) 565 // glTranslated(12,0,0); // Move To The Right Of The Character 566 glEndList(); // Done Building The Display List 567 568 this->glyphArray[i]->width = 12; 569 } 570 } 571 SDL_FreeSurface(image); 572 return true; 573 } 574 575 576 /** 491 577 * sets a specific renderStyle 492 578 * @param renderStyle the Style to render: a char-array containing: … … 519 605 this->fontSize = fontSize; 520 606 } 607 608 Font* Font::defaultFont = NULL; 609 /** 610 * initializes the default font 611 */ 612 void Font::initDefaultFont() 613 { 614 if (Font::defaultFont == NULL) 615 Font::defaultFont = new Font(font_xpm); 616 } 617 618 /** 619 * deletes the default font 620 */ 621 void Font::removeDefaultFont() 622 { 623 if (Font::defaultFont != NULL) 624 delete Font::defaultFont; 625 Font::defaultFont = NULL; 626 } 627 521 628 522 629 /** … … 582 689 } 583 690 691 /** 692 * creates a Fast-Texture of this Font 693 */ 584 694 GLuint Font::createFastTexture() 585 695 { … … 878 988 879 989 TextEngine::checkVersion(); 990 Font::initDefaultFont(); 880 991 } 881 992 else … … 890 1001 if (TTF_WasInit()) 891 1002 { 1003 Font::removeDefaultFont(); 892 1004 TTF_Quit(); 893 1005 } -
trunk/src/lib/graphics/text_engine.h
r5306 r5336 21 21 22 22 #ifdef HAVE_SDL_TTF_H 23 #include "SDL_ttf.h"23 #include <SDL_ttf.h> 24 24 #else 25 #include "SDL/SDL_ttf.h"25 #include <SDL/SDL_ttf.h> 26 26 #endif 27 27 … … 46 46 #define FONT_NUM_COLORS 256 //!< number of colors. 47 47 48 #define FONT_HIGHEST_KNOWN_CHAR 128//!< The highest character known to the textEngine.48 #define FONT_HIGHEST_KNOWN_CHAR 256 //!< The highest character known to the textEngine. 49 49 50 50 #define TEXT_DEFAULT_ALIGNMENT TEXT_ALIGN_CENTER //!< default alignment … … 129 129 void debug() const; 130 130 131 // helpers. 132 static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord); 133 static int powerOfTwo(int input); 134 131 135 private: 132 136 Text(Font* font = NULL, TEXT_RENDER_TYPE type = TEXT_RENDER_DYNAMIC); 133 137 void setFont(Font* font); 134 138 135 static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);136 static int powerOfTwo(int input);137 139 138 140 private: … … 163 165 Font(const char* fontFile, 164 166 unsigned int fontSize = FONT_DEFAULT_SIZE); 167 Font(char** xpmArray); 165 168 166 169 virtual ~Font(); … … 168 171 // font 169 172 bool loadFont(const char* fontFile); 173 bool loadFontFromXPMArray(char** xpmArray); 174 170 175 void setSize(unsigned int fontSize); 171 176 void setStyle(const char* renderStyle); … … 175 180 /** @returns the texture to the fast-texture */ 176 181 inline GLuint getFastTextureID() const { return fastTextureID; }; 182 /** @returns the default Font */ 183 inline static Font* getDefaultFont() { return Font::defaultFont; }; 184 185 static void initDefaultFont(); 186 static void removeDefaultFont(); 177 187 178 188 private: … … 190 200 191 201 private: 192 // general purpose 193 GLdouble projMat[16]; //!< The Projection Matrix 194 202 static Font* defaultFont; //!< a default font, that is used, if other fonts were unable to be loaded. 195 203 // information about the Font 196 204 TTF_Font* font; //!< The font we use for this.
Note: See TracChangeset
for help on using the changeset viewer.