- 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;
+TexturePtr
+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;
+ return create_dummy_texture();
+ }
+}
+
+TexturePtr
+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
+ {
+ return VideoSystem::new_texture(image.get());
+ }
+}
+
+TexturePtr
+TextureManager::create_dummy_texture()
+{
+ const std::string dummy_texture_fname = "images/engine/missing.png";
+
+ // on error, try loading placeholder file
+ try
+ {
+ TexturePtr 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
+ {
+ log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl;
+ return VideoSystem::new_texture(image.get());
+ }
+ }