Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5368 in orxonox.OLD


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

Location:
trunk/src/lib/graphics/text_engine
Files:
3 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)
  • trunk/src/lib/graphics/text_engine/font.h

    r5367 r5368  
    3737  // Glyph-specific (size and so on)
    3838  Uint16   character;         //!< The character
    39   int      minX;              //!< The minimum distance from the origin in X
    40   int      maxX;              //!< The maximum distance from the origin in X
    41   int      minY;              //!< The minimum distance from the origin in Y
    42   int      maxY;              //!< The maximum distance from the origin in Y
    43   int      width;             //!< The width of the Glyph
    44   int      height;            //!< The height of the Glyph
    45   int      bearingX;          //!< How much is right of the Origin
    46   int      bearingY;          //!< How much is above the Origin
    47   int      advance;           //!< How big a Glyph would be in monospace-mode
     39  float    minX;              //!< The minimum distance from the origin in X
     40  float    maxX;              //!< The maximum distance from the origin in X
     41  float    minY;              //!< The minimum distance from the origin in Y
     42  float    maxY;              //!< The maximum distance from the origin in Y
     43  float    width;             //!< The width of the Glyph
     44  float    height;            //!< The height of the Glyph
     45  float    bearingX;          //!< How much is right of the Origin
     46  float    bearingY;          //!< How much is above the Origin
     47  float    advance;           //!< How big a Glyph would be in monospace-mode
    4848
    4949  GLfloat texCoord[4];        //!< Texture coordinates: 0:left, 1:right, 2: top, 3: bottom.
     
    5858  public:
    5959    Font(const char* fontFile,
    60          unsigned int fontSize);
     60         unsigned int renderSize);
    6161    Font(const char* imageFile);
    6262    Font(char** xpmArray);
     
    6666
    6767  // font
    68     bool loadFont(const char* fontFile);
     68    bool loadFontFromTTF(const char* fontFile);
    6969    bool loadFontFromSDL_Surface(SDL_Surface* surface);
    7070
    71     void setSize(unsigned int fontSize);
    7271    void setStyle(const char* renderStyle);
    7372
     
    8281    static void initDefaultFont();
    8382    static void removeDefaultFont();
     83
    8484
    8585  private:
     
    9999    static Font*  defaultFont;         //!< a default font, that is used, if other fonts were unable to be loaded.
    100100    // information about the Font
    101     TTF_Font*     font;                //!< The font we use for this.
    102     unsigned int  fontSize;            //!< The size of the font in pixels. each Font has one size.
     101    TTF_Font*     fontTTF;             //!< The font we use for this.
    103102    int           renderStyle;         //!< The Renderstyle
     103    unsigned int  renderSize;          //!< How big the Font should be rendered.
    104104
    105105    Glyph**       glyphArray;          //!< An Array of all the Glyphs stored in the Array of Glyphs.
  • trunk/src/lib/graphics/text_engine/text.cc

    r5367 r5368  
    3232 * @param type The renderType to display this font in
    3333 */
    34 Text::Text(const char* fontFile, unsigned int fontSize, TEXT_RENDER_TYPE type)
     34Text::Text(const char* fontFile, unsigned int textSize, TEXT_RENDER_TYPE type)
    3535{
    3636  this->init();
    3737
    3838  if (fontFile != NULL)
    39     this->setFont(fontFile, fontSize);
     39    this->setFont(fontFile, textSize);
    4040  this->setType(type);
     41  this->size = textSize;
    4142}
    4243
     
    109110void Text::setType(TEXT_RENDER_TYPE type)
    110111{
    111   if (this->font != NULL && this->font->font)
     112  if (this->font != NULL && this->font->fontTTF)
    112113    this->type = type;
    113114  else
     
    150151    Glyph** glyphArray = this->font->getGlyphArray();
    151152
    152     int width = 0;
     153    float width = 0;
    153154    const char* tmpText = this->externText;
    154155    if (this->externText == NULL)
     
    184185  {
    185186    SDL_Color theColor = { (int)(this->color.x*255), (int)(this->color.y*255), (int)(this->color.z*255) };
    186     tmpSurf = TTF_RenderText_Blended(this->font->font,
     187    tmpSurf = TTF_RenderText_Blended(this->font->fontTTF,
    187188                                     this->text,
    188189                                     theColor);
     
    248249          glBegin(GL_QUADS);
    249250          glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
    250           glVertex2d(0, - tmpGlyph->bearingY);
     251          glVertex2d(0, - tmpGlyph->bearingY * this->size);
    251252          glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
    252           glVertex2d(0, tmpGlyph->height - tmpGlyph->bearingY);
     253          glVertex2d(0, (tmpGlyph->height - tmpGlyph->bearingY)*this->size);
    253254          glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
    254           glVertex2d(tmpGlyph->width, tmpGlyph->height - tmpGlyph->bearingY);
     255          glVertex2d(tmpGlyph->width*this->size, (tmpGlyph->height - tmpGlyph->bearingY)*this->size);
    255256          glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
    256           glVertex2d(tmpGlyph->width, - tmpGlyph->bearingY);
     257          glVertex2d(tmpGlyph->width*this->size, - tmpGlyph->bearingY*this->size);
    257258          glEnd();
    258259          glEndList();
    259           glTranslatef(glyphArray[*tmpText]->width, 0, 0);
     260          glTranslatef(glyphArray[*tmpText]->width*this->size, 0, 0);
    260261        }
    261262        tmpText++;
     
    371372  glGenTextures(1, &texture);
    372373  glBindTexture(GL_TEXTURE_2D, texture);
    373   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     374  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    374375  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    375376  glTexImage2D(GL_TEXTURE_2D,
Note: See TracChangeset for help on using the changeset viewer.