Index: trunk/src/lib/graphics/importer/texture.cc
===================================================================
--- trunk/src/lib/graphics/importer/texture.cc	(revision 7751)
+++ trunk/src/lib/graphics/importer/texture.cc	(revision 7785)
@@ -20,5 +20,4 @@
 #include "debug.h"
 #include "compiler.h"
-#include <math.h>
 
 // INCLUDING SDL_Image
@@ -29,21 +28,10 @@
 #endif
 
-/**
- * @brief Constructor for a Texture
-*/
-Texture::Texture(const std::string& imageName, GLenum target)
-{
-  this->setClassID(CL_TEXTURE, "Texture");
-
+
+TextureData::TextureData()
+{
   this->bAlpha = false;
   this->texture = 0;
   this->image = NULL;
-  this->priority = 0.5;
-
-  if (!imageName.empty())
-  {
-    this->setName(imageName);
-    this->loadImage(imageName, target);
-  }
 }
 
@@ -52,7 +40,7 @@
  * @brief Destructor of a Texture
  *
- * Frees Data, and deletes the textures from GL
- */
-Texture::~Texture()
+ *  Frees Data, and deletes the textures from GL
+ */
+TextureData::~TextureData()
 {
   if (this->texture != 0)
@@ -64,4 +52,111 @@
 
 /**
+ * @brief Loads an SDL_Surface.
+ */
+bool TextureData::loadSurface(SDL_Surface* surface, GLenum target)
+{
+  if (Texture::getTextureEnableState())
+  {
+    SDL_Surface* newSurf = Texture::prepareSurface(surface, this->bAlpha);
+    if (newSurf != NULL)
+    {
+      this->setSurface(newSurf);
+      this->setTexture(Texture::loadTexToGL(newSurf, target));
+      return true;
+    }
+  }
+  return false;
+}
+
+
+
+/**
+ * @brief set the surface this Texture handles
+ * @param newSurface the new Surface to set as the image for this Texture.
+ *
+ * This deletes the old version of the stored Texture,
+ * and sets the newly given Surface as current.
+ */
+bool TextureData::setSurface(SDL_Surface* newSurface)
+{
+  if (this->image != NULL)
+    SDL_FreeSurface(this->image);
+
+  this->image = newSurface;
+
+  return (this->image != NULL);
+}
+
+
+
+bool TextureData::setTexture(GLuint texture)
+{
+     // unload the old Texture.
+  if (this->texture != 0 && glIsTexture(this->getTexture()))
+  {
+    glDeleteTextures(1, &this->texture);
+  }
+  this->texture = texture;
+  return (texture != 0);
+}
+
+
+
+
+
+Texture::Texture(GLenum target)
+{
+  this->init();
+  GLuint texture;
+  this->generateTexture(texture, target);
+  this->data->setTexture(texture);
+}
+
+/**
+ *  Constructor for a Texture
+ */
+Texture::Texture(const std::string& imageName, GLenum target)
+{
+  this->init();
+
+  if (!imageName.empty())
+  {
+    this->setName(imageName);
+    this->loadImage(imageName, target);
+  }
+}
+
+
+
+Texture::Texture(SDL_Surface* surface, GLenum target)
+{
+  this->init();
+
+  if(surface != NULL)
+  {
+    this->data->loadSurface(surface, target);
+  }
+}
+
+void Texture::init()
+{
+  this->setClassID(CL_TEXTURE, "Texture");
+
+  this->data = CountPointer<TextureData>(new TextureData());
+
+  this->priority = 0.5;
+}
+
+/**
+ * @brief Destructor of a Texture
+ *
+ * Frees Data, and deletes the textures from GL
+ */
+Texture::~Texture()
+{
+}
+
+
+/**
  * @brief loads an Image from a file to a Texture
  * @param imageName The image to load
@@ -71,33 +166,13 @@
   if (Texture::texturesEnabled)
   {
-    if (this->image != NULL)
-    {
-      SDL_FreeSurface(this->image);
-      this->image = NULL;
-    }
-    if (this->texture != 0)
-    {
-      glDeleteTextures(1, &this->texture);
-      this->texture = 0;
-    }
     if (!imageName.empty())
     {
       SDL_Surface* tmpSurf;
-      if (this->texture != 0 && glIsTexture(this->texture))
-        glDeleteTextures(1, &this->texture);
+
       // load the new Image to memory
       tmpSurf = IMG_Load(imageName.c_str());
       if(tmpSurf != NULL)
       {
-        PRINTF(4)("loading Image %s\n", imageName.c_str());
-        bool hasAlpha;
-        SDL_Surface* newSurf = this->prepareSurface(tmpSurf, hasAlpha);
-        if (newSurf != NULL)
-        {
-          this->setSurface(newSurf);
-          this->setAlpha(hasAlpha);
-          this->setTexture(Texture::loadTexToGL(newSurf, target));
-        }
-
+        this->data->loadSurface(tmpSurf, target);
         SDL_FreeSurface(tmpSurf);
         return true;
@@ -106,5 +181,5 @@
       {
         PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
-        this->texture = 0;
+        this->setTexture(0);
         return false;
       }
@@ -119,5 +194,4 @@
 }
 
-
 /**
  * @brief rebuilds the texture.
@@ -127,36 +201,12 @@
 bool Texture::rebuild()
 {
-  if (this->texture != 0)
-  {
-    if (glIsTexture(this->texture))
-      glDeleteTextures(1,&this->texture);
-    this->setTexture(0);
-  }
-
-  if (this->image != NULL)
+  this->data->setTexture(0);
+
+  if (this->data->getStoredImage() != NULL)
   {
     PRINTF(3)("Reloading Texture of %s '%s'\n", this->getClassName(), this->getName());
-    this->setTexture(loadTexToGL(this->image));
-  }
-}
-
-
-/**
- * @brief set the surface this Texture handles
- * @param newSurface the new Surface to set as the image for this Texture.
- *
- * This deletes the old version of the stored Texture,
- * and sets the newly given Surface as current.
- */
-bool Texture::setSurface(SDL_Surface* newSurface)
-{
-  if (this->image != NULL)
-    SDL_FreeSurface(this->image);
-
-  this->image = newSurface;
-
-  return (this->image != NULL);
-}
-
+    this->setTexture(Texture::loadTexToGL(this->data->getStoredImage()));
+  }
+}
 
 bool Texture::texturesEnabled = true;
@@ -181,5 +231,5 @@
  * @returns a !!new!! Surface, that is loadable by openGL.
  */
-SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha) const
+SDL_Surface* Texture::prepareSurface(SDL_Surface* surface, bool& hasAlpha)
 {
   assert(surface != NULL);
@@ -222,5 +272,5 @@
     return NULL;
 
-  /* Copy the surface into the GL texture image */
+  /* Copy the surface into the GL texture this->data->getStoredImage() */
   area.x = 0;
   area.y = 0;
@@ -245,18 +295,18 @@
  * @returns The ID of the texture.
  */
-GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target) const
-{
-  //   if (this->texture != 0 && glIsTexture(this->texture))
-  //     glDeleteTextures(1, &this->texture);
-  //   this->texture = 0;
+GLuint Texture::loadTexToGL (const SDL_Surface* surface, GLenum target)
+{
+  //   if (this->data->getTexture() != 0 && glIsTexture(this->data->getTexture()))
+  //     glDeleteTextures(1, &this->data->getTexture());
+  //   this->data->getTexture() = 0;
   assert(surface != NULL);
 
   int      errorCode = 0;           //!< the error code for the texture loading functions
-  GLuint   texture;                 //!< the OpenGL texture handle
+  GLuint   texture = 0;             //!< the OpenGL texture handle
   int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
   int      mipmapWidth = 0;         //!< the width of the mipmap
   int      mipmapHight = 0;         //!< the height of the mipmap
   GLenum   format = GL_RGB;
-  if (this->bAlpha)
+  if (surface->format->BitsPerPixel == 32)
   {
     format = GL_RGBA;
@@ -268,7 +318,6 @@
   }
 
-  /* Create an OpenGL texture for the image */
-  glGenTextures(1, &texture);
-  glBindTexture(target, texture);
+  /* Create an OpenGL texture for the this->data->getStoredImage() */
+  Texture::generateTexture(texture, target);
 
 //   glTexImage2D(target,  0,  format,
@@ -277,16 +326,9 @@
 //                surface->pixels);
 
-  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
-  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
-
-  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR/*_MIPMAP_LINEAR*/);
-  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
-  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
-
-
-  /* control the mipmap levels */
-  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
-  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
+///  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
+///  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
+
+  /// TODO CHECK THIS BACK in
+  //glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, this->priority);
 
   /* build the Texture  OpenGL V >= 1.1 */
@@ -306,2 +348,20 @@
 }
 
+void Texture::generateTexture(GLuint& texture, GLenum target)
+{
+  if (texture == 0 && !glIsTexture(texture))
+  {
+    glGenTextures(1, &texture);
+  }
+  glBindTexture(target, texture);
+
+  glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
+  glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
+
+  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+  /* control the mipmap levels */
+  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
+  glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
+}
