Changeset 3769 in orxonox.OLD for orxonox/branches/textEngine/src/lib
- Timestamp:
- Apr 9, 2005, 10:32:18 PM (20 years ago)
- Location:
- orxonox/branches/textEngine/src/lib
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/textEngine/src/lib/graphics/font/text_engine.cc
r3768 r3769 31 31 32 32 #include "graphics_engine.h" 33 #include "resource_manager.h" 34 33 35 #include "p_node.h" 34 36 #include "vector.h" … … 39 41 /// TEXT /// 40 42 //////////// 41 42 Text::Text(void) 43 { 44 45 } 43 Text::Text(Font* font, int type) 44 { 45 // initialize this Text 46 this->bindNode = NULL; 47 this->text = NULL; 48 this->texture = 0; 49 this->setType(TEXT_DYNAMIC); 50 this->setPosition(0, 0); 51 52 this->setText(FONT_DEFAULT_TEXT); 53 } 54 46 55 47 56 Text::~Text(void) … … 89 98 } 90 99 100 /** 101 \brief sets a new color to the font 102 \param r Red 103 \param g Green 104 \param b Blue 105 */ 106 void Text::setColor(Uint8 r, Uint8 g, Uint8 b) 107 { 108 this->color.r = r; 109 this->color.g = g; 110 this->color.b = b; 111 } 112 113 /** 114 \brief creates a texture out of the given parameters 115 116 this has to be called every time by the user, to if changes were made. 117 */ 118 void Text::createTexture(void) 119 { 120 SDL_Surface* tmpSurf; 121 if (this->texture) 122 glDeleteTextures(1, &this->texture); 123 tmpSurf = TTF_RenderText_Blended(this->font->font, 124 this->text, 125 this->color); 126 if (tmpSurf) 127 this->texture = loadTexture(tmpSurf, &this->texCoord); 128 129 this->posSize.w = tmpSurf->w; 130 this->posSize.h = tmpSurf->h; 131 SDL_FreeSurface(tmpSurf); 132 } 91 133 92 134 /** … … 97 139 { 98 140 // storing all the Transformation Matrices. 99 100 141 GLdouble modMat[16]; 101 142 GLdouble projMat[16]; … … 104 145 glGetDoublev(GL_MODELVIEW_MATRIX, modMat); 105 146 glGetIntegerv(GL_VIEWPORT, viewPort); 106 // this->enter2DMode(); 147 148 printf("rendering some Text\n"); 149 //enter2DMode(); 107 150 108 151 … … 168 211 } 169 212 } 170 // this->leave2DMode();213 //leave2DMode(); 171 214 } 172 215 … … 186 229 Font::Font(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b) 187 230 { 188 if (!TTF_WasInit())189 TextEngine::enableFonts();190 191 231 // setting default values. 192 232 this->font = NULL; 193 233 this->fontFile = NULL; 194 234 this->glyphArray = NULL; 195 this->fastTextureID = 0; 196 197 this->currentText = new Text; 198 199 this->currentText->bindNode = NULL; 200 this->currentText->text = NULL; 201 this->currentText->texture = 0; 235 this->fastTextureID = 0; 202 236 203 237 this->setSize(fontSize); 204 this->setStyle(TTF_STYLE_NORMAL); 238 this->setStyle("c");//TTF_STYLE_NORMAL); 239 205 240 this->setFont(fontFile); 206 241 207 242 208 this->currentText->setType(TEXT_DYNAMIC); 209 this->currentText->setPosition(0, 0); 210 211 this->currentText->setText(FONT_DEFAULT_TEXT); 212 213 this->setColor(r, g, b); 214 215 this->createTexture(); 243 this->setFastColor(r, g, b); 216 244 217 245 this->fastTextureID = this->createFastTexture(); … … 223 251 Font::~Font(void) 224 252 { 225 delete this->currentText; 226 253 // deleting the List of all Texts 254 255 // deleting all Glyphs 227 256 if (this->glyphArray) 228 257 { … … 232 261 } 233 262 263 // erease this font out of the memory. 234 264 if (this->font) 235 265 TTF_CloseFont(this->font); … … 303 333 \param b Blue 304 334 */ 305 void Font::setColor(Uint8 r, Uint8 g, Uint8 b) 306 { 307 this->currentText->color.r = r; 308 this->currentText->color.g = g; 309 this->currentText->color.b = b; 310 } 311 312 /** 313 \brief creates a texture out of the given parameters 314 315 this has to be called every time by the user, to if changes were made. 316 */ 317 void Font::createTexture(void) 318 { 319 SDL_Surface* tmpSurf; 320 if (this->currentText->texture) 321 glDeleteTextures(1, &this->currentText->texture); 322 tmpSurf = TTF_RenderText_Blended(this->font, 323 this->currentText->text, 324 this->currentText->color); 325 if (tmpSurf) 326 this->currentText->texture = loadTexture(tmpSurf, &this->currentText->texCoord); 327 328 this->currentText->posSize.w = tmpSurf->w; 329 this->currentText->posSize.h = tmpSurf->h; 330 SDL_FreeSurface(tmpSurf); 331 } 332 335 void Font::setFastColor(Uint8 r, Uint8 g, Uint8 b) 336 { 337 this->fastColor.r = r; 338 this->fastColor.g = g; 339 this->fastColor.b = b; 340 } 333 341 334 342 /** … … 506 514 } 507 515 // reading in the new Glyph 508 //glyphSurf = TTF_RenderGlyph_Shaded(this->font, i, this->currentText->color, tmpColor); 509 glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->currentText->color); 516 glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->fastColor); 510 517 if( glyphSurf ) 511 518 { … … 569 576 570 577 /** 578 \brief stores Glyph Metrics in an Array. 579 \param from The Glyph to start from. 580 \param count The number of Glyphs to start From. 581 */ 582 void Font::initGlyphs(Uint16 from, Uint16 count) 583 { 584 /* initialize the Array, and set all its entries to NULL 585 * only if the Glyph-array has not been initialized 586 */ 587 if (!this->glyphArray) 588 { 589 this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR]; 590 for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) 591 this->glyphArray[i] = NULL; 592 } 593 594 Uint16 lastGlyph = from + count; 595 596 for (int i = from; i <= lastGlyph; i++) 597 { 598 // setting up all the Glyphs we like. 599 glyphArray[i] = getGlyphMetrics(i); 600 } 601 return; 602 } 603 604 /** 605 \returns the optimal size to use as the texture size 606 607 \todo: this algorithm can be a lot more faster, althought it does 608 not really matter within the init-context, and 128 glyphs. 609 610 This function searches for a 2^n sizes texture-size, this is for 611 openGL-version < 1.2 compatibility. and because it is realy easy like this. 612 */ 613 int Font::findOptimalFastTextureSize(void) 614 { 615 int i; 616 int x,y; // the counters 617 int maxLineHeight; 618 int size = 32; // starting Value, we have to start somewhere 32 seems reasonable. 619 bool sizeOK = false; 620 Glyph* tmpGlyph; 621 622 while (!sizeOK) 623 { 624 x = 0; y = 0; 625 maxLineHeight = 0; 626 for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) 627 { 628 if(tmpGlyph = this->glyphArray[i]) 629 { 630 // getting the height of the highest Glyph in the Line. 631 if (tmpGlyph->height > maxLineHeight) 632 maxLineHeight = tmpGlyph->height; 633 634 if (x + tmpGlyph->width > size) 635 { 636 x = 0; 637 y = y + maxLineHeight; 638 maxLineHeight = 0; 639 } 640 if (y + maxLineHeight + 1 > size) 641 break; 642 x += tmpGlyph->width + 1; 643 644 } 645 } 646 if (i == FONT_HIGHEST_KNOWN_CHAR) 647 sizeOK = true; 648 else 649 size *= 2; 650 } 651 return size; 652 653 } 654 655 656 /** 657 \brief a simple function to get some interesting information about this class 658 */ 659 void Font::debug(void) 660 { 661 662 // print the loaded font's style 663 int style; 664 style=TTF_GetFontStyle(this->font); 665 PRINTF(0)("The font style is:"); 666 if(style==TTF_STYLE_NORMAL) 667 PRINTF(0)(" normal"); 668 else { 669 if(style&TTF_STYLE_BOLD) 670 PRINTF(0)(" bold"); 671 if(style&TTF_STYLE_ITALIC) 672 PRINTF(0)(" italic"); 673 if(style&TTF_STYLE_UNDERLINE) 674 PRINTF(0)(" underline"); 675 } 676 PRINTF(0)("\n"); 677 678 679 } 680 681 //////////// 682 /// UTIL /// 683 //////////// 684 /** 571 685 \brief Loads a Font from an SDL_surface into a texture. 572 686 \param surface The surface to make the texture of … … 574 688 \returns the ID of the texture 575 689 */ 576 GLuint Font::loadTexture(SDL_Surface *surface, TexCoord* texCoord)690 GLuint loadTexture(SDL_Surface *surface, TexCoord* texCoord) 577 691 { 578 692 GLuint texture; … … 650 764 651 765 /** 652 \brief stores Glyph Metrics in an Array.653 \param from The Glyph to start from.654 \param count The number of Glyphs to start From.655 */656 void Font::initGlyphs(Uint16 from, Uint16 count)657 {658 /* initialize the Array, and set all its entries to NULL659 * only if the Glyph-array has not been initialized660 */661 if (!this->glyphArray)662 {663 this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];664 for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)665 this->glyphArray[i] = NULL;666 }667 668 Uint16 lastGlyph = from + count;669 670 for (int i = from; i <= lastGlyph; i++)671 {672 // setting up all the Glyphs we like.673 glyphArray[i] = getGlyphMetrics(i);674 }675 return;676 }677 678 /**679 \returns the optimal size to use as the texture size680 681 \todo: this algorithm can be a lot more faster, althought it does682 not really matter within the init-context, and 128 glyphs.683 684 This function searches for a 2^n sizes texture-size, this is for685 openGL-version < 1.2 compatibility. and because it is realy easy like this.686 */687 int Font::findOptimalFastTextureSize(void)688 {689 int i;690 int x,y; // the counters691 int maxLineHeight;692 int size = 32; // starting Value, we have to start somewhere 32 seems reasonable.693 bool sizeOK = false;694 Glyph* tmpGlyph;695 696 while (!sizeOK)697 {698 x = 0; y = 0;699 maxLineHeight = 0;700 for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)701 {702 if(tmpGlyph = this->glyphArray[i])703 {704 // getting the height of the highest Glyph in the Line.705 if (tmpGlyph->height > maxLineHeight)706 maxLineHeight = tmpGlyph->height;707 708 if (x + tmpGlyph->width > size)709 {710 x = 0;711 y = y + maxLineHeight;712 maxLineHeight = 0;713 }714 if (y + maxLineHeight + 1 > size)715 break;716 x += tmpGlyph->width + 1;717 718 }719 }720 if (i == FONT_HIGHEST_KNOWN_CHAR)721 sizeOK = true;722 else723 size *= 2;724 }725 return size;726 727 }728 729 /**730 766 \brief Quick utility function for texture creation 731 767 \param input an integer 732 768 \returns the next bigger 2^n-integer than input 733 769 */ 734 int Font::powerOfTwo(int input)770 int powerOfTwo(int input) 735 771 { 736 772 int value = 1; … … 744 780 745 781 746 /**747 \brief a simple function to get some interesting information about this class748 */749 void Font::debug(void)750 {751 752 // print the loaded font's style753 int style;754 style=TTF_GetFontStyle(this->font);755 PRINTF(0)("The font style is:");756 if(style==TTF_STYLE_NORMAL)757 PRINTF(0)(" normal");758 else {759 if(style&TTF_STYLE_BOLD)760 PRINTF(0)(" bold");761 if(style&TTF_STYLE_ITALIC)762 PRINTF(0)(" italic");763 if(style&TTF_STYLE_UNDERLINE)764 PRINTF(0)(" underline");765 }766 PRINTF(0)("\n");767 768 769 }770 771 772 773 774 775 776 782 777 783 … … 786 792 { 787 793 this->setClassName ("TextEngine"); 788 794 this->enableFonts(); 795 796 this->textList = new tList<Text>; 789 797 } 790 798 … … 810 818 TextEngine::~TextEngine () 811 819 { 820 this->disableFonts(); 821 822 delete this->textList; 823 812 824 TextEngine::singletonRef = NULL; 813 825 … … 842 854 else 843 855 PRINTF(4)("Fonts were not initialized.\n"); 856 } 857 858 /** 859 \brief creates a new Text with a certain font. 860 \see Font::Font 861 \see Text::Text 862 */ 863 Text* TextEngine::createText(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b) 864 { 865 Font* tmpFont; 866 Text* newText; 867 868 tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME); 869 if (!tmpFont) 870 { 871 PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile); 872 return NULL; 873 } 874 875 newText = new Text(tmpFont, TEXT_DYNAMIC); 876 } 877 878 void TextEngine::draw(void) 879 { 880 tIterator<Text>* textIterator = textList->getIterator(); 881 Text* text = textIterator->nextElement(); 882 while( text != NULL) 883 { 884 text->draw(); 885 text = textIterator->nextElement(); 886 } 887 delete textIterator; 844 888 } 845 889 -
orxonox/branches/textEngine/src/lib/graphics/font/text_engine.h
r3768 r3769 93 93 { 94 94 public: 95 Text( void);95 Text(Font* font, int type = TEXT_DYNAMIC); 96 96 ~Text(void); 97 97 … … 101 101 void setText(const char* text); 102 102 void setPosition(int x, int y); 103 104 // Static Text 105 void setColor(Uint8 r, Uint8 g, Uint8 b); 106 void setStyle(char* renderStyle); 107 void createTexture(); 108 109 // Dynamic Text 110 111 103 112 104 113 virtual void draw(void); … … 113 122 GLuint texture; //!< A GL-texture to hold the text 114 123 TexCoord texCoord; //!< Texture-coordinates \todo fix this to have a struct 115 SDL_Rect posSize; //!< An SDL-Rectangle representing the position and size of the Text on the screen.124 SDL_Rect posSize; //!< An SDL-Rectangle representing the position and size of the Text on the screen. 116 125 117 126 PNode* bindNode; //!< A node the Text is bind to. (if NULL thr node will not be bound to anything.) … … 124 133 class Font 125 134 { 135 friend class Text; 126 136 public: 127 137 Font(const char* fontFile, unsigned int fontSize = FONT_DEFAULT_SIZE, … … 132 142 bool setFont(const char* fontFile); 133 143 void setSize(unsigned int fontSize); 134 void set Color(Uint8 r, Uint8 g, Uint8 b);144 void setFastColor(Uint8 r, Uint8 g, Uint8 b); 135 145 void setStyle(char* renderStyle); 136 146 137 147 inline Glyph** getGlyphArray(void) {return glyphArray;} 138 148 inline GLuint getFastTextureID(void) {return fastTextureID;} 139 void createTexture(void);140 149 141 150 … … 148 157 char* fontFile; //!< The fontfile from whitch the font was loaded. 149 158 unsigned int fontSize; //!< The size of the font in pixels. each Font has one size. 150 int renderStyle; //!< The Renderstyle159 int renderStyle; //!< The Renderstyle 151 160 152 161 Glyph** glyphArray; //!< An Array of all the Glyphs stored in the Array of Glyphs. 153 162 GLuint fastTextureID; //!< The fast textureID. 163 SDL_Color fastColor; //!< A Color for the fast Texture. 154 164 155 165 tList<Text>* textList; 156 Text* currentText;157 166 158 167 int getMaxHeight(void); … … 166 175 167 176 GLuint createFastTexture(); 168 GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);169 177 170 178 void initGlyphs(Uint16 from, Uint16 count); 171 179 int findOptimalFastTextureSize(void); 172 static int powerOfTwo(int input);173 180 174 181 void debug(void); 175 182 176 183 }; 184 GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord); 185 int powerOfTwo(int input); 177 186 178 187 … … 187 196 virtual ~TextEngine(void); 188 197 198 Text* createText(const char* fontFile, 199 unsigned int fontSize = FONT_DEFAULT_SIZE, 200 Uint8 r = FONT_DEFAULT_COLOR_R, 201 Uint8 g = FONT_DEFAULT_COLOR_G, 202 Uint8 b = FONT_DEFAULT_COLOR_B); 203 204 void draw(void); 205 206 private: 207 TextEngine(void); 208 static TextEngine* singletonRef; 209 189 210 // general 190 211 static void enableFonts(void); … … 192 213 static bool checkVersion(void); 193 214 194 private: 195 TextEngine(void); 196 static TextEngine* singletonRef; 197 198 tList<Font>* fontList; 215 216 // tList<Font>* fontList; 217 tList<Text>* textList; 199 218 200 219 }; -
orxonox/branches/textEngine/src/lib/util/resource_manager.cc
r3703 r3769 22 22 #include "primitive_model.h" 23 23 #include "texture.h" 24 #include "text_engine.h" 24 25 25 26 #include "list.h" … … 158 159 !strcmp(fileName, "cone")) 159 160 tmpType = PRIM; 161 else if (!strncmp(fileName+(strlen(fileName)-4), ".ttf", 4)) 162 tmpType = TTF; 160 163 else 161 164 tmpType = IMAGE; … … 220 223 else if (!strcmp(tmpResource->name, "cone")) 221 224 tmpResource->pointer = new PrimitiveModel(CONE); 225 break; 226 case TTF: 227 if(isFile(fullName)) 228 tmpResource->pointer = new Font(fullName); 229 else 230 PRINTF(2)("Sorry, %s does not exist. Not loading Font\n", fullName); 222 231 break; 223 232 case IMAGE: -
orxonox/branches/textEngine/src/lib/util/resource_manager.h
r3681 r3769 19 19 20 20 //! An eumerator for different fileTypes the resourceManager supports \todo WAV, MP3, OGG support 21 enum ResourceType {OBJ, PRIM, WAV, MP3, OGG, IMAGE};21 enum ResourceType {OBJ, PRIM, WAV, MP3, OGG, TTF, IMAGE}; 22 22 //! An enumerator for different UNLOAD-types. 23 23 /** … … 49 49 50 50 It does it by looking, if a desired file has already been loaded. 51 52 \todo loading also dependant by parameters. 51 53 */ 52 54 class ResourceManager : public BaseObject
Note: See TracChangeset
for help on using the changeset viewer.