+
+TexturePtr
+TextureManager::create_image_texture_raw(const std::string& filename, const Rect& rect)
+{
+ SDL_Surface *image = nullptr;
+
+ Surfaces::iterator i = m_surfaces.find(filename);
+ if (i != m_surfaces.end())
+ {
+ image = i->second;
+ }
+ else
+ {
+ 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());
+ }
+
+ 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) +
+ rect.top * image->pitch +
+ rect.left * image->format->BytesPerPixel,
+ rect.get_width(), rect.get_height(),
+ image->format->BitsPerPixel,
+ image->pitch,
+ image->format->Rmask,
+ image->format->Gmask,
+ image->format->Bmask,
+ image->format->Amask));
+ if (!subimage)
+ {
+ throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed");
+ }
+
+#ifdef OLD_SDL
+ if (image->format->palette)
+ { // copy the image palette to subimage if present
+ SDL_SetSurfacePalette(subimage.get(), image->format->palette->colors);
+ }