Change imageToSurface to return a unique_ptr to avoid manual surface cleanup

0.6.3
Nikolay Kasyanov 7 years ago
parent 224b94c0ce
commit 359f87ab9f

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

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

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

Loading…
Cancel
Save