Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3769 in orxonox.OLD for orxonox/branches/textEngine/src/lib


Ignore:
Timestamp:
Apr 9, 2005, 10:32:18 PM (20 years ago)
Author:
bensch
Message:

orxonox/branches/textEngine: compiling again, many movements… still working

Location:
orxonox/branches/textEngine/src/lib
Files:
4 edited

Legend:

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

    r3768 r3769  
    3131
    3232#include "graphics_engine.h"
     33#include "resource_manager.h"
     34
    3335#include "p_node.h"
    3436#include "vector.h"
     
    3941/// TEXT ///
    4042////////////
    41 
    42 Text::Text(void)
    43 {
    44 
    45 }
     43Text::Text(Font* font, int type)
     44{
     45  // initialize this Text
     46  this->bindNode = NULL;
     47  this->text = NULL;
     48  this->texture = 0;
     49  this->setType(TEXT_DYNAMIC);
     50  this->setPosition(0, 0);
     51
     52  this->setText(FONT_DEFAULT_TEXT);
     53}
     54
    4655
    4756Text::~Text(void)
     
    8998}
    9099
     100/**
     101   \brief sets a new color to the font
     102   \param r Red
     103   \param g Green
     104   \param b Blue
     105*/
     106void Text::setColor(Uint8 r, Uint8 g, Uint8 b)
     107{
     108  this->color.r = r;
     109  this->color.g = g;
     110  this->color.b = b;
     111}
     112
     113/**
     114   \brief creates a texture out of the given parameters
     115
     116   this has to be called every time by the user, to if changes were made.
     117*/
     118void Text::createTexture(void)
     119{
     120  SDL_Surface* tmpSurf;
     121  if (this->texture)
     122    glDeleteTextures(1, &this->texture);
     123  tmpSurf = TTF_RenderText_Blended(this->font->font,
     124                                   this->text,
     125                                   this->color);
     126  if (tmpSurf)
     127    this->texture = loadTexture(tmpSurf, &this->texCoord);
     128
     129  this->posSize.w = tmpSurf->w;
     130  this->posSize.h = tmpSurf->h;
     131  SDL_FreeSurface(tmpSurf);
     132}
    91133
    92134/**
     
    97139{
    98140  // storing all the Transformation Matrices.
    99 
    100141  GLdouble modMat[16];
    101142  GLdouble projMat[16];
     
    104145  glGetDoublev(GL_MODELVIEW_MATRIX, modMat);
    105146  glGetIntegerv(GL_VIEWPORT, viewPort);
    106 //    this->enter2DMode();
     147
     148  printf("rendering some Text\n");
     149  //enter2DMode();
    107150
    108151
     
    168211        }
    169212    }
    170   //  this->leave2DMode();
     213  //leave2DMode();
    171214}
    172215
     
    186229Font::Font(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b)
    187230{
    188    if (!TTF_WasInit())
    189     TextEngine::enableFonts();
    190 
    191231  // setting default values.
    192232  this->font = NULL;
    193233  this->fontFile = NULL;
    194234  this->glyphArray = NULL;
    195   this->fastTextureID = 0;
    196 
    197   this->currentText = new Text;
    198 
    199   this->currentText->bindNode = NULL;
    200   this->currentText->text = NULL;
    201   this->currentText->texture = 0;
     235  this->fastTextureID = 0; 
    202236 
    203237  this->setSize(fontSize);
    204   this->setStyle(TTF_STYLE_NORMAL);
     238  this->setStyle("c");//TTF_STYLE_NORMAL);
     239
    205240  this->setFont(fontFile);
    206241
    207242
    208   this->currentText->setType(TEXT_DYNAMIC);
    209   this->currentText->setPosition(0, 0);
    210 
    211   this->currentText->setText(FONT_DEFAULT_TEXT);
    212  
    213   this->setColor(r, g, b);
    214 
    215   this->createTexture();
     243  this->setFastColor(r, g, b);
    216244
    217245  this->fastTextureID = this->createFastTexture();
     
    223251Font::~Font(void)
    224252{
    225   delete this->currentText;
    226 
     253  // deleting the List of all Texts
     254
     255  // deleting all Glyphs
    227256  if (this->glyphArray)
    228257    {
     
    232261    }
    233262
     263  // erease this font out of the memory.
    234264  if (this->font)
    235265    TTF_CloseFont(this->font);
     
    303333   \param b Blue
    304334*/
    305 void Font::setColor(Uint8 r, Uint8 g, Uint8 b)
    306 {
    307   this->currentText->color.r = r;
    308   this->currentText->color.g = g;
    309   this->currentText->color.b = b;
    310 }
    311 
    312 /**
    313    \brief creates a texture out of the given parameters
    314 
    315    this has to be called every time by the user, to if changes were made.
    316 */
    317 void Font::createTexture(void)
    318 {
    319   SDL_Surface* tmpSurf;
    320   if (this->currentText->texture)
    321     glDeleteTextures(1, &this->currentText->texture);
    322   tmpSurf = TTF_RenderText_Blended(this->font,
    323                                    this->currentText->text,
    324                                    this->currentText->color);
    325   if (tmpSurf)
    326     this->currentText->texture = loadTexture(tmpSurf, &this->currentText->texCoord);
    327 
    328   this->currentText->posSize.w = tmpSurf->w;
    329   this->currentText->posSize.h = tmpSurf->h;
    330   SDL_FreeSurface(tmpSurf);
    331 }
    332 
     335void Font::setFastColor(Uint8 r, Uint8 g, Uint8 b)
     336{
     337  this->fastColor.r = r;
     338  this->fastColor.g = g;
     339  this->fastColor.b = b;
     340}
    333341
    334342/**
     
    506514            }
    507515          // reading in the new Glyph
    508           //glyphSurf = TTF_RenderGlyph_Shaded(this->font, i, this->currentText->color, tmpColor);
    509           glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->currentText->color);
     516          glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->fastColor);
    510517          if( glyphSurf )
    511518            {
     
    569576
    570577/**
     578   \brief stores Glyph Metrics in an Array.
     579   \param from The Glyph to start from.
     580   \param count The number of Glyphs to start From.
     581*/
     582void Font::initGlyphs(Uint16 from, Uint16 count)
     583{
     584  /* initialize the Array, and set all its entries to NULL
     585   *  only if the Glyph-array has not been initialized
     586   */
     587  if (!this->glyphArray)
     588    {
     589      this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
     590      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
     591        this->glyphArray[i] = NULL;
     592    }
     593 
     594  Uint16 lastGlyph = from + count;
     595 
     596  for (int i = from; i <= lastGlyph; i++)
     597    {
     598      // setting up all the Glyphs we like.
     599      glyphArray[i] = getGlyphMetrics(i);
     600    }
     601  return;
     602}
     603
     604/**
     605   \returns the optimal size to use as the texture size
     606
     607   \todo: this algorithm can be a lot more faster, althought it does
     608   not really matter within the init-context, and 128 glyphs.
     609
     610   This function searches for a 2^n sizes texture-size, this is for
     611   openGL-version < 1.2 compatibility. and because it is realy easy like this.
     612*/
     613int Font::findOptimalFastTextureSize(void)
     614{
     615  int i;
     616  int x,y; // the counters
     617  int maxLineHeight;
     618  int size = 32;      // starting Value, we have to start somewhere 32 seems reasonable.
     619  bool sizeOK = false;
     620  Glyph* tmpGlyph;
     621
     622  while (!sizeOK)
     623    {
     624      x = 0; y = 0;
     625      maxLineHeight = 0;
     626      for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
     627        {
     628          if(tmpGlyph = this->glyphArray[i])
     629            {
     630              // getting the height of the highest Glyph in the Line.
     631              if (tmpGlyph->height > maxLineHeight)
     632                maxLineHeight = tmpGlyph->height;
     633
     634              if (x + tmpGlyph->width > size)
     635                {
     636                  x = 0;
     637                  y = y + maxLineHeight;
     638                  maxLineHeight = 0;
     639                }
     640              if (y + maxLineHeight + 1 > size)
     641                break;
     642              x += tmpGlyph->width + 1;
     643
     644            }
     645        }
     646      if (i == FONT_HIGHEST_KNOWN_CHAR)
     647        sizeOK = true;
     648      else
     649        size *= 2;
     650    }
     651  return size;
     652 
     653}
     654
     655
     656/**
     657   \brief a simple function to get some interesting information about this class
     658*/
     659void Font::debug(void)
     660{
     661
     662  // print the loaded font's style
     663  int style;
     664  style=TTF_GetFontStyle(this->font);
     665  PRINTF(0)("The font style is:");
     666  if(style==TTF_STYLE_NORMAL)
     667    PRINTF(0)(" normal");
     668  else {
     669    if(style&TTF_STYLE_BOLD)
     670      PRINTF(0)(" bold");
     671    if(style&TTF_STYLE_ITALIC)
     672      PRINTF(0)(" italic");
     673    if(style&TTF_STYLE_UNDERLINE)
     674      PRINTF(0)(" underline");
     675  }
     676  PRINTF(0)("\n");
     677
     678
     679}
     680
     681////////////
     682/// UTIL ///
     683////////////
     684/**
    571685   \brief Loads a Font from an SDL_surface into a texture.
    572686   \param surface The surface to make the texture of
     
    574688   \returns the ID of the texture
    575689*/
    576 GLuint Font::loadTexture(SDL_Surface *surface, TexCoord* texCoord)
     690GLuint loadTexture(SDL_Surface *surface, TexCoord* texCoord)
    577691{
    578692  GLuint texture;
     
    650764
    651765/**
    652    \brief stores Glyph Metrics in an Array.
    653    \param from The Glyph to start from.
    654    \param count The number of Glyphs to start From.
    655 */
    656 void Font::initGlyphs(Uint16 from, Uint16 count)
    657 {
    658   /* initialize the Array, and set all its entries to NULL
    659    *  only if the Glyph-array has not been initialized
    660    */
    661   if (!this->glyphArray)
    662     {
    663       this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
    664       for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
    665         this->glyphArray[i] = NULL;
    666     }
    667  
    668   Uint16 lastGlyph = from + count;
    669  
    670   for (int i = from; i <= lastGlyph; i++)
    671     {
    672       // setting up all the Glyphs we like.
    673       glyphArray[i] = getGlyphMetrics(i);
    674     }
    675   return;
    676 }
    677 
    678 /**
    679    \returns the optimal size to use as the texture size
    680 
    681    \todo: this algorithm can be a lot more faster, althought it does
    682    not really matter within the init-context, and 128 glyphs.
    683 
    684    This function searches for a 2^n sizes texture-size, this is for
    685    openGL-version < 1.2 compatibility. and because it is realy easy like this.
    686 */
    687 int Font::findOptimalFastTextureSize(void)
    688 {
    689   int i;
    690   int x,y; // the counters
    691   int maxLineHeight;
    692   int size = 32;      // starting Value, we have to start somewhere 32 seems reasonable.
    693   bool sizeOK = false;
    694   Glyph* tmpGlyph;
    695 
    696   while (!sizeOK)
    697     {
    698       x = 0; y = 0;
    699       maxLineHeight = 0;
    700       for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
    701         {
    702           if(tmpGlyph = this->glyphArray[i])
    703             {
    704               // getting the height of the highest Glyph in the Line.
    705               if (tmpGlyph->height > maxLineHeight)
    706                 maxLineHeight = tmpGlyph->height;
    707 
    708               if (x + tmpGlyph->width > size)
    709                 {
    710                   x = 0;
    711                   y = y + maxLineHeight;
    712                   maxLineHeight = 0;
    713                 }
    714               if (y + maxLineHeight + 1 > size)
    715                 break;
    716               x += tmpGlyph->width + 1;
    717 
    718             }
    719         }
    720       if (i == FONT_HIGHEST_KNOWN_CHAR)
    721         sizeOK = true;
    722       else
    723         size *= 2;
    724     }
    725   return size;
    726  
    727 }
    728 
    729 /**
    730766   \brief Quick utility function for texture creation
    731767   \param input an integer
    732768   \returns the next bigger 2^n-integer than input
    733769*/
    734 int Font::powerOfTwo(int input)
     770int powerOfTwo(int input)
    735771{
    736772  int value = 1;
     
    744780
    745781
    746 /**
    747    \brief a simple function to get some interesting information about this class
    748 */
    749 void Font::debug(void)
    750 {
    751 
    752   // print the loaded font's style
    753   int style;
    754   style=TTF_GetFontStyle(this->font);
    755   PRINTF(0)("The font style is:");
    756   if(style==TTF_STYLE_NORMAL)
    757     PRINTF(0)(" normal");
    758   else {
    759     if(style&TTF_STYLE_BOLD)
    760       PRINTF(0)(" bold");
    761     if(style&TTF_STYLE_ITALIC)
    762       PRINTF(0)(" italic");
    763     if(style&TTF_STYLE_UNDERLINE)
    764       PRINTF(0)(" underline");
    765   }
    766   PRINTF(0)("\n");
    767 
    768 
    769 }
    770 
    771 
    772 
    773 
    774 
    775 
    776782
    777783
     
    786792{
    787793   this->setClassName ("TextEngine");
    788 
     794   this->enableFonts();
     795
     796   this->textList = new tList<Text>;
    789797}
    790798
     
    810818TextEngine::~TextEngine ()
    811819{
     820  this->disableFonts();
     821 
     822  delete this->textList;
     823
    812824  TextEngine::singletonRef = NULL;
    813825
     
    842854  else
    843855    PRINTF(4)("Fonts were not initialized.\n");
     856}
     857
     858/**
     859   \brief creates a new Text with a certain font.
     860   \see Font::Font
     861   \see Text::Text
     862*/
     863Text* TextEngine::createText(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b)
     864{
     865  Font* tmpFont;
     866  Text* newText;
     867
     868  tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME);
     869  if (!tmpFont)
     870    {
     871      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile);
     872      return NULL;
     873    }
     874
     875  newText = new Text(tmpFont, TEXT_DYNAMIC);
     876}
     877
     878void TextEngine::draw(void)
     879{
     880  tIterator<Text>* textIterator = textList->getIterator();
     881  Text* text = textIterator->nextElement();
     882  while( text != NULL)
     883    {
     884      text->draw();
     885      text = textIterator->nextElement();
     886    }
     887  delete textIterator;
    844888}
    845889
  • orxonox/branches/textEngine/src/lib/graphics/font/text_engine.h

    r3768 r3769  
    9393{
    9494 public:
    95   Text(void);
     95  Text(Font* font, int type = TEXT_DYNAMIC);
    9696  ~Text(void);
    9797
     
    101101  void setText(const char* text);
    102102  void setPosition(int x, int y);
     103
     104  // Static Text
     105  void setColor(Uint8 r, Uint8 g, Uint8 b);
     106  void setStyle(char* renderStyle);
     107  void createTexture();
     108
     109  // Dynamic Text
     110 
     111
    103112
    104113  virtual void draw(void);
     
    113122  GLuint texture;                //!< A GL-texture to hold the text
    114123  TexCoord texCoord;             //!< Texture-coordinates \todo fix this to have a struct
    115   SDL_Rect posSize;          //!< An SDL-Rectangle representing the position and size of the Text on the screen.
     124  SDL_Rect posSize;              //!< An SDL-Rectangle representing the position and size of the Text on the screen.
    116125 
    117126  PNode* bindNode;               //!< A node the Text is bind to. (if NULL thr node will not be bound to anything.)
     
    124133class Font
    125134{
     135  friend class Text;
    126136 public:
    127137  Font(const char* fontFile, unsigned int fontSize = FONT_DEFAULT_SIZE,
     
    132142  bool setFont(const char* fontFile);
    133143  void setSize(unsigned int fontSize);
    134   void setColor(Uint8 r, Uint8 g, Uint8 b);
     144  void setFastColor(Uint8 r, Uint8 g, Uint8 b);
    135145  void setStyle(char* renderStyle);
    136146
    137147  inline Glyph** getGlyphArray(void) {return glyphArray;}
    138148  inline GLuint getFastTextureID(void) {return fastTextureID;}
    139   void createTexture(void);
    140149 
    141150 
     
    148157  char* fontFile;                   //!< The fontfile from whitch the font was loaded.
    149158  unsigned int fontSize;            //!< The size of the font in pixels. each Font has one size.
    150   int renderStyle;               //!< The Renderstyle
     159  int renderStyle;                  //!< The Renderstyle
    151160 
    152161  Glyph** glyphArray;               //!< An Array of all the Glyphs stored in the Array of Glyphs.
    153162  GLuint fastTextureID;             //!< The fast textureID.
     163  SDL_Color fastColor;              //!< A Color for the fast Texture.
    154164
    155165  tList<Text>* textList;
    156   Text* currentText;
    157166
    158167  int getMaxHeight(void);
     
    166175
    167176  GLuint createFastTexture();
    168   GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
    169177
    170178  void initGlyphs(Uint16 from, Uint16 count);
    171179  int findOptimalFastTextureSize(void);
    172   static int powerOfTwo(int input);
    173180
    174181  void debug(void);
    175182
    176183};
     184GLuint loadTexture(SDL_Surface* surface, TexCoord* texCoord);
     185int powerOfTwo(int input);
    177186
    178187
     
    187196  virtual ~TextEngine(void);
    188197
     198  Text* createText(const char* fontFile,
     199                   unsigned int fontSize = FONT_DEFAULT_SIZE,
     200                   Uint8 r = FONT_DEFAULT_COLOR_R,
     201                   Uint8 g = FONT_DEFAULT_COLOR_G,
     202                   Uint8 b = FONT_DEFAULT_COLOR_B);
     203
     204  void draw(void);
     205
     206 private:
     207  TextEngine(void);
     208  static TextEngine* singletonRef;
     209
    189210  // general
    190211  static void enableFonts(void);
     
    192213  static bool checkVersion(void);
    193214
    194  private:
    195   TextEngine(void);
    196   static TextEngine* singletonRef;
    197 
    198   tList<Font>* fontList;
     215
     216  //  tList<Font>* fontList;
     217  tList<Text>* textList;
    199218
    200219};
  • orxonox/branches/textEngine/src/lib/util/resource_manager.cc

    r3703 r3769  
    2222#include "primitive_model.h"
    2323#include "texture.h"
     24#include "text_engine.h"
    2425
    2526#include "list.h"
     
    158159           !strcmp(fileName, "cone"))
    159160    tmpType = PRIM;
     161  else if (!strncmp(fileName+(strlen(fileName)-4), ".ttf", 4))
     162    tmpType = TTF;
    160163  else
    161164    tmpType = IMAGE;
     
    220223          else if (!strcmp(tmpResource->name, "cone"))
    221224            tmpResource->pointer = new PrimitiveModel(CONE);
     225          break;
     226        case TTF:
     227          if(isFile(fullName))
     228            tmpResource->pointer = new Font(fullName);
     229          else
     230            PRINTF(2)("Sorry, %s does not exist. Not loading Font\n", fullName);
    222231          break;
    223232        case IMAGE:
  • orxonox/branches/textEngine/src/lib/util/resource_manager.h

    r3681 r3769  
    1919
    2020//! An eumerator for different fileTypes the resourceManager supports \todo WAV, MP3, OGG support
    21 enum ResourceType {OBJ, PRIM, WAV, MP3, OGG, IMAGE};
     21enum ResourceType {OBJ, PRIM, WAV, MP3, OGG, TTF, IMAGE};
    2222//! An enumerator for different UNLOAD-types.
    2323/**
     
    4949
    5050   It does it by looking, if a desired file has already been loaded.
     51
     52   \todo loading also dependant by parameters.
    5153*/
    5254class ResourceManager : public BaseObject
Note: See TracChangeset for help on using the changeset viewer.