Fix for images with invalid pixel formats
[supertux.git] / src / video / texture_manager.cpp
index 3a5f849..ae272b3 100644 (file)
@@ -146,7 +146,17 @@ TextureManager::create_image_texture_raw(const std::string& filename, const Rect
       throw std::runtime_error(msg.str());
     }
 
-    m_surfaces[filename] = image;
+    SDL_PixelFormat* format = image->format;
+    if(format->Rmask == 0 && format->Gmask == 0 && format->Bmask == 0 && format->Amask == 0) {
+      log_warning << "Wrong surface format for image " << filename << ". Compensating." << std::endl;
+
+      SDL_Surface* converted_surf = SDL_ConvertSurfaceFormat(image, SDL_PIXELFORMAT_RGBA8888, 0);
+      m_surfaces[filename] = converted_surf;
+    }
+    else
+    {
+      m_surfaces[filename] = image;
+    }
   }
 
   SDLSurfacePtr subimage(SDL_CreateRGBSurfaceFrom(static_cast<uint8_t*>(image->pixels) +
@@ -224,7 +234,7 @@ TextureManager::create_dummy_texture()
     SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
     if (!image)
     {
-      throw err;
+      throw;
     }
     else
     {
@@ -240,7 +250,7 @@ TextureManager::create_dummy_texture()
 void
 TextureManager::save_textures()
 {
-#ifdef GL_PACK_ROW_LENGTH
+#if defined(GL_PACK_ROW_LENGTH) || defined(USE_GLBINDING)
   /* all this stuff is not support by OpenGL ES */
   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
   glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
@@ -259,7 +269,11 @@ TextureManager::save_textures()
   for(ImageTextures::iterator i = m_image_textures.begin();
       i != m_image_textures.end(); ++i)
   {
-    save_texture(dynamic_cast<GLTexture*>(i->second.lock().get()));
+    GLTexture* texture = dynamic_cast<GLTexture*>(i->second.lock().get());
+    if(texture == NULL)
+      continue;
+
+    save_texture(texture);
   }
 }
 
@@ -305,7 +319,7 @@ TextureManager::save_texture(GLTexture* texture)
 void
 TextureManager::reload_textures()
 {
-#ifdef GL_UNPACK_ROW_LENGTH
+#if defined(GL_UNPACK_ROW_LENGTH) || defined(USE_GLBINDING)
   /* OpenGL ES doesn't support these */
   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
   glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
@@ -324,7 +338,7 @@ TextureManager::reload_textures()
     assert_gl("creating texture handle");
 
     glBindTexture(GL_TEXTURE_2D, handle);
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
+    glTexImage2D(GL_TEXTURE_2D, 0, static_cast<GLint>(GL_RGBA),
                  saved_texture.width, saved_texture.height,
                  saved_texture.border, GL_RGBA,
                  GL_UNSIGNED_BYTE, saved_texture.pixels);