From 359f87ab9f4ad628b231705bcfd7b5f76c30071f Mon Sep 17 00:00:00 2001 From: Nikolay Kasyanov Date: Sat, 16 Jun 2018 12:12:32 +0200 Subject: [PATCH] Change imageToSurface to return a unique_ptr to avoid manual surface cleanup --- apps/openmw/engine.cpp | 5 ++--- components/sdlutil/imagetosurface.cpp | 4 ++-- components/sdlutil/imagetosurface.hpp | 6 ++++-- components/sdlutil/sdlcursormanager.cpp | 17 +++++------------ 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 78e368cfc..65ea181fb 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -428,9 +428,8 @@ void OMW::Engine::setWindowIcon() else { osg::ref_ptr 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()); } } diff --git a/components/sdlutil/imagetosurface.cpp b/components/sdlutil/imagetosurface.cpp index 6313c0a8f..6e68c0f45 100644 --- a/components/sdlutil/imagetosurface.cpp +++ b/components/sdlutil/imagetosurface.cpp @@ -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(clr.g() * 255), static_cast(clr.b() * 255), static_cast(clr.a() * 255)); } - return surface; + return SurfaceUniquePtr(surface, SDL_FreeSurface); } } diff --git a/components/sdlutil/imagetosurface.hpp b/components/sdlutil/imagetosurface.hpp index ad0457433..9ce56b909 100644 --- a/components/sdlutil/imagetosurface.hpp +++ b/components/sdlutil/imagetosurface.hpp @@ -1,6 +1,8 @@ #ifndef OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H #define OPENMW_COMPONENTS_SDLUTIL_IMAGETOSURFACE_H +#include + struct SDL_Surface; namespace osg @@ -10,10 +12,10 @@ namespace osg namespace SDLUtil { + typedef std::unique_ptr 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); } diff --git a/components/sdlutil/sdlcursormanager.cpp b/components/sdlutil/sdlcursormanager.cpp index 368d8e502..bf265dc7e 100644 --- a/components/sdlutil/sdlcursormanager.cpp +++ b/components/sdlutil/sdlcursormanager.cpp @@ -221,9 +221,7 @@ namespace SDLUtil } #if OPENMW_USE_SOFTWARE_CURSOR_DECOMPRESSION - typedef std::unique_ptr 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)); }