Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3736 in orxonox.OLD


Ignore:
Timestamp:
Apr 6, 2005, 8:35:06 PM (19 years ago)
Author:
bensch
Message:

orxonox/branches/textEngine: some new functions to get the optimum size (sqare) of the FastTexture-Array, and to initialize all the Glyphs

Location:
orxonox/branches/textEngine/src/lib/graphics/font
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc

    r3735 r3736  
    1515   ***
    1616   * This file is extended to the needs of the orxonox-project.         *
    17    * the Copyright of the original file is below this copyright         *
     17   * Originally it came from the glfont.c-example from SDL_ttf.         *
    1818   *                                                                  ***
    1919
     
    2323   adding them to orxonox. This is really important, because we do not
    2424   want to defend anyone.
    25 */
    26 
    27 /*
    28   glfont:  An example of using the SDL_ttf library with OpenGL.
    29   Copyright (C) 1997-2004 Sam Lantinga
    30  
    31   This library is free software; you can redistribute it and/or
    32   modify it under the terms of the GNU Library General Public
    33   License as published by the Free Software Foundation; either
    34   version 2 of the License, or (at your option) any later version.
    35  
    36   This library is distributed in the hope that it will be useful,
    37   but WITHOUT ANY WARRANTY; without even the implied warranty of
    38   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    39   Library General Public License for more details.
    40  
    41   You should have received a copy of the GNU Library General Public
    42   License along with this library; if not, write to the Free
    43   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    44  
    45   The SDL_GL_* functions in this file are available in the public domain.
    46  
    47   Sam Lantinga
    48   slouken@libsdl.org
    4925*/
    5026
     
    7652  delete this->currentText;
    7753
     54  if (this->glyphArray)
     55    {
     56      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
     57        delete this->glyphArray[i];
     58      delete []this->glyphArray;
     59    }
     60
    7861  if (this->font)
    7962    TTF_CloseFont(this->font);
     
    128111  this->font = NULL;
    129112  this->fontFile = NULL;
     113  this->glyphArray = NULL;
    130114
    131115  this->currentText = new Text;
     
    369353/**
    370354   \param character The character to get info about.
    371    \returns a Glyph struct of a character.
     355   \returns a Glyph struct of a character. This Glyph is a pointer,
     356   and MUST be deleted by the user..
    372357
    373358   This only works for horizontal fonts. see
     
    375360   for more info about vertical Fonts
    376361*/
    377 Glyph GLFont::getGlyphMetrics(Uint16 character)
    378 {
    379   Glyph rg;
    380   rg.character = character;
    381   TTF_GlyphMetrics(this->font, rg.character,
    382                    &rg.minX, &rg.maxX,
    383                    &rg.minY, &rg.maxY,
    384                    &rg.advance);
    385   rg.height = rg.maxY - rg.minY;
    386   rg.width = rg.maxX - rg.minX;
    387   rg.bearingX = (rg.advance - rg.width) / 2;
    388   rg.bearingY = rg.maxY;
     362Glyph* GLFont::getGlyphMetrics(Uint16 character)
     363{
     364  Glyph* rg = new Glyph;
     365  rg->character = character;
     366  TTF_GlyphMetrics(this->font, rg->character,
     367                   &rg->minX, &rg->maxX,
     368                   &rg->minY, &rg->maxY,
     369                   &rg->advance);
     370  rg->height = rg->maxY - rg->minY;
     371  rg->width = rg->maxX - rg->minX;
     372  rg->bearingX = (rg->advance - rg->width) / 2;
     373  rg->bearingY = rg->maxY;
    389374  return rg;
    390375}
     
    451436   *  97-122: a-z
    452437   */
     438  int numberOfGlyphs = 90;
     439  this->initGlyphs(33, numberOfGlyphs);
     440  int rectSize = this->findOptimalFastTextureSize();
     441
     442  PRINTF(1)("rectSize = %d\n", rectSize);
     443
    453444  SDL_Color tmpColor;
    454445  tmpColor.r = tmpColor.g = tmpColor.b = 0;
    455446  SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(
    456447                                               SDL_SWSURFACE,
    457                                                1000, 300,
     448                                               rectSize, rectSize,
    458449                                               32,
    459450#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
     
    473464  tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h;
    474465  SDL_SetClipRect(tmpSurf, &tmpRect);
     466  int maxLineHeight = 0;
    475467  for ( int i = 33; i <= 122; i++ )
    476468    {
    477469      SDL_Surface* glyph = NULL;
    478470      SDL_Surface* glyph2 = NULL;
    479       Glyph tmpGlyph;
    480 
    481       tmpGlyph = getGlyphMetrics(i);
    482       if (tmpRect.x+tmpGlyph.width > tmpSurf->w)
     471      Glyph* tmpGlyph;
     472
     473      tmpGlyph = this->glyphArray[i];
     474      if (tmpGlyph->height > maxLineHeight)
     475        maxLineHeight = tmpGlyph->height;
     476
     477      if (tmpRect.x+tmpGlyph->width > tmpSurf->w)
    483478        {
    484479          tmpRect.x = 0;
    485           tmpRect.y = tmpRect.y+tmpGlyph.height+1;
     480          tmpRect.y = tmpRect.y + maxLineHeight;
    486481          printf("x:%d, y:%d\n", tmpRect.x, tmpRect.y);
    487482        }
    488       if (tmpRect.y +tmpGlyph.height > tmpSurf->h)
     483      if (tmpRect.y +tmpGlyph->height > tmpSurf->h)
    489484        {
    490485          PRINTF(1)("Protection, so font cannot write over the boundraries error\n");
     
    513508        SDL_BlitSurface(glyph, NULL, tmpSurf, &tmpRect);
    514509        SDL_SaveBMP(tmpSurf, outname);
    515         tmpRect.x += tmpGlyph.width+1;
     510        tmpRect.x += tmpGlyph->width+1;
     511        delete tmpGlyph;
    516512      }
    517513    }
     
    601597 
    602598  return texture;
     599}
     600
     601/**
     602   \brief stores Glyph Metrics in an Array.
     603   \param from The Glyph to start from.
     604   \param count The number of Glyphs to start From.
     605*/
     606void GLFont::initGlyphs(Uint16 from, Uint16 count)
     607{
     608  /* initialize the Array, and set all its entries to NULL
     609   *  only if the Glyph-array has not been initialized
     610   */
     611  if (!this->glyphArray)
     612    {
     613      this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
     614      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
     615        this->glyphArray[i] = NULL;
     616    }
     617 
     618  Uint16 lastGlyph = from + count;
     619 
     620  for (int i = from; i <= lastGlyph; i++)
     621    {
     622      // setting up all the Glyphs we like.
     623      glyphArray[i] = getGlyphMetrics(i);
     624    }
     625  return;
     626}
     627
     628/**
     629   \returns the optimal size to use as the texture size
     630
     631   \todo: this algorithm can be a lot more faster, althought it does
     632   not really matter within the init-context, and 128 glyphs.
     633*/
     634int GLFont::findOptimalFastTextureSize(void)
     635{
     636  int i;
     637  int x,y; // the counters
     638  int maxLineHeight;
     639  int size = 32; // starting Value, we have to start somewhere
     640  bool sizeOK = false;
     641  Glyph* tmpGlyph;
     642
     643  while (!sizeOK)
     644    {
     645      x = 0; y = 0;
     646      maxLineHeight = 0;
     647      for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
     648        {
     649          if(tmpGlyph = this->glyphArray[i])
     650            {
     651              // getting the height of the highest Glyph in the Line.
     652              if (tmpGlyph->height > maxLineHeight)
     653                maxLineHeight = tmpGlyph->height;
     654
     655              if (x + tmpGlyph->width > size)
     656                {
     657                  x = 0;
     658                  y = y + maxLineHeight;
     659                  maxLineHeight = 0;
     660                }
     661              if (y + maxLineHeight + 1 > size)
     662                break;
     663              x += tmpGlyph->width + 1;
     664
     665            }
     666        }
     667      if (i == FONT_HIGHEST_KNOWN_CHAR)
     668        sizeOK = true;
     669      else
     670        size *= 2;
     671    }
     672  return size;
     673 
    603674}
    604675
  • orxonox/branches/textEngine/src/lib/graphics/font/glfont.h

    r3735 r3736  
    2525#define FONT_NUM_COLORS         256                  //!< The number of colors.
    2626
     27#define FONT_HIGHEST_KNOWN_CHAR 128                  //!< The highest character known to the textEngine.
    2728
    2829
     
    8889
    8990  // information about the Font
    90   TTF_Font* font;                  //!< The font we use for this.
    91   char* fontFile;                  //!< The fontfile from whitch the font was loaded.
    92   unsigned int fontSize;           //!< The size of the font in pixels. each Font has one size.
     91  TTF_Font* font;                   //!< The font we use for this.
     92  char* fontFile;                   //!< The fontfile from whitch the font was loaded.
     93  unsigned int fontSize;            //!< The size of the font in pixels. each Font has one size.
     94 
     95  Glyph** glyphArray;               //!< An Array of all the Glyphs stored in the Array of Glyphs.
    9396
    9497  //! Represents one textElement.
     
    112115  int getMaxAscent(void);
    113116  int getMaxDescent(void);
    114   Glyph getGlyphMetrics(Uint16 character);
     117  Glyph* getGlyphMetrics(Uint16 character);
    115118
    116119  static bool ttfInitialized;
     
    124127  GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
    125128
     129  void initGlyphs(Uint16 from, Uint16 count);
     130  int findOptimalFastTextureSize(void);
    126131  static int powerOfTwo(int input);
    127132
Note: See TracChangeset for help on using the changeset viewer.