Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7450 in orxonox.OLD for trunk


Ignore:
Timestamp:
Apr 29, 2006, 9:38:44 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: added MultiLineText a Text for multiple line-input, that should automatically shilft position

Location:
trunk/src
Files:
6 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/defs/class_id.h

    r7393 r7450  
    259259  CL_TEXTURE_SEQUENCE           =    0x00802000,
    260260  CL_TEXT                       =    0x00804000,
     261  CL_MULTI_LINE_TEXT            =    0x00000840,
    261262
    262263  // Texutres.
  • trunk/src/lib/graphics/Makefile.am

    r7165 r7450  
    44noinst_LIBRARIES = libORXgraphics.a
    55
    6 libORXgraphics_a_SOURCES = graphics_engine.cc \
    7                            light.cc \
    8                            shader.cc \
    9                            \
    10                            render2D/render_2d.cc \
    11                            render2D/element_2d.cc \
    12                            render2D/billboard.cc \
    13                            \
    14                            text_engine/text_engine.cc \
    15                            text_engine/text.cc \
    16                            text_engine/font.cc \
    17                            \
    18                            effects/graphics_effect.cc \
    19                            effects/fog_effect.cc \
    20                            effects/lense_flare.cc
     6libORXgraphics_a_SOURCES = \
     7                graphics_engine.cc \
     8                light.cc \
     9                shader.cc \
     10                \
     11                render2D/render_2d.cc \
     12                render2D/element_2d.cc \
     13                render2D/billboard.cc \
     14                \
     15                text_engine/text_engine.cc \
     16                text_engine/text.cc \
     17                text_engine/multi_line_text.cc \
     18                text_engine/font.cc \
     19                \
     20                effects/graphics_effect.cc \
     21                effects/fog_effect.cc \
     22                effects/lense_flare.cc
    2123
    2224
    23 noinst_HEADERS = graphics_engine.h \
    24                  light.h \
    25                  shader.h \
    26                  \
    27                  render2D/render_2d.h \
    28                  render2D/element_2d.h \
    29                  render2D/billboard.h \
    30                  \
    31                  text_engine/text_engine.h \
    32                  text_engine/text.h \
    33                  text_engine/font.h \
    34                  text_engine/default_font.xpm \
    35                  \
    36                  effects/graphics_effect.h \
    37                  effects/fog_effect.h \
    38                  effects/lense_flare.h
     25noinst_HEADERS = \
     26                graphics_engine.h \
     27                light.h \
     28                shader.h \
     29                \
     30                render2D/render_2d.h \
     31                render2D/element_2d.h \
     32                render2D/billboard.h \
     33                \
     34                text_engine/text_engine.h \
     35                text_engine/text.h \
     36                text_engine/multi_line_text.cc \
     37                text_engine/font.h \
     38                text_engine/default_font.xpm \
     39                \
     40                effects/graphics_effect.h \
     41                effects/fog_effect.h \
     42                effects/lense_flare.h
    3943
    4044
  • trunk/src/lib/graphics/text_engine/font.cc

    r7449 r7450  
    307307
    308308/**
    309  * initializes the default font
     309 * @brief initializes the default font
    310310 */
    311311void Font::initDefaultFont()
     
    316316
    317317/**
    318  * deletes the default font
     318 * @brief deletes the default font
    319319 */
    320320void Font::removeDefaultFont()
     
    334334    return TTF_FontHeight(this->fontTTF);
    335335  else
    336     return 0;
     336    return 1;
    337337}
    338338
  • trunk/src/lib/graphics/text_engine/font.h

    r7449 r7450  
    7272    inline TTF_Font* getTTF() const { return this->fontTTF; };
    7373
     74    int getMaxHeight() const;
     75    int getMaxAscent() const;
     76    int getMaxDescent() const;
     77
    7478    /** @returns the default Font */
    7579    inline static Font* getDefaultFont() { if (Font::defaultFont == NULL) initDefaultFont(); return Font::defaultFont; };
     
    8185  private:
    8286    void init();
    83     int getMaxHeight() const;
    84     int getMaxAscent() const;
    85     int getMaxDescent() const;
    8687    bool getGlyphMetrics(Glyph* glyph, Uint16 character);
    8788
  • trunk/src/lib/graphics/text_engine/multi_line_text.cc

    r7449 r7450  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
    1717
    18 #include "text.h"
     18#include "multi_line_text.h"
    1919#include "font.h"
    20 
    21 #include "util/loading/resource_manager.h"
    22 #include "debug.h"
    2320
    2421using namespace std;
     
    2926 * @param type The renderType to display this font in
    3027 */
    31 Text::Text(const std::string& fontFile, unsigned int textSize)
     28MultiLineText::MultiLineText(const std::string& fontFile, unsigned int textSize)
     29  : Text(fontFile, textSize)
    3230{
    33   this->setClassID(CL_TEXT, "Text");
     31  this->setClassID(CL_MULTI_LINE_TEXT, "MultiLineText");
    3432
    35   // initialize this Text
    36   this->font = NULL;
    37   this->text = "";
    38   this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
    39   this->blending = TEXT_DEFAULT_BLENDING;
    40   this->color = TEXT_DEFAULT_COLOR;
    41   this->setSize(TEXT_DEFAULT_SIZE);
    42   this->setText("");
    43 
    44 
    45   if (!fontFile.empty())
    46     this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
    47   this->setSizeY2D(textSize);
     33  this->lineSpacing = 1.0f;
     34  this->lineWidth = 100.0f;
     35  this->setupTextWidth();
    4836}
    4937
    5038/**
    51  * @brief deletes a Text out of memory
     39 * @brief sets the maximum Line width
     40 * @param lineWidth the maximum lineWidth.
    5241 */
    53 Text::~Text()
     42void MultiLineText::setLineWidth(float lineWidth)
    5443{
    55   if (this->font != NULL && this->font != Font::getDefaultFont())
    56     ResourceManager::getInstance()->unload(this->font);
     44  this->lineWidth = lineWidth;
     45  this->setupTextWidth();
    5746}
    5847
    5948
    6049/**
    61  * @brief sets the Font of this Text to font from fontFile
    62  * @param fontFile the File to load the Font from.
    63  * @param fontSize the Size of the Font
    64  */
    65 void Text::setFont(const std::string& fontFile, unsigned int fontSize)
    66 {
    67   Font* newFont;
    68   Font* oldFont = this->font;
    69 
    70   // load a new Font
    71   if (!fontFile.empty())
    72   {
    73     newFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
    74     if (newFont == NULL)
    75     {
    76       newFont = Font::getDefaultFont();
    77       PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
    78     }
    79   }
    80   else
    81     newFont = Font::getDefaultFont();
    82 
    83   // unloading the Font if we alrady have one loaded.
    84   this->font = newFont;
    85   if (oldFont != NULL && oldFont != Font::getDefaultFont())
    86     ResourceManager::getInstance()->unload(oldFont);
    87 }
    88 
    89 /**
    90  * @brief Sets a new Text to the font
    91  * @param text the new text to set
    92  */
    93 void Text::setText(const std::string& text)
    94 {
    95   this->text = text;
    96 
    97   // setting up the Text-Width if DYNAMIC
    98   //  if (this->type & TEXT_RENDER_DYNAMIC && this->getAlignment() != TEXT_ALIGN_LEFT && this->font != NULL)
    99   const Font* calcSizeFont = this->font;
    100   if (calcSizeFont != NULL || (calcSizeFont = Font::getDefaultFont()) != NULL)
    101   {
    102     Glyph** glyphArray = calcSizeFont->getGlyphArray();
    103 
    104     float width = 0;
    105     if (!this->text.empty())
    106     {
    107       for (unsigned int i = 0; i < this->text.size(); i++)
    108       {
    109         if(glyphArray[this->text[i]] != NULL)
    110         {
    111           width += glyphArray[this->text[i]]->advance;
    112         }
    113       }
    114       this->setSizeX2D(width *this->getSizeY2D());
    115     }
    116   }
    117 }
    118 
    119 /**
    12050 * @brief draws the Text
    12151 */
    122 void Text::draw() const
     52void MultiLineText::draw() const
    12353{
    124   if (unlikely(this->text.empty()))
     54  if (unlikely(this->getText().empty()))
    12555    return;
    12656  glPushMatrix();
    12757  // transform for alignment.
     58  // TODO make the Stuff with the alignment
    12859  if (this->getAlignment() == TEXT_ALIGN_RIGHT)
    12960    glTranslatef(-this->getSizeX2D(), 0, 0);
     
    13364  // drawing this Text.
    13465  // setting the Blending effects
    135   glColor4f(this->color.x, this->color.y, this->color.z, this->blending);
     66  glColor4f(this->getColor().x, this->getColor().y, this->getColor().z, this->getBlending());
    13667  glEnable(GL_BLEND);
    13768  glEnable(GL_TEXTURE_2D);
     
    13970  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
    14071
    141   Glyph** glyphArray = this->font->getGlyphArray();
    142   glBindTexture(GL_TEXTURE_2D, font->getTexture());
    143   glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
     72  glBindTexture(GL_TEXTURE_2D, this->getFont()->getTexture());
     73  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
    14474  glRotatef(this->getAbsDir2D(), 0, 0, 1);
    14575
    14676  Glyph* tmpGlyph;
    14777  float posX = 0.0f;
     78  float posY = 0.0f;
     79  unsigned int lineNumber = 0;
     80
    14881  glBegin(GL_QUADS);
    149   for (unsigned int i = 0; i < this->text.size(); i++)
     82  for (unsigned int i = 0; i < this->getText().size(); i++)
    15083  {
    151     if(likely((tmpGlyph = glyphArray[this->text[i]]) != NULL))
     84    if (unlikely(tmpGlyph->character == '\n' || i == this->lineEnds[lineNumber]))
     85    {
     86    // go to the next Line.
     87      lineNumber++;
     88      posX = 0.0f;
     89      posY += this->lineSpacing + this->getFont()->getMaxHeight();
     90    }
     91
     92    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->getText()[i]]) != NULL))
    15293    {
    15394      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
    154       glVertex2d(posX+tmpGlyph->maxX*this->getSizeY2D(), 0);
     95      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY);
    15596
    15697      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
    157       glVertex2d(posX+tmpGlyph->maxX*this->getSizeY2D(), this->getSizeY2D());
     98      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), posY + this->getSize());
    15899
    159100      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
    160       glVertex2d(posX+tmpGlyph->minX*this->getSizeY2D(), this->getSizeY2D());
     101      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY+ this->getSize());
    161102
    162103      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
    163       glVertex2d(posX+tmpGlyph->minX*this->getSizeY2D(), 0);
     104      glVertex2d(posX+tmpGlyph->minX*this->getSize(), posY);
    164105
    165       posX += tmpGlyph->advance * this->getSizeY2D();
     106      posX += tmpGlyph->advance * this->getSize();
    166107    }
    167108  }
     
    171112
    172113/**
    173  * @brief prints out some nice debug information about this text
     114 * @brief setting up the Text-Width if DYNAMIC
    174115 */
    175 void Text::debug() const
     116void MultiLineText::setupTextWidth()
    176117{
    177   PRINT(0)("=== TEXT: %s ===\n", this->text.c_str());
    178   PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
     118  this->lineEnds.clear();
     119  float width = 0.0f;
     120  // TODO make size local to this (not using the one from Element2D.
     121  float maxWidth = this->lineWidth / this->getSize();
     122
     123  for (unsigned int i = 0; i < this->getText().size(); i++)
     124  {
     125    if (width > maxWidth || this->getText()[i] == '\n')
     126    {
     127      this->lineEnds.push_back(i);
     128      width = 0.0f;
     129    }
     130    // Advance the Text.
     131    if(this->getFont()->getGlyphArray()[this->getText()[i]] != NULL)
     132      width += this->getFont()->getGlyphArray()[this->getText()[i]]->advance;
     133  }
    179134}
    180 
    181 
    182 ////////////
    183 /// UTIL ///
    184 ////////////
    185 /**
    186  * @brief Loads a Font from an SDL_surface into a texture.
    187  * @param surface The surface to make the texture of
    188  * @param texCoord The texture coordinates of the 4 corners of the texture
    189  * @returns the ID of the texture
    190  */
    191 GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord)
    192 {
    193   GLuint texture;
    194   int w, h;
    195   SDL_Surface *image;
    196   SDL_Rect area;
    197   Uint32 saved_flags;
    198   Uint8  saved_alpha;
    199 
    200   /* Use the surface width and height expanded to powers of 2 */
    201   w = powerOfTwo(surface->w);
    202   h = powerOfTwo(surface->h);
    203   if (texCoord != NULL)
    204   {
    205     texCoord->minU = 0.0f;
    206     texCoord->minV = 0.0f;
    207     texCoord->maxU = (GLfloat)surface->w / w;
    208     texCoord->maxV = (GLfloat)surface->h / h;
    209   }
    210   image = SDL_CreateRGBSurface(SDL_SWSURFACE,
    211                                w, h,
    212                                32,
    213 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    214                                0x000000FF,
    215                                0x0000FF00,
    216                                0x00FF0000,
    217                                0xFF000000
    218 #else
    219                                0xFF000000,
    220                                0x00FF0000,
    221                                0x0000FF00,
    222                                0x000000FF
    223 #endif
    224                               );
    225   if ( image == NULL )
    226   {
    227     return 0;
    228   }
    229 
    230   /* Save the alpha blending attributes */
    231   saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
    232   saved_alpha = surface->format->alpha;
    233   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    234   {
    235     SDL_SetAlpha(surface, 0, 0);
    236   }
    237 
    238   /* Copy the surface into the GL texture image */
    239   area.x = 0;
    240   area.y = 0;
    241   area.w = surface->w;
    242   area.h = surface->h;
    243   SDL_BlitSurface(surface, &area, image, &area);
    244 
    245   /* Restore the alpha blending attributes */
    246   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    247   {
    248     SDL_SetAlpha(surface, saved_flags, saved_alpha);
    249   }
    250 
    251   /* Create an OpenGL texture for the image */
    252   glGenTextures(1, &texture);
    253   glBindTexture(GL_TEXTURE_2D, texture);
    254   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    255   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    256   glTexImage2D(GL_TEXTURE_2D,
    257                0,
    258                GL_RGBA,
    259                w, h,
    260                0,
    261                GL_RGBA,
    262                GL_UNSIGNED_BYTE,
    263                image->pixels);
    264   SDL_FreeSurface(image); /* No longer needed the data */
    265 
    266   return texture;
    267 }
    268 
    269 /**
    270  * @brief Quick utility function for texture creation
    271  * @param input an integer
    272  * @returns the next bigger 2^n-integer than input
    273  */
    274 int Text::powerOfTwo(int input)
    275 {
    276   int value = 1;
    277 
    278   while ( value < input )
    279     value <<= 1;
    280   return value;
    281 }
  • trunk/src/lib/graphics/text_engine/multi_line_text.h

    r7449 r7450  
    11/*!
    2  * @file text.h
     2 * @file multi_line_text.h
    33 * @brief Definition of a text Class, that is able to render text.
    44 */
    55
    6 #ifndef _TEXT_H
    7 #define _TEXT_H
     6#ifndef _MULTI_LINE_TEXT_H
     7#define _MULTI_LINE_TEXT_H
    88
    9 #include "element_2d.h"
     9#include "text.h"
    1010
    11 #include "glincl.h"
    12 
    13 #define  TEXT_ALIGN_LEFT             E2D_ALIGN_LEFT
    14 #define  TEXT_ALIGN_RIGHT            E2D_ALIGN_RIGHT
    15 #define  TEXT_ALIGN_CENTER           E2D_ALIGN_CENTER
    16 #define  TEXT_ALIGN_SCREEN_CENTER    E2D_ALIGN_SCREEN_CENTER
    17 #define  TEXT_DEFAULT_COLOR          Vector(1.0, 1.0, 1.0)      //!< the default Color (white)
    18 #define  TEXT_DEFAULT_BLENDING       1.0f                       //!< the default blending of the text, (no blending at all)
    19 
    20 #define  TEXT_DEFAULT_ALIGNMENT      TEXT_ALIGN_LEFT            //!< default alignment
    21 #define  TEXT_DEFAULT_SIZE           20                         //!< default size of the Text
    22 
    23 
    24 // FORWARD DECLARATION
    25 class Font;
    26 struct SDL_Surface;
    27 
    28 //! A Struct to handel Texture Coordinates for quads
    29 struct TexCoord
    30 {
    31   float    minU;              //!< The minimum U-Coordinate
    32   float    maxU;              //!< The maximum U-Coordinate
    33   float    minV;              //!< The minimum V-Coordinate
    34   float    maxV;              //!< The maximum V-Coordinate
    35 };
     11#include <vector>
    3612
    3713//! Represents one textElement.
    38 class Text : public Element2D
     14class MultiLineText : public Text
    3915{
    4016  public:
    41     Text(const std::string& fontFile = "", unsigned int fontSize = TEXT_DEFAULT_SIZE);
    42     virtual ~Text();
     17    MultiLineText(const std::string& fontFile = "", unsigned int fontSize = TEXT_DEFAULT_SIZE);
    4318
    44     void setFont(const std::string& fontFile, unsigned int renderSize);
     19    // Setup:
     20    void setLineWidth(float lineWidth);
     21    /** @param lineSpacing: the Spacing between the lines */
     22    void setLineSpacing(float lineSpacing) { this->lineSpacing = lineSpacing; };
    4523
    46     void setText(const std::string& text);
    47 
    48     /** @returns the String this Text displays */
    49     inline const std::string& getText() const { return this->text; };
    50     /** @param blending the blending intensity to set (between 0.0 and 1.0) */
    51     inline void setBlending(float blending) { this->blending = blending; };
    52     /** sets the Color of the Text to render (values in [0-1]) @param r red @param g green @param b blue */
    53     void setColor(float r, float g, float b) { this->color = Vector(r, g, b); };
    54     /** sets the Size of the Font */
    55     void setSize(float size) { this->setSizeY2D(size); };
    56     /** @returns the Size of the Text */
    57 //    void getSize(float &x, float& y) const { return this->size; };
     24    // Retrieve:
     25    /** @returns the LineWidth (maximum distance from the left to the right */
     26    inline float getLineWidth() const { return this->lineWidth; };
     27    /** @returns the LineSpacing */
     28    inline float getLineSpacing() const { return this->lineSpacing; };
    5829
    5930    virtual void draw() const;
    6031
    61     void debug() const;
    62 
    63   // helpers.
    64     static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
    65     static int powerOfTwo(int input);
     32  protected:
     33    virtual void setupTextWidth();
    6634
    6735  private:
    68     Font*             font;           //!< Font of this text
    69 
    70     std::string       text;           //!< The text to display
    71     Vector            color;          //!< The color of the font.
    72     float             blending;       //!< The blending intensity.
     36    float                      lineWidth;
     37    float                      lineSpacing;
     38    std::vector<unsigned int>  lineEnds;
    7339};
    7440
    75 #endif /* _TEXT_H */
     41#endif /* _MULTI_LINE_TEXT_H */
  • trunk/src/lib/graphics/text_engine/text.cc

    r7448 r7450  
    8585  if (oldFont != NULL && oldFont != Font::getDefaultFont())
    8686    ResourceManager::getInstance()->unload(oldFont);
     87
     88  this->setupTextWidth();
    8789}
    8890
     
    9597  this->text = text;
    9698
    97   // setting up the Text-Width if DYNAMIC
    98   //  if (this->type & TEXT_RENDER_DYNAMIC && this->getAlignment() != TEXT_ALIGN_LEFT && this->font != NULL)
    99   const Font* calcSizeFont = this->font;
    100   if (calcSizeFont != NULL || (calcSizeFont = Font::getDefaultFont()) != NULL)
    101   {
    102     Glyph** glyphArray = calcSizeFont->getGlyphArray();
    103 
    104     float width = 0;
    105     if (!this->text.empty())
    106     {
    107       for (unsigned int i = 0; i < this->text.size(); i++)
    108       {
    109         if(glyphArray[this->text[i]] != NULL)
    110         {
    111           width += glyphArray[this->text[i]]->advance;
    112         }
    113       }
    114       this->setSizeX2D(width *this->getSizeY2D());
    115     }
    116   }
     99  this->setupTextWidth();
    117100}
    118101
     
    139122  glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, GL_MODULATE );
    140123
    141   Glyph** glyphArray = this->font->getGlyphArray();
    142124  glBindTexture(GL_TEXTURE_2D, font->getTexture());
    143125  glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
     
    149131  for (unsigned int i = 0; i < this->text.size(); i++)
    150132  {
    151     if(likely((tmpGlyph = glyphArray[this->text[i]]) != NULL))
     133    if(likely((tmpGlyph = this->getFont()->getGlyphArray()[this->text[i]]) != NULL))
    152134    {
    153135      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
    154       glVertex2d(posX+tmpGlyph->maxX*this->getSizeY2D(), 0);
     136      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), 0);
    155137
    156138      glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[3]);
    157       glVertex2d(posX+tmpGlyph->maxX*this->getSizeY2D(), this->getSizeY2D());
     139      glVertex2d(posX+tmpGlyph->maxX*this->getSize(), this->getSize());
    158140
    159141      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[3]);
    160       glVertex2d(posX+tmpGlyph->minX*this->getSizeY2D(), this->getSizeY2D());
     142      glVertex2d(posX+tmpGlyph->minX*this->getSize(), this->getSize());
    161143
    162144      glTexCoord2f(tmpGlyph->texCoord[0], tmpGlyph->texCoord[2]);
    163       glVertex2d(posX+tmpGlyph->minX*this->getSizeY2D(), 0);
     145      glVertex2d(posX+tmpGlyph->minX*this->getSize(), 0);
    164146
    165       posX += tmpGlyph->advance * this->getSizeY2D();
     147      posX += tmpGlyph->advance * this->getSize();
    166148    }
    167149  }
     
    170152}
    171153
     154
     155/**
     156 * @brief setting up the Text-Width.
     157 */
     158void Text::setupTextWidth()
     159{
     160  float width = 0;
     161  for (unsigned int i = 0; i < this->text.size(); i++)
     162    if(this->font->getGlyphArray()[this->text[i]] != NULL)
     163      width += this->font->getGlyphArray()[this->text[i]]->advance;
     164  this->setSizeX2D(width *this->getSize());
     165}
     166
     167
    172168/**
    173169 * @brief prints out some nice debug information about this text
     
    175171void Text::debug() const
    176172{
    177   PRINT(0)("=== TEXT: %s ===\n", this->text.c_str());
     173  PRINT(0)("=== TEXT: %s (with Font:'%s')  displaying %s ===\n", this->getName(), this->font->getName(), this->text.c_str());
    178174  PRINT(0)("Color: %0.2f %0.2f %0.2f\n", this->color.x, this->color.y, this->color.z);
    179175}
    180176
    181 
    182 ////////////
    183 /// UTIL ///
    184 ////////////
    185 /**
    186  * @brief Loads a Font from an SDL_surface into a texture.
    187  * @param surface The surface to make the texture of
    188  * @param texCoord The texture coordinates of the 4 corners of the texture
    189  * @returns the ID of the texture
    190  */
    191 GLuint Text::loadTexture(SDL_Surface *surface, TexCoord* texCoord)
    192 {
    193   GLuint texture;
    194   int w, h;
    195   SDL_Surface *image;
    196   SDL_Rect area;
    197   Uint32 saved_flags;
    198   Uint8  saved_alpha;
    199 
    200   /* Use the surface width and height expanded to powers of 2 */
    201   w = powerOfTwo(surface->w);
    202   h = powerOfTwo(surface->h);
    203   if (texCoord != NULL)
    204   {
    205     texCoord->minU = 0.0f;
    206     texCoord->minV = 0.0f;
    207     texCoord->maxU = (GLfloat)surface->w / w;
    208     texCoord->maxV = (GLfloat)surface->h / h;
    209   }
    210   image = SDL_CreateRGBSurface(SDL_SWSURFACE,
    211                                w, h,
    212                                32,
    213 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    214                                0x000000FF,
    215                                0x0000FF00,
    216                                0x00FF0000,
    217                                0xFF000000
    218 #else
    219                                0xFF000000,
    220                                0x00FF0000,
    221                                0x0000FF00,
    222                                0x000000FF
    223 #endif
    224                               );
    225   if ( image == NULL )
    226   {
    227     return 0;
    228   }
    229 
    230   /* Save the alpha blending attributes */
    231   saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
    232   saved_alpha = surface->format->alpha;
    233   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    234   {
    235     SDL_SetAlpha(surface, 0, 0);
    236   }
    237 
    238   /* Copy the surface into the GL texture image */
    239   area.x = 0;
    240   area.y = 0;
    241   area.w = surface->w;
    242   area.h = surface->h;
    243   SDL_BlitSurface(surface, &area, image, &area);
    244 
    245   /* Restore the alpha blending attributes */
    246   if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
    247   {
    248     SDL_SetAlpha(surface, saved_flags, saved_alpha);
    249   }
    250 
    251   /* Create an OpenGL texture for the image */
    252   glGenTextures(1, &texture);
    253   glBindTexture(GL_TEXTURE_2D, texture);
    254   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    255   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    256   glTexImage2D(GL_TEXTURE_2D,
    257                0,
    258                GL_RGBA,
    259                w, h,
    260                0,
    261                GL_RGBA,
    262                GL_UNSIGNED_BYTE,
    263                image->pixels);
    264   SDL_FreeSurface(image); /* No longer needed the data */
    265 
    266   return texture;
    267 }
    268 
    269 /**
    270  * @brief Quick utility function for texture creation
    271  * @param input an integer
    272  * @returns the next bigger 2^n-integer than input
    273  */
    274 int Text::powerOfTwo(int input)
    275 {
    276   int value = 1;
    277 
    278   while ( value < input )
    279     value <<= 1;
    280   return value;
    281 }
  • trunk/src/lib/graphics/text_engine/text.h

    r7355 r7450  
    88
    99#include "element_2d.h"
    10 
    11 #include "glincl.h"
    1210
    1311#define  TEXT_ALIGN_LEFT             E2D_ALIGN_LEFT
     
    2119#define  TEXT_DEFAULT_SIZE           20                         //!< default size of the Text
    2220
    23 
    2421// FORWARD DECLARATION
    2522class Font;
    2623struct SDL_Surface;
    27 
    28 //! A Struct to handel Texture Coordinates for quads
    29 struct TexCoord
    30 {
    31   float    minU;              //!< The minimum U-Coordinate
    32   float    maxU;              //!< The maximum U-Coordinate
    33   float    minV;              //!< The minimum V-Coordinate
    34   float    maxV;              //!< The maximum V-Coordinate
    35 };
    3624
    3725//! Represents one textElement.
     
    4230    virtual ~Text();
    4331
     32    // SETUP
    4433    void setFont(const std::string& fontFile, unsigned int renderSize);
     34    void setText(const std::string& text);
     35    /** @param blending the blending intensity to set (between 0.0 and 1.0) */
     36    inline void setBlending(float blending) { this->blending = blending; };
     37    /** @param r red @param g green @param b blue @brief sets the Color of the Text to render (values in [0-1]) */
     38    void setColor(float r, float g, float b) { this->color = Vector(r, g, b); };
     39    /** @param size the Size of the Text @brief sets the Size of the Font */
     40    inline void setSize(float size) { this->setSizeY2D(size); };
    4541
    46     void setText(const std::string& text);
    47 
     42    /// RETRIEVE
     43    /** @returns the pointer to the stored Font (not changeable) */
     44    inline const Font* const getFont() const { return this->font; };
    4845    /** @returns the String this Text displays */
    4946    inline const std::string& getText() const { return this->text; };
    50     /** @param blending the blending intensity to set (between 0.0 and 1.0) */
    51     inline void setBlending(float blending) { this->blending = blending; };
    52     /** sets the Color of the Text to render (values in [0-1]) @param r red @param g green @param b blue */
    53     void setColor(float r, float g, float b) { this->color = Vector(r, g, b); };
    54     /** sets the Size of the Font */
    55     void setSize(float size) { this->setSizeY2D(size); };
     47    /** @returns the Blending Value [0 invisible 1.0 full visible */
     48    inline float getBlending() const { return this->blending; };
     49    /** @returns: a Vector(r,g,b) @brief: retrieve a Vector holding the Color of the Text */
     50    inline const Vector& getColor() const { return this->color; };
    5651    /** @returns the Size of the Text */
    57 //    void getSize(float &x, float& y) const { return this->size; };
     52    inline float getSize() const { return this->getSizeY2D(); };
    5853
    5954    virtual void draw() const;
     
    6156    void debug() const;
    6257
    63   // helpers.
    64     static GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
    65     static int powerOfTwo(int input);
    66 
     58  protected:
     59    virtual void setupTextWidth();
    6760  private:
    6861    Font*             font;           //!< Font of this text
Note: See TracChangeset for help on using the changeset viewer.