Index: orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc
===================================================================
--- orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc	(revision 3735)
+++ orxonox/branches/textEngine/src/lib/graphics/font/glfont.cc	(revision 3736)
@@ -15,5 +15,5 @@
    ***
    * This file is extended to the needs of the orxonox-project.         *
-   * the Copyright of the original file is below this copyright         *
+   * Originally it came from the glfont.c-example from SDL_ttf.         *
    *                                                                  ***
 
@@ -23,28 +23,4 @@
    adding them to orxonox. This is really important, because we do not 
    want to defend anyone.
-*/
-
-/*
-  glfont:  An example of using the SDL_ttf library with OpenGL.
-  Copyright (C) 1997-2004 Sam Lantinga
-  
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Library General Public
-  License as published by the Free Software Foundation; either
-  version 2 of the License, or (at your option) any later version.
-  
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Library General Public License for more details.
-  
-  You should have received a copy of the GNU Library General Public
-  License along with this library; if not, write to the Free
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-  
-  The SDL_GL_* functions in this file are available in the public domain.
-  
-  Sam Lantinga
-  slouken@libsdl.org
 */
 
@@ -76,4 +52,11 @@
   delete this->currentText;
 
+  if (this->glyphArray)
+    {
+      for (int i = 0; i < FONT_HIGHEST_KNOWN_CHAR; i++)
+	delete this->glyphArray[i];
+      delete []this->glyphArray;
+    }
+
   if (this->font)
     TTF_CloseFont(this->font);
@@ -128,4 +111,5 @@
   this->font = NULL;
   this->fontFile = NULL;
+  this->glyphArray = NULL;
 
   this->currentText = new Text;
@@ -369,5 +353,6 @@
 /**
    \param character The character to get info about.
-   \returns a Glyph struct of a character.
+   \returns a Glyph struct of a character. This Glyph is a pointer,
+   and MUST be deleted by the user..
 
    This only works for horizontal fonts. see 
@@ -375,16 +360,16 @@
    for more info about vertical Fonts
 */
-Glyph GLFont::getGlyphMetrics(Uint16 character)
-{
-  Glyph rg;
-  rg.character = character;
-  TTF_GlyphMetrics(this->font, rg.character,
-		   &rg.minX, &rg.maxX,
-		   &rg.minY, &rg.maxY,
-		   &rg.advance);
-  rg.height = rg.maxY - rg.minY;
-  rg.width = rg.maxX - rg.minX;
-  rg.bearingX = (rg.advance - rg.width) / 2;
-  rg.bearingY = rg.maxY;
+Glyph* GLFont::getGlyphMetrics(Uint16 character)
+{
+  Glyph* rg = new Glyph;
+  rg->character = character;
+  TTF_GlyphMetrics(this->font, rg->character,
+		   &rg->minX, &rg->maxX,
+		   &rg->minY, &rg->maxY,
+		   &rg->advance);
+  rg->height = rg->maxY - rg->minY;
+  rg->width = rg->maxX - rg->minX;
+  rg->bearingX = (rg->advance - rg->width) / 2;
+  rg->bearingY = rg->maxY;
   return rg;
 }
@@ -451,9 +436,15 @@
    *  97-122: a-z
    */
+  int numberOfGlyphs = 90;
+  this->initGlyphs(33, numberOfGlyphs);
+  int rectSize = this->findOptimalFastTextureSize();
+
+  PRINTF(1)("rectSize = %d\n", rectSize);
+
   SDL_Color tmpColor;
   tmpColor.r = tmpColor.g = tmpColor.b = 0;
   SDL_Surface* tmpSurf =  SDL_CreateRGBSurface(
 					       SDL_SWSURFACE,
-					       1000, 300,
+					       rectSize, rectSize,
 					       32,
 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
@@ -473,18 +464,22 @@
   tmpRect.x = 0; tmpRect.y = 0; tmpRect.w = tmpSurf->w; tmpRect.h = tmpSurf->h;
   SDL_SetClipRect(tmpSurf, &tmpRect);
+  int maxLineHeight = 0;
   for ( int i = 33; i <= 122; i++ ) 
     {
       SDL_Surface* glyph = NULL;
       SDL_Surface* glyph2 = NULL;
-      Glyph tmpGlyph;
-
-      tmpGlyph = getGlyphMetrics(i);
-      if (tmpRect.x+tmpGlyph.width > tmpSurf->w)
+      Glyph* tmpGlyph;
+
+      tmpGlyph = this->glyphArray[i];
+      if (tmpGlyph->height > maxLineHeight)
+	maxLineHeight = tmpGlyph->height;
+
+      if (tmpRect.x+tmpGlyph->width > tmpSurf->w)
 	{
 	  tmpRect.x = 0;
-	  tmpRect.y = tmpRect.y+tmpGlyph.height+1;
+	  tmpRect.y = tmpRect.y + maxLineHeight;
 	  printf("x:%d, y:%d\n", tmpRect.x, tmpRect.y);
 	}
-      if (tmpRect.y +tmpGlyph.height > tmpSurf->h)
+      if (tmpRect.y +tmpGlyph->height > tmpSurf->h)
 	{
 	  PRINTF(1)("Protection, so font cannot write over the boundraries error\n");
@@ -513,5 +508,6 @@
 	SDL_BlitSurface(glyph, NULL, tmpSurf, &tmpRect);
 	SDL_SaveBMP(tmpSurf, outname);
-	tmpRect.x += tmpGlyph.width+1;
+	tmpRect.x += tmpGlyph->width+1;
+	delete tmpGlyph;
       }
     }
@@ -601,4 +597,79 @@
   
   return texture;
+}
+
+/**
+   \brief stores Glyph Metrics in an Array.
+   \param from The Glyph to start from.
+   \param count The number of Glyphs to start From.
+*/
+void GLFont::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.
+*/
+int GLFont::findOptimalFastTextureSize(void)
+{
+  int i;
+  int x,y; // the counters
+  int maxLineHeight;
+  int size = 32; // starting Value, we have to start somewhere
+  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;
+  
 }
 
