diff --git a/src/browsedialog.cpp b/src/browsedialog.cpp index 9cce30a..f0fc906 100644 --- a/src/browsedialog.cpp +++ b/src/browsedialog.cpp @@ -230,7 +230,7 @@ void BrowseDialog::paint() unsigned int firstElement, lastElement; unsigned int offsetY; - OffscreenSurface bg(gmenu2x->bg); + OffscreenSurface bg(*gmenu2x->bg); drawTitleIcon(bg, "icons/explorer.png", true); writeTitle(bg, title); writeSubTitle(bg, subtitle); diff --git a/src/browsedialog.h b/src/browsedialog.h index c1c4634..61d2657 100644 --- a/src/browsedialog.h +++ b/src/browsedialog.h @@ -29,6 +29,7 @@ #include #include +class OffscreenSurface; class Touchscreen; class BrowseDialog : protected Dialog { @@ -72,9 +73,9 @@ private: bool ts_pressed; - Surface *iconGoUp; - Surface *iconFolder; - Surface *iconFile; + OffscreenSurface *iconGoUp; + OffscreenSurface *iconFolder; + OffscreenSurface *iconFile; ButtonBox buttonBox; diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 412331e..9bff18e 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -313,9 +313,9 @@ GMenu2X::~GMenu2X() { void GMenu2X::initBG() { sc.del("bgmain"); + bg.reset(); // Load wallpaper. - delete bg; bg = OffscreenSurface::loadImage(confStr["wallpaper"]); if (!bg) { bg = OffscreenSurface::emptySurface(resX, resY); @@ -326,20 +326,22 @@ void GMenu2X::initBG() { OffscreenSurface *bgmain = sc.add(*bg, "bgmain"); - Surface *sd = OffscreenSurface::loadImage("imgs/sd.png", confStr["skin"]); - if (sd) sd->blit(*bgmain, 3, bottomBarIconY); + { + auto sd = OffscreenSurface::loadImage("imgs/sd.png", confStr["skin"]); + if (sd) sd->blit(*bgmain, 3, bottomBarIconY); + } string df = getDiskFree(getHome().c_str()); font->write(*bgmain, df, 22, bottomBarTextY, Font::HAlignLeft, Font::VAlignMiddle); - delete sd; - cpuX = font->getTextWidth(df)+32; + cpuX = font->getTextWidth(df) + 32; #ifdef ENABLE_CPUFREQ - Surface *cpu = OffscreenSurface::loadImage("imgs/cpu.png", confStr["skin"]); - if (cpu) cpu->blit(bgmain, cpuX, bottomBarIconY); + { + auto cpu = OffscreenSurface::loadImage("imgs/cpu.png", confStr["skin"]); + if (cpu) cpu->blit(bgmain, cpuX, bottomBarIconY); + } cpuX += 19; - manualX = cpuX+font->getTextWidth("300MHz")+5; - delete cpu; + manualX = cpuX + font->getTextWidth("300MHz") + 5; #else manualX = cpuX; #endif @@ -347,24 +349,22 @@ void GMenu2X::initBG() { int serviceX = resX-38; if (usbnet) { if (web) { - Surface *webserver = OffscreenSurface::loadImage( - "imgs/webserver.png", confStr["skin"]); + auto webserver = OffscreenSurface::loadImage( + "imgs/webserver.png", confStr["skin"]); if (webserver) webserver->blit(*bgmain, serviceX, bottomBarIconY); serviceX -= 19; - delete webserver; } if (samba) { - Surface *sambaS = OffscreenSurface::loadImage( - "imgs/samba.png", confStr["skin"]); + auto sambaS = OffscreenSurface::loadImage( + "imgs/samba.png", confStr["skin"]); if (sambaS) sambaS->blit(*bgmain, serviceX, bottomBarIconY); serviceX -= 19; - delete sambaS; } if (inet) { - Surface *inetS = OffscreenSurface::loadImage("imgs/inet.png", confStr["skin"]); + auto inetS = OffscreenSurface::loadImage( + "imgs/inet.png", confStr["skin"]); if (inetS) inetS->blit(*bgmain, serviceX, bottomBarIconY); serviceX -= 19; - delete inetS; } } diff --git a/src/gmenu2x.h b/src/gmenu2x.h index dcb6194..8552320 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -42,8 +42,6 @@ class Launcher; class Layer; class MediaMonitor; class Menu; -class OutputSurface; -class Surface; #ifndef GMENU2X_SYSTEM_DIR #define GMENU2X_SYSTEM_DIR "/usr/share/gmenu2x" @@ -160,7 +158,7 @@ public: SurfaceCollection sc; Translator tr; std::unique_ptr s; - Surface *bg; + std::unique_ptr bg; std::unique_ptr font; //Status functions diff --git a/src/iconbutton.h b/src/iconbutton.h index 34ae21b..02c1a84 100644 --- a/src/iconbutton.h +++ b/src/iconbutton.h @@ -7,6 +7,7 @@ #include #include +class OffscreenSurface; class Surface; class Touchscreen; @@ -34,7 +35,7 @@ private: function_t action; SDL_Rect rect, iconRect, labelRect; - Surface *iconSurface; + OffscreenSurface *iconSurface; }; #endif diff --git a/src/inputdialog.cpp b/src/inputdialog.cpp index 2f0071b..c48feb5 100644 --- a/src/inputdialog.cpp +++ b/src/inputdialog.cpp @@ -149,7 +149,7 @@ bool InputDialog::exec() { Uint32 caretTick = 0, curTick; bool caretOn = true; - OffscreenSurface bg(gmenu2x->bg); + OffscreenSurface bg(*gmenu2x->bg); drawTitleIcon(bg, icon, false); writeTitle(bg, title); writeSubTitle(bg, text); diff --git a/src/link.h b/src/link.h index 48260af..ba8a01d 100644 --- a/src/link.h +++ b/src/link.h @@ -27,7 +27,7 @@ #include class GMenu2X; -class Surface; +class OffscreenSurface; class Touchscreen; @@ -68,8 +68,7 @@ protected: bool edited; std::string title, description, launchMsg, icon, iconPath; - Surface *iconSurface; - Surface *icon_hover; + OffscreenSurface *iconSurface; virtual const std::string &searchIcon(); void setIconPath(const std::string &icon); diff --git a/src/linkapp.cpp b/src/linkapp.cpp index 0c562bd..6cdd7ce 100644 --- a/src/linkapp.cpp +++ b/src/linkapp.cpp @@ -433,11 +433,11 @@ void LinkApp::showManual() { gmenu2x->setSafeMaxClock(); #endif - OffscreenSurface *pngman = OffscreenSurface::loadImage(manual); + auto pngman = OffscreenSurface::loadImage(manual); if (!pngman) { return; } - OffscreenSurface *bg = OffscreenSurface::loadImage(gmenu2x->confStr["wallpaper"]); + auto bg = OffscreenSurface::loadImage(gmenu2x->confStr["wallpaper"]); if (!bg) { bg = OffscreenSurface::emptySurface(gmenu2x->s->width(), gmenu2x->s->height()); } @@ -502,7 +502,6 @@ void LinkApp::showManual() { break; } } - delete bg; return; } diff --git a/src/linkapp.h b/src/linkapp.h index 5ce8d2a..3ac34b9 100644 --- a/src/linkapp.h +++ b/src/linkapp.h @@ -28,6 +28,7 @@ class GMenu2X; class Launcher; +class Surface; /** Parses links files. diff --git a/src/selector.cpp b/src/selector.cpp index 61d61f9..dfc5e7b 100644 --- a/src/selector.cpp +++ b/src/selector.cpp @@ -59,7 +59,7 @@ int Selector::exec(int startSelection) { fl.setFilter(link->getSelectorFilter()); fl.browse(); - OffscreenSurface bg(gmenu2x->bg); + OffscreenSurface bg(*gmenu2x->bg); drawTitleIcon(bg, link->getIconPath(), true); writeTitle(bg, link->getTitle()); writeSubTitle(bg, link->getDescription()); diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index 1a2e700..fbce2ff 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -50,7 +50,7 @@ SettingsDialog::~SettingsDialog() { } bool SettingsDialog::exec() { - OffscreenSurface bg(gmenu2x->bg); + OffscreenSurface bg(*gmenu2x->bg); bg.convertToDisplayFormat(); bool close = false, ts_pressed = false; diff --git a/src/surface.cpp b/src/surface.cpp index 1f99a6e..16bed8d 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -281,17 +281,18 @@ void Surface::fillRectAlpha(SDL_Rect rect, RGBAColor c) { // OffscreenSurface: -OffscreenSurface *OffscreenSurface::emptySurface(int width, int height) +unique_ptr OffscreenSurface::emptySurface( + int width, int height) { SDL_Surface *raw = SDL_CreateRGBSurface( SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0); - if (!raw) return nullptr; + if (!raw) return unique_ptr(); SDL_FillRect(raw, nullptr, SDL_MapRGB(raw->format, 0, 0, 0)); - return new OffscreenSurface(raw); + return unique_ptr(new OffscreenSurface(raw)); } -OffscreenSurface *OffscreenSurface::loadImage( - const string &img, const string &skin, bool loadAlpha) +unique_ptr OffscreenSurface::loadImage( + string const& img,string const& skin, bool loadAlpha) { string skinpath; if (!skin.empty() && !img.empty() && img[0]!='/') @@ -302,10 +303,10 @@ OffscreenSurface *OffscreenSurface::loadImage( SDL_Surface *raw = loadPNG(skinpath, loadAlpha); if (!raw) { ERROR("Couldn't load surface '%s'\n", img.c_str()); - return nullptr; + return unique_ptr(); } - return new OffscreenSurface(raw); + return unique_ptr(new OffscreenSurface(raw)); } OffscreenSurface::OffscreenSurface(OffscreenSurface&& other) diff --git a/src/surface.h b/src/surface.h index b5d90df..a4a9f74 100644 --- a/src/surface.h +++ b/src/surface.h @@ -107,14 +107,14 @@ private: */ class OffscreenSurface: public Surface { public: - static OffscreenSurface *emptySurface(int width, int height); - static OffscreenSurface *loadImage(const std::string &img, - const std::string &skin="", bool loadAlpha=true); - - // TODO: Remove this once naked Surface pointers are no longer in use. - OffscreenSurface(Surface *other) : Surface(*other) {} + static std::unique_ptr emptySurface( + int width, int height); + static std::unique_ptr loadImage( + std::string const& img, std::string const& skin = "", + bool loadAlpha = true); OffscreenSurface(Surface const& other) : Surface(other) {} + OffscreenSurface(OffscreenSurface const& other) : Surface(other) {} OffscreenSurface(OffscreenSurface&& other); ~OffscreenSurface(); OffscreenSurface& operator=(OffscreenSurface other); diff --git a/src/surfacecollection.cpp b/src/surfacecollection.cpp index 25a762e..02e9038 100644 --- a/src/surfacecollection.cpp +++ b/src/surfacecollection.cpp @@ -120,7 +120,8 @@ OffscreenSurface *SurfaceCollection::add(const string &path) { } DEBUG("Adding surface: '%s'\n", path.c_str()); - auto s = OffscreenSurface::loadImage(filePath, "", defaultAlpha); + // TODO: Be safe. + auto s = OffscreenSurface::loadImage(filePath, "", defaultAlpha).release(); if (s) { surfaces[path] = s; } @@ -136,7 +137,8 @@ OffscreenSurface *SurfaceCollection::addSkinRes(const string &path, bool useDefa return NULL; DEBUG("Adding skin surface: '%s'\n", path.c_str()); - auto s = OffscreenSurface::loadImage(skinpath); + // TODO: Be safe. + auto s = OffscreenSurface::loadImage(skinpath).release(); if (s) { surfaces[path] = s; } diff --git a/src/textdialog.cpp b/src/textdialog.cpp index b524003..de74310 100644 --- a/src/textdialog.cpp +++ b/src/textdialog.cpp @@ -60,7 +60,7 @@ void TextDialog::drawText(const vector &text, unsigned int y, void TextDialog::exec() { bool close = false; - OffscreenSurface bg(gmenu2x->bg); + OffscreenSurface bg(*gmenu2x->bg); //link icon if (!fileExists(icon)) diff --git a/src/textmanualdialog.cpp b/src/textmanualdialog.cpp index 6a18c0d..d846ef3 100644 --- a/src/textmanualdialog.cpp +++ b/src/textmanualdialog.cpp @@ -68,7 +68,7 @@ TextManualDialog::TextManualDialog(GMenu2X *gmenu2x, const string &title, const } void TextManualDialog::exec() { - OffscreenSurface bg(gmenu2x->bg); + OffscreenSurface bg(*gmenu2x->bg); //link icon if (!fileExists(icon))