Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8765 in orxonox.OLD for trunk/src/lib


Ignore:
Timestamp:
Jun 24, 2006, 3:49:33 AM (19 years ago)
Author:
bensch
Message:

trunk: much better loading of the Font

Location:
trunk/src/lib/graphics/text_engine
Files:
4 edited

Legend:

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

    r8764 r8765  
    192192void Font::setStyle(const std::string& renderStyle)
    193193{
    194   this->data->setStyle(renderStyle);
     194  /// FIXME
     195  //this->data->setStyle(renderStyle);
    195196}
    196197
     
    207208 * @param fileName the File to write the image into.
    208209 */
    209 void Font::createAsciiImage(const std::string& fileName, unsigned int size) const
    210 {
    211   if (this->data->fontTTF == NULL)
     210void Font::createAsciiImage(const std::string& ttfFile, const std::string& fileName, unsigned int size)
     211{
     212  TTF_Font* fontTTF = TTF_OpenFont(ttfFile.c_str(), size);
     213
     214  if (fontTTF == NULL)
    212215    return;
    213   int height = this->getMaxHeight();
     216  int height = TTF_FontHeight(fontTTF);
    214217
    215218  //
     
    241244    {
    242245      SDL_Surface* glyphSurf = NULL;
    243       if (likely(this->data->fontTTF != NULL))
    244       {
    245         SDL_Color white = {255, 255, 255};
    246         glyphSurf = TTF_RenderGlyph_Blended(this->data->fontTTF, posX+size*posY, white);
    247       }
     246      SDL_Color white = {255, 255, 255};
     247      glyphSurf = TTF_RenderGlyph_Blended(fontTTF, posX+size*posY, white);
     248
    248249      if( glyphSurf != NULL )
    249250      {
     
    259260  SDL_SaveBMP(tmpSurf, fileName.c_str());
    260261  SDL_FreeSurface(tmpSurf);
     262
     263  TTF_CloseFont(fontTTF);
    261264}
    262265
     
    273276  PRINT(0)("TEST %p and %p\n", this->data.get(), this->data->texData.get());
    274277  // print the loaded font's style
    275   int style = TTF_STYLE_NORMAL;
     278/*  int style = TTF_STYLE_NORMAL;
    276279  if (likely(this->data->fontTTF != NULL))
    277280    style = TTF_GetFontStyle(this->data->fontTTF);
     
    286289      PRINTF(0)(" italic");
    287290    if(style&TTF_STYLE_UNDERLINE)
    288       PRINTF(0)(" underline");
    289   }
     291      PRINTF(0)(" underline");*/
     292//  }
    290293  PRINTF(0)("\n");
    291294}
  • trunk/src/lib/graphics/text_engine/font.h

    r8764 r8765  
    4040  /** @returns a Pointer to the Array of Glyphs */
    4141  inline Glyph** getGlyphArray() const { return this->data->getGlyphArray(); };
    42   /** @returns the a pointer to the TTF */
    43   inline TTF_Font* getTTF() const { return this->data->getTTF(); };
    4442
    4543  inline int getMaxHeight() const { return data->getMaxHeight(); };
     
    4846
    4947
    50   void createAsciiImage(const std::string& fileName, unsigned int size) const;
     48  static void createAsciiImage(const std::string& ttfFile, const std::string& fileName, unsigned int size);
    5149
    5250  void debug() const;
  • trunk/src/lib/graphics/text_engine/font_data.cc

    r8764 r8765  
    2828 */
    2929FontData::FontData()
    30   : texData(new TextureData)
    31 {
    32   printf("CREATE FONT_DATA\n");
    33   this->fontTTF = NULL;
     30    : texData(new TextureData)
     31{
    3432  this->glyphArray = NULL;
    3533  this->renderStyle = TTF_STYLE_NORMAL;
    3634  this->renderSize = FONT_DEFAULT_RENDER_SIZE;
     35
     36  this->maxHeight = 0;
     37  this->maxAscent = 0;
     38  this->maxDescent = 0;
    3739}
    3840
     
    6062  //     if(glIsTexture(this->fastTextureID))
    6163  //       glDeleteTextures(1, &this->fastTextureID);
    62 
    63   // erease this font out of the memory.
    64   if (likely(this->fontTTF != NULL))
    65     TTF_CloseFont(this->fontTTF);
    66 }
    67 
    68 /**
    69  * @returns the maximum height of the Font, if the font was initialized, 0 otherwise
    70  */
    71 int FontData::getMaxHeight() const
    72 {
    73   if (likely (this->fontTTF != NULL))
    74     return TTF_FontHeight(this->fontTTF);
    75   else
    76     return 1;
    77 }
    78 
    79 /**
    80  * @returns the maximum ascent of the Font, if the font was initialized, 0 otherwise
    81  *
    82  * the ascent is the pixels of the font above the baseline
    83  */
    84 int FontData::getMaxAscent() const
    85 {
    86   if (likely(this->fontTTF != NULL))
    87     return TTF_FontAscent(this->fontTTF);
    88   else
    89     return 0;
    90 }
    91 
    92 /**
    93  * @returns the maximum descent of the Font, if the font was initialized, 0 otherwise
    94  *
    95  * the descent is the pixels of the font below the baseline
    96  */
    97 int FontData::getMaxDescent() const
    98 {
    99   if (likely(this->fontTTF != NULL))
    100     return TTF_FontDescent(this->fontTTF);
    101   else
    102     return 0;
    103 }
    104 
     64}
    10565
    10666
     
    11272bool FontData::loadFontFromTTF(const std::string& fontFile, unsigned int renderSize)
    11373{
     74  TTF_Font* fontTTF;
    11475  //this->setName(fontFile);
    115   this->fontTTF = TTF_OpenFont(fontFile.c_str(), renderSize);
     76  fontTTF = TTF_OpenFont(fontFile.c_str(), renderSize);
    11677  this->renderSize = renderSize;
    11778
    118   if(this->fontTTF != NULL)
    119   {
    120     this->setStyle("c");
    121     if (this->createFastTexture())
     79  if(fontTTF != NULL)
     80  {
     81    this->maxHeight = TTF_FontHeight(fontTTF);
     82    this->maxAscent = TTF_FontAscent(fontTTF);
     83    this->maxDescent = TTF_FontDescent(fontTTF);
     84
     85    this->setStyle(fontTTF, "c");
     86    if (this->createFastTexture(fontTTF))
     87    {
     88      TTF_CloseFont(fontTTF);
    12289      return true;
     90    }
     91
    12392    else
    12493    {
     94      TTF_CloseFont(fontTTF);
     95      PRINTF(1)("Unable to createa a Fast Texture fot %s\n", fontFile.c_str() );
    12596      return false;
    12697    }
     
    12899  else
    129100  {
    130     PRINTF(1)("TTF_OpenFont: %s\n", TTF_GetError());
     101    PRINTF(1)("TTF_OpenFont: %s for %s\n", TTF_GetError(), fontFile.c_str());
    131102    return false;
    132103  }
     
    194165 *   i: italic, b: bold, u, underline
    195166 */
    196 void FontData::setStyle(const std::string& renderStyle)
     167void FontData::setStyle(TTF_Font* font, const std::string& renderStyle)
    197168{
    198169  this->renderStyle = TTF_STYLE_NORMAL;
     
    207178      this->renderStyle |= TTF_STYLE_UNDERLINE;
    208179  }
    209   if (likely(this->fontTTF != NULL))
    210     TTF_SetFontStyle(this->fontTTF, this->renderStyle);
     180
     181  /// !TODO REBUILD THE FONT
     182
     183  if (likely(font != NULL))
     184      TTF_SetFontStyle(font, this->renderStyle);
    211185  //  else
    212186  //    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
     
    225199 * for more info about vertical Fonts
    226200 */
    227 bool FontData::getGlyphMetrics(Glyph* glyph, Uint16 character)
     201bool FontData::getGlyphMetrics(TTF_Font* font, Glyph* glyph, Uint16 character)
    228202{
    229203  glyph->character = character;
    230   if (likely (this->fontTTF!= NULL))
     204  if (likely (font != NULL))
    231205  {
    232206    int miX, maX, miY, maY, adv;
    233     if (TTF_GlyphMetrics(this->fontTTF, glyph->character,
    234         &miX, &maX,
    235         &miY, &maY,
    236         &adv) == -1)
     207    if (TTF_GlyphMetrics(font, glyph->character,
     208                         &miX, &maX,
     209                         &miY, &maY,
     210                         &adv) == -1)
    237211      return false;
    238212    glyph->minX = (float)miX / (float)this->renderSize;
     
    259233 * @brief creates a Fast-Texture of this Font
    260234 */
    261 bool FontData::createFastTexture()
     235bool FontData::createFastTexture(TTF_Font* fontTTF)
    262236{
    263237  /* interesting GLYPHS:
     
    271245  int numberOfGlyphs = 91;
    272246
    273   this->initGlyphs(32, numberOfGlyphs);
     247  this->initGlyphs(fontTTF, 32, numberOfGlyphs);
    274248  //  this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
    275249
     
    281255  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
    282256  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_HWSURFACE,
    283                                                rectSize, rectSize,
    284                                                32,
     257                          rectSize, rectSize,
     258                          32,
    285259#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    286260                          0x000000FF,
     
    289263                          0xFF000000
    290264#else
    291                               0xFF000000,
     265                          0xFF000000,
    292266                          0x00FF0000,
    293267                          0x0000FF00,
     
    321295      }
    322296      // reading in the new Glyph
    323       if (likely(this->fontTTF != NULL))
     297      if (likely(fontTTF != NULL))
    324298      {
    325299        SDL_Color white = {255, 255, 255};
    326         glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, i, white);
     300        glyphSurf = TTF_RenderGlyph_Blended(fontTTF, i, white);
    327301      }
    328302      if( glyphSurf != NULL )
     
    344318  }
    345319  // outputting the GLYPH-table
    346 //       char outName[1024];
    347 //       sprintf( outName, "%s-glyphs.bmp", this->getName());
    348 //       SDL_SaveBMP(tmpSurf, outName);
     320  //       char outName[1024];
     321  //       sprintf( outName, "%s-glyphs.bmp", this->getName());
     322  //       SDL_SaveBMP(tmpSurf, outName);
    349323  this->texData->setAlpha(true);
    350324  if (this->texData->setSurface(tmpSurf))
     
    358332 * @param count The number of Glyphs to start From.
    359333 */
    360 void FontData::initGlyphs(Uint16 from, Uint16 count)
     334void FontData::initGlyphs(TTF_Font* font, Uint16 from, Uint16 count)
    361335{
    362336  /* initialize the Array, and set all its entries to NULL
     
    376350    // setting up all the Glyphs we like.
    377351    Glyph* newGlyph = new Glyph;
    378     if (getGlyphMetrics(newGlyph, i))
     352    if (getGlyphMetrics(font, newGlyph, i))
    379353      this->glyphArray[i] = newGlyph;
    380354    else
  • trunk/src/lib/graphics/text_engine/font_data.h

    r8764 r8765  
    5959  bool loadFontFromSDL_Surface(SDL_Surface* surface);
    6060
    61   void setStyle(const std::string& renderStyle);
     61  void setStyle(TTF_Font* font, const std::string& renderStyle);
    6262
    6363
     
    6565  /** @returns a Pointer to the Array of Glyphs */
    6666  inline Glyph** getGlyphArray() const { return this->glyphArray; };
    67   /** @returns the a pointer to the TTF */
    68   inline TTF_Font* getTTF() const { return this->fontTTF; };
    6967
    70   int getMaxHeight() const;
    71   int getMaxAscent() const;
    72   int getMaxDescent() const;
     68  int getMaxHeight() const { return maxHeight; };
     69  int getMaxAscent() const { return maxAscent; };
     70  int getMaxDescent() const { return maxDescent; };
    7371
    7472
     
    8280  FontData();
    8381
    84   void initGlyphs(Uint16 from, Uint16 count);
    85   bool getGlyphMetrics(Glyph* glyph, Uint16 character);
     82  void initGlyphs(TTF_Font* font, Uint16 from, Uint16 count);
     83  bool getGlyphMetrics(TTF_Font* font, Glyph* glyph, Uint16 character);
    8684
    8785  int findOptimalFastTextureSize();
    88   bool createFastTexture();
     86  bool createFastTexture(TTF_Font* font);
    8987
    9088private:
    91   TTF_Font*     fontTTF;             //!< The font we use for this.
     89  std::string   fontFile;            //!< The FileName the Font was loaded from.
     90
    9291  int           renderStyle;         //!< The Renderstyle
    9392  unsigned int  renderSize;          //!< How big the Font should be rendered.
    9493
    9594  Glyph**       glyphArray;          //!< An Array of all the Glyphs stored in the Array of Glyphs.
     95
     96  int           maxHeight;           //!< Max Height of the Font.
     97  int           maxAscent;           //!< Max Ascent of the Font.
     98  int           maxDescent;          //!< Max Desent of the Font.
    9699
    97100  TextureDataPointer   texData;
Note: See TracChangeset for help on using the changeset viewer.