Index: orxonox/branches/physics/src/lib/graphics/importer/texture.cc
===================================================================
--- orxonox/branches/physics/src/lib/graphics/importer/texture.cc	(revision 3866)
+++ orxonox/branches/physics/src/lib/graphics/importer/texture.cc	(revision 3953)
@@ -23,22 +23,9 @@
    \brief Constructor for a Texture
 */
-Texture::Texture(void)
-{
-  this->pImage = new Image;
-  this->pImage->data = NULL;
-  this->map = NULL;
-  this->texture = 0;
-}
-
-/**
-   \brief Constructor for a Texture
-*/
 Texture::Texture(const char* imageName)
 {
-  this->pImage = new Image;
-  this->pImage->data = NULL;
-  this->map = NULL;
   this->texture = 0;
-  this->loadImage(imageName);
+  if (imageName)
+    this->loadImage(imageName);
 }  
 
@@ -50,7 +37,4 @@
 Texture::~Texture(void)
 {
-  if (this->pImage->data)
-    delete []this->pImage->data;
-  delete pImage;
   if (this->texture)
     glDeleteTextures(1, &this->texture);
@@ -73,22 +57,86 @@
 /**
    \brief Loads a Texture to the openGL-environment.
-   \param pImage The Image to load to openGL
+   \param surface the Image to load to openGL
+   \returns The ID of the texture.
 */
-bool Texture::loadTexToGL (Image* pImage)
+GLuint Texture::loadTexToGL (SDL_Surface* surface)
 {
   if (GraphicsEngine::texturesEnabled)
     {
       PRINTF(4)("Loading texture to OpenGL-Environment.\n");
-      glGenTextures(1, &this->texture);
-      glBindTexture(GL_TEXTURE_2D, this->texture);
-      /* not Working, and not needed. 
-	 glTexImage2D( GL_TEXTURE_2D, 0, 3, width,
-	 height, 0, GL_BGR,
-	 GL_UNSIGNED_BYTE, map->pixels );
-      */ 
-      gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, pImage->format, GL_UNSIGNED_BYTE, pImage->data);
+
+      GLuint texture;
+      int w, h;
+      SDL_Surface *image;
+      SDL_Rect area;
+      Uint32 saved_flags;
+      Uint8  saved_alpha;
       
-      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
-      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);	
+      w = surface->w;
+      h = surface->h;
+      
+      image = SDL_CreateRGBSurface(SDL_SWSURFACE,
+				   w, h,
+				   32,
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
+				   0x000000FF, 
+				   0x0000FF00, 
+				   0x00FF0000, 
+				   0xFF000000
+#else
+				   0xFF000000,
+				   0x00FF0000, 
+				   0x0000FF00, 
+				   0x000000FF
+#endif
+				   );
+      if ( image == NULL ) {
+	return 0;
+      }
+      
+      /* Save the alpha blending attributes */
+      saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
+      saved_alpha = surface->format->alpha;
+      if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
+	SDL_SetAlpha(surface, 0, 0);
+      }
+      
+      /* Copy the surface into the GL texture image */
+      area.x = 0;
+      area.y = 0;
+      area.w = surface->w;
+      area.h = surface->h;
+      SDL_BlitSurface(surface, &area, image, &area);
+      
+      /* Restore the alpha blending attributes */
+      if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
+	SDL_SetAlpha(surface, saved_flags | SDL_OPENGL, saved_alpha);
+      }
+      
+      /* Create an OpenGL texture for the image */
+      glGenTextures(1, &texture);
+      glBindTexture(GL_TEXTURE_2D, texture);
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+      /*      glTexImage2D(GL_TEXTURE_2D,
+		   0,
+		   GL_RGBA,
+		   w, h,
+		   0,
+		   GL_RGBA,
+		   GL_UNSIGNED_BYTE,
+		   image->pixels);
+      */
+      gluBuild2DMipmaps(GL_TEXTURE_2D,
+			3,
+			w,
+			h,
+			GL_RGBA,
+			GL_UNSIGNED_BYTE,
+			image->pixels);
+
+      SDL_FreeSurface(image); /* No longer needed */
+      
+      return texture;
     }
 }
@@ -104,38 +152,31 @@
       if (imageName)
 	{
-	  this->map=IMG_Load(imageName);
-	  if(!map)
+	  SDL_Surface* tmpSurf;
+	  if (this->texture)
+	    glDeleteTextures(1, &this->texture);
+	  // load the new Image to memory
+	  tmpSurf = IMG_Load(imageName);
+	  if(!tmpSurf)
 	    {
 	      PRINTF(1)("IMG_Load: %s\n", IMG_GetError());
 	      return false;
 	    }
-	  pImage->height = map->h;
-	  pImage->width  = map->w;
-	  pImage->data   = (GLubyte*)map->pixels;
-	  pImage->bpp    = map->format->BytesPerPixel;
-	  if (pImage->bpp == 3)
-	    pImage->format = GL_RGB;
-	  else if (pImage->bpp == 4)
-	    {
-	      pImage->format = GL_RGBA;
-	      SDL_SetAlpha(this->map, 0, 0);
-	    }
 
-	  if( !IMG_isPNG(SDL_RWFromFile(imageName, "rb")) && !IMG_isJPG(SDL_RWFromFile(imageName, "rb")))
-	    for (int i=0;i<map->h * map->w *3;i+=3)
-	      { 
-		GLuint temp = pImage->data[i];
-		pImage->data[i] = pImage->data[i+2];
-		pImage->data[i+2] = temp;
-	      }
-	  /* this is the real swapping algorithm */
-	  for( int i = 0 ; i < (pImage->height / 2) ; ++i )
-	    for( int j = 0 ; j < pImage->width * pImage->bpp; j += pImage->bpp )
-	      for(int k = 0; k < pImage->bpp; ++k)
-		swap( pImage->data[ (i * pImage->width * pImage->bpp) + j + k], pImage->data[ ( (pImage->height - i - 1) * pImage->width * pImage->bpp ) + j + k]);
+	  GLubyte* pixels = (GLubyte*)tmpSurf->pixels;
 	  
-	  this->loadTexToGL (this->pImage);
-	  SDL_FreeSurface(map);
-	  pImage->data = NULL;
+	  /* this swaps the Mapping so lowel left will be upper left */
+	  for( int i = 0 ; i < (tmpSurf->h / 2) ; ++i )
+	    for( int j = 0 ; j < tmpSurf->w * tmpSurf->format->BytesPerPixel; j += tmpSurf->format->BytesPerPixel )
+	      for(int k = 0; k < tmpSurf->format->BytesPerPixel; ++k)
+		swap( pixels[(i * tmpSurf->w * tmpSurf->format->BytesPerPixel) + j + k],
+		      pixels[ ( (tmpSurf->h - i - 1) * tmpSurf->w *  tmpSurf->format->BytesPerPixel) + j + k]);
+
+	  PRINTF(3)("loading Image %s\n", imageName);
+	  if (tmpSurf)
+	    this->texture = loadTexToGL(tmpSurf);
+
+	  
+	  SDL_FreeSurface(tmpSurf);
+	  return true;
 	}
       else
