[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5343] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[5357] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
[1853] | 17 | |
---|
[5343] | 18 | #include "text.h" |
---|
| 19 | #include "font.h" |
---|
[1853] | 20 | |
---|
[5343] | 21 | #include "graphics_engine.h" |
---|
| 22 | #include "resource_manager.h" |
---|
| 23 | #include "class_list.h" |
---|
| 24 | #include "debug.h" |
---|
| 25 | #include "p_node.h" |
---|
| 26 | |
---|
[1856] | 27 | using namespace std; |
---|
[1853] | 28 | |
---|
[5343] | 29 | /** |
---|
| 30 | * creates a new Text Element |
---|
| 31 | * @param fontFile the Font to render this text in |
---|
| 32 | * @param type The renderType to display this font in |
---|
| 33 | */ |
---|
[5368] | 34 | Text::Text(const char* fontFile, unsigned int textSize, TEXT_RENDER_TYPE type) |
---|
[5343] | 35 | { |
---|
| 36 | this->init(); |
---|
[1856] | 37 | |
---|
[5343] | 38 | if (fontFile != NULL) |
---|
[5369] | 39 | this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE); |
---|
[5343] | 40 | this->setType(type); |
---|
[5418] | 41 | this->setSizeY2D(this->size = textSize); |
---|
[5343] | 42 | } |
---|
| 43 | |
---|
[3245] | 44 | /** |
---|
[5343] | 45 | * deletes a Text out of memory |
---|
| 46 | * |
---|
| 47 | * This also ereases the text from the textList of the TextEngine |
---|
| 48 | */ |
---|
| 49 | Text::~Text() |
---|
| 50 | { |
---|
| 51 | if (this->font != NULL) |
---|
| 52 | ResourceManager::getInstance()->unload(this->font); |
---|
| 53 | |
---|
| 54 | if (this->text) |
---|
| 55 | delete[] this->text; |
---|
[3365] | 56 | } |
---|
[1853] | 57 | |
---|
[5362] | 58 | /** |
---|
| 59 | * initializes Text |
---|
| 60 | */ |
---|
[5343] | 61 | void Text::init() |
---|
| 62 | { |
---|
| 63 | this->setClassID(CL_TEXT, "Text"); |
---|
[1853] | 64 | |
---|
[5343] | 65 | // initialize this Text |
---|
| 66 | this->font = NULL; |
---|
| 67 | this->text = NULL; |
---|
| 68 | this->externText = NULL; |
---|
| 69 | this->setAlignment(TEXT_DEFAULT_ALIGNMENT); |
---|
| 70 | this->texture = 0; |
---|
| 71 | this->blending = TEXT_DEFAULT_BLENDING; |
---|
| 72 | this->color = TEXT_DEFAULT_COLOR; |
---|
[5367] | 73 | this->size = TEXT_DEFAULT_SIZE; |
---|
[5343] | 74 | this->setType(TEXT_RENDER_DYNAMIC); |
---|
| 75 | |
---|
| 76 | this->setText(NULL); |
---|
| 77 | } |
---|
| 78 | |
---|
[3245] | 79 | /** |
---|
[5343] | 80 | * sets the Font of this Text to font from fontFile |
---|
| 81 | * @param fontFile the File to load the Font from. |
---|
| 82 | * @param fontSize the Size of the Font |
---|
| 83 | */ |
---|
| 84 | void Text::setFont(const char* fontFile, unsigned int fontSize) |
---|
| 85 | { |
---|
| 86 | Font* tmpFont; |
---|
| 87 | Text* newText; |
---|
| 88 | Vector tmpVec; |
---|
| 89 | |
---|
[5345] | 90 | // unloading the Font if we alrady have one loaded. |
---|
| 91 | if (this->font != NULL) |
---|
| 92 | ResourceManager::getInstance()->unload(this->font); |
---|
| 93 | this->font = NULL; |
---|
[5344] | 94 | |
---|
[5345] | 95 | // load a new Font |
---|
| 96 | if (fontFile != NULL) |
---|
[5344] | 97 | { |
---|
[5345] | 98 | tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, &fontSize); |
---|
| 99 | if (tmpFont != NULL) |
---|
| 100 | this->font = tmpFont; |
---|
| 101 | else |
---|
| 102 | PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile); |
---|
[5343] | 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * sets the Type of this Text |
---|
| 108 | * @param type the type to set. |
---|
| 109 | */ |
---|
| 110 | void Text::setType(TEXT_RENDER_TYPE type) |
---|
| 111 | { |
---|
[5368] | 112 | if (this->font != NULL && this->font->fontTTF) |
---|
[5343] | 113 | this->type = type; |
---|
| 114 | else |
---|
| 115 | this->type = TEXT_RENDER_DYNAMIC; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
[5362] | 119 | * Sets a new Text to the font |
---|
[5343] | 120 | * @param text the new text to set |
---|
| 121 | */ |
---|
| 122 | void Text::setText(const char* text, bool isExtern) |
---|
| 123 | { |
---|
| 124 | if (isExtern) |
---|
| 125 | { |
---|
| 126 | this->externText = text; |
---|
| 127 | |
---|
| 128 | if (unlikely(this->text != NULL)) |
---|
| 129 | { |
---|
| 130 | delete[] this->text; |
---|
| 131 | this->text = NULL; |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | else |
---|
| 135 | { |
---|
| 136 | this->externText = NULL; |
---|
| 137 | if (this->text) |
---|
| 138 | delete[] this->text; |
---|
| 139 | if (text != NULL) |
---|
| 140 | { |
---|
| 141 | this->text = new char[strlen(text)+1]; |
---|
| 142 | strcpy(this->text, text); |
---|
| 143 | } |
---|
| 144 | else |
---|
| 145 | this->text = NULL; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | // setting up the Text-Width if DYNAMIC |
---|
[5418] | 149 | // if (this->type & TEXT_RENDER_DYNAMIC && this->getAlignment() != TEXT_ALIGN_LEFT && this->font != NULL) |
---|
| 150 | const Font* calcSizeFont = this->font; |
---|
| 151 | if (calcSizeFont != NULL || (calcSizeFont = Font::getDefaultFont()) != NULL) |
---|
[5343] | 152 | { |
---|
[5418] | 153 | Glyph** glyphArray = calcSizeFont->getGlyphArray(); |
---|
[5343] | 154 | |
---|
[5368] | 155 | float width = 0; |
---|
[5343] | 156 | const char* tmpText = this->externText; |
---|
| 157 | if (this->externText == NULL) |
---|
| 158 | tmpText = this->text; |
---|
| 159 | if (tmpText != NULL) |
---|
| 160 | { |
---|
| 161 | while (*tmpText != '\0') |
---|
| 162 | { |
---|
[5418] | 163 | if(glyphArray[*tmpText] != NULL) |
---|
[5343] | 164 | { |
---|
[5418] | 165 | width += glyphArray[*tmpText]->advance; |
---|
[5343] | 166 | } |
---|
| 167 | tmpText++; |
---|
| 168 | } |
---|
[5418] | 169 | this->setSizeX2D(this->width = width *this->getSizeY2D()); |
---|
[5343] | 170 | } |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
[5345] | 174 | |
---|
[5343] | 175 | /** |
---|
[5362] | 176 | * creates a texture out of the given parameters !! TEXT_STATIC !! - mode |
---|
[5345] | 177 | * |
---|
| 178 | * this has to be called every time by the user, to if changes were made. |
---|
| 179 | * this is only for TEXT_STATIC-mode |
---|
[5343] | 180 | */ |
---|
| 181 | void Text::createTexture() |
---|
| 182 | { |
---|
[5345] | 183 | SDL_Surface* tmpSurf = NULL; |
---|
[5343] | 184 | if (this->texture) |
---|
| 185 | glDeleteTextures(1, &this->texture); |
---|
| 186 | if (likely(this->font != NULL)) |
---|
| 187 | { |
---|
| 188 | SDL_Color theColor = { (int)(this->color.x*255), (int)(this->color.y*255), (int)(this->color.z*255) }; |
---|
[5368] | 189 | tmpSurf = TTF_RenderText_Blended(this->font->fontTTF, |
---|
[5343] | 190 | this->text, |
---|
| 191 | theColor); |
---|
| 192 | } |
---|
[5345] | 193 | if (tmpSurf != NULL) |
---|
| 194 | { |
---|
[5343] | 195 | this->texture = loadTexture(tmpSurf, &this->texCoord); |
---|
| 196 | |
---|
[5345] | 197 | this->width = tmpSurf->w; |
---|
| 198 | this->height = tmpSurf->h; |
---|
| 199 | SDL_FreeSurface(tmpSurf); |
---|
| 200 | } |
---|
[5343] | 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * draws the Text |
---|
| 205 | */ |
---|
| 206 | void Text::draw() const |
---|
| 207 | { |
---|
| 208 | glPushMatrix(); |
---|
| 209 | // transform for alignment. |
---|
| 210 | if (this->getAlignment() == TEXT_ALIGN_RIGHT) |
---|
| 211 | glTranslatef(-this->width, 0, 0); |
---|
| 212 | else if (this->getAlignment() == TEXT_ALIGN_CENTER || this->getAlignment() == TEXT_ALIGN_SCREEN_CENTER) |
---|
| 213 | glTranslatef(-this->width/2, 0, 0); |
---|
| 214 | |
---|
| 215 | // drawing this Text. |
---|
| 216 | // setting the Blending effects |
---|
| 217 | glColor4f(this->color.x, this->color.y, this->color.z, this->blending); |
---|
| 218 | glEnable(GL_BLEND); |
---|
| 219 | glEnable(GL_TEXTURE_2D); |
---|
| 220 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
| 221 | |
---|
| 222 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE ); |
---|
| 223 | |
---|
| 224 | if(likely(type & TEXT_RENDER_DYNAMIC )) |
---|
| 225 | { |
---|
| 226 | Glyph** glyphArray; |
---|
| 227 | if (likely (this->font != NULL)) |
---|
| 228 | { |
---|
| 229 | glyphArray = this->font->getGlyphArray(); |
---|
| 230 | glBindTexture(GL_TEXTURE_2D, font->getFastTextureID()); |
---|
| 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
| 234 | if (unlikely(Font::getDefaultFont() == NULL)) |
---|
| 235 | Font::initDefaultFont(); |
---|
| 236 | glyphArray = Font::getDefaultFont()->getGlyphArray(); |
---|
| 237 | glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getFastTextureID()); |
---|
| 238 | } |
---|
| 239 | const char* tmpText = this->externText; |
---|
| 240 | if (this->externText == NULL) |
---|
| 241 | tmpText = this->text; |
---|
| 242 | if (likely(tmpText != NULL)) |
---|
| 243 | { |
---|
[5376] | 244 | glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0); |
---|
[5420] | 245 | glRotatef(this->getAbsDir2D(), 0, 0, 1); |
---|
[5367] | 246 | Glyph* tmpGlyph; |
---|
[5370] | 247 | float posX = 0.0f; |
---|
[5343] | 248 | while (likely(*tmpText != '\0')) |
---|
| 249 | { |
---|
[5367] | 250 | if(likely((tmpGlyph = glyphArray[*tmpText]) != NULL)) |
---|
[5343] | 251 | { |
---|
[5367] | 252 | glBegin(GL_QUADS); |
---|
[5419] | 253 | |
---|
[5367] | 254 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]); |
---|
[5420] | 255 | glVertex2d(posX, 0); |
---|
[5419] | 256 | |
---|
[5367] | 257 | glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]); |
---|
[5420] | 258 | glVertex2d(posX, this->size); |
---|
[5419] | 259 | |
---|
[5367] | 260 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]); |
---|
[5420] | 261 | glVertex2d(posX+tmpGlyph->width*this->size, this->size); |
---|
[5419] | 262 | |
---|
[5367] | 263 | glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]); |
---|
[5420] | 264 | glVertex2d(posX+tmpGlyph->width*this->size, 0); |
---|
[5419] | 265 | |
---|
[5367] | 266 | glEnd(); |
---|
| 267 | glEndList(); |
---|
[5419] | 268 | posX += tmpGlyph->advance * this->size; |
---|
[5343] | 269 | } |
---|
[5419] | 270 | ++tmpText; |
---|
[5343] | 271 | } |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | else //(if type & TEXT_RENDER_STATIC) |
---|
| 275 | { |
---|
| 276 | glBindTexture(GL_TEXTURE_2D, this->texture); |
---|
| 277 | glBegin(GL_QUADS); |
---|
| 278 | |
---|
| 279 | glTexCoord2f(this->texCoord.minU, this->texCoord.minV); |
---|
| 280 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
| 281 | |
---|
| 282 | glTexCoord2f(this->texCoord.maxU, this->texCoord.minV); |
---|
| 283 | glVertex2f(this->getAbsCoor2D().x + this->width, this->getAbsCoor2D().y ); |
---|
| 284 | |
---|
| 285 | glTexCoord2f(this->texCoord.maxU, this->texCoord.maxV); |
---|
| 286 | glVertex2f(this->getAbsCoor2D().x + this->width, getAbsCoor2D().y + this->height); |
---|
| 287 | |
---|
| 288 | glTexCoord2f(this->texCoord.minU, this->texCoord.maxV); |
---|
| 289 | glVertex2f(getAbsCoor2D().x, getAbsCoor2D().y + this->height); |
---|
| 290 | |
---|
| 291 | glEnd(); |
---|
| 292 | |
---|
| 293 | } |
---|
| 294 | glPopMatrix(); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | * prints out some nice debug information about this text |
---|
| 299 | */ |
---|
| 300 | void Text::debug() const |
---|
| 301 | { |
---|
| 302 | if (this->externText == NULL) |
---|
| 303 | PRINT(0)("=== TEXT: %s ===\n", this->text); |
---|
| 304 | else |
---|
| 305 | PRINT(0)("=== TEXT: %s ===\n", this->externText); |
---|
| 306 | |
---|
| 307 | if (this->getBindNode()) |
---|
| 308 | PRINT(0)("is bind to %s; ref=%p\n", this->getBindNode()->getName(), this->getBindNode()); |
---|
| 309 | PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | //////////// |
---|
| 314 | /// UTIL /// |
---|
| 315 | //////////// |
---|
| 316 | /** |
---|
[5345] | 317 | * Loads a Font from an SDL_surface into a texture. |
---|
[5343] | 318 | * @param surface The surface to make the texture of |
---|
| 319 | * @param texCoord The texture coordinates of the 4 corners of the texture |
---|
| 320 | * @returns the ID of the texture |
---|
| 321 | */ |
---|
| 322 | GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord) |
---|
| 323 | { |
---|
| 324 | GLuint texture; |
---|
| 325 | int w, h; |
---|
| 326 | SDL_Surface *image; |
---|
| 327 | SDL_Rect area; |
---|
| 328 | Uint32 saved_flags; |
---|
| 329 | Uint8 saved_alpha; |
---|
| 330 | |
---|
| 331 | /* Use the surface width and height expanded to powers of 2 */ |
---|
| 332 | w = powerOfTwo(surface->w); |
---|
| 333 | h = powerOfTwo(surface->h); |
---|
| 334 | if (texCoord != NULL) |
---|
| 335 | { |
---|
| 336 | texCoord->minU = 0.0f; |
---|
| 337 | texCoord->minV = 0.0f; |
---|
| 338 | texCoord->maxU = (GLfloat)surface->w / w; |
---|
| 339 | texCoord->maxV = (GLfloat)surface->h / h; |
---|
| 340 | } |
---|
| 341 | image = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
| 342 | w, h, |
---|
| 343 | 32, |
---|
| 344 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
| 345 | 0x000000FF, |
---|
| 346 | 0x0000FF00, |
---|
| 347 | 0x00FF0000, |
---|
| 348 | 0xFF000000 |
---|
| 349 | #else |
---|
| 350 | 0xFF000000, |
---|
| 351 | 0x00FF0000, |
---|
| 352 | 0x0000FF00, |
---|
| 353 | 0x000000FF |
---|
| 354 | #endif |
---|
| 355 | ); |
---|
| 356 | if ( image == NULL ) { |
---|
| 357 | return 0; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | /* Save the alpha blending attributes */ |
---|
| 361 | saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); |
---|
| 362 | saved_alpha = surface->format->alpha; |
---|
| 363 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 364 | SDL_SetAlpha(surface, 0, 0); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | /* Copy the surface into the GL texture image */ |
---|
| 368 | area.x = 0; |
---|
| 369 | area.y = 0; |
---|
| 370 | area.w = surface->w; |
---|
| 371 | area.h = surface->h; |
---|
| 372 | SDL_BlitSurface(surface, &area, image, &area); |
---|
| 373 | |
---|
| 374 | /* Restore the alpha blending attributes */ |
---|
| 375 | if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { |
---|
| 376 | SDL_SetAlpha(surface, saved_flags, saved_alpha); |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /* Create an OpenGL texture for the image */ |
---|
| 380 | glGenTextures(1, &texture); |
---|
| 381 | glBindTexture(GL_TEXTURE_2D, texture); |
---|
[5368] | 382 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
---|
[5343] | 383 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
| 384 | glTexImage2D(GL_TEXTURE_2D, |
---|
| 385 | 0, |
---|
| 386 | GL_RGBA, |
---|
| 387 | w, h, |
---|
| 388 | 0, |
---|
| 389 | GL_RGBA, |
---|
| 390 | GL_UNSIGNED_BYTE, |
---|
| 391 | image->pixels); |
---|
| 392 | SDL_FreeSurface(image); /* No longer needed the data */ |
---|
| 393 | |
---|
| 394 | return texture; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | /** |
---|
| 398 | * Quick utility function for texture creation |
---|
| 399 | * @param input an integer |
---|
| 400 | * @returns the next bigger 2^n-integer than input |
---|
| 401 | */ |
---|
| 402 | int Text::powerOfTwo(int input) |
---|
| 403 | { |
---|
| 404 | int value = 1; |
---|
| 405 | |
---|
| 406 | while ( value < input ) |
---|
| 407 | value <<= 1; |
---|
| 408 | return value; |
---|
| 409 | } |
---|