Splitted TextureManager::create_image_texture() into multiple functions
authorgrumbel <grumbel@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Sun, 6 Dec 2009 12:09:24 +0000 (12:09 +0000)
committergrumbel <grumbel@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Sun, 6 Dec 2009 12:09:24 +0000 (12:09 +0000)
Added SDLSurfacePtr class that handles the SDL_FreeSurface() call in an exception safe manner

git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6185 837edb03-e0f3-0310-88ca-d4d4e8b29345

src/video/sdl_surface_ptr.hpp [new file with mode: 0644]
src/video/texture_manager.cpp
src/video/texture_manager.hpp

diff --git a/src/video/sdl_surface_ptr.hpp b/src/video/sdl_surface_ptr.hpp
new file mode 100644 (file)
index 0000000..2fffc95
--- /dev/null
@@ -0,0 +1,55 @@
+//  SuperTux
+//  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.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 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
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  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, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_VIDEO_SDL_SURFACE_PTR_HPP
+#define HEADER_SUPERTUX_VIDEO_SDL_SURFACE_PTR_HPP
+
+#include <SDL.h>
+
+/** Simple Wrapper class around SDL_Surface that provides execption
+    safety */
+class SDLSurfacePtr
+{
+private:
+  SDL_Surface* m_surface;
+
+public:
+  SDLSurfacePtr(SDL_Surface* surface) :
+    m_surface(surface)
+  {}
+
+  ~SDLSurfacePtr() 
+  {
+    SDL_FreeSurface(m_surface);
+  }
+
+  SDL_Surface* get()
+  {
+    return m_surface;
+  }
+
+  operator void*() {
+    return m_surface;
+  }
+
+private:
+  SDLSurfacePtr(const SDLSurfacePtr&);
+  SDLSurfacePtr& operator=(const SDLSurfacePtr&);
+};
+
+#endif
+
+/* EOF */
index 5cb962b..4073639 100644 (file)
 #include "video/texture_manager.hpp"
 
 #include <SDL_image.h>
+#include <assert.h>
 #include <iostream>
 
 #include "physfs/physfs_sdl.hpp"
 #include "util/file_system.hpp"
 #include "util/log.hpp"
 #include "video/gl/gl_texture.hpp"
+#include "video/sdl_surface_ptr.hpp"
 #include "video/video_systems.hpp"
 
 TextureManager::TextureManager() :
@@ -63,6 +65,12 @@ TextureManager::get(const std::string& _filename)
   return texture;
 }
 
+Texture*
+TextureManager::get(const std::string& filename, const Rect& rect)
+{
+  return 0;
+}
+
 void
 TextureManager::release(Texture* texture)
 {
@@ -85,69 +93,71 @@ TextureManager::remove_texture(GLTexture* texture)
 #endif
 
 Texture*
-TextureManager::create_image_texture(const std::string& filename)
+TextureManager::create_image_texture(const std::string& filename, const Rect& rect)
 {
-  try {
-
-    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());
-    }
+  assert(!"not implemented");
+  return 0;
+}
 
-    Texture* result = 0;
-    try {
-      result = VideoSystem::new_texture(image);
-      result->set_filename(filename);
-    } catch(...) {
-      delete result;
-      SDL_FreeSurface(image);
-      throw;
-    }
+Texture*
+TextureManager::create_image_texture(const std::string& filename)
+{
+  try 
+  {
+    return create_image_texture_raw(filename);
+  }
+  catch (const std::exception& err)
+  {
+    log_warning << "Couldn't load texture '" << filename << "' (now using dummy texture): " << err.what() << std::endl;
+    Texture* texture = create_dummy_texture();
+    return texture;
+  }
+}
 
-    SDL_FreeSurface(image);
+Texture*
+TextureManager::create_image_texture_raw(const std::string& filename)
+{
+  SDLSurfacePtr image(IMG_Load_RW(get_physfs_SDLRWops(filename), 1));
+  if (!image) 
+  {
+    std::ostringstream msg;
+    msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
+    throw std::runtime_error(msg.str());
+  }
+  else
+  {
+    Texture* result = VideoSystem::new_texture(image.get());
+    result->set_filename(filename);
     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 = VideoSystem::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;
-      }
+Texture*
+TextureManager::create_dummy_texture()
+{
+  const std::string dummy_texture_fname = "images/engine/missing.png";
+  // on error, try loading placeholder file
+  try 
+  {
+    Texture* tex = create_image_texture_raw(dummy_texture_fname);
+    return tex;
+  }
+  catch (const std::exception& err) 
+  {
+    // on error (when loading placeholder), try using empty surface,
+    // when that fails to, just give up
+    SDLSurfacePtr image(SDL_CreateRGBSurface(0, 1024, 1024, 8, 0, 0, 0, 0));
+    if (!image)
+    {
+      throw err;
+    }
+    else
+    {
+      Texture* result = VideoSystem::new_texture(image.get());
+      result->set_filename("-dummy-texture-.png");
+      log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
+      return result;
     }
   }
 }
index 496c415..a9a1fac 100644 (file)
@@ -28,6 +28,7 @@
 
 class Texture;
 class GLTexture;
+class Rect;
 
 class TextureManager
 {
@@ -36,6 +37,7 @@ public:
   ~TextureManager();
 
   Texture* get(const std::string& filename);
+  Texture* get(const std::string& filename, const Rect& rect);
 
 #ifdef HAVE_OPENGL
   void register_texture(GLTexture* texture);
@@ -52,8 +54,16 @@ private:
   typedef std::map<std::string, Texture*> ImageTextures;
   ImageTextures image_textures;
 
+  Texture* create_image_texture(const std::string& filename, const Rect& rect);
+
+  /** on failure a dummy texture is returned and no exception is thrown */
   Texture* create_image_texture(const std::string& filename);
 
+  /** throw an exception on error */
+  Texture* create_image_texture_raw(const std::string& filename);
+
+  Texture* create_dummy_texture();
+  
 #ifdef HAVE_OPENGL
   typedef std::set<GLTexture*> Textures;
   Textures textures;