Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7221 in orxonox.OLD for trunk/src/lib/graphics/text_engine


Ignore:
Timestamp:
Mar 15, 2006, 3:10:45 PM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

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

Legend:

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

    r6609 r7221  
    3737 * @param fontSize the Size of the Font in Pixels
    3838 */
    39 Font::Font(const char* fontFile, unsigned int renderSize)
     39Font::Font(const std::string& fontFile, unsigned int renderSize)
    4040{
    4141  this->init();
     
    4444  this->setStyle("c");
    4545
    46   if (fontFile != NULL)
     46  if (!fontFile.empty())
    4747    this->loadFontFromTTF(fontFile);
    4848}
     
    5252 * @param imageFile the ImageFile to load the Font From.
    5353 */
    54 Font::Font(const char* imageFile)
     54Font::Font(const std::string& imageFile)
    5555{
    5656  this->init();
     
    5858  //  this->setSize(fontSize);
    5959  SDL_Surface* image = NULL;
    60   if (imageFile != NULL)
    61     image = IMG_Load(imageFile);
     60  if (!imageFile.empty())
     61    image = IMG_Load(imageFile.c_str());
    6262  else
    6363    return;
     
    6868  }
    6969  else
    70     PRINTF(1)("loading from surface %s failed: %s\n", imageFile, IMG_GetError());
     70    PRINTF(1)("loading from surface %s failed: %s\n", imageFile.c_str(), IMG_GetError());
    7171}
    7272
     
    114114
    115115  //! @todo check if we really do not need to delete the fastTextureID here.
    116 //   if (this->fastTextureID != 0)
    117 //     if(glIsTexture(this->fastTextureID))
    118 //       glDeleteTextures(1, &this->fastTextureID);
     116  //   if (this->fastTextureID != 0)
     117  //     if(glIsTexture(this->fastTextureID))
     118  //       glDeleteTextures(1, &this->fastTextureID);
    119119
    120120  // erease this font out of the memory.
     
    140140 * @returns true if loaded, false if something went wrong, or if a font was loaded before.
    141141 */
    142 bool Font::loadFontFromTTF(const char* fontFile)
     142bool Font::loadFontFromTTF(const std::string& fontFile)
    143143{
    144144  // checking for existent Font.
     
    222222 *   i: italic, b: bold, u, underline
    223223 */
    224 void Font::setStyle(const char* renderStyle)
     224void Font::setStyle(const std::string& renderStyle)
    225225{
    226226  this->renderStyle = TTF_STYLE_NORMAL;
    227227
    228   for (int i = 0; i < strlen(renderStyle); i++)
    229     if (strncmp(renderStyle+i, "b", 1) == 0)
     228  for (int i = 0; i < renderStyle.size(); i++)
     229  {
     230    if (renderStyle[i] == 'b')
    230231      this->renderStyle |= TTF_STYLE_BOLD;
    231   else if (strncmp(renderStyle+i, "i", 1) == 0)
    232     this->renderStyle |= TTF_STYLE_ITALIC;
    233   else if (strncmp(renderStyle+i, "u", 1) == 0)
    234     this->renderStyle |= TTF_STYLE_UNDERLINE;
    235 
     232    else if (renderStyle[i] == 'i')
     233      this->renderStyle |= TTF_STYLE_ITALIC;
     234    else if (renderStyle[i] == 'u')
     235      this->renderStyle |= TTF_STYLE_UNDERLINE;
     236  }
    236237  if (likely(this->fontTTF != NULL))
    237238    TTF_SetFontStyle(this->fontTTF, this->renderStyle);
    238 //  else
    239 //    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
     239  //  else
     240  //    PRINTF(2)("Font was not initialized, please do so before setting the Font-Style.\n");
    240241}
    241242
     
    247248 * @param fileName the File to write the image into.
    248249 */
    249 void Font::createAsciiImage(const char* fileName, unsigned int size) const
     250void Font::createAsciiImage(const std::string& fileName, unsigned int size) const
    250251{
    251252  if (this->fontTTF == NULL)
     
    258259  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
    259260  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_SWSURFACE,
    260                                                height*size, height*size,
    261                                                32,
     261                          height*size, height*size,
     262                          32,
    262263#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    263                                                0x000000FF,
    264                                                0x0000FF00,
    265                                                0x00FF0000,
    266                                                0xFF000000
     264                          0x000000FF,
     265                          0x0000FF00,
     266                          0x00FF0000,
     267                          0xFF000000
    267268#else
    268                                                0xFF000000,
    269                                                0x00FF0000,
    270                                                0x0000FF00,
    271                                                0x000000FF
     269                          0xFF000000,
     270                          0x00FF0000,
     271                          0x0000FF00,
     272                          0x000000FF
    272273#endif
    273274                                              );
     
    299300    }
    300301  }
    301   SDL_SaveBMP(tmpSurf, fileName);
     302  SDL_SaveBMP(tmpSurf, fileName.c_str());
    302303  SDL_FreeSurface(tmpSurf);
    303304}
     
    409410
    410411  this->initGlyphs(32, numberOfGlyphs);
    411 //  this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
     412  //  this->glyphArray[32]->width = .5f; //!< @todo find out the real size of a Space
    412413
    413414  int rectSize = this->findOptimalFastTextureSize();
     
    418419  SDL_Rect tmpRect; // this represents a Rectangle for blitting.
    419420  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(SDL_SWSURFACE,
    420                                                rectSize, rectSize,
    421                                                32,
     421                          rectSize, rectSize,
     422                          32,
    422423#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
    423                                                0x000000FF,
    424                                                0x0000FF00,
    425                                                0x00FF0000,
    426                                                0xFF000000
     424                          0x000000FF,
     425                          0x0000FF00,
     426                          0x00FF0000,
     427                          0xFF000000
    427428#else
    428                                                0xFF000000,
    429                                                0x00FF0000,
    430                                                0x0000FF00,
    431                                                0x000000FF
     429                          0xFF000000,
     430                          0x00FF0000,
     431                          0x0000FF00,
     432                          0x000000FF
    432433#endif
    433434                                              );
     
    457458        break;
    458459      }
    459           // reading in the new Glyph
     460      // reading in the new Glyph
    460461      if (likely(this->fontTTF != NULL))
    461462      {
     
    521522  for (int i = from; i <= lastGlyph; i++)
    522523  {
    523       // setting up all the Glyphs we like.
     524    // setting up all the Glyphs we like.
    524525    glyphArray[i] = getGlyphMetrics(i);
    525526  }
     
    555556      if((tmpGlyph = this->glyphArray[i]) != NULL)
    556557      {
    557               // getting the height of the highest Glyph in the Line.
     558        // getting the height of the highest Glyph in the Line.
    558559        if (tmpGlyph->height*this->renderSize > maxLineHeight)
    559560          maxLineHeight = (int)(tmpGlyph->height*this->renderSize);
     
    563564          x = 0;
    564565          y = y + maxLineHeight;
    565                   //maxLineHeight = 0;
     566          //maxLineHeight = 0;
    566567        }
    567568        if (y + maxLineHeight + 1 > size)
     
    592593  if(style==TTF_STYLE_NORMAL)
    593594    PRINTF(0)(" normal");
    594   else {
     595  else
     596  {
    595597    if(style&TTF_STYLE_BOLD)
    596598      PRINTF(0)(" bold");
  • trunk/src/lib/graphics/text_engine/font.h

    r6609 r7221  
    5555{
    5656  public:
    57     Font(const char* fontFile,
     57    Font(const std::string& fontFile,
    5858         unsigned int renderSize);
    59     Font(const char* imageFile);
     59    Font(const std::string& imageFile);
    6060    Font(char** xpmArray);
    6161    virtual ~Font();
     
    6464
    6565  //  font
    66     bool loadFontFromTTF(const char* fontFile);
     66    bool loadFontFromTTF(const std::string& fontFile);
    6767    bool loadFontFromSDL_Surface(SDL_Surface* surface);
    6868
    69     void setStyle(const char* renderStyle);
     69    void setStyle(const std::string& renderStyle);
    7070
    7171    /** @returns a Pointer to the Array of Glyphs */
     
    7878    inline static Font* getDefaultFont() { if (Font::defaultFont == NULL) initDefaultFont(); return Font::defaultFont; };
    7979
    80     void createAsciiImage(const char* fileName, unsigned int size) const;
     80    void createAsciiImage(const std::string& fileName, unsigned int size) const;
    8181    static void initDefaultFont();
    8282    static void removeDefaultFont();
  • trunk/src/lib/graphics/text_engine/text.cc

    r7193 r7221  
    3232 * @param type The renderType to display this font in
    3333 */
    34 Text::Text(const char* fontFile, unsigned int textSize)
     34Text::Text(const std::string& fontFile, unsigned int textSize)
    3535{
    3636  this->init();
    3737
    38   if (fontFile != NULL)
     38  if (!fontFile.empty())
    3939    this->setFont(fontFile, FONT_DEFAULT_RENDER_SIZE);
    4040  this->setSizeY2D(textSize);
     
    5050  if (this->font != NULL && this->font != Font::getDefaultFont())
    5151    ResourceManager::getInstance()->unload(this->font);
    52 
    53   if (this->text)
    54     delete[] this->text;
    5552}
    5653
     
    6461  // initialize this Text
    6562  this->font = NULL;
    66   this->text = NULL;
    67   this->externText = NULL;
     63  this->text = "";
    6864  this->setAlignment(TEXT_DEFAULT_ALIGNMENT);
    6965  this->blending = TEXT_DEFAULT_BLENDING;
    7066  this->color = TEXT_DEFAULT_COLOR;
    7167  this->setSize(TEXT_DEFAULT_SIZE);
    72   this->setText(NULL);
     68  this->setText("");
    7369}
    7470
     
    7874 * @param fontSize the Size of the Font
    7975 */
    80 void Text::setFont(const char* fontFile, unsigned int fontSize)
     76void Text::setFont(const std::string& fontFile, unsigned int fontSize)
    8177{
    8278  Font* tmpFont;
     
    9086
    9187  // load a new Font
    92   if (fontFile != NULL)
     88  if (!fontFile.empty())
    9389  {
    9490    tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME, (int)fontSize);
     
    9692      this->font = tmpFont;
    9793    else
    98       PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile);
     94      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile.c_str());
    9995  }
    10096}
     
    104100 * @param text the new text to set
    105101 */
    106 void Text::setText(const char* text, bool isExtern)
    107 {
    108   if (isExtern)
    109   {
    110     this->externText = text;
    111 
    112     if (unlikely(this->text != NULL))
    113     {
    114       delete[] this->text;
    115       this->text = NULL;
    116     }
    117   }
    118   else
    119   {
    120     this->externText = NULL;
    121     if (this->text)
    122       delete[] this->text;
    123     if (text != NULL)
    124     {
    125       this->text = new char[strlen(text)+1];
    126       strcpy(this->text, text);
    127     }
    128     else
    129       this->text = NULL;
    130   }
     102void Text::setText(const std::string& text)
     103{
     104  this->text = text;
    131105
    132106  // setting up the Text-Width if DYNAMIC
     
    138112
    139113    float width = 0;
    140     const char* tmpText = this->externText;
    141     if (this->externText == NULL)
    142       tmpText = this->text;
    143     if (tmpText != NULL)
     114    if (!this->text.empty())
    144115    {
    145       while (*tmpText != '\0')
     116      for (unsigned int i = 0; i < this->text.size(); i++)
    146117      {
    147         if(glyphArray[*tmpText] != NULL)
     118        if(glyphArray[this->text[i]] != NULL)
    148119        {
    149           width += glyphArray[*tmpText]->advance;
     120          width += glyphArray[this->text[i]]->advance;
    150121        }
    151         tmpText++;
    152122      }
    153123      this->setSizeX2D(width *this->getSizeY2D());
     
    190160    glBindTexture(GL_TEXTURE_2D, Font::getDefaultFont()->getTexture());
    191161  }
    192   const char* tmpText = this->externText;
    193   if (this->externText == NULL)
    194     tmpText = this->text;
    195   if (likely(tmpText != NULL))
     162  if (likely(!this->text.empty()))
    196163  {
    197164    glTranslatef(getAbsCoor2D().x, getAbsCoor2D().y, 0);
     
    201168
    202169    glBegin(GL_QUADS);
    203     while (likely(*tmpText != '\0'))
     170    for (unsigned int i = 0; i < this->text.size(); i++)
    204171    {
    205       if(likely((tmpGlyph = glyphArray[*tmpText]) != NULL))
     172      if(likely((tmpGlyph = glyphArray[this->text[i]]) != NULL))
    206173      {
    207174        glTexCoord2f(tmpGlyph->texCoord[1], tmpGlyph->texCoord[2]);
     
    219186        posX += tmpGlyph->advance * this->getSizeY2D();
    220187      }
    221       ++tmpText;
    222188    }
    223189    glEnd();
     
    232198void Text::debug() const
    233199{
    234   if (this->externText == NULL)
    235     PRINT(0)("=== TEXT: %s ===\n", this->text);
    236   else
    237     PRINT(0)("=== TEXT: %s ===\n", this->externText);
     200  PRINT(0)("=== TEXT: %s ===\n", this->text.c_str());
    238201
    239202  if (this->getBindNode())
  • trunk/src/lib/graphics/text_engine/text.h

    r6981 r7221  
    3939{
    4040  public:
    41     Text(const char* fontFile = NULL, unsigned int fontSize = TEXT_DEFAULT_SIZE);
     41    Text(const std::string& fontFile = "", unsigned int fontSize = TEXT_DEFAULT_SIZE);
    4242    virtual ~Text();
    4343    void init();
    4444
    45     void setFont(const char* fontFile, unsigned int renderSize);
     45    void setFont(const std::string& fontFile, unsigned int renderSize);
    4646
    47     void setText(const char* text, bool isExtern = false);
     47    void setText(const std::string& text);
    4848
    4949    /** @returns the String this Text displays */
    50     inline const char* getText() const { return (externText == NULL)?this->text:this->externText; };
     50    inline const std::string& getText() const { return this->text; };
    5151    /** @param blending the blending intensity to set (between 0.0 and 1.0) */
    5252    inline void setBlending(float blending) { this->blending = blending; };
     
    6969    Font*             font;           //!< Font of this text
    7070
    71     char*             text;           //!< The text to display
    72     const char*       externText;     //!< the text to Display from an external Source.
     71    std::string       text;           //!< The text to display
    7372    Vector            color;          //!< The color of the font.
    7473    float             blending;       //!< The blending intensity.
Note: See TracChangeset for help on using the changeset viewer.