Change imageToSurface to return a unique_ptr to avoid manual surface cleanup

pull/456/head
Nikolay Kasyanov 6 years ago
parent 224b94c0ce
commit 359f87ab9f

@ -428,9 +428,8 @@ void OMW::Engine::setWindowIcon()
else
{
osg::ref_ptr<osg::Image> image = result.getImage();
SDL_Surface* surface = SDLUtil::imageToSurface(image, true);
SDL_SetWindowIcon(mWindow, surface);
SDL_FreeSurface(surface);
auto surface = SDLUtil::imageToSurface(image, true);
SDL_SetWindowIcon(mWindow, surface.get());
}
}

@ -6,7 +6,7 @@
namespace SDLUtil
{
SDL_Surface* imageToSurface(osg::Image *image, bool flip)
SurfaceUniquePtr imageToSurface(osg::Image *image, bool flip)
{
int width = image->s();
int height = image->t();
@ -22,7 +22,7 @@ SDL_Surface* imageToSurface(osg::Image *image, bool flip)
static_cast<Uint8>(clr.g() * 255), static_cast<Uint8>(clr.b() * 255), static_cast<Uint8>(clr.a() * 255));
}
return surface;
return SurfaceUniquePtr(surface, SDL_FreeSurface);
}
}

@ -1,6 +1,8 @@
#ifndef OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H
#define OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H
#include <memory>
struct SDL_Surface;
namespace osg
@ -10,10 +12,10 @@ namespace osg
namespace SDLUtil
{
typedef std::unique_ptr<SDL_Surface, void (*)(SDL_Surface *)> SurfaceUniquePtr;
/// Convert an osg::Image to an SDL_Surface.
/// @note The returned surface must be freed using SDL_FreeSurface.
SDL_Surface* imageToSurface(osg::Image* image, bool flip=false);
SurfaceUniquePtr imageToSurface(osg::Image* image, bool flip=false);
}

@ -221,9 +221,7 @@ namespace SDLUtil
}
#if OPENMW_USE_SOFTWARE_CURSOR_DECOMPRESSION
typedef std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)> SDLSurfacePtr;
SDLSurfacePtr decompress(osg::Image* source, int rotDegrees)
SurfaceUniquePtr decompress(osg::Image* source, int rotDegrees)
{
int width = source->s();
int height = source->t();
@ -265,7 +263,7 @@ namespace SDLUtil
SDL_FreeSurface(cursorSurface);
SDL_DestroyRenderer(renderer);
return SDLSurfacePtr(targetSurface, SDL_FreeSurface);
return SurfaceUniquePtr(targetSurface, SDL_FreeSurface);
}
#endif
@ -285,18 +283,13 @@ namespace SDLUtil
return;
}
SDL_Surface* surf = SDLUtil::imageToSurface(decompressed, true);
//set the cursor and store it for later
SDL_Cursor* curs = SDL_CreateColorCursor(surf, hotspot_x, hotspot_y);
//clean up
SDL_FreeSurface(surf);
auto surf = SDLUtil::imageToSurface(decompressed, true);
#else
auto surf = decompress(image, rotDegrees);
#endif
//set the cursor and store it for later
SDL_Cursor* curs = SDL_CreateColorCursor(surf.get(), hotspot_x, hotspot_y);
#endif
mCursorMap.insert(CursorMap::value_type(std::string(name), curs));
}

Loading…
Cancel
Save