Changeset 8761 in orxonox.OLD for trunk/src/lib/graphics/text_engine/font.cc
- Timestamp:
- Jun 24, 2006, 2:16:12 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/text_engine/font.cc
r8751 r8761 17 17 18 18 #include "font.h" 19 #include "text.h"20 19 21 20 #ifdef HAVE_SDL_IMAGE_H … … 24 23 #include <SDL/SDL_image.h> 25 24 #endif 25 26 26 #include "default_font.xpm" 27 27 28 28 #include "debug.h" 29 #include "stdlibincl.h"30 29 #include "compiler.h" 31 using namespace std; 30 31 32 Font::Font() 33 : data(Font::defaultFontData) 34 { 35 this->init(); 36 37 } 32 38 33 39 /** … … 37 43 */ 38 44 Font::Font(const std::string& fontFile, unsigned int renderSize) 39 : data( new FontData())45 : data(Font::defaultFontData) 40 46 { 41 47 this->init(); 42 48 43 44 this->data->renderSize = renderSize;45 this->setStyle("c");46 47 49 if (!fontFile.empty()) 48 this->loadFontFromTTF(fontFile); 49 } 50 this->loadFontFromTTF(fontFile, renderSize); 51 } 52 50 53 51 54 /** … … 54 57 */ 55 58 Font::Font(const std::string& imageFile) 56 : data( new FontData())59 : data(Font::defaultFontData) 57 60 { 58 61 this->init(); 62 59 63 this->setName(imageFile); 60 64 // this->setSize(fontSize); … … 78 82 */ 79 83 Font::Font(char** xpmArray) 80 : data( new FontData())84 : data(Font::defaultFontData) 81 85 { 82 86 this->init(); … … 92 96 } 93 97 else 94 PRINTF(1)(" loading from surfacefailed: %s\n", IMG_GetError());98 PRINTF(1)("Loading from XPM-array failed: %s\n", IMG_GetError()); 95 99 } 96 100 … … 105 109 { } 106 110 111 Font& Font::operator=(const Font& font) 112 { 113 Material::operator=(font); 114 this->data = font.data; 115 116 return *this; 117 }; 118 119 107 120 /** 108 121 * @brief initializes a Font (with default values) … … 110 123 void Font::init() 111 124 { 125 this->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 126 112 127 this->setClassID(CL_FONT, "Font"); 113 } 114 128 if (Font::defaultFontData.get() == NULL) 129 { 130 Font::initDefaultFont(); 131 this->data = Font::defaultFontData; 132 } 133 } 134 135 FontDataPointer Font::defaultFontData(NULL); 136 137 /** 138 * @brief initializes the default font 139 */ 140 void Font::initDefaultFont() 141 { 142 // temporarily create a Font. 143 Font::defaultFontData = FontDataPointer(new FontData); 144 // apply the Data. 145 printf("before: %p\n", defaultFontData.get()); 146 Font::defaultFontData = Font(font_xpm).data; 147 printf("after: %p\n", defaultFontData.get()); 148 } 115 149 116 150 /** … … 119 153 * @returns true if loaded, false if something went wrong, or if a font was loaded before. 120 154 */ 121 bool Font::loadFontFromTTF(const std::string& fontFile) 122 { 123 // checking for existent Font. 124 if (this->data->fontTTF != NULL) 125 { 126 TTF_CloseFont(this->data->fontTTF); 127 this->data->fontTTF = NULL; 128 } 129 155 bool Font::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize) 156 { 157 this->data = FontDataPointer (new FontData()); 130 158 131 159 this->setName(fontFile); 132 this->data->fontTTF = TTF_OpenFont(this->getName(), this->data->renderSize); 160 this->data->fontTTF = TTF_OpenFont(fontFile.c_str(), renderSize); 161 this->data->renderSize = renderSize; 133 162 134 163 if(this->data->fontTTF != NULL) 135 164 { 136 this->createFastTexture(); 137 return (this->getTexture() != 0); 165 this->setStyle("c"); 166 if (this->createFastTexture()) 167 return true; 168 else 169 { 170 this->data = Font::defaultFontData; 171 return false; 172 } 138 173 } 139 174 else 140 175 { 141 176 PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError()); 177 this->data = Font::defaultFontData; 142 178 return false; 143 179 } … … 151 187 bool Font::loadFontFromSDL_Surface(SDL_Surface* surface) 152 188 { 153 // loading to a texture.154 189 if(surface == NULL) 155 190 return false; 156 191 157 if (this->data->fontTTF != NULL) 158 { 159 TTF_CloseFont(this->data->fontTTF); 160 this->data->fontTTF = NULL; 161 } 192 this->data = FontDataPointer (new FontData()); 193 // loading to a texture. 194 162 195 bool hasAlpha; 163 SDL_Surface* newSurf = this->prepareSurface(surface, hasAlpha);196 SDL_Surface* newSurf = Texture::prepareSurface(surface, hasAlpha); 164 197 if (newSurf != NULL) 165 198 { 166 this-> setSurface(newSurf);167 this-> setAlpha(hasAlpha);199 this->data->texData->setSurface(newSurf); 200 this->data->texData->setAlpha(hasAlpha); 168 201 this->setTexture(Texture::loadTexToGL(newSurf)); 202 } 203 else 204 { 205 this->data = Font::defaultFontData; 206 return false; 169 207 } 170 208 … … 223 261 // PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n"); 224 262 } 225 226 Font* Font::defaultFont = NULL;227 263 228 264 /** … … 283 319 SDL_SaveBMP(tmpSurf, fileName.c_str()); 284 320 SDL_FreeSurface(tmpSurf); 285 }286 287 /**288 * @brief initializes the default font289 */290 void Font::initDefaultFont()291 {292 if (Font::defaultFont == NULL)293 Font::defaultFont = new Font(font_xpm);294 }295 296 /**297 * @brief deletes the default font298 */299 void Font::removeDefaultFont()300 {301 if (Font::defaultFont != NULL)302 delete Font::defaultFont;303 Font::defaultFont = NULL;304 321 } 305 322 … … 473 490 // sprintf( outName, "%s-glyphs.bmp", this->getName()); 474 491 // SDL_SaveBMP(tmpSurf, outName); 475 this-> setAlpha(true);476 if (this-> setSurface(tmpSurf))477 (this->setTexture(Texture::loadTexToGL(tmpSurf)));492 this->data->texData->setAlpha(true); 493 if (this->data->texData->setSurface(tmpSurf)) 494 this->setTexture(Texture::loadTexToGL(tmpSurf)); 478 495 return true; 479 496 } 497 498 /** 499 * @brief the Internal implementation of setting up the Texture. 500 * @param texture the Texture to load. 501 * @returns true on success, false otherwise. 502 */ 503 bool Font::setTexture(GLuint texture) 504 { 505 bool retVal = this->data->texData->setTexture(texture); 506 this->setDiffuseMap(data->texData, 0); 507 printf("this->texture %d\n", texture); 508 //printf(this->getT) 509 this->debug(); 510 return retVal; 511 }; 480 512 481 513 /** … … 569 601 * @brief a simple function to get some interesting information about this class 570 602 */ 571 void Font::debug() 572 { 603 void Font::debug() const 604 { 605 Material::debug(); 606 607 PRINT(0)("TEST %p and %p\n", this->data.get(), this->data->texData.get()); 573 608 // print the loaded font's style 574 609 int style = TTF_STYLE_NORMAL;
Note: See TracChangeset
for help on using the changeset viewer.