Changeset 7221 in orxonox.OLD for trunk/src/lib/graphics/text_engine
- Timestamp:
- Mar 15, 2006, 3:10:45 PM (19 years ago)
- Location:
- trunk/src/lib/graphics/text_engine
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/text_engine/font.cc
r6609 r7221 37 37 * @param fontSize the Size of the Font in Pixels 38 38 */ 39 Font::Font(const char*fontFile, unsigned int renderSize)39 Font::Font(const std::string& fontFile, unsigned int renderSize) 40 40 { 41 41 this->init(); … … 44 44 this->setStyle("c"); 45 45 46 if ( fontFile != NULL)46 if (!fontFile.empty()) 47 47 this->loadFontFromTTF(fontFile); 48 48 } … … 52 52 * @param imageFile the ImageFile to load the Font From. 53 53 */ 54 Font::Font(const char*imageFile)54 Font::Font(const std::string& imageFile) 55 55 { 56 56 this->init(); … … 58 58 // this->setSize(fontSize); 59 59 SDL_Surface* image = NULL; 60 if ( imageFile != NULL)61 image = IMG_Load(imageFile );60 if (!imageFile.empty()) 61 image = IMG_Load(imageFile.c_str()); 62 62 else 63 63 return; … … 68 68 } 69 69 else 70 PRINTF(1)("loading from surface %s failed: %s\n", imageFile , IMG_GetError());70 PRINTF(1)("loading from surface %s failed: %s\n", imageFile.c_str(), IMG_GetError()); 71 71 } 72 72 … … 114 114 115 115 //! @todo check if we really do not need to delete the fastTextureID here. 116 // if (this->fastTextureID != 0)117 // if(glIsTexture(this->fastTextureID))118 // glDeleteTextures(1, &this->fastTextureID);116 // if (this->fastTextureID != 0) 117 // if(glIsTexture(this->fastTextureID)) 118 // glDeleteTextures(1, &this->fastTextureID); 119 119 120 120 // erease this font out of the memory. … … 140 140 * @returns true if loaded, false if something went wrong, or if a font was loaded before. 141 141 */ 142 bool Font::loadFontFromTTF(const char*fontFile)142 bool Font::loadFontFromTTF(const std::string& fontFile) 143 143 { 144 144 // checking for existent Font. … … 222 222 * i: italic, b: bold, u, underline 223 223 */ 224 void Font::setStyle(const char*renderStyle)224 void Font::setStyle(const std::string& renderStyle) 225 225 { 226 226 this->renderStyle = TTF_STYLE_NORMAL; 227 227 228 for (int i = 0; i < strlen(renderStyle); i++) 229 if (strncmp(renderStyle+i, "b", 1) == 0) 228 for (int i = 0; i < renderStyle.size(); i++) 229 { 230 if (renderStyle[i] == 'b') 230 231 this->renderStyle |= TTF_STYLE_BOLD; 231 else if (strncmp(renderStyle+i, "i", 1) == 0)232 this->renderStyle |= TTF_STYLE_ITALIC;233 else if (strncmp(renderStyle+i, "u", 1) == 0)234 this->renderStyle |= TTF_STYLE_UNDERLINE;235 232 else if (renderStyle[i] == 'i') 233 this->renderStyle |= TTF_STYLE_ITALIC; 234 else if (renderStyle[i] == 'u') 235 this->renderStyle |= TTF_STYLE_UNDERLINE; 236 } 236 237 if (likely(this->fontTTF != NULL)) 237 238 TTF_SetFontStyle(this->fontTTF, this->renderStyle); 238 // else239 // PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");239 // else 240 // PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n"); 240 241 } 241 242 … … 247 248 * @param fileName the File to write the image into. 248 249 */ 249 void Font::createAsciiImage(const char*fileName, unsigned int size) const250 void Font::createAsciiImage(const std::string& fileName, unsigned int size) const 250 251 { 251 252 if (this->fontTTF == NULL) … … 258 259 SDL_Rect tmpRect; // this represents a Rectangle for blitting. 259 260 SDL_Surface* tmpSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, 260 261 261 height*size, height*size, 262 32, 262 263 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 263 264 265 266 264 0x000000FF, 265 0x0000FF00, 266 0x00FF0000, 267 0xFF000000 267 268 #else 268 269 270 271 269 0xFF000000, 270 0x00FF0000, 271 0x0000FF00, 272 0x000000FF 272 273 #endif 273 274 ); … … 299 300 } 300 301 } 301 SDL_SaveBMP(tmpSurf, fileName );302 SDL_SaveBMP(tmpSurf, fileName.c_str()); 302 303 SDL_FreeSurface(tmpSurf); 303 304 } … … 409 410 410 411 this->initGlyphs(32, numberOfGlyphs); 411 // this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space412 // this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space 412 413 413 414 int rectSize = this->findOptimalFastTextureSize(); … … 418 419 SDL_Rect tmpRect; // this represents a Rectangle for blitting. 419 420 SDL_Surface* tmpSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, 420 421 421 rectSize, rectSize, 422 32, 422 423 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 423 424 425 426 424 0x000000FF, 425 0x0000FF00, 426 0x00FF0000, 427 0xFF000000 427 428 #else 428 429 430 431 429 0xFF000000, 430 0x00FF0000, 431 0x0000FF00, 432 0x000000FF 432 433 #endif 433 434 ); … … 457 458 break; 458 459 } 459 460 // reading in the new Glyph 460 461 if (likely(this->fontTTF != NULL)) 461 462 { … … 521 522 for (int i = from; i <= lastGlyph; i++) 522 523 { 523 524 // setting up all the Glyphs we like. 524 525 glyphArray[i] = getGlyphMetrics(i); 525 526 } … … 555 556 if((tmpGlyph = this->glyphArray[i]) != NULL) 556 557 { 557 558 // getting the height of the highest Glyph in the Line. 558 559 if (tmpGlyph->height*this->renderSize > maxLineHeight) 559 560 maxLineHeight = (int)(tmpGlyph->height*this->renderSize); … … 563 564 x = 0; 564 565 y = y + maxLineHeight; 565 566 //maxLineHeight = 0; 566 567 } 567 568 if (y + maxLineHeight + 1 > size) … … 592 593 if(style==TTF_STYLE_NORMAL) 593 594 PRINTF(0)(" normal"); 594 else { 595 else 596 { 595 597 if(style&TTF_STYLE_BOLD) 596 598 PRINTF(0)(" bold"); -
trunk/src/lib/graphics/text_engine/font.h
r6609 r7221 55 55 { 56 56 public: 57 Font(const char*fontFile,57 Font(const std::string& fontFile, 58 58 unsigned int renderSize); 59 Font(const char*imageFile);59 Font(const std::string& imageFile); 60 60 Font(char** xpmArray); 61 61 virtual ~Font(); … … 64 64 65 65 // font 66 bool loadFontFromTTF(const char*fontFile);66 bool loadFontFromTTF(const std::string& fontFile); 67 67 bool loadFontFromSDL_Surface(SDL_Surface* surface); 68 68 69 void setStyle(const char*renderStyle);69 void setStyle(const std::string& renderStyle); 70 70 71 71 /** @returns a Pointer to the Array of Glyphs */ … … 78 78 inline static Font* getDefaultFont() { if (Font::defaultFont == NULL) initDefaultFont(); return Font::defaultFont; }; 79 79 80 void createAsciiImage(const char*fileName, unsigned int size) const;80 void createAsciiImage(const std::string& fileName, unsigned int size) const; 81 81 static void initDefaultFont(); 82 82 static void removeDefaultFont(); -
trunk/src/lib/graphics/text_engine/text.cc
r7193 r7221 32 32 * @param type The renderType to display this font in 33 33 */ 34 Text::Text(const char*fontFile, unsigned int textSize)34 Text::Text(const std::string& fontFile, unsigned int textSize) 35 35 { 36 36 this->init(); 37 37 38 if ( fontFile != NULL)38 if (!fontFile.empty()) 39 39 this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE); 40 40 this->setSizeY2D(textSize); … … 50 50 if (this->font != NULL && this->font != Font::getDefaultFont()) 51 51 ResourceManager::getInstance()->unload(this->font); 52 53 if (this->text)54 delete[] this->text;55 52 } 56 53 … … 64 61 // initialize this Text 65 62 this->font = NULL; 66 this->text = NULL; 67 this->externText = NULL; 63 this->text = ""; 68 64 this->setAlignment(TEXT_DEFAULT_ALIGNMENT); 69 65 this->blending = TEXT_DEFAULT_BLENDING; 70 66 this->color = TEXT_DEFAULT_COLOR; 71 67 this->setSize(TEXT_DEFAULT_SIZE); 72 this->setText( NULL);68 this->setText(""); 73 69 } 74 70 … … 78 74 * @param fontSize the Size of the Font 79 75 */ 80 void Text::setFont(const char*fontFile, unsigned int fontSize)76 void Text::setFont(const std::string& fontFile, unsigned int fontSize) 81 77 { 82 78 Font* tmpFont; … … 90 86 91 87 // load a new Font 92 if ( fontFile != NULL)88 if (!fontFile.empty()) 93 89 { 94 90 tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize); … … 96 92 this->font = tmpFont; 97 93 else 98 PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile );94 PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str()); 99 95 } 100 96 } … … 104 100 * @param text the new text to set 105 101 */ 106 void Text::setText(const char* text, bool isExtern) 107 { 108 if (isExtern) 109 { 110 this->externText = text; 111 112 if (unlikely(this->text != NULL)) 113 { 114 delete[] this->text; 115 this->text = NULL; 116 } 117 } 118 else 119 { 120 this->externText = NULL; 121 if (this->text) 122 delete[] this->text; 123 if (text != NULL) 124 { 125 this->text = new char[strlen(text)+1]; 126 strcpy(this->text, text); 127 } 128 else 129 this->text = NULL; 130 } 102 void Text::setText(const std::string& text) 103 { 104 this->text = text; 131 105 132 106 // setting up the Text-Width if DYNAMIC … … 138 112 139 113 float width = 0; 140 const char* tmpText = this->externText; 141 if (this->externText == NULL) 142 tmpText = this->text; 143 if (tmpText != NULL) 114 if (!this->text.empty()) 144 115 { 145 while (*tmpText != '\0')116 for (unsigned int i = 0; i < this->text.size(); i++) 146 117 { 147 if(glyphArray[ *tmpText] != NULL)118 if(glyphArray[this->text[i]] != NULL) 148 119 { 149 width += glyphArray[ *tmpText]->advance;120 width += glyphArray[this->text[i]]->advance; 150 121 } 151 tmpText++;152 122 } 153 123 this->setSizeX2D(width *this->getSizeY2D()); … … 190 160 glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getTexture()); 191 161 } 192 const char* tmpText = this->externText; 193 if (this->externText == NULL) 194 tmpText = this->text; 195 if (likely(tmpText != NULL)) 162 if (likely(!this->text.empty())) 196 163 { 197 164 glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); … … 201 168 202 169 glBegin(GL_QUADS); 203 while (likely(*tmpText != '\0'))170 for (unsigned int i = 0; i < this->text.size(); i++) 204 171 { 205 if(likely((tmpGlyph = glyphArray[ *tmpText]) != NULL))172 if(likely((tmpGlyph = glyphArray[this->text[i]]) != NULL)) 206 173 { 207 174 glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); … … 219 186 posX += tmpGlyph->advance * this->getSizeY2D(); 220 187 } 221 ++tmpText;222 188 } 223 189 glEnd(); … … 232 198 void Text::debug() const 233 199 { 234 if (this->externText == NULL) 235 PRINT(0)("=== TEXT: %s ===\n", this->text); 236 else 237 PRINT(0)("=== TEXT: %s ===\n", this->externText); 200 PRINT(0)("=== TEXT: %s ===\n", this->text.c_str()); 238 201 239 202 if (this->getBindNode()) -
trunk/src/lib/graphics/text_engine/text.h
r6981 r7221 39 39 { 40 40 public: 41 Text(const char* fontFile = NULL, unsigned int fontSize = TEXT_DEFAULT_SIZE);41 Text(const std::string& fontFile = "", unsigned int fontSize = TEXT_DEFAULT_SIZE); 42 42 virtual ~Text(); 43 43 void init(); 44 44 45 void setFont(const char*fontFile, unsigned int renderSize);45 void setFont(const std::string& fontFile, unsigned int renderSize); 46 46 47 void setText(const char* text, bool isExtern = false);47 void setText(const std::string& text); 48 48 49 49 /** @returns the String this Text displays */ 50 inline const char* getText() const { return (externText == NULL)?this->text:this->externText; };50 inline const std::string& getText() const { return this->text; }; 51 51 /** @param blending the blending intensity to set (between 0.0 and 1.0) */ 52 52 inline void setBlending(float blending) { this->blending = blending; }; … … 69 69 Font* font; //!< Font of this text 70 70 71 char* text; //!< The text to display 72 const char* externText; //!< the text to Display from an external Source. 71 std::string text; //!< The text to display 73 72 Vector color; //!< The color of the font. 74 73 float blending; //!< The blending intensity.
Note: See TracChangeset
for help on using the changeset viewer.