Index: orxonox/branches/textEngine/src/lib/graphics/font/text_engine.cc
===================================================================
--- orxonox/branches/textEngine/src/lib/graphics/font/text_engine.cc	(revision 3768)
+++ orxonox/branches/textEngine/src/lib/graphics/font/text_engine.cc	(revision 3769)
@@ -31,4 +31,6 @@
 
 #include "graphics_engine.h"
+#include "resource_manager.h"
+
 #include "p_node.h"
 #include "vector.h"
@@ -39,9 +41,16 @@
 /// TEXT ///
 ////////////
-
-Text::Text(void)
-{
-
-}
+Text::Text(Font* font, int type)
+{
+  // initialize this Text
+  this->bindNode = NULL;
+  this->text = NULL;
+  this->texture = 0;
+  this->setType(TEXT_DYNAMIC);
+  this->setPosition(0, 0);
+
+  this->setText(FONT_DEFAULT_TEXT);
+}
+
 
 Text::~Text(void)
@@ -89,4 +98,37 @@
 }
 
+/**
+   \brief sets a new color to the font
+   \param r Red
+   \param g Green
+   \param b Blue
+*/
+void Text::setColor(Uint8 r, Uint8 g, Uint8 b)
+{
+  this->color.r = r;
+  this->color.g = g;
+  this->color.b = b;
+}
+
+/**
+   \brief creates a texture out of the given parameters
+
+   this has to be called every time by the user, to if changes were made.
+*/
+void Text::createTexture(void)
+{
+  SDL_Surface* tmpSurf;
+  if (this->texture)
+    glDeleteTextures(1, &this->texture);
+  tmpSurf = TTF_RenderText_Blended(this->font->font,
+				   this->text,
+				   this->color);
+  if (tmpSurf)
+    this->texture = loadTexture(tmpSurf, &this->texCoord);
+
+  this->posSize.w = tmpSurf->w;
+  this->posSize.h = tmpSurf->h;
+  SDL_FreeSurface(tmpSurf);
+}
 
 /**
@@ -97,5 +139,4 @@
 {
   // storing all the Transformation Matrices.
-
   GLdouble modMat[16];
   GLdouble projMat[16];
@@ -104,5 +145,7 @@
   glGetDoublev(GL_MODELVIEW_MATRIX, modMat);
   glGetIntegerv(GL_VIEWPORT, viewPort);
-//    this->enter2DMode();
+
+  printf("rendering some Text\n");
+  //enter2DMode();
 
 
@@ -168,5 +211,5 @@
 	}
     }
-  //  this->leave2DMode();
+  //leave2DMode();
 }
 
@@ -186,32 +229,17 @@
 Font::Font(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b)
 {
-   if (!TTF_WasInit())
-    TextEngine::enableFonts();
-
   // setting default values.
   this->font = NULL;
   this->fontFile = NULL;
   this->glyphArray = NULL;
-  this->fastTextureID = 0;
-
-  this->currentText = new Text;
-
-  this->currentText->bindNode = NULL;
-  this->currentText->text = NULL;
-  this->currentText->texture = 0;
+  this->fastTextureID = 0;  
   
   this->setSize(fontSize);
-  this->setStyle(TTF_STYLE_NORMAL);
+  this->setStyle("c");//TTF_STYLE_NORMAL);
+
   this->setFont(fontFile);
 
 
-  this->currentText->setType(TEXT_DYNAMIC);
-  this->currentText->setPosition(0, 0);
-
-  this->currentText->setText(FONT_DEFAULT_TEXT);
-  
-  this->setColor(r, g, b);
-
-  this->createTexture();
+  this->setFastColor(r, g, b);
 
   this->fastTextureID = this->createFastTexture();
@@ -223,6 +251,7 @@
 Font::~Font(void)
 {
-  delete this->currentText;
-
+  // deleting the List of all Texts
+
+  // deleting all Glyphs
   if (this->glyphArray)
     {
@@ -232,4 +261,5 @@
     }
 
+  // erease this font out of the memory.
   if (this->font)
     TTF_CloseFont(this->font);
@@ -303,32 +333,10 @@
    \param b Blue
 */
-void Font::setColor(Uint8 r, Uint8 g, Uint8 b)
-{
-  this->currentText->color.r = r;
-  this->currentText->color.g = g;
-  this->currentText->color.b = b;
-}
-
-/**
-   \brief creates a texture out of the given parameters
-
-   this has to be called every time by the user, to if changes were made.
-*/
-void Font::createTexture(void)
-{
-  SDL_Surface* tmpSurf;
-  if (this->currentText->texture)
-    glDeleteTextures(1, &this->currentText->texture);
-  tmpSurf = TTF_RenderText_Blended(this->font,
-				   this->currentText->text,
-				   this->currentText->color);
-  if (tmpSurf)
-    this->currentText->texture = loadTexture(tmpSurf, &this->currentText->texCoord);
-
-  this->currentText->posSize.w = tmpSurf->w;
-  this->currentText->posSize.h = tmpSurf->h;
-  SDL_FreeSurface(tmpSurf);
-}
-
+void Font::setFastColor(Uint8 r, Uint8 g, Uint8 b)
+{
+  this->fastColor.r = r;
+  this->fastColor.g = g;
+  this->fastColor.b = b;
+}
 
 /**
@@ -506,6 +514,5 @@
 	    }
 	  // reading in the new Glyph
-	  //glyphSurf = TTF_RenderGlyph_Shaded(this->font, i, this->currentText->color, tmpColor);
-	  glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->currentText->color);
+	  glyphSurf = TTF_RenderGlyph_Blended(this->font, i, this->fastColor);
 	  if( glyphSurf ) 
 	    {
@@ -569,4 +576,111 @@
 
 /**
+   \brief stores Glyph Metrics in an Array.
+   \param from The Glyph to start from.
+   \param count The number of Glyphs to start From.
+*/
+void Font::initGlyphs(Uint16 from, Uint16 count)
+{
+  /* initialize the Array, and set all its entries to NULL
+   *  only if the Glyph-array has not been initialized
+   */
+  if (!this->glyphArray)
+    {
+      this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
+      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
+	this->glyphArray[i] = NULL;
+    }
+  
+  Uint16 lastGlyph = from + count;
+  
+  for (int i = from; i <= lastGlyph; i++)
+    {
+      // setting up all the Glyphs we like.
+      glyphArray[i] = getGlyphMetrics(i);
+    }
+  return;
+}
+
+/**
+   \returns the optimal size to use as the texture size
+
+   \todo: this algorithm can be a lot more faster, althought it does 
+   not really matter within the init-context, and 128 glyphs.
+
+   This function searches for a 2^n sizes texture-size, this is for 
+   openGL-version < 1.2 compatibility. and because it is realy easy like this.
+*/
+int Font::findOptimalFastTextureSize(void)
+{
+  int i;
+  int x,y; // the counters
+  int maxLineHeight;
+  int size = 32;      // starting Value, we have to start somewhere 32 seems reasonable.
+  bool sizeOK = false;
+  Glyph* tmpGlyph;
+
+  while (!sizeOK)
+    {
+      x = 0; y = 0;
+      maxLineHeight = 0;
+      for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
+	{
+	  if(tmpGlyph = this->glyphArray[i])
+	    {
+	      // getting the height of the highest Glyph in the Line.
+	      if (tmpGlyph->height > maxLineHeight)
+		maxLineHeight = tmpGlyph->height;
+
+	      if (x + tmpGlyph->width > size)
+		{
+		  x = 0;
+		  y = y + maxLineHeight;
+		  maxLineHeight = 0;
+		}
+	      if (y + maxLineHeight + 1 > size)
+		break;
+	      x += tmpGlyph->width + 1;
+
+	    }
+	}
+      if (i == FONT_HIGHEST_KNOWN_CHAR)
+	sizeOK = true;
+      else
+	size *= 2;
+    }
+  return size;
+  
+}
+
+
+/**
+   \brief a simple function to get some interesting information about this class
+*/
+void Font::debug(void)
+{
+
+  // print the loaded font's style
+  int style;
+  style=TTF_GetFontStyle(this->font);
+  PRINTF(0)("The font style is:");
+  if(style==TTF_STYLE_NORMAL)
+    PRINTF(0)(" normal");
+  else {
+    if(style&TTF_STYLE_BOLD)
+      PRINTF(0)(" bold");
+    if(style&TTF_STYLE_ITALIC)
+      PRINTF(0)(" italic");
+    if(style&TTF_STYLE_UNDERLINE)
+      PRINTF(0)(" underline");
+  }
+  PRINTF(0)("\n");
+
+
+}
+
+////////////
+/// UTIL ///
+////////////
+/**
    \brief Loads a Font from an SDL_surface into a texture.
    \param surface The surface to make the texture of
@@ -574,5 +688,5 @@
    \returns the ID of the texture
 */
-GLuint Font::loadTexture(SDL_Surface *surface, TexCoord* texCoord)
+GLuint loadTexture(SDL_Surface *surface, TexCoord* texCoord)
 {
   GLuint texture;
@@ -650,87 +764,9 @@
 
 /**
-   \brief stores Glyph Metrics in an Array.
-   \param from The Glyph to start from.
-   \param count The number of Glyphs to start From.
-*/
-void Font::initGlyphs(Uint16 from, Uint16 count)
-{
-  /* initialize the Array, and set all its entries to NULL
-   *  only if the Glyph-array has not been initialized
-   */
-  if (!this->glyphArray)
-    {
-      this->glyphArray = new Glyph*[FONT_HIGHEST_KNOWN_CHAR];
-      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
-	this->glyphArray[i] = NULL;
-    }
-  
-  Uint16 lastGlyph = from + count;
-  
-  for (int i = from; i <= lastGlyph; i++)
-    {
-      // setting up all the Glyphs we like.
-      glyphArray[i] = getGlyphMetrics(i);
-    }
-  return;
-}
-
-/**
-   \returns the optimal size to use as the texture size
-
-   \todo: this algorithm can be a lot more faster, althought it does 
-   not really matter within the init-context, and 128 glyphs.
-
-   This function searches for a 2^n sizes texture-size, this is for 
-   openGL-version < 1.2 compatibility. and because it is realy easy like this.
-*/
-int Font::findOptimalFastTextureSize(void)
-{
-  int i;
-  int x,y; // the counters
-  int maxLineHeight;
-  int size = 32;      // starting Value, we have to start somewhere 32 seems reasonable.
-  bool sizeOK = false;
-  Glyph* tmpGlyph;
-
-  while (!sizeOK)
-    {
-      x = 0; y = 0;
-      maxLineHeight = 0;
-      for (i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
-	{
-	  if(tmpGlyph = this->glyphArray[i])
-	    {
-	      // getting the height of the highest Glyph in the Line.
-	      if (tmpGlyph->height > maxLineHeight)
-		maxLineHeight = tmpGlyph->height;
-
-	      if (x + tmpGlyph->width > size)
-		{
-		  x = 0;
-		  y = y + maxLineHeight;
-		  maxLineHeight = 0;
-		}
-	      if (y + maxLineHeight + 1 > size)
-		break;
-	      x += tmpGlyph->width + 1;
-
-	    }
-	}
-      if (i == FONT_HIGHEST_KNOWN_CHAR)
-	sizeOK = true;
-      else
-	size *= 2;
-    }
-  return size;
-  
-}
-
-/**
    \brief Quick utility function for texture creation 
    \param input an integer
    \returns the next bigger 2^n-integer than input
 */
-int Font::powerOfTwo(int input)
+int powerOfTwo(int input)
 {
   int value = 1;
@@ -744,34 +780,4 @@
 
 
-/**
-   \brief a simple function to get some interesting information about this class
-*/
-void Font::debug(void)
-{
-
-  // print the loaded font's style
-  int style;
-  style=TTF_GetFontStyle(this->font);
-  PRINTF(0)("The font style is:");
-  if(style==TTF_STYLE_NORMAL)
-    PRINTF(0)(" normal");
-  else {
-    if(style&TTF_STYLE_BOLD)
-      PRINTF(0)(" bold");
-    if(style&TTF_STYLE_ITALIC)
-      PRINTF(0)(" italic");
-    if(style&TTF_STYLE_UNDERLINE)
-      PRINTF(0)(" underline");
-  }
-  PRINTF(0)("\n");
-
-
-}
-
-
-
-
-
-
 
 
@@ -786,5 +792,7 @@
 {
    this->setClassName ("TextEngine");
-
+   this->enableFonts();
+
+   this->textList = new tList<Text>;
 }
 
@@ -810,4 +818,8 @@
 TextEngine::~TextEngine () 
 {
+  this->disableFonts();
+  
+  delete this->textList;
+
   TextEngine::singletonRef = NULL;
 
@@ -842,4 +854,36 @@
   else
     PRINTF(4)("Fonts were not initialized.\n");
+}
+
+/**
+   \brief creates a new Text with a certain font.
+   \see Font::Font
+   \see Text::Text
+*/
+Text* TextEngine::createText(const char* fontFile, unsigned int fontSize, Uint8 r, Uint8 g, Uint8 b)
+{
+  Font* tmpFont;
+  Text* newText;
+
+  tmpFont = (Font*)ResourceManager::getInstance()->load(fontFile, TTF, RP_GAME);
+  if (!tmpFont)
+    {
+      PRINTF(2)("Font %s could not be loaded, probably file not found\n", fontFile);
+      return NULL;
+    }
+
+  newText = new Text(tmpFont, TEXT_DYNAMIC);
+}
+
+void TextEngine::draw(void)
+{
+  tIterator<Text>* textIterator = textList->getIterator();
+  Text* text = textIterator->nextElement();
+  while( text != NULL)
+    {
+      text->draw();
+      text = textIterator->nextElement();
+    }
+  delete textIterator;
 }
 
