1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-22 11:46:14 +02:00

Removed most uses of naked Surface pointers

However, SurfaceCollection remains a very important user.
This commit is contained in:
Maarten ter Huurne 2014-08-10 14:21:14 +02:00
parent 4bd1c799bd
commit 60183e539d
16 changed files with 53 additions and 51 deletions

View File

@ -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);

View File

@ -29,6 +29,7 @@
#include <SDL.h>
#include <string>
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;

View File

@ -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"]);
{
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"]);
{
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(
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(
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;
}
}

View File

@ -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<OutputSurface> s;
Surface *bg;
std::unique_ptr<OffscreenSurface> bg;
std::unique_ptr<Font> font;
//Status functions

View File

@ -7,6 +7,7 @@
#include <SDL.h>
#include <string>
class OffscreenSurface;
class Surface;
class Touchscreen;
@ -34,7 +35,7 @@ private:
function_t action;
SDL_Rect rect, iconRect, labelRect;
Surface *iconSurface;
OffscreenSurface *iconSurface;
};
#endif

View File

@ -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);

View File

@ -27,7 +27,7 @@
#include <string>
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);

View File

@ -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;
}

View File

@ -28,6 +28,7 @@
class GMenu2X;
class Launcher;
class Surface;
/**
Parses links files.

View File

@ -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());

View File

@ -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;

View File

@ -281,17 +281,18 @@ void Surface::fillRectAlpha(SDL_Rect rect, RGBAColor c) {
// OffscreenSurface:
OffscreenSurface *OffscreenSurface::emptySurface(int width, int height)
unique_ptr<OffscreenSurface> 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<OffscreenSurface>();
SDL_FillRect(raw, nullptr, SDL_MapRGB(raw->format, 0, 0, 0));
return new OffscreenSurface(raw);
return unique_ptr<OffscreenSurface>(new OffscreenSurface(raw));
}
OffscreenSurface *OffscreenSurface::loadImage(
const string &img, const string &skin, bool loadAlpha)
unique_ptr<OffscreenSurface> 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<OffscreenSurface>();
}
return new OffscreenSurface(raw);
return unique_ptr<OffscreenSurface>(new OffscreenSurface(raw));
}
OffscreenSurface::OffscreenSurface(OffscreenSurface&& other)

View File

@ -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<OffscreenSurface> emptySurface(
int width, int height);
static std::unique_ptr<OffscreenSurface> 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);

View File

@ -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;
}

View File

@ -60,7 +60,7 @@ void TextDialog::drawText(const vector<string> &text, unsigned int y,
void TextDialog::exec() {
bool close = false;
OffscreenSurface bg(gmenu2x->bg);
OffscreenSurface bg(*gmenu2x->bg);
//link icon
if (!fileExists(icon))

View File

@ -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))