Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5368 in orxonox.OLD for trunk/src/lib/graphics/text_engine/font.cc


Ignore:
Timestamp:
Oct 13, 2005, 12:00:01 AM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: rendering font with RenderSize, but not with TextSize. This makes the Font more controllable

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/text_engine/font.cc

    r5367 r5368  
    3636 * @param fontSize the Size of the Font in Pixels
    3737 */
    38 Font::Font(const char* fontFile, unsigned int fontSize)
     38Font::Font(const char* fontFile, unsigned int renderSize)
    3939{
    4040  this->init();
    4141
    42   this->setSize(fontSize);
     42  this->renderSize = renderSize;
    4343  this->setStyle("c");
    4444
    4545  if (fontFile != NULL)
    46     this->loadFont(fontFile);
     46    this->loadFontFromTTF(fontFile);
    4747}
    4848
     
    118118
    119119  // erease this font out of the memory.
    120   if (likely(this->font != NULL))
    121     TTF_CloseFont(this->font);
     120  if (likely(this->fontTTF != NULL))
     121    TTF_CloseFont(this->fontTTF);
    122122}
    123123
     
    129129  this->setClassID(CL_FONT, "Font");
    130130  // setting default values.
    131   this->font = NULL;
     131  this->fontTTF = NULL;
    132132  this->glyphArray = NULL;
    133133  this->fastTextureID = 0;
     
    140140 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
    141141 */
    142 bool Font::loadFont(const char* fontFile)
     142bool Font::loadFontFromTTF(const char* fontFile)
    143143{
    144144  // checking for existent Font.
    145   if (this->font != NULL)
    146   {
    147     TTF_CloseFont(this->font);
    148     this->font = NULL;
     145  if (this->fontTTF != NULL)
     146  {
     147    TTF_CloseFont(this->fontTTF);
     148    this->fontTTF = NULL;
    149149  }
    150150  if (this->fastTextureID != 0)
     
    156156
    157157  this->setName(fontFile);
    158   this->font = TTF_OpenFont(this->getName(), this->fontSize);
    159 
    160   if(this->font != NULL)
     158  this->fontTTF = TTF_OpenFont(this->getName(), this->renderSize);
     159
     160  if(this->fontTTF != NULL)
    161161  {
    162162    this->fastTextureID = this->createFastTexture();
     
    184184    return false;
    185185
    186   if (this->font != NULL)
    187   {
    188     TTF_CloseFont(this->font);
    189     this->font = NULL;
     186  if (this->fontTTF != NULL)
     187  {
     188    TTF_CloseFont(this->fontTTF);
     189    this->fontTTF = NULL;
    190190  }
    191191  if (this->fastTextureID != 0)
     
    214214      tmpGlyph->texCoord[2] = cy+0.001f;
    215215      tmpGlyph->texCoord[3] = cy+0.0625f;
    216       tmpGlyph->width = 16;
    217       tmpGlyph->bearingX = 16;
    218       tmpGlyph->bearingY = 16;
    219       tmpGlyph->height = 16;
     216      tmpGlyph->width = 1;
     217      tmpGlyph->bearingX = 1;
     218      tmpGlyph->bearingY = 1;
     219      tmpGlyph->height = 1;
    220220    }
    221221  }
     
    241241    this->renderStyle |= TTF_STYLE_UNDERLINE;
    242242
    243   if (likely(this->font != NULL))
    244     TTF_SetFontStyle(this->font, this->renderStyle);
     243  if (likely(this->fontTTF != NULL))
     244    TTF_SetFontStyle(this->fontTTF, this->renderStyle);
    245245//  else
    246246//    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
    247247}
    248248
    249 /**
    250  *  Sets a new Size to the font
    251  * @param fontSize The new Size in pixels.
    252  */
    253 void Font::setSize(unsigned int fontSize)
    254 {
    255   this->fontSize = fontSize;
    256 }
    257 
    258249Font* Font::defaultFont = NULL;
    259250
    260251void Font::createAsciiImage(const char* fileName)
    261252{
    262   if (this->font == NULL)
     253  if (this->fontTTF == NULL)
    263254    return;
    264255  int height = this->getMaxHeight();
     
    294285    {
    295286      SDL_Surface* glyphSurf = NULL;
    296       if (likely(this->font != NULL))
     287      if (likely(this->fontTTF != NULL))
    297288      {
    298289        SDL_Color white = {255, 255, 255};
    299         glyphSurf = TTF_RenderGlyph_Blended(this->font, posX+16*posY, white);
     290        glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, posX+16*posY, white);
    300291      }
    301292      if( glyphSurf != NULL )
     
    339330int Font::getMaxHeight()
    340331{
    341   if (likely (this->font != NULL))
    342     return TTF_FontHeight(this->font);
     332  if (likely (this->fontTTF != NULL))
     333    return TTF_FontHeight(this->fontTTF);
    343334  else
    344335    return 0;
     
    352343int Font::getMaxAscent()
    353344{
    354   if (likely(this->font != NULL))
    355     return TTF_FontAscent(this->font);
     345  if (likely(this->fontTTF != NULL))
     346    return TTF_FontAscent(this->fontTTF);
    356347  else
    357348    return 0;
     
    365356int Font::getMaxDescent()
    366357{
    367   if (likely(this->font != NULL))
    368     return TTF_FontDescent(this->font);
     358  if (likely(this->fontTTF != NULL))
     359    return TTF_FontDescent(this->fontTTF);
    369360  else
    370361    return 0;
     
    384375  Glyph* rg = new Glyph;
    385376  rg->character = character;
    386   if (likely (this->font!= NULL))
    387     TTF_GlyphMetrics(this->font, rg->character,
    388                      &rg->minX, &rg->maxX,
    389                      &rg->minY, &rg->maxY,
    390                      &rg->advance);
     377  if (likely (this->fontTTF!= NULL))
     378  {
     379    int miX, maX, miY, maY, adv;
     380    TTF_GlyphMetrics(this->fontTTF, rg->character,
     381                     &miX, &maX,
     382                     &miY, &maY,
     383                     &adv);
     384    rg->minX = (float)miX / (float)this->renderSize;
     385    rg->maxX = (float)maX / (float)this->renderSize;
     386    rg->minY = (float)miY / (float)this->renderSize;
     387    rg->maxY = (float)maY / (float)this->renderSize;
     388    rg->advance = (float)adv / (float)this->renderSize;
     389  }
    391390  rg->height = rg->maxY - rg->minY;
    392391  rg->width = rg->maxX - rg->minX;
     
    412411
    413412  this->initGlyphs(32, numberOfGlyphs);
    414   this->glyphArray[32]->width = fontSize/2; //!< @todo find out the real size of a Space
     413  this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
    415414
    416415  int rectSize = this->findOptimalFastTextureSize();
     
    447446    if (tmpGlyph = this->glyphArray[i])
    448447    {
    449       if (tmpGlyph->height > maxLineHeight)
    450         maxLineHeight = tmpGlyph->height;
    451 
    452       if (tmpRect.x+tmpGlyph->advance > tmpSurf->w)
     448      if (tmpGlyph->height*this->renderSize > maxLineHeight)
     449        maxLineHeight = (int)(tmpGlyph->height*this->renderSize);
     450
     451      if (tmpRect.x+tmpGlyph->advance*this->renderSize > tmpSurf->w)
    453452      {
    454453        tmpRect.x = 0;
     
    462461      }
    463462          // reading in the new Glyph
    464       if (likely(this->font != NULL))
     463      if (likely(this->fontTTF != NULL))
    465464      {
    466465        SDL_Color white = {255, 255, 255};
    467         glyphSurf = TTF_RenderGlyph_Blended(this->font, i, white);
     466        glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, i, white);
    468467      }
    469468      if( glyphSurf != NULL )
     
    473472        SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect);
    474473        tmpGlyph->texCoord[0] = (float)tmpRect.x/(float)tmpSurf->w;
    475         tmpGlyph->texCoord[1] = (float)(tmpRect.x + tmpGlyph->advance)/(float)tmpSurf->w;
     474        tmpGlyph->texCoord[1] = (float)(tmpRect.x + tmpGlyph->advance*this->renderSize)/(float)tmpSurf->w;
    476475        tmpGlyph->texCoord[2] = (float)(tmpRect.y)/(float)tmpSurf->w;
    477         tmpGlyph->texCoord[3] = (float)(tmpRect.y+tmpGlyph->height)/(float)tmpSurf->w;
     476        tmpGlyph->texCoord[3] = (float)(tmpRect.y+tmpGlyph->height*this->renderSize)/(float)tmpSurf->w;
    478477        SDL_FreeSurface(glyphSurf);
    479478
    480         tmpRect.x += tmpGlyph->advance;
     479        tmpRect.x += (int)(tmpGlyph->advance*this->renderSize);
    481480
    482481              // Outputting Glyphs to BMP-files.
    483 /*
     482        /*
    484483        char outname[512];
    485484        if (i < 10)
     
    489488        else
    490489        sprintf( outname, "%s-glyph-%d.bmp", this->getName(), i );
    491         SDL_SaveBMP(tmpSurf, outname);*/
    492 
     490        SDL_SaveBMP(tmpSurf, outname);
     491        */
    493492      }
    494493    }
     
    568567      {
    569568              // getting the height of the highest Glyph in the Line.
    570         if (tmpGlyph->height > maxLineHeight)
    571           maxLineHeight = tmpGlyph->height;
    572 
    573         if (x + tmpGlyph->advance > size)
     569        if (tmpGlyph->height*this->renderSize > maxLineHeight)
     570          maxLineHeight = (int)(tmpGlyph->height*this->renderSize);
     571
     572        if (x + tmpGlyph->advance*this->renderSize > size)
    574573        {
    575574          x = 0;
     
    579578        if (y + maxLineHeight + 1 > size)
    580579          break;
    581         x += tmpGlyph->advance;
     580        x += (int)(tmpGlyph->advance*this->renderSize);
    582581
    583582      }
     
    599598  // print the loaded font's style
    600599  int style;
    601   if (likely(this->font != NULL))
    602     style = TTF_GetFontStyle(this->font);
     600  if (likely(this->fontTTF != NULL))
     601    style = TTF_GetFontStyle(this->fontTTF);
    603602  PRINTF(0)("The font style is:");
    604603  if(style==TTF_STYLE_NORMAL)
Note: See TracChangeset for help on using the changeset viewer.