Changeset 7450 in orxonox.OLD for trunk/src/lib/graphics/text_engine/text.cc
- Timestamp:
- Apr 29, 2006, 9:38:44 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/graphics/text_engine/text.cc
r7448 r7450 85 85 if (oldFont != NULL && oldFont != Font::getDefaultFont()) 86 86 ResourceManager::getInstance()->unload(oldFont); 87 88 this->setupTextWidth(); 87 89 } 88 90 … … 95 97 this->text = text; 96 98 97 // setting up the Text-Width if DYNAMIC 98 // if (this->type & TEXT_RENDER_DYNAMIC && this->getAlignment() != TEXT_ALIGN_LEFT && this->font != NULL) 99 const Font* calcSizeFont = this->font; 100 if (calcSizeFont != NULL || (calcSizeFont = Font::getDefaultFont()) != NULL) 101 { 102 Glyph** glyphArray = calcSizeFont->getGlyphArray(); 103 104 float width = 0; 105 if (!this->text.empty()) 106 { 107 for (unsigned int i = 0; i < this->text.size(); i++) 108 { 109 if(glyphArray[this->text[i]] != NULL) 110 { 111 width += glyphArray[this->text[i]]->advance; 112 } 113 } 114 this->setSizeX2D(width *this->getSizeY2D()); 115 } 116 } 99 this->setupTextWidth(); 117 100 } 118 101 … … 139 122 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); 140 123 141 Glyph** glyphArray = this->font->getGlyphArray();142 124 glBindTexture(GL_TEXTURE_2D, font->getTexture()); 143 125 glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); … … 149 131 for (unsigned int i = 0; i < this->text.size(); i++) 150 132 { 151 if(likely((tmpGlyph = glyphArray[this->text[i]]) != NULL))133 if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL)) 152 134 { 153 135 glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); 154 glVertex2d(posX+tmpGlyph->maxX*this->getSize Y2D(), 0);136 glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0); 155 137 156 138 glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); 157 glVertex2d(posX+tmpGlyph->maxX*this->getSize Y2D(), this->getSizeY2D());139 glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize()); 158 140 159 141 glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); 160 glVertex2d(posX+tmpGlyph->minX*this->getSize Y2D(), this->getSizeY2D());142 glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize()); 161 143 162 144 glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); 163 glVertex2d(posX+tmpGlyph->minX*this->getSize Y2D(), 0);145 glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0); 164 146 165 posX += tmpGlyph->advance * this->getSize Y2D();147 posX += tmpGlyph->advance * this->getSize(); 166 148 } 167 149 } … … 170 152 } 171 153 154 155 /** 156 * @brief setting up the Text-Width. 157 */ 158 void Text::setupTextWidth() 159 { 160 float width = 0; 161 for (unsigned int i = 0; i < this->text.size(); i++) 162 if(this->font->getGlyphArray()[this->text[i]] != NULL) 163 width += this->font->getGlyphArray()[this->text[i]]->advance; 164 this->setSizeX2D(width *this->getSize()); 165 } 166 167 172 168 /** 173 169 * @brief prints out some nice debug information about this text … … 175 171 void Text::debug() const 176 172 { 177 PRINT(0)("=== TEXT: %s ===\n", this->text.c_str());173 PRINT(0)("=== TEXT: %s (with Font:'%s') displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str()); 178 174 PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z); 179 175 } 180 176 181 182 ////////////183 /// UTIL ///184 ////////////185 /**186 * @brief Loads a Font from an SDL_surface into a texture.187 * @param surface The surface to make the texture of188 * @param texCoord The texture coordinates of the 4 corners of the texture189 * @returns the ID of the texture190 */191 GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord)192 {193 GLuint texture;194 int w, h;195 SDL_Surface *image;196 SDL_Rect area;197 Uint32 saved_flags;198 Uint8 saved_alpha;199 200 /* Use the surface width and height expanded to powers of 2 */201 w = powerOfTwo(surface->w);202 h = powerOfTwo(surface->h);203 if (texCoord != NULL)204 {205 texCoord->minU = 0.0f;206 texCoord->minV = 0.0f;207 texCoord->maxU = (GLfloat)surface->w / w;208 texCoord->maxV = (GLfloat)surface->h / h;209 }210 image = SDL_CreateRGBSurface(SDL_SWSURFACE,211 w, h,212 32,213 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */214 0x000000FF,215 0x0000FF00,216 0x00FF0000,217 0xFF000000218 #else219 0xFF000000,220 0x00FF0000,221 0x0000FF00,222 0x000000FF223 #endif224 );225 if ( image == NULL )226 {227 return 0;228 }229 230 /* Save the alpha blending attributes */231 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);232 saved_alpha = surface->format->alpha;233 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )234 {235 SDL_SetAlpha(surface, 0, 0);236 }237 238 /* Copy the surface into the GL texture image */239 area.x = 0;240 area.y = 0;241 area.w = surface->w;242 area.h = surface->h;243 SDL_BlitSurface(surface, &area, image, &area);244 245 /* Restore the alpha blending attributes */246 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )247 {248 SDL_SetAlpha(surface, saved_flags, saved_alpha);249 }250 251 /* Create an OpenGL texture for the image */252 glGenTextures(1, &texture);253 glBindTexture(GL_TEXTURE_2D, texture);254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);255 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);256 glTexImage2D(GL_TEXTURE_2D,257 0,258 GL_RGBA,259 w, h,260 0,261 GL_RGBA,262 GL_UNSIGNED_BYTE,263 image->pixels);264 SDL_FreeSurface(image); /* No longer needed the data */265 266 return texture;267 }268 269 /**270 * @brief Quick utility function for texture creation271 * @param input an integer272 * @returns the next bigger 2^n-integer than input273 */274 int Text::powerOfTwo(int input)275 {276 int value = 1;277 278 while ( value < input )279 value <<= 1;280 return value;281 }
Note: See TracChangeset
for help on using the changeset viewer.