[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 "font.h" |
---|
| 19 | #include "text.h" |
---|
[1853] | 20 | |
---|
[5343] | 21 | #ifdef HAVE_SDL_IMAGE_H |
---|
| 22 | #include <SDL_image.h> |
---|
| 23 | #else |
---|
| 24 | #include <SDL/SDL_image.h> |
---|
| 25 | #endif |
---|
[5347] | 26 | #include "default_font.xpm" |
---|
[5343] | 27 | |
---|
| 28 | #include "debug.h" |
---|
[5427] | 29 | #include "stdlibincl.h" |
---|
[5343] | 30 | #include "compiler.h" |
---|
| 31 | |
---|
[1856] | 32 | using namespace std; |
---|
[1853] | 33 | |
---|
[5343] | 34 | /** |
---|
[5347] | 35 | * constructs a Font out of a TTF-FIle |
---|
[5343] | 36 | * @param fontFile the File to load the font from |
---|
| 37 | * @param fontSize the Size of the Font in Pixels |
---|
| 38 | */ |
---|
[5368] | 39 | Font::Font(const char* fontFile, unsigned int renderSize) |
---|
[5343] | 40 | { |
---|
| 41 | this->init(); |
---|
[1856] | 42 | |
---|
[5368] | 43 | this->renderSize = renderSize; |
---|
[5347] | 44 | this->setStyle("c"); |
---|
[5343] | 45 | |
---|
| 46 | if (fontFile != NULL) |
---|
[5368] | 47 | this->loadFontFromTTF(fontFile); |
---|
[5347] | 48 | } |
---|
[5343] | 49 | |
---|
[5347] | 50 | /** |
---|
| 51 | * constructs a Font out of an ImageFile |
---|
| 52 | * @param imageFile the ImageFile to load the Font From. |
---|
| 53 | */ |
---|
| 54 | Font::Font(const char* imageFile) |
---|
| 55 | { |
---|
| 56 | this->init(); |
---|
| 57 | this->setName(imageFile); |
---|
| 58 | // this->setSize(fontSize); |
---|
| 59 | SDL_Surface* image = NULL; |
---|
| 60 | if (imageFile != NULL) |
---|
| 61 | image = IMG_Load(imageFile); |
---|
| 62 | else |
---|
| 63 | return; |
---|
| 64 | if (image != NULL) |
---|
| 65 | { |
---|
| 66 | this->loadFontFromSDL_Surface(image); |
---|
| 67 | SDL_FreeSurface(image); |
---|
| 68 | } |
---|
| 69 | else |
---|
| 70 | PRINTF(1)("loading from surface %s failed: %s\n", imageFile, IMG_GetError()); |
---|
[5343] | 71 | } |
---|
| 72 | |
---|
[3245] | 73 | /** |
---|
[5343] | 74 | * constructs a Font |
---|
[5347] | 75 | * @param xpmArray the xpm-ARRAY to load the font from |
---|
[5343] | 76 | */ |
---|
| 77 | Font::Font(char** xpmArray) |
---|
[3365] | 78 | { |
---|
[5343] | 79 | this->init(); |
---|
[5347] | 80 | this->setName("XPM-array-font"); |
---|
[5343] | 81 | // this->setSize(fontSize); |
---|
| 82 | SDL_Surface* image = NULL; |
---|
| 83 | if (xpmArray != NULL) |
---|
| 84 | image = IMG_ReadXPMFromArray(xpmArray); |
---|
| 85 | if (image != NULL) |
---|
| 86 | { |
---|
| 87 | this->loadFontFromSDL_Surface(image); |
---|
| 88 | SDL_FreeSurface(image); |
---|
| 89 | } |
---|
| 90 | else |
---|
| 91 | PRINTF(1)("loading from surface failed: %s\n", IMG_GetError()); |
---|
[3365] | 92 | } |
---|
[1853] | 93 | |
---|
| 94 | |
---|
[3245] | 95 | /** |
---|
[5343] | 96 | * destructs a font |
---|
| 97 | * this releases the memory a font uses to be opened. |
---|
| 98 | * deletes the glLists, and the TTF-handler, if present. |
---|
| 99 | */ |
---|
| 100 | Font::~Font() |
---|
[3543] | 101 | { |
---|
[5343] | 102 | // deleting all Glyphs |
---|
| 103 | if (this->glyphArray != NULL) |
---|
| 104 | { |
---|
| 105 | for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) |
---|
| 106 | { |
---|
| 107 | if (this->glyphArray[i] != NULL) |
---|
| 108 | { |
---|
| 109 | delete this->glyphArray[i]; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | delete[] this->glyphArray; |
---|
| 113 | } |
---|
| 114 | |
---|
[5347] | 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); |
---|
| 119 | |
---|
[5343] | 120 | // erease this font out of the memory. |
---|
[5368] | 121 | if (likely(this->fontTTF != NULL)) |
---|
| 122 | TTF_CloseFont(this->fontTTF); |
---|
[3543] | 123 | } |
---|
[5343] | 124 | |
---|
| 125 | /** |
---|
| 126 | * initializes a Font (with default values) |
---|
| 127 | */ |
---|
| 128 | void Font::init() |
---|
| 129 | { |
---|
| 130 | this->setClassID(CL_FONT, "Font"); |
---|
| 131 | // setting default values. |
---|
[5368] | 132 | this->fontTTF = NULL; |
---|
[5343] | 133 | this->glyphArray = NULL; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * sets The Font. |
---|
| 139 | * @param fontFile The file containing the font. |
---|
| 140 | * @returns true if loaded, false if something went wrong, or if a font was loaded before. |
---|
| 141 | */ |
---|
[5368] | 142 | bool Font::loadFontFromTTF(const char* fontFile) |
---|
[5343] | 143 | { |
---|
[5347] | 144 | // checking for existent Font. |
---|
[5368] | 145 | if (this->fontTTF != NULL) |
---|
[5343] | 146 | { |
---|
[5368] | 147 | TTF_CloseFont(this->fontTTF); |
---|
| 148 | this->fontTTF = NULL; |
---|
[5347] | 149 | } |
---|
[5343] | 150 | |
---|
[5768] | 151 | |
---|
[5347] | 152 | this->setName(fontFile); |
---|
[5368] | 153 | this->fontTTF = TTF_OpenFont(this->getName(), this->renderSize); |
---|
[5347] | 154 | |
---|
[5368] | 155 | if(this->fontTTF != NULL) |
---|
[5347] | 156 | { |
---|
[5768] | 157 | this->createFastTexture(); |
---|
| 158 | return (this->getTexture() != 0); |
---|
[5343] | 159 | } |
---|
| 160 | else |
---|
| 161 | { |
---|
[5347] | 162 | PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError()); |
---|
[5343] | 163 | return false; |
---|
| 164 | } |
---|
[5347] | 165 | |
---|
[5343] | 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * loads a font From an XPM-array. |
---|
| 170 | * @param xpmArray the array of the XPM to load the font from. |
---|
| 171 | */ |
---|
| 172 | bool Font::loadFontFromSDL_Surface(SDL_Surface* surface) |
---|
| 173 | { |
---|
| 174 | // loading to a texture. |
---|
| 175 | if(surface == NULL) |
---|
| 176 | return false; |
---|
[5347] | 177 | |
---|
[5368] | 178 | if (this->fontTTF != NULL) |
---|
[5347] | 179 | { |
---|
[5368] | 180 | TTF_CloseFont(this->fontTTF); |
---|
| 181 | this->fontTTF = NULL; |
---|
[5347] | 182 | } |
---|
[5768] | 183 | if (this->prepareSurface(surface)) |
---|
| 184 | this->loadTexToGL( ); |
---|
[5347] | 185 | |
---|
[5343] | 186 | // initializing the Glyphs. |
---|
| 187 | if (this->glyphArray == NULL) |
---|
| 188 | { |
---|
| 189 | float cx,cy; |
---|
[5367] | 190 | Glyph* tmpGlyph; |
---|
[5343] | 191 | this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR]; |
---|
| 192 | for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) |
---|
| 193 | { |
---|
[5367] | 194 | tmpGlyph = this->glyphArray[i] = new Glyph; |
---|
[5343] | 195 | cx=(float)(i%16)/16.0f; // X Position Of Current Character |
---|
| 196 | cy=(float)(i/16)/16.0f; // Y Position Of Current Character |
---|
[5367] | 197 | |
---|
| 198 | tmpGlyph->texCoord[0] = cx; |
---|
| 199 | tmpGlyph->texCoord[1] = cx+0.0625f; |
---|
| 200 | tmpGlyph->texCoord[2] = cy+0.001f; |
---|
| 201 | tmpGlyph->texCoord[3] = cy+0.0625f; |
---|
[5368] | 202 | tmpGlyph->width = 1; |
---|
[5418] | 203 | tmpGlyph->advance = 1; |
---|
[5368] | 204 | tmpGlyph->bearingX = 1; |
---|
| 205 | tmpGlyph->bearingY = 1; |
---|
| 206 | tmpGlyph->height = 1; |
---|
[5343] | 207 | } |
---|
| 208 | } |
---|
| 209 | return true; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * sets a specific renderStyle |
---|
[5347] | 215 | * @param renderStyle the Style to render: a string (char-array) containing: |
---|
| 216 | * i: italic, b: bold, u, underline |
---|
[5343] | 217 | */ |
---|
| 218 | void Font::setStyle(const char* renderStyle) |
---|
| 219 | { |
---|
| 220 | this->renderStyle = TTF_STYLE_NORMAL; |
---|
| 221 | |
---|
| 222 | for (int i = 0; i < strlen(renderStyle); i++) |
---|
| 223 | if (strncmp(renderStyle+i, "b", 1) == 0) |
---|
| 224 | this->renderStyle |= TTF_STYLE_BOLD; |
---|
| 225 | else if (strncmp(renderStyle+i, "i", 1) == 0) |
---|
| 226 | this->renderStyle |= TTF_STYLE_ITALIC; |
---|
| 227 | else if (strncmp(renderStyle+i, "u", 1) == 0) |
---|
| 228 | this->renderStyle |= TTF_STYLE_UNDERLINE; |
---|
| 229 | |
---|
[5368] | 230 | if (likely(this->fontTTF != NULL)) |
---|
| 231 | TTF_SetFontStyle(this->fontTTF, this->renderStyle); |
---|
[5347] | 232 | // else |
---|
| 233 | // PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n"); |
---|
[5343] | 234 | } |
---|
| 235 | |
---|
| 236 | Font* Font::defaultFont = NULL; |
---|
| 237 | |
---|
[5768] | 238 | /** |
---|
| 239 | * creates and exports an Image, that has all the characters |
---|
| 240 | * stored in a Array (as an image) |
---|
| 241 | * @param fileName the File to write the image into. |
---|
| 242 | */ |
---|
[5343] | 243 | void Font::createAsciiImage(const char* fileName) |
---|
| 244 | { |
---|
[5368] | 245 | if (this->fontTTF == NULL) |
---|
[5343] | 246 | return; |
---|
| 247 | int height = this->getMaxHeight(); |
---|
| 248 | |
---|
| 249 | // |
---|
| 250 | SDL_Color tmpColor = {0, 0, 0}; |
---|
| 251 | // Surface definition. |
---|
| 252 | SDL_Rect tmpRect; // this represents a Rectangle for blitting. |
---|
| 253 | SDL_Surface* tmpSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
| 254 | height*16, height*16, |
---|
| 255 | 32, |
---|
| 256 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
| 257 | 0x000000FF, |
---|
| 258 | 0x0000FF00, |
---|
| 259 | 0x00FF0000, |
---|
| 260 | 0xFF000000 |
---|
| 261 | #else |
---|
[5367] | 262 | 0xFF000000, |
---|
[5343] | 263 | 0x00FF0000, |
---|
| 264 | 0x0000FF00, |
---|
| 265 | 0x000000FF |
---|
| 266 | #endif |
---|
| 267 | ); |
---|
| 268 | tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h; |
---|
| 269 | SDL_SetClipRect(tmpSurf, &tmpRect); |
---|
| 270 | int maxLineHeight = 0; |
---|
| 271 | |
---|
| 272 | int posX, posY; |
---|
| 273 | // all the interessting Glyphs |
---|
| 274 | for (posY = 0; posY < 16; posY++) |
---|
| 275 | { |
---|
| 276 | for (posX = 0; posX < 16; posX++) |
---|
| 277 | { |
---|
| 278 | SDL_Surface* glyphSurf = NULL; |
---|
[5368] | 279 | if (likely(this->fontTTF != NULL)) |
---|
[5343] | 280 | { |
---|
| 281 | SDL_Color white = {255, 255, 255}; |
---|
[5368] | 282 | glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, posX+16*posY, white); |
---|
[5343] | 283 | } |
---|
| 284 | if( glyphSurf != NULL ) |
---|
| 285 | { |
---|
| 286 | tmpRect.x = height*posX; |
---|
| 287 | tmpRect.y = height*posY; |
---|
| 288 | SDL_SetAlpha(glyphSurf, 0, 0); |
---|
| 289 | |
---|
| 290 | SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect); |
---|
| 291 | SDL_FreeSurface(glyphSurf); |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | SDL_SaveBMP(tmpSurf, fileName); |
---|
| 296 | SDL_FreeSurface(tmpSurf); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * initializes the default font |
---|
| 301 | */ |
---|
| 302 | void Font::initDefaultFont() |
---|
| 303 | { |
---|
| 304 | if (Font::defaultFont == NULL) |
---|
| 305 | Font::defaultFont = new Font(font_xpm); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | /** |
---|
| 309 | * deletes the default font |
---|
| 310 | */ |
---|
| 311 | void Font::removeDefaultFont() |
---|
| 312 | { |
---|
| 313 | if (Font::defaultFont != NULL) |
---|
| 314 | delete Font::defaultFont; |
---|
| 315 | Font::defaultFont = NULL; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * @returns the maximum height of the Font, if the font was initialized, 0 otherwise |
---|
| 321 | */ |
---|
| 322 | int Font::getMaxHeight() |
---|
| 323 | { |
---|
[5368] | 324 | if (likely (this->fontTTF != NULL)) |
---|
| 325 | return TTF_FontHeight(this->fontTTF); |
---|
[5343] | 326 | else |
---|
| 327 | return 0; |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | /** |
---|
| 331 | * @returns the maximum ascent of the Font, if the font was initialized, 0 otherwise |
---|
| 332 | |
---|
| 333 | the ascent is the pixels of the font above the baseline |
---|
| 334 | */ |
---|
| 335 | int Font::getMaxAscent() |
---|
| 336 | { |
---|
[5368] | 337 | if (likely(this->fontTTF != NULL)) |
---|
| 338 | return TTF_FontAscent(this->fontTTF); |
---|
[5343] | 339 | else |
---|
| 340 | return 0; |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | /** |
---|
| 344 | * @returns the maximum descent of the Font, if the font was initialized, 0 otherwise |
---|
| 345 | |
---|
| 346 | the descent is the pixels of the font below the baseline |
---|
| 347 | */ |
---|
| 348 | int Font::getMaxDescent() |
---|
| 349 | { |
---|
[5368] | 350 | if (likely(this->fontTTF != NULL)) |
---|
| 351 | return TTF_FontDescent(this->fontTTF); |
---|
[5343] | 352 | else |
---|
| 353 | return 0; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | /** |
---|
| 357 | * @param character The character to get info about. |
---|
| 358 | * @returns a Glyph struct of a character. This Glyph is a pointer, |
---|
| 359 | and MUST be deleted by the user.. |
---|
| 360 | |
---|
| 361 | This only works for horizontal fonts. see |
---|
| 362 | http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html |
---|
| 363 | for more info about vertical Fonts |
---|
| 364 | */ |
---|
| 365 | Glyph* Font::getGlyphMetrics(Uint16 character) |
---|
| 366 | { |
---|
| 367 | Glyph* rg = new Glyph; |
---|
| 368 | rg->character = character; |
---|
[5368] | 369 | if (likely (this->fontTTF!= NULL)) |
---|
| 370 | { |
---|
| 371 | int miX, maX, miY, maY, adv; |
---|
| 372 | TTF_GlyphMetrics(this->fontTTF, rg->character, |
---|
| 373 | &miX, &maX, |
---|
| 374 | &miY, &maY, |
---|
| 375 | &adv); |
---|
| 376 | rg->minX = (float)miX / (float)this->renderSize; |
---|
| 377 | rg->maxX = (float)maX / (float)this->renderSize; |
---|
| 378 | rg->minY = (float)miY / (float)this->renderSize; |
---|
| 379 | rg->maxY = (float)maY / (float)this->renderSize; |
---|
| 380 | rg->advance = (float)adv / (float)this->renderSize; |
---|
| 381 | } |
---|
[5343] | 382 | rg->height = rg->maxY - rg->minY; |
---|
| 383 | rg->width = rg->maxX - rg->minX; |
---|
| 384 | rg->bearingX = (rg->advance - rg->width) / 2; |
---|
| 385 | rg->bearingY = rg->maxY; |
---|
| 386 | return rg; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | /** |
---|
| 390 | * creates a Fast-Texture of this Font |
---|
| 391 | */ |
---|
[5768] | 392 | bool Font::createFastTexture() |
---|
[5343] | 393 | { |
---|
| 394 | /* interesting GLYPHS: |
---|
| 395 | * 32: space |
---|
| 396 | * 33-47: Special Characters. |
---|
| 397 | * 48-57: 0-9 |
---|
| 398 | * 58-63: some more special chars (minor) |
---|
| 399 | * 65-90: A-Z |
---|
| 400 | * 97-122: a-z |
---|
| 401 | */ |
---|
| 402 | int numberOfGlyphs = 91; |
---|
| 403 | |
---|
| 404 | this->initGlyphs(32, numberOfGlyphs); |
---|
[5420] | 405 | // this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space |
---|
[5343] | 406 | |
---|
| 407 | int rectSize = this->findOptimalFastTextureSize(); |
---|
| 408 | |
---|
| 409 | // setting default values. (maybe not needed afterwards) |
---|
| 410 | SDL_Color tmpColor; tmpColor.r = tmpColor.g = tmpColor.b = 0; |
---|
| 411 | // Surface definition. |
---|
| 412 | SDL_Rect tmpRect; // this represents a Rectangle for blitting. |
---|
| 413 | SDL_Surface* tmpSurf = SDL_CreateRGBSurface(SDL_SWSURFACE, |
---|
| 414 | rectSize, rectSize, |
---|
| 415 | 32, |
---|
| 416 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ |
---|
| 417 | 0x000000FF, |
---|
| 418 | 0x0000FF00, |
---|
| 419 | 0x00FF0000, |
---|
| 420 | 0xFF000000 |
---|
| 421 | #else |
---|
[5369] | 422 | 0xFF000000, |
---|
[5343] | 423 | 0x00FF0000, |
---|
| 424 | 0x0000FF00, |
---|
| 425 | 0x000000FF |
---|
| 426 | #endif |
---|
| 427 | ); |
---|
| 428 | tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h; |
---|
| 429 | SDL_SetClipRect(tmpSurf, &tmpRect); |
---|
[5420] | 430 | int maxLineHeight = this->getMaxHeight(); |
---|
[5343] | 431 | |
---|
| 432 | // all the interessting Glyphs |
---|
| 433 | for (int i = 0; i < 128; i++) |
---|
| 434 | { |
---|
| 435 | SDL_Surface* glyphSurf = NULL; |
---|
| 436 | Glyph* tmpGlyph; |
---|
| 437 | |
---|
| 438 | if (tmpGlyph = this->glyphArray[i]) |
---|
| 439 | { |
---|
[5368] | 440 | if (tmpGlyph->height*this->renderSize > maxLineHeight) |
---|
| 441 | maxLineHeight = (int)(tmpGlyph->height*this->renderSize); |
---|
[5343] | 442 | |
---|
[5368] | 443 | if (tmpRect.x+tmpGlyph->advance*this->renderSize > tmpSurf->w) |
---|
[5343] | 444 | { |
---|
| 445 | tmpRect.x = 0; |
---|
[5420] | 446 | tmpRect.y = tmpRect.y + maxLineHeight; |
---|
[5343] | 447 | } |
---|
| 448 | if (tmpRect.y + maxLineHeight > tmpSurf->h) |
---|
| 449 | { |
---|
[5367] | 450 | PRINTF(1)("Protection, so font cannot write over the boundraries (!!this should not heappen!!)\n"); |
---|
[5343] | 451 | break; |
---|
| 452 | } |
---|
| 453 | // reading in the new Glyph |
---|
[5368] | 454 | if (likely(this->fontTTF != NULL)) |
---|
[5343] | 455 | { |
---|
| 456 | SDL_Color white = {255, 255, 255}; |
---|
[5368] | 457 | glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, i, white); |
---|
[5343] | 458 | } |
---|
| 459 | if( glyphSurf != NULL ) |
---|
| 460 | { |
---|
| 461 | SDL_SetAlpha(glyphSurf, 0, 0); |
---|
[5420] | 462 | int tmp = tmpRect.y; |
---|
| 463 | tmpRect.y += this->getMaxAscent()-(int)((float)tmpGlyph->bearingY*this->renderSize); |
---|
| 464 | SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect); |
---|
| 465 | tmpRect.y = tmp; |
---|
[5343] | 466 | |
---|
[5420] | 467 | tmpGlyph->texCoord[0] = (float)(tmpRect.x)/(float)tmpSurf->w; |
---|
| 468 | tmpGlyph->texCoord[1] = (float)(tmpRect.x + tmpGlyph->width*(float)this->renderSize)/(float)tmpSurf->w; |
---|
[5367] | 469 | tmpGlyph->texCoord[2] = (float)(tmpRect.y)/(float)tmpSurf->w; |
---|
[5420] | 470 | tmpGlyph->texCoord[3] = (float)(tmpRect.y+this->getMaxHeight())/(float)tmpSurf->w; |
---|
[5343] | 471 | SDL_FreeSurface(glyphSurf); |
---|
[5420] | 472 | tmpRect.x += (int)(tmpGlyph->advance * this->renderSize)+1; |
---|
[5343] | 473 | |
---|
[5368] | 474 | /* |
---|
[5420] | 475 | // Outputting Glyphs to BMP-files. |
---|
| 476 | char outname[1024]; |
---|
[5343] | 477 | if (i < 10) |
---|
| 478 | sprintf( outname, "%s-glyph-00%d.bmp", this->getName(), i ); |
---|
| 479 | else if (i <100) |
---|
| 480 | sprintf( outname, "%s-glyph-0%d.bmp", this->getName(), i ); |
---|
| 481 | else |
---|
| 482 | sprintf( outname, "%s-glyph-%d.bmp", this->getName(), i ); |
---|
[5420] | 483 | SDL_SaveBMP(tmpSurf, outname);*/ |
---|
[5343] | 484 | } |
---|
| 485 | } |
---|
| 486 | } |
---|
[5420] | 487 | // outputting the GLYPH-table |
---|
| 488 | // char outName[1024]; |
---|
| 489 | // sprintf( outName, "%s-glyphs.bmp", this->getName()); |
---|
| 490 | // SDL_SaveBMP(tmpSurf, outName); |
---|
[5343] | 491 | |
---|
[5768] | 492 | if (this->setSurface(tmpSurf)) |
---|
| 493 | loadTexToGL(); |
---|
[5343] | 494 | } |
---|
| 495 | |
---|
| 496 | /** |
---|
| 497 | * stores Glyph Metrics in an Array. |
---|
| 498 | * @param from The Glyph to start from. |
---|
| 499 | * @param count The number of Glyphs to start From. |
---|
| 500 | */ |
---|
| 501 | void Font::initGlyphs(Uint16 from, Uint16 count) |
---|
| 502 | { |
---|
| 503 | /* initialize the Array, and set all its entries to NULL |
---|
| 504 | * only if the Glyph-array has not been initialized |
---|
| 505 | */ |
---|
| 506 | if (!this->glyphArray) |
---|
| 507 | { |
---|
| 508 | this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR]; |
---|
| 509 | for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) |
---|
| 510 | this->glyphArray[i] = NULL; |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | Uint16 lastGlyph = from + count; |
---|
| 514 | |
---|
| 515 | for (int i = from; i <= lastGlyph; i++) |
---|
| 516 | { |
---|
| 517 | // setting up all the Glyphs we like. |
---|
| 518 | glyphArray[i] = getGlyphMetrics(i); |
---|
| 519 | } |
---|
| 520 | return; |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | /** |
---|
| 524 | * @returns the optimal size to use as the texture size |
---|
| 525 | |
---|
[5347] | 526 | @todo: this algorithm can be a lot faster, althought it does |
---|
[5343] | 527 | not really matter within the init-context, and 128 glyphs. |
---|
| 528 | |
---|
| 529 | This function searches for a 2^n sizes texture-size, this is for |
---|
| 530 | openGL-version < 1.2 compatibility ( and because it is realy easy like this :)) |
---|
| 531 | */ |
---|
| 532 | int Font::findOptimalFastTextureSize() |
---|
| 533 | { |
---|
[5347] | 534 | if (this->glyphArray == NULL) |
---|
| 535 | return 0; |
---|
| 536 | |
---|
[5343] | 537 | int i; |
---|
| 538 | int x,y; // the counters |
---|
| 539 | int maxLineHeight = this->getMaxHeight(); |
---|
| 540 | unsigned int size = 32; // starting Value, we have to start somewhere 32 seems reasonable. (take any small enough 2^i number) |
---|
| 541 | bool sizeOK = false; |
---|
| 542 | Glyph* tmpGlyph; |
---|
| 543 | |
---|
| 544 | while (!sizeOK) |
---|
| 545 | { |
---|
| 546 | x = 0; y = 0; |
---|
[5347] | 547 | for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++) |
---|
[5343] | 548 | { |
---|
| 549 | if((tmpGlyph = this->glyphArray[i]) != NULL) |
---|
| 550 | { |
---|
| 551 | // getting the height of the highest Glyph in the Line. |
---|
[5368] | 552 | if (tmpGlyph->height*this->renderSize > maxLineHeight) |
---|
| 553 | maxLineHeight = (int)(tmpGlyph->height*this->renderSize); |
---|
[5343] | 554 | |
---|
[5368] | 555 | if (x + tmpGlyph->advance*this->renderSize > size) |
---|
[5343] | 556 | { |
---|
| 557 | x = 0; |
---|
| 558 | y = y + maxLineHeight; |
---|
| 559 | //maxLineHeight = 0; |
---|
| 560 | } |
---|
| 561 | if (y + maxLineHeight + 1 > size) |
---|
| 562 | break; |
---|
[5370] | 563 | x += (int)(tmpGlyph->advance*this->renderSize)+1; |
---|
[5343] | 564 | |
---|
| 565 | } |
---|
| 566 | } |
---|
| 567 | if (i >= FONT_HIGHEST_KNOWN_CHAR-1 || size > 8192) |
---|
| 568 | sizeOK = true; |
---|
| 569 | else |
---|
| 570 | size *= 2; |
---|
| 571 | } |
---|
| 572 | return size; |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | |
---|
| 576 | /** |
---|
| 577 | * a simple function to get some interesting information about this class |
---|
| 578 | */ |
---|
| 579 | void Font::debug() |
---|
| 580 | { |
---|
| 581 | // print the loaded font's style |
---|
| 582 | int style; |
---|
[5368] | 583 | if (likely(this->fontTTF != NULL)) |
---|
| 584 | style = TTF_GetFontStyle(this->fontTTF); |
---|
[5343] | 585 | PRINTF(0)("The font style is:"); |
---|
| 586 | if(style==TTF_STYLE_NORMAL) |
---|
| 587 | PRINTF(0)(" normal"); |
---|
| 588 | else { |
---|
| 589 | if(style&TTF_STYLE_BOLD) |
---|
| 590 | PRINTF(0)(" bold"); |
---|
| 591 | if(style&TTF_STYLE_ITALIC) |
---|
| 592 | PRINTF(0)(" italic"); |
---|
| 593 | if(style&TTF_STYLE_UNDERLINE) |
---|
| 594 | PRINTF(0)(" underline"); |
---|
| 595 | } |
---|
| 596 | PRINTF(0)("\n"); |
---|
| 597 | } |
---|