Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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


Ignore:
Timestamp:
Jun 23, 2006, 6:12:07 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: Preparing for FONT-DATA

Location:
trunk/src/lib/graphics
Files:
4 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/Makefile.am

    r8619 r8751  
    1818                text_engine/limited_width_text.cc \
    1919                text_engine/font.cc \
     20                text_engine/font_data.cc \
    2021                \
    2122                effects/graphics_effect.cc \
     
    3132                effects/lense_flare.cc
    3233
    33 noinst_HEADERS = graphics_engine.h \
    34                  light.h \
    35                  shader.h \
    36                  \
    37                  render2D/render_2d.h \
    38                  render2D/element_2d.h \
    39                  render2D/image_plane.h \
    40                  \
    41                  text_engine/text_engine.h \
    42                  text_engine/text.h \
    43                  text_engine/multi_line_text.h \
    44                  text_engine/limited_width_text.h \
    45                  text_engine/font.h \
    46                  text_engine/default_font.xpm \
    47                  \
    48                  effects/graphics_effect.h \
    49                  effects/atmospheric_engine.h \
    50                  effects/weather_effect.h \
    51                  effects/sun_effect.h \
    52                  effects/fog_effect.h \
    53                  effects/volfog_effect.h \
    54                  effects/rain_effect.h \
    55                  effects/snow_effect.h \
    56                  effects/cloud_effect.h \
    57                  effects/lightening_effect.h \
    58                  effects/lense_flare.h
     34noinst_HEADERS =\
     35                graphics_engine.h \
     36                light.h \
     37                shader.h \
     38                \
     39                render2D/render_2d.h \
     40                render2D/element_2d.h \
     41                render2D/image_plane.h \
     42                \
     43                text_engine/text_engine.h \
     44                text_engine/text.h \
     45                text_engine/multi_line_text.h \
     46                text_engine/limited_width_text.h \
     47                text_engine/font.h \
     48                text_engine/font_data.h \
     49                text_engine/default_font.xpm \
     50                \
     51                effects/graphics_effect.h \
     52                effects/atmospheric_engine.h \
     53                effects/weather_effect.h \
     54                effects/sun_effect.h \
     55                effects/fog_effect.h \
     56                effects/volfog_effect.h \
     57                effects/rain_effect.h \
     58                effects/snow_effect.h \
     59                effects/cloud_effect.h \
     60                effects/lightening_effect.h \
     61                effects/lense_flare.h
    5962
    6063
  • trunk/src/lib/graphics/importer/texture_data.h

    r8363 r8751  
    11/*!
    2  * @file texture.h
     2 * @file texture_data.h
    33 * @brief Contains the texture class, that handles the reading of Images into Texutre-files.
    44 */
  • trunk/src/lib/graphics/text_engine/font.cc

    r8316 r8751  
    3737 */
    3838Font::Font(const std::string& fontFile, unsigned int renderSize)
     39  : data(new FontData())
    3940{
    4041  this->init();
    4142
    42   this->renderSize = renderSize;
     43
     44  this->data->renderSize = renderSize;
    4345  this->setStyle("c");
    4446
     
    5254 */
    5355Font::Font(const std::string& imageFile)
     56  : data(new FontData())
    5457{
    5558  this->init();
     
    7578 */
    7679Font::Font(char** xpmArray)
     80  : data(new FontData())
    7781{
    7882  this->init();
     
    99103 */
    100104Font::~Font()
    101 {
    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         delete this->glyphArray[i];
    109     }
    110     delete[] this->glyphArray;
    111   }
    112 
    113   //! @todo check if we really do not need to delete the fastTextureID here.
    114   //   if (this->fastTextureID != 0)
    115   //     if(glIsTexture(this->fastTextureID))
    116   //       glDeleteTextures(1, &this->fastTextureID);
    117 
    118   // erease this font out of the memory.
    119   if (likely(this->fontTTF != NULL))
    120     TTF_CloseFont(this->fontTTF);
    121 }
     105{ }
    122106
    123107/**
     
    127111{
    128112  this->setClassID(CL_FONT, "Font");
    129   // setting default values.
    130   this->fontTTF = NULL;
    131   this->glyphArray = NULL;
    132 }
    133 
    134 
    135 /**
    136  * sets The Font.
     113}
     114
     115
     116/**
     117 * @brief sets The Font.
    137118 * @param fontFile The file containing the font.
    138119 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
     
    141122{
    142123  // checking for existent Font.
    143   if (this->fontTTF != NULL)
    144   {
    145     TTF_CloseFont(this->fontTTF);
    146     this->fontTTF = NULL;
     124  if (this->data->fontTTF != NULL)
     125  {
     126    TTF_CloseFont(this->data->fontTTF);
     127    this->data->fontTTF = NULL;
    147128  }
    148129
    149130
    150131  this->setName(fontFile);
    151   this->fontTTF = TTF_OpenFont(this->getName(), this->renderSize);
    152 
    153   if(this->fontTTF != NULL)
     132  this->data->fontTTF = TTF_OpenFont(this->getName(), this->data->renderSize);
     133
     134  if(this->data->fontTTF != NULL)
    154135  {
    155136    this->createFastTexture();
     
    174155    return false;
    175156
    176   if (this->fontTTF != NULL)
    177   {
    178     TTF_CloseFont(this->fontTTF);
    179     this->fontTTF = NULL;
     157  if (this->data->fontTTF != NULL)
     158  {
     159    TTF_CloseFont(this->data->fontTTF);
     160    this->data->fontTTF = NULL;
    180161  }
    181162  bool hasAlpha;
     
    189170
    190171  // initializing the Glyphs.
    191   if (this->glyphArray == NULL)
     172  if (this->data->glyphArray == NULL)
    192173  {
    193174    float cx,cy;
    194175    Glyph* tmpGlyph;
    195     this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
     176    this->data->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
    196177    for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
    197178    {
    198       tmpGlyph = this->glyphArray[i] = new Glyph;
     179      tmpGlyph = this->data->glyphArray[i] = new Glyph;
    199180      cx=(float)(i%16)/16.0f;                  // X Position Of Current Character
    200181      cy=(float)(i/16)/16.0f;                  // Y Position Of Current Character
     
    220201
    221202/**
    222  * @brief sets a specific renderStyle
    223  * @param renderStyle the Style to render: a string (char-array) containing:
     203 * @brief sets a specific data->renderStyle
     204 * @param data->renderStyle the Style to render: a string (char-array) containing:
    224205 *   i: italic, b: bold, u, underline
    225206 */
    226207void Font::setStyle(const std::string& renderStyle)
    227208{
    228   this->renderStyle = TTF_STYLE_NORMAL;
     209  this->data->renderStyle = TTF_STYLE_NORMAL;
    229210
    230211  for (unsigned int i = 0; i < renderStyle.size(); i++)
    231212  {
    232213    if (renderStyle[i] == 'b')
    233       this->renderStyle |= TTF_STYLE_BOLD;
     214      this->data->renderStyle |= TTF_STYLE_BOLD;
    234215    else if (renderStyle[i] == 'i')
    235       this->renderStyle |= TTF_STYLE_ITALIC;
     216      this->data->renderStyle |= TTF_STYLE_ITALIC;
    236217    else if (renderStyle[i] == 'u')
    237       this->renderStyle |= TTF_STYLE_UNDERLINE;
    238   }
    239   if (likely(this->fontTTF != NULL))
    240     TTF_SetFontStyle(this->fontTTF, this->renderStyle);
     218      this->data->renderStyle |= TTF_STYLE_UNDERLINE;
     219  }
     220  if (likely(this->data->fontTTF != NULL))
     221    TTF_SetFontStyle(this->data->fontTTF, this->data->renderStyle);
    241222  //  else
    242223  //    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
     
    252233void Font::createAsciiImage(const std::string& fileName, unsigned int size) const
    253234{
    254   if (this->fontTTF == NULL)
     235  if (this->data->fontTTF == NULL)
    255236    return;
    256237  int height = this->getMaxHeight();
     
    284265    {
    285266      SDL_Surface* glyphSurf = NULL;
    286       if (likely(this->fontTTF != NULL))
     267      if (likely(this->data->fontTTF != NULL))
    287268      {
    288269        SDL_Color white = {255, 255, 255};
    289         glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, posX+size*posY, white);
     270        glyphSurf = TTF_RenderGlyph_Blended(this->data->fontTTF, posX+size*posY, white);
    290271      }
    291272      if( glyphSurf != NULL )
     
    329310int Font::getMaxHeight() const
    330311{
    331   if (likely (this->fontTTF != NULL))
    332     return TTF_FontHeight(this->fontTTF);
     312  if (likely (this->data->fontTTF != NULL))
     313    return TTF_FontHeight(this->data->fontTTF);
    333314  else
    334315    return 1;
     
    342323int Font::getMaxAscent() const
    343324{
    344   if (likely(this->fontTTF != NULL))
    345     return TTF_FontAscent(this->fontTTF);
     325  if (likely(this->data->fontTTF != NULL))
     326    return TTF_FontAscent(this->data->fontTTF);
    346327  else
    347328    return 0;
     
    355336int Font::getMaxDescent() const
    356337{
    357   if (likely(this->fontTTF != NULL))
    358     return TTF_FontDescent(this->fontTTF);
     338  if (likely(this->data->fontTTF != NULL))
     339    return TTF_FontDescent(this->data->fontTTF);
    359340  else
    360341    return 0;
     
    374355{
    375356  glyph->character = character;
    376   if (likely (this->fontTTF!= NULL))
     357  if (likely (this->data->fontTTF!= NULL))
    377358  {
    378359    int miX, maX, miY, maY, adv;
    379     if (TTF_GlyphMetrics(this->fontTTF, glyph->character,
     360    if (TTF_GlyphMetrics(this->data->fontTTF, glyph->character,
    380361                     &miX, &maX,
    381362                     &miY, &maY,
    382363                     &adv) == -1)
    383364      return false;
    384     glyph->minX = (float)miX / (float)this->renderSize;
    385     glyph->maxX = (float)maX / (float)this->renderSize;
    386     glyph->minY = (float)miY / (float)this->renderSize;
    387     glyph->maxY = (float)maY / (float)this->renderSize;
    388     glyph->advance = (float)adv / (float)this->renderSize;
     365    glyph->minX = (float)miX / (float)this->data->renderSize;
     366    glyph->maxX = (float)maX / (float)this->data->renderSize;
     367    glyph->minY = (float)miY / (float)this->data->renderSize;
     368    glyph->maxY = (float)maY / (float)this->data->renderSize;
     369    glyph->advance = (float)adv / (float)this->data->renderSize;
    389370
    390371    // Calculate the Rest.
     
    417398
    418399  this->initGlyphs(32, numberOfGlyphs);
    419   //  this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
     400  //  this->data->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
    420401
    421402  int rectSize = this->findOptimalFastTextureSize();
     
    450431    Glyph* tmpGlyph;
    451432
    452     if (tmpGlyph = this->glyphArray[i])
     433    if (tmpGlyph = this->data->glyphArray[i])
    453434    {
    454       if (tmpGlyph->height*this->renderSize > maxLineHeight)
    455         maxLineHeight = (int)(tmpGlyph->height*this->renderSize);
    456 
    457       if (tmpRect.x+tmpGlyph->advance*this->renderSize > tmpSurf->w)
     435      if (tmpGlyph->height*this->data->renderSize > maxLineHeight)
     436        maxLineHeight = (int)(tmpGlyph->height*this->data->renderSize);
     437
     438      if (tmpRect.x+tmpGlyph->advance*this->data->renderSize > tmpSurf->w)
    458439      {
    459440        tmpRect.x = 0;
     
    466447      }
    467448      // reading in the new Glyph
    468       if (likely(this->fontTTF != NULL))
     449      if (likely(this->data->fontTTF != NULL))
    469450      {
    470451        SDL_Color white = {255, 255, 255};
    471         glyphSurf = TTF_RenderGlyph_Blended(this->fontTTF, i, white);
     452        glyphSurf = TTF_RenderGlyph_Blended(this->data->fontTTF, i, white);
    472453      }
    473454      if( glyphSurf != NULL )
     
    475456        SDL_SetAlpha(glyphSurf, 0, 0);
    476457        int tmpY = tmpRect.y;
    477         tmpRect.y += this->getMaxAscent()-(int)((float)tmpGlyph->bearingY*this->renderSize);
     458        tmpRect.y += this->getMaxAscent()-(int)((float)tmpGlyph->bearingY*this->data->renderSize);
    478459        SDL_BlitSurface(glyphSurf, NULL, tmpSurf, &tmpRect);
    479460        tmpRect.y = tmpY;
    480461
    481462        tmpGlyph->texCoord[0] = (float)((float)tmpRect.x )/(float)tmpSurf->w;
    482         tmpGlyph->texCoord[1] = (float)((float)tmpRect.x + tmpGlyph->width*(float)this->renderSize)/(float)tmpSurf->w;
     463        tmpGlyph->texCoord[1] = (float)((float)tmpRect.x + tmpGlyph->width*(float)this->data->renderSize)/(float)tmpSurf->w;
    483464        tmpGlyph->texCoord[2] = (float)(tmpRect.y)/(float)tmpSurf->w;
    484465        tmpGlyph->texCoord[3] = (float)((float)tmpRect.y+(float)this->getMaxHeight())/(float)tmpSurf->w;
    485466        SDL_FreeSurface(glyphSurf);
    486         tmpRect.x += (int)(tmpGlyph->width * this->renderSize) + 1;
     467        tmpRect.x += (int)(tmpGlyph->width * this->data->renderSize) + 1;
    487468      }
    488469    }
     
    508489  *  only if the Glyph-array has not been initialized
    509490  */
    510   if (!this->glyphArray)
    511   {
    512     this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
     491  if (!this->data->glyphArray)
     492  {
     493    this->data->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
    513494    for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
    514       this->glyphArray[i] = NULL;
     495      this->data->glyphArray[i] = NULL;
    515496  }
    516497
     
    522503    Glyph* newGlyph = new Glyph;
    523504    if (getGlyphMetrics(newGlyph, i))
    524       glyphArray[i] = newGlyph;
     505      data->glyphArray[i] = newGlyph;
    525506    else
    526507    {
    527508      delete newGlyph;
    528       glyphArray[i] = NULL;
     509      data->glyphArray[i] = NULL;
    529510    }
    530511  }
     
    543524int Font::findOptimalFastTextureSize()
    544525{
    545   if (this->glyphArray == NULL)
     526  if (this->data->glyphArray == NULL)
    546527    return 0;
    547528
     
    558539    for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
    559540    {
    560       if((tmpGlyph = this->glyphArray[i]) != NULL)
     541      if((tmpGlyph = this->data->glyphArray[i]) != NULL)
    561542      {
    562543        // getting the height of the highest Glyph in the Line.
    563         if (tmpGlyph->height*this->renderSize > maxLineHeight)
    564           maxLineHeight = (int)(tmpGlyph->height*this->renderSize);
    565 
    566         if (x + tmpGlyph->advance*this->renderSize > size)
     544        if (tmpGlyph->height*this->data->renderSize > maxLineHeight)
     545          maxLineHeight = (int)(tmpGlyph->height*this->data->renderSize);
     546
     547        if (x + tmpGlyph->advance*this->data->renderSize > size)
    567548        {
    568549          x = 0;
     
    572553        if (y + maxLineHeight + 1 > size)
    573554          break;
    574         x += (int)(tmpGlyph->advance*this->renderSize)+1;
     555        x += (int)(tmpGlyph->advance*this->data->renderSize)+1;
    575556
    576557      }
     
    592573  // print the loaded font's style
    593574  int style = TTF_STYLE_NORMAL;
    594   if (likely(this->fontTTF != NULL))
    595     style = TTF_GetFontStyle(this->fontTTF);
     575  if (likely(this->data->fontTTF != NULL))
     576    style = TTF_GetFontStyle(this->data->fontTTF);
    596577  PRINTF(0)("The font style is:");
    597578  if(style==TTF_STYLE_NORMAL)
  • trunk/src/lib/graphics/text_engine/font.h

    r7450 r8751  
    1313#include "texture.h"
    1414
    15 #include "glincl.h"
    16 
    17 
    18 #ifdef HAVE_SDL_TTF_H
    19 #include <SDL_ttf.h>
    20 #else
    21 #include <SDL/SDL_ttf.h>
    22 #endif
    23 
    24 /* some default values */
    25 #define FONT_NUM_COLORS              256           //!< number of colors.
    26 
    27 #define FONT_HIGHEST_KNOWN_CHAR      128           //!< The highest character known to the textEngine.
    28 #define FONT_DEFAULT_RENDER_SIZE     50            //!< At what Resolution to render fonts.
    29 // FORWARD DECLARATION
    30 
    31 //! A struct for handling glyphs
    32 /**
    33  * a Glyph is one letter of a certain font
    34  */
    35 struct Glyph
    36 {
    37   // Glyph-specific (size and so on)
    38   Uint16   character;         //!< The character
    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
    48 
    49   GLfloat texCoord[4];        //!< Texture coordinates: 0:left, 1:right, 2: top, 3: bottom.
    50 };
     15#include "font_data.h"
    5116
    5217
     
    5419class Font : public Texture
    5520{
    56   public:
    57     Font(const std::string& fontFile,
    58          unsigned int renderSize);
    59     Font(const std::string& imageFile);
    60     Font(char** xpmArray);
    61     virtual ~Font();
     21
     22public:
     23  Font(const std::string& fontFile,
     24       unsigned int renderSize);
     25  Font(const std::string& imageFile);
     26  Font(char** xpmArray);
     27  virtual ~Font();
    6228
    6329  //  font
    64     bool loadFontFromTTF(const std::string& fontFile);
    65     bool loadFontFromSDL_Surface(SDL_Surface* surface);
     30  bool loadFontFromTTF(const std::string& fontFile);
     31  bool loadFontFromSDL_Surface(SDL_Surface* surface);
    6632
    67     void setStyle(const std::string& renderStyle);
     33  void setStyle(const std::string& renderStyle);
    6834
    69     /** @returns a Pointer to the Array of Glyphs */
    70     inline Glyph** getGlyphArray() const { return this->glyphArray; };
    71     /** @returns the a pointer to the TTF */
    72     inline TTF_Font* getTTF() const { return this->fontTTF; };
     35  /** @returns a Pointer to the Array of Glyphs */
     36  inline Glyph** getGlyphArray() const { return this->data->getGlyphArray(); };
     37  /** @returns the a pointer to the TTF */
     38  inline TTF_Font* getTTF() const { return this->data->getTTF(); };
    7339
    74     int getMaxHeight() const;
    75     int getMaxAscent() const;
    76     int getMaxDescent() const;
     40  int getMaxHeight() const;
     41  int getMaxAscent() const;
     42  int getMaxDescent() const;
    7743
    78     /** @returns the default Font */
    79     inline static Font* getDefaultFont() { if (Font::defaultFont == NULL) initDefaultFont(); return Font::defaultFont; };
     44  /** @returns the default Font */
     45  inline static Font* getDefaultFont() { if (Font::defaultFont == NULL) initDefaultFont(); return Font::defaultFont; };
    8046
    81     void createAsciiImage(const std::string& fileName, unsigned int size) const;
    82     static void initDefaultFont();
    83     static void removeDefaultFont();
     47  void createAsciiImage(const std::string& fileName, unsigned int size) const;
     48  static void initDefaultFont();
     49  static void removeDefaultFont();
    8450
    85   private:
    86     void init();
    87     bool getGlyphMetrics(Glyph* glyph, Uint16 character);
    8851
    89     bool createFastTexture();
     52private:
     53  void init();
     54  bool getGlyphMetrics(Glyph* glyph, Uint16 character);
    9055
    91     void initGlyphs(Uint16 from, Uint16 count);
    92     int findOptimalFastTextureSize();
     56  bool createFastTexture();
    9357
    94     void debug();
     58  void initGlyphs(Uint16 from, Uint16 count);
     59  int findOptimalFastTextureSize();
    9560
    96   private:
    97     static Font*  defaultFont;         //!< a default font, that is used, if other fonts were unable to be loaded.
    98     // information about the Font
    99     TTF_Font*     fontTTF;             //!< The font we use for this.
    100     int           renderStyle;         //!< The Renderstyle
    101     unsigned int  renderSize;          //!< How big the Font should be rendered.
     61  void debug();
    10262
    103     Glyph**       glyphArray;          //!< An Array of all the Glyphs stored in the Array of Glyphs.
     63private:
     64  static Font*  defaultFont;         //!< a default font, that is used, if other fonts were unable to be loaded.
     65
     66  fontDataPointer data;
    10467};
    10568
  • trunk/src/lib/graphics/text_engine/font_data.cc

    r8749 r8751  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER
    1717
    18 #include "texture.h"
     18#include "font_data.h"
    1919
    2020#include "debug.h"
     
    2424
    2525/**
    26  * @brief creates a new Texture Data.
     26 * @brief creates a new Font Data.
    2727 */
    28 TextureData::TextureData()
     28FontData::FontData()
    2929{
    30   this->bAlpha = false;
    31   this->texture = 0;
    32   this->image = NULL;
     30  this->fontTTF = NULL;
     31  this->glyphArray = NULL;
    3332}
    3433
    3534
    3635/**
    37  * @brief Destructor of a Texture
     36 * @brief Destructor of a Font
    3837 *
    39  *  Frees Data, and deletes the textures from GL
     38 *  Frees Data, and deletes the fonts from GL
    4039 */
    41 TextureData::~TextureData()
     40FontData::~FontData()
    4241{
    43   if (this->texture != 0)
    44     glDeleteTextures(1, &this->texture);
    45   if (this->image != NULL)
    46     SDL_FreeSurface(this->image);
     42  // deleting all Glyphs
     43  if (this->glyphArray != NULL)
     44  {
     45    for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
     46    {
     47      if (this->glyphArray[i] != NULL)
     48        delete this->glyphArray[i];
     49    }
     50    delete[] this->glyphArray;
     51  }
     52
     53  //! @todo check if we really do not need to delete the fastTextureID here.
     54  //   if (this->fastTextureID != 0)
     55  //     if(glIsTexture(this->fastTextureID))
     56  //       glDeleteTextures(1, &this->fastTextureID);
     57
     58  // erease this font out of the memory.
     59  if (likely(this->fontTTF != NULL))
     60    TTF_CloseFont(this->fontTTF);
    4761}
    4862
    4963
    50 /**
    51  * @brief Loads an SDL_Surface.
    52  * @param surface the Surface to Load.
    53  * @param target the GL-Target to load the Surface to default GL_TEXTURE_2D
    54  * @returns true on success, false otherwise.
    55  */
    56 bool TextureData::loadSurface(SDL_Surface* surface, GLenum target)
    57 {
    58   if (Texture::getTextureEnableState())
    59   {
    60     SDL_Surface* newSurf = Texture::prepareSurface(surface, this->bAlpha);
    61     if (newSurf != NULL)
    62     {
    63       this->setSurface(newSurf);
    64       this->setTexture(Texture::loadTexToGL(newSurf, target));
    65       return true;
    66     }
    67   }
    68   return false;
    69 }
    70 
    71 
    72 
    73 /**
    74  * @brief set the surface this Texture handles
    75  * @param newSurface the new Surface to set as the image for this Texture.
    76  * @returns true on success.
    77  *
    78  * This deletes the old version of the stored Texture,
    79  * and sets the newly given Surface as current.
    80  */
    81 bool TextureData::setSurface(SDL_Surface* newSurface)
    82 {
    83   if (this->image != NULL)
    84     SDL_FreeSurface(this->image);
    85 
    86   this->image = newSurface;
    87 
    88   return (this->image != NULL);
    89 }
    90 
    91 
    92 /**
    93  * @brief sets a new Texture to the Data.
    94  * @param texture the Texture
    95  * @returns true on success.
    96  */
    97 bool TextureData::setTexture(GLuint texture)
    98 {
    99      // unload the old Texture.
    100   if (this->texture != 0 && glIsTexture(this->getTexture()))
    101   {
    102     glDeleteTextures(1, &this->texture);
    103   }
    104   this->texture = texture;
    105   return (texture != 0);
    106 }
  • trunk/src/lib/graphics/text_engine/font_data.h

    r8749 r8751  
    11/*!
    2  * @file texture.h
    3  * @brief Contains the texture class, that handles the reading of Images into Texutre-files.
     2 * @file font_data.h
     3 * @brief Contains the font-data class, that handles the reading of Images into Texutre-files.
    44 */
    55
    6 #ifndef _TEXTURE_DATA_H
    7 #define _TEXTURE_DATA_H
     6#ifndef _FONT_DATA_H
     7#define _FONT_DATA_H
    88
    99#include "base_object.h"
    1010
    1111#include "glincl.h"
    12 #include "count_pointer.h"
     12#include "texture_data.h"
    1313
    14 /* Forward Declaration */
    15 struct SDL_Surface;
     14#ifdef HAVE_SDL_TTF_H
     15#include <SDL_ttf.h>
     16#else
     17#include <SDL/SDL_ttf.h>
     18#endif
    1619
    1720
    18 class TextureData
     21
     22/* some default values */
     23#define FONT_NUM_COLORS              256           //!< number of colors.
     24
     25#define FONT_HIGHEST_KNOWN_CHAR      128           //!< The highest character known to the textEngine.
     26#define FONT_DEFAULT_RENDER_SIZE     50            //!< At what Resolution to render fonts.
     27
     28//! A struct for handling glyphs
     29/**
     30 * a Glyph is one letter of a certain font
     31 */
     32struct Glyph
    1933{
    20   public:
    21     TextureData();
    22     ~TextureData();
     34  // Glyph-specific (size and so on)
     35  Uint16   character;         //!< The character
     36  float    minX;              //!< The minimum distance from the origin in X
     37  float    maxX;              //!< The maximum distance from the origin in X
     38  float    minY;              //!< The minimum distance from the origin in Y
     39  float    maxY;              //!< The maximum distance from the origin in Y
     40  float    width;             //!< The width of the Glyph
     41  float    height;            //!< The height of the Glyph
     42  float    bearingX;          //!< How much is right of the Origin
     43  float    bearingY;          //!< How much is above the Origin
     44  float    advance;           //!< How big a Glyph would be in monospace-mode
    2345
    24     inline GLuint getTexture() const { return this->texture; };
    25     /** @returns true if texture has alpha, false otherwise */
    26     inline bool hasAlpha() const  {return this->bAlpha; }
    27     /** @returns the stored image of this Texture */
    28     const SDL_Surface* const getStoredImage() const { return this->image; };
    29 
    30     bool loadSurface(SDL_Surface* surface, GLenum target = GL_TEXTURE_2D);
    31 
    32     bool rebuild();
    33 
    34     bool setSurface(SDL_Surface* newSurface);
    35     /** @returns true if the Surface has an Alpha Value. */
    36     bool setAlpha(bool hasAlpha) { this->bAlpha = hasAlpha; return this->bAlpha; };
    37     bool setTexture(GLuint texture);
    38 
    39   private:
    40     GLuint           texture;            //!< The Texture-ID of opengl from this Texture.
    41     bool             bAlpha;             //!< if the texture has an alpha channel.
    42     SDL_Surface*     image;              //!< The SDL_Surfce that stores the Texture on it.
     46  GLfloat texCoord[4];        //!< Texture coordinates: 0:left, 1:right, 2: top, 3: bottom.
    4347};
    4448
    45 #endif /* _TEXTURE_DATA_H */
     49
     50class FontData : public TextureData
     51{
     52  friend class Font;
     53public:
     54  FontData();
     55  ~FontData();
     56
     57
     58  /** @returns a Pointer to the Array of Glyphs */
     59  inline Glyph** getGlyphArray() const { return this->glyphArray; };
     60  /** @returns the a pointer to the TTF */
     61  inline TTF_Font* getTTF() const { return this->fontTTF; };
     62
     63  bool rebuild();
     64
     65
     66private:
     67  TTF_Font*     fontTTF;             //!< The font we use for this.
     68  int           renderStyle;         //!< The Renderstyle
     69  unsigned int  renderSize;          //!< How big the Font should be rendered.
     70
     71  Glyph**       glyphArray;          //!< An Array of all the Glyphs stored in the Array of Glyphs.
     72};
     73
     74typedef CountPointer<FontData> fontDataPointer;
     75
     76#endif /* _FONT_DATA_H */
Note: See TracChangeset for help on using the changeset viewer.