Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / video / texture_manager.cpp
index d45eb84..218de7d 100644 (file)
@@ -1,12 +1,10 @@
-//  $Id$
-//
 //  SuperTux
 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
-//  This program is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU General Public License
-//  as published by the Free Software Foundation; either version 2
-//  of the License, or (at your option) any later version.
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
 //
 //  This program is distributed in the hope that it will be useful,
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  GNU General Public License for more details.
 //
 //  You should have received a copy of the GNU General Public License
-//  along with this program; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-
-#include <config.h>
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-#include "texture_manager.hpp"
+#include "video/texture_manager.hpp"
 
-#include <assert.h>
-#include <SDL.h>
 #include <SDL_image.h>
-#include <GL/gl.h>
-#include <GL/glext.h>
 #include <iostream>
-#include <sstream>
-#include <stdexcept>
+
 #include "physfs/physfs_sdl.hpp"
-#include "image_texture.hpp"
-#include "glutil.hpp"
-#include "file_system.hpp"
-#include "log.hpp"
+#include "util/file_system.hpp"
+#include "util/log.hpp"
+#include "video/gl/gl_texture.hpp"
+#include "video/video_systems.hpp"
 
 TextureManager* texture_manager = NULL;
 
@@ -52,13 +42,13 @@ TextureManager::~TextureManager()
   }
 }
 
-ImageTexture*
+Texture*
 TextureManager::get(const std::string& _filename)
 {
   std::string filename = FileSystem::normalize(_filename);
   ImageTextures::iterator i = image_textures.find(filename);
 
-  ImageTexture* texture = NULL;
+  Texture* texture = NULL;
   if(i != image_textures.end())
     texture = i->second;
 
@@ -71,101 +61,126 @@ TextureManager::get(const std::string& _filename)
 }
 
 void
-TextureManager::release(ImageTexture* texture)
+TextureManager::release(Texture* texture)
 {
-  image_textures.erase(texture->filename);
+  image_textures.erase(texture->get_filename());
   delete texture;
 }
 
+#ifdef HAVE_OPENGL
 void
-TextureManager::register_texture(Texture* texture)
+TextureManager::register_texture(GLTexture* texture)
 {
   textures.insert(texture);
 }
 
 void
-TextureManager::remove_texture(Texture* texture)
+TextureManager::remove_texture(GLTexture* texture)
 {
   textures.erase(texture);
 }
+#endif
 
-static inline int next_power_of_two(int val)
-{
-  int result = 1;
-  while(result < val)
-    result *= 2;
-  return result;
-}
-
-ImageTexture*
+Texture*
 TextureManager::create_image_texture(const std::string& filename)
 {
-  SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
-  if(image == NULL) {
-    std::ostringstream msg;
-    msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
-    throw std::runtime_error(msg.str());
-  }
-
-  int texture_w = next_power_of_two(image->w);
-  int texture_h = next_power_of_two(image->h);
-
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-  SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
-      texture_w, texture_h, 32,
-      0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
-#else
-  SDL_Surface* convert = SDL_CreateRGBSurface(SDL_SWSURFACE,
-      texture_w, texture_h, 32,
-      0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
-#endif
-
-  if(convert == 0)
-    throw std::runtime_error("Couldn't create texture: out of memory");
-
-  SDL_SetAlpha(image, 0, 0);
-  SDL_BlitSurface(image, 0, convert, 0);
-
-  ImageTexture* result = NULL;
   try {
-    result = new ImageTexture(convert);
-    result->filename = filename;
-    result->image_width = image->w;
-    result->image_height = image->h;
-  } catch(...) {
-    delete result;
-    SDL_FreeSurface(convert);
-    throw;
-  }
 
-  SDL_FreeSurface(convert);
-  return result;
+    SDL_Surface* image = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
+    if(image == 0) {
+      std::ostringstream msg;
+      msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
+      throw std::runtime_error(msg.str());
+    }
+
+    Texture* result = 0;
+    try {
+      result = new_texture(image);
+      result->set_filename(filename);
+    } catch(...) {
+      delete result;
+      SDL_FreeSurface(image);
+      throw;
+    }
+
+    SDL_FreeSurface(image);
+    return result;
+
+  } catch (const std::runtime_error& err) {
+    const std::string dummy_texture_fname = "images/engine/missing.png";
+    if (filename == dummy_texture_fname) throw err;
+
+    // on error, try loading placeholder file
+    try {
+
+      Texture* tex = create_image_texture(dummy_texture_fname);
+      log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
+      return tex;
+
+    } catch (...) {
+
+      // on error (when loading placeholder), try using empty surface
+      try {
+
+        SDL_Surface* image = SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0);
+        if(image == 0) {
+          throw err;
+        }
+
+        Texture* result = 0;
+        try {
+          result = new_texture(image);
+          result->set_filename("-dummy-texture-.png");
+        } catch(...) {
+          delete result;
+          SDL_FreeSurface(image);
+          throw err;
+        }
+
+        SDL_FreeSurface(image);
+        log_warning << "Couldn't load texture '" << filename << "' (now using empty one): " << err.what() << std::endl;
+        return result;
+
+        // on error (when trying to use empty surface), give up
+      } catch (const std::runtime_error& err) {
+        throw err;
+      }
+    }
+  }
 }
 
+#ifdef HAVE_OPENGL
 void
 TextureManager::save_textures()
 {
+#ifdef GL_PACK_ROW_LENGTH
+  /* all this stuff is not support by OpenGL ES */
   glPixelStorei(GL_PACK_ROW_LENGTH, 0);
   glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0);
   glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
   glPixelStorei(GL_PACK_SKIP_ROWS, 0);
   glPixelStorei(GL_PACK_SKIP_IMAGES, 0);
+#endif
+
   glPixelStorei(GL_PACK_ALIGNMENT, 1);
   for(Textures::iterator i = textures.begin(); i != textures.end(); ++i) {
     save_texture(*i);
   }
   for(ImageTextures::iterator i = image_textures.begin();
       i != image_textures.end(); ++i) {
-    save_texture(i->second);
+    save_texture(dynamic_cast<GLTexture *>(i->second));
   }
 }
 
 void
-TextureManager::save_texture(Texture* texture)
+TextureManager::save_texture(GLTexture* texture)
 {
   SavedTexture saved_texture;
   saved_texture.texture = texture;
   glBindTexture(GL_TEXTURE_2D, texture->get_handle());
+
+  //this doesn't work with OpenGL ES (but we don't need it on the GP2X anyway)
+#ifndef GL_VERSION_ES_CM_1_0
   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
                            &saved_texture.width);
   glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
@@ -186,11 +201,12 @@ TextureManager::save_texture(Texture* texture)
 
   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE,
                 saved_texture.pixels);
+#endif
 
   saved_textures.push_back(saved_texture);
 
-  glDeleteTextures(1, &(texture->handle));
-  texture->handle = 0;
+  glDeleteTextures(1, &(texture->get_handle()));
+  texture->set_handle(0);
 
   assert_gl("retrieving texture for save");
 }
@@ -198,11 +214,14 @@ TextureManager::save_texture(Texture* texture)
 void
 TextureManager::reload_textures()
 {
+#ifdef GL_UNPACK_ROW_LENGTH
+  /* OpenGL ES doesn't support these */
   glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
   glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
   glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
   glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
   glPixelStorei(GL_UNPACK_SKIP_IMAGES, 0);
+#endif
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
 
   for(std::vector<SavedTexture>::iterator i = saved_textures.begin();
@@ -231,8 +250,11 @@ TextureManager::reload_textures()
                     saved_texture.wrap_t);
 
     assert_gl("setting texture_params");
-    saved_texture.texture->handle = handle;
+    saved_texture.texture->set_handle(handle);
   }
 
   saved_textures.clear();
 }
+#endif
+
+/* EOF */