diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-02-24 11:41:53 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2025-02-26 21:18:09 +0000 |
commit | 593361ba81764dae93c02bd5bc4ee238db55aac1 (patch) | |
tree | 90d295bb82a1534fd750c27401f29652ecb95061 /src/gui | |
parent | a5d1cc6d7e37c4ca45b2dc390074e01f9a9589b7 (diff) | |
download | mana-593361ba81764dae93c02bd5bc4ee238db55aac1.tar.gz mana-593361ba81764dae93c02bd5bc4ee238db55aac1.tar.bz2 mana-593361ba81764dae93c02bd5bc4ee238db55aac1.tar.xz mana-593361ba81764dae93c02bd5bc4ee238db55aac1.zip |
Use ResourceRef for all resource types
All ResourceManager functions that load resources now return respective
ResourceRef values, which helps to make sure resources are properly
cleaned up.
The Sound class was cleaned up and now also allows SoundEffect resources
to be unloaded.
The Animation class now keeps its ImageSet loaded only as long as
necessary. Previously, SimpleAnimation and ParticleEmitter would keep
the ImageSet loaded indefinitely by never decreasing its reference
count.
Reduced duplicated animation loading code between SimpleAnimation and
ParticleEmitter.
Diffstat (limited to 'src/gui')
36 files changed, 137 insertions, 194 deletions
diff --git a/src/gui/emotepopup.cpp b/src/gui/emotepopup.cpp index a906eb7e..e759ab25 100644 --- a/src/gui/emotepopup.cpp +++ b/src/gui/emotepopup.cpp @@ -52,10 +52,7 @@ EmotePopup::EmotePopup() setVisible(true); } -EmotePopup::~EmotePopup() -{ - mSelectionImage->decRef(); -} +EmotePopup::~EmotePopup() = default; void EmotePopup::draw(gcn::Graphics *graphics) { diff --git a/src/gui/emotepopup.h b/src/gui/emotepopup.h index 19336426..c95c5723 100644 --- a/src/gui/emotepopup.h +++ b/src/gui/emotepopup.h @@ -23,6 +23,7 @@ #pragma once #include "gui/widgets/popup.h" +#include "resources/resource.h" #include <guichan/mouselistener.hpp> @@ -103,7 +104,7 @@ class EmotePopup : public Popup */ void distributeValueChangedEvent(); - Image *mSelectionImage; + ResourceRef<Image> mSelectionImage; int mSelectedEmoteId = -1; int mHoveredEmoteIndex = -1; diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp index e7eeb048..569bcf8b 100644 --- a/src/gui/equipmentwindow.cpp +++ b/src/gui/equipmentwindow.cpp @@ -96,10 +96,7 @@ void EquipmentWindow::loadEquipBoxes() Net::getInventoryHandler()->getBoxBackground(i); if (!backgroundFile.empty()) - { - box.backgroundImage = - Theme::instance()->getImageFromTheme(backgroundFile); - } + box.backgroundImage = Theme::getImageFromTheme(backgroundFile); } } diff --git a/src/gui/equipmentwindow.h b/src/gui/equipmentwindow.h index a9635279..a5cda4e3 100644 --- a/src/gui/equipmentwindow.h +++ b/src/gui/equipmentwindow.h @@ -28,6 +28,8 @@ #include <guichan/actionlistener.hpp> +#include <vector> + class Inventory; class Item; class ItemPopup; @@ -72,7 +74,7 @@ class EquipmentWindow : public Window, public gcn::ActionListener { int posX = 0; int posY = 0; - Image *backgroundImage = nullptr; + ResourceRef<Image> backgroundImage; }; std::vector<EquipBox> mBoxes; /**< Equipment boxes. */ diff --git a/src/gui/itempopup.cpp b/src/gui/itempopup.cpp index 7f1dec3b..c21f4003 100644 --- a/src/gui/itempopup.cpp +++ b/src/gui/itempopup.cpp @@ -153,8 +153,8 @@ void ItemPopup::setItem(const ItemInfo &item, bool showImage) if (showImage) { ResourceManager *resman = ResourceManager::getInstance(); - auto image = resman->getImageRef(paths.getStringValue("itemIcons") + - item.display.image); + auto image = resman->getImage(paths.getStringValue("itemIcons") + + item.display.image); mIcon->setImage(image); if (image) diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index 41044431..14e2a257 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -91,7 +91,7 @@ void Minimap::setMap(Map *map) minimapName = tempname; if (!minimapName.empty()) - mMapImage = resman->getImageRef(minimapName); + mMapImage = resman->getImage(minimapName); } if (mMapImage) diff --git a/src/gui/skilldialog.cpp b/src/gui/skilldialog.cpp index 5b85252d..b830064a 100644 --- a/src/gui/skilldialog.cpp +++ b/src/gui/skilldialog.cpp @@ -61,7 +61,7 @@ struct SkillInfo { unsigned short id; std::string name; - Image *icon = nullptr; + ResourceRef<Image> icon; bool modifiable; bool visible; SkillModel *model = nullptr; @@ -73,25 +73,16 @@ struct SkillInfo float progress; gcn::Color color; - ~SkillInfo() - { - if (icon) - icon->decRef(); - } + ~SkillInfo() = default; void setIcon(const std::string &iconPath) { ResourceManager *res = ResourceManager::getInstance(); if (!iconPath.empty()) - { icon = res->getImage(iconPath); - } if (!icon) - { - icon = Theme::getImageFromTheme( - paths.getStringValue("unknownItemFile")); - } + icon = Theme::getImageFromTheme(paths.getStringValue("unknownItemFile")); } void update(); diff --git a/src/gui/widgets/avatarlistbox.cpp b/src/gui/widgets/avatarlistbox.cpp index ec3327b2..47e29ed0 100644 --- a/src/gui/widgets/avatarlistbox.cpp +++ b/src/gui/widgets/avatarlistbox.cpp @@ -33,8 +33,8 @@ #include <guichan/font.hpp> int AvatarListBox::instances = 0; -Image *AvatarListBox::onlineIcon = nullptr; -Image *AvatarListBox::offlineIcon = nullptr; +ResourceRef<Image> AvatarListBox::onlineIcon; +ResourceRef<Image> AvatarListBox::offlineIcon; AvatarListBox::AvatarListBox(AvatarListModel *model): ListBox(model) @@ -56,8 +56,8 @@ AvatarListBox::~AvatarListBox() if (instances == 0) { - onlineIcon->decRef(); - offlineIcon->decRef(); + onlineIcon = nullptr; + offlineIcon = nullptr; } } diff --git a/src/gui/widgets/avatarlistbox.h b/src/gui/widgets/avatarlistbox.h index 638e6223..9b0588ac 100644 --- a/src/gui/widgets/avatarlistbox.h +++ b/src/gui/widgets/avatarlistbox.h @@ -23,6 +23,7 @@ #include "avatar.h" #include "gui/widgets/listbox.h" +#include "resources/resource.h" #include <string> @@ -53,6 +54,6 @@ public: private: static int instances; - static Image *onlineIcon; - static Image *offlineIcon; + static ResourceRef<Image> onlineIcon; + static ResourceRef<Image> offlineIcon; }; diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index 274f329b..ce7a856f 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -39,7 +39,7 @@ float Button::mAlpha = 1.0; ImageRect *Button::mButton; TextPopup *Button::mTextPopup = nullptr; -enum{ +enum { BUTTON_STANDARD, // 0 BUTTON_HIGHLIGHTED, // 1 BUTTON_PRESSED, // 2 @@ -80,18 +80,17 @@ Button::Button(const std::string &caption, const std::string &actionEventId, adjustSize(); } -bool Button::setButtonIcon(const std::string& iconFile) +bool Button::setButtonIcon(const std::string &iconFile) { // We clean up possible older references. - if (mButtonIcon) - removeButtonIcon(); + removeButtonIcon(); // If nothing relevant was set, we can quit now. if (iconFile.empty()) return false; // Load the icon frames. - Image *btnIcons = Theme::getImageFromTheme(iconFile); + auto btnIcons = Theme::getImageFromTheme(iconFile); if (!btnIcons) return false; @@ -101,36 +100,27 @@ bool Button::setButtonIcon(const std::string& iconFile) if (frameWidth > 0 && frameHeight > 0) { - mButtonIcon = new Image*[BUTTON_COUNT]; + mButtonIcon.resize(BUTTON_COUNT); + for (int mode = 0; mode < BUTTON_COUNT; ++mode) { - mButtonIcon[mode] = btnIcons->getSubImage(mode * frameWidth, 0, - frameWidth, frameHeight); + mButtonIcon[mode].reset( + btnIcons->getSubImage(mode * frameWidth, 0, frameWidth, frameHeight)); } adjustSize(); } - btnIcons->decRef(); - return (mButtonIcon); + return !mButtonIcon.empty(); } -void Button::removeButtonIcon(bool adjustButtonSize) +void Button::removeButtonIcon() { - if (!mButtonIcon) + if (mButtonIcon.empty()) return; - // Delete potential button icons - for (int mode = 0; mode < BUTTON_COUNT; ++mode) - { - delete mButtonIcon[mode]; - mButtonIcon[mode] = nullptr; - } - delete[] mButtonIcon; - mButtonIcon = nullptr; - - if (adjustButtonSize) - adjustSize(); + mButtonIcon.clear(); + adjustSize(); } void Button::init() @@ -144,7 +134,7 @@ void Button::init() for (int mode = 0; mode < BUTTON_COUNT; ++mode) { - Image *modeImage = Theme::getImageFromTheme(data[mode].file); + auto modeImage = Theme::getImageFromTheme(data[mode].file); int a = 0; for (int y = 0; y < 3; y++) { @@ -157,7 +147,6 @@ void Button::init() a++; } } - modeImage->decRef(); } updateAlpha(); @@ -185,8 +174,6 @@ Button::~Button() delete mTextPopup; mTextPopup = nullptr; } - // Don' try to readjust the size when it's about to be deleted. - removeButtonIcon(false); } void Button::updateAlpha() @@ -227,15 +214,12 @@ void Button::draw(gcn::Graphics *graphics) else graphics->setColor(Theme::getThemeColor(Theme::BUTTON)); + Image *icon = mButtonIcon.empty() ? nullptr : mButtonIcon[mode].get(); int textX = 0; int textY = getHeight() / 2 - getFont()->getHeight() / 2; int btnIconX = 0; - int btnIconY = getHeight() / 2 - - ((mButtonIcon && mButtonIcon[mode]) ? - mButtonIcon[mode]->getHeight() / 2 : 0); - - int btnIconWidth = (mButtonIcon && mButtonIcon[mode]) ? - mButtonIcon[mode]->getWidth() : 0; + int btnIconY = getHeight() / 2 - (icon ? icon->getHeight() / 2 : 0); + int btnIconWidth = icon ? icon->getWidth() : 0; switch (getAlignment()) { @@ -243,7 +227,7 @@ void Button::draw(gcn::Graphics *graphics) if (btnIconWidth) { btnIconX = 4; - textX = btnIconX + mButtonIcon[mode]->getWidth() + 2; + textX = btnIconX + icon->getWidth() + 2; } else { @@ -254,8 +238,8 @@ void Button::draw(gcn::Graphics *graphics) if (btnIconWidth) { btnIconX = getWidth() / 2 - (getFont()->getWidth(mCaption) - + mButtonIcon[mode]->getWidth() + 2) / 2; - textX = getWidth() / 2 + mButtonIcon[mode]->getWidth() / 2 + 2; + + icon->getWidth() + 2) / 2; + textX = getWidth() / 2 + icon->getWidth() / 2 + 2; } else { @@ -280,8 +264,7 @@ void Button::draw(gcn::Graphics *graphics) } if (btnIconWidth) - static_cast<Graphics*>(graphics)->drawImage(mButtonIcon[mode], - btnIconX, btnIconY); + static_cast<Graphics *>(graphics)->drawImage(icon, btnIconX, btnIconY); graphics->drawText(getCaption(), textX, textY, getAlignment()); } @@ -289,14 +272,13 @@ void Button::adjustSize() { // Size of the image button. int iconWidth = 0, iconHeight = 0; - if (mButtonIcon) + if (!mButtonIcon.empty()) { for (int mode = 0; mode < BUTTON_COUNT; ++mode) { - iconWidth = std::max(iconWidth, mButtonIcon[mode] ? - mButtonIcon[mode]->getWidth() + 2 : 0); - iconHeight = std::max(iconHeight, mButtonIcon[mode] ? - mButtonIcon[mode]->getHeight() : 0); + const Image *icon = mButtonIcon[mode].get(); + iconWidth = std::max(iconWidth, icon->getWidth() + 2); + iconHeight = std::max(iconHeight, icon->getHeight()); } } diff --git a/src/gui/widgets/button.h b/src/gui/widgets/button.h index 0f63c9f4..1502dc43 100644 --- a/src/gui/widgets/button.h +++ b/src/gui/widgets/button.h @@ -23,6 +23,9 @@ #include <guichan/widgets/button.hpp> +#include <memory> +#include <vector> + class ImageRect; class Image; class TextPopup; @@ -88,13 +91,13 @@ class Button : public gcn::Button private: void init(); - void removeButtonIcon(bool adjustButtonSize = true); + void removeButtonIcon(); static ImageRect* mButton; /**< Button state graphics */ static int mInstances; /**< Number of button instances */ static float mAlpha; - Image** mButtonIcon = nullptr; /**< Button Icons graphics */ + std::vector<std::unique_ptr<Image>> mButtonIcon; /**< Button Icons graphics */ /** * The buttons popup diff --git a/src/gui/widgets/checkbox.cpp b/src/gui/widgets/checkbox.cpp index 274855fd..399ffeb4 100644 --- a/src/gui/widgets/checkbox.cpp +++ b/src/gui/widgets/checkbox.cpp @@ -41,7 +41,7 @@ CheckBox::CheckBox(const std::string &caption, bool selected): { if (instances == 0) { - Image *checkBox = Theme::getImageFromTheme("checkbox.png"); + auto checkBox = Theme::getImageFromTheme("checkbox.png"); checkBoxNormal = checkBox->getSubImage(0, 0, 9, 10); checkBoxChecked = checkBox->getSubImage(9, 0, 9, 10); checkBoxDisabled = checkBox->getSubImage(18, 0, 9, 10); @@ -54,7 +54,6 @@ CheckBox::CheckBox(const std::string &caption, bool selected): checkBoxDisabledChecked->setAlpha(mAlpha); checkBoxNormalHi->setAlpha(mAlpha); checkBoxCheckedHi->setAlpha(mAlpha); - checkBox->decRef(); } instances++; diff --git a/src/gui/widgets/desktop.cpp b/src/gui/widgets/desktop.cpp index c8ded9f5..e424beec 100644 --- a/src/gui/widgets/desktop.cpp +++ b/src/gui/widgets/desktop.cpp @@ -105,7 +105,7 @@ void Desktop::setBestFittingWallpaper() return; ResourceManager *resman = ResourceManager::getInstance(); - auto wallpaper = resman->getImageRef(wallpaperName); + auto wallpaper = resman->getImage(wallpaperName); if (wallpaper) { diff --git a/src/gui/widgets/dropdown.cpp b/src/gui/widgets/dropdown.cpp index 8811eb8d..2c78a3d7 100644 --- a/src/gui/widgets/dropdown.cpp +++ b/src/gui/widgets/dropdown.cpp @@ -37,7 +37,7 @@ #include <algorithm> int DropDown::instances = 0; -Image *DropDown::buttons[2][2]; +ResourceRef<Image> DropDown::buttons[2][2]; ImageRect DropDown::skin; float DropDown::mAlpha = 1.0; @@ -65,7 +65,7 @@ DropDown::DropDown(gcn::ListModel *listModel): buttons[1][1]->setAlpha(mAlpha); // get the border skin - Image *boxBorder = Theme::getImageFromTheme("deepbox.png"); + auto boxBorder = Theme::getImageFromTheme("deepbox.png"); int gridx[4] = {0, 3, 28, 31}; int gridy[4] = {0, 3, 28, 31}; int a = 0; @@ -84,8 +84,6 @@ DropDown::DropDown(gcn::ListModel *listModel): } skin.setAlpha(mAlpha); - - boxBorder->decRef(); } instances++; @@ -97,10 +95,10 @@ DropDown::~DropDown() // Free images memory if (instances == 0) { - buttons[0][0]->decRef(); - buttons[0][1]->decRef(); - buttons[1][0]->decRef(); - buttons[1][1]->decRef(); + buttons[0][0] = nullptr; + buttons[0][1] = nullptr; + buttons[1][0] = nullptr; + buttons[1][1] = nullptr; std::for_each(skin.grid, skin.grid + 9, dtor<Image*>()); } diff --git a/src/gui/widgets/dropdown.h b/src/gui/widgets/dropdown.h index 9022e196..7c4e6d1d 100644 --- a/src/gui/widgets/dropdown.h +++ b/src/gui/widgets/dropdown.h @@ -21,6 +21,7 @@ #pragma once +#include "resources/resource.h" #include <guichan/widgets/dropdown.hpp> class Image; @@ -81,7 +82,7 @@ class DropDown : public gcn::DropDown // Add own Images. static int instances; - static Image *buttons[2][2]; + static ResourceRef<Image> buttons[2][2]; static ImageRect skin; static float mAlpha; }; diff --git a/src/gui/widgets/emoteshortcutcontainer.cpp b/src/gui/widgets/emoteshortcutcontainer.cpp index 812377e9..7f33538f 100644 --- a/src/gui/widgets/emoteshortcutcontainer.cpp +++ b/src/gui/widgets/emoteshortcutcontainer.cpp @@ -40,26 +40,18 @@ EmoteShortcutContainer::EmoteShortcutContainer() mBackgroundImg = Theme::getImageFromTheme("item_shortcut_bgr.png"); - mBackgroundImg->setAlpha(config.guiAlpha); - mMaxItems = std::min(EmoteDB::getEmoteCount(), MAX_ITEMS); - mBoxHeight = mBackgroundImg->getHeight(); - mBoxWidth = mBackgroundImg->getWidth(); -} - -EmoteShortcutContainer::~EmoteShortcutContainer() -{ - mBackgroundImg->decRef(); + if (mBackgroundImg) + { + mBoxHeight = mBackgroundImg->getHeight(); + mBoxWidth = mBackgroundImg->getWidth(); + } } void EmoteShortcutContainer::draw(gcn::Graphics *graphics) { - if (config.guiAlpha != mAlpha) - { - mAlpha = config.guiAlpha; - mBackgroundImg->setAlpha(mAlpha); - } + mBackgroundImg->setAlpha(config.guiAlpha); auto *g = static_cast<Graphics*>(graphics); diff --git a/src/gui/widgets/emoteshortcutcontainer.h b/src/gui/widgets/emoteshortcutcontainer.h index 08133a50..57d5efd2 100644 --- a/src/gui/widgets/emoteshortcutcontainer.h +++ b/src/gui/widgets/emoteshortcutcontainer.h @@ -33,8 +33,6 @@ class EmoteShortcutContainer : public ShortcutContainer public: EmoteShortcutContainer(); - ~EmoteShortcutContainer() override; - /** * Draws the items. */ diff --git a/src/gui/widgets/icon.cpp b/src/gui/widgets/icon.cpp index 67fd8384..61506a6b 100644 --- a/src/gui/widgets/icon.cpp +++ b/src/gui/widgets/icon.cpp @@ -27,7 +27,7 @@ #include "resources/resourcemanager.h" Icon::Icon(const std::string &file) - : Icon(ResourceManager::getInstance()->getImageRef(file)) + : Icon(ResourceManager::getInstance()->getImage(file)) { } diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index 940c69f4..d1d00677 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -64,7 +64,6 @@ ItemContainer::ItemContainer(Inventory *inventory): ItemContainer::~ItemContainer() { - mSelImg->decRef(); delete mItemPopup; } diff --git a/src/gui/widgets/itemcontainer.h b/src/gui/widgets/itemcontainer.h index 2fb6bb9e..7c972347 100644 --- a/src/gui/widgets/itemcontainer.h +++ b/src/gui/widgets/itemcontainer.h @@ -21,6 +21,8 @@ #pragma once +#include "resources/resource.h" + #include <guichan/keylistener.hpp> #include <guichan/mouselistener.hpp> #include <guichan/widget.hpp> @@ -177,7 +179,7 @@ class ItemContainer : public gcn::Widget, Inventory *mInventory; int mGridColumns = 1; int mGridRows = 1; - Image *mSelImg; + ResourceRef<Image> mSelImg; int mSelectedIndex = -1; int mHighlightedIndex = -1; int mLastUsedSlot = -1; diff --git a/src/gui/widgets/itemshortcutcontainer.cpp b/src/gui/widgets/itemshortcutcontainer.cpp index 0b8f0c8c..594ad1ce 100644 --- a/src/gui/widgets/itemshortcutcontainer.cpp +++ b/src/gui/widgets/itemshortcutcontainer.cpp @@ -48,25 +48,21 @@ ItemShortcutContainer::ItemShortcutContainer() mBackgroundImg = Theme::getImageFromTheme("item_shortcut_bgr.png"); mMaxItems = itemShortcut->getItemCount(); - mBackgroundImg->setAlpha(config.guiAlpha); - - mBoxHeight = mBackgroundImg->getHeight(); - mBoxWidth = mBackgroundImg->getWidth(); + if (mBackgroundImg) + { + mBoxHeight = mBackgroundImg->getHeight(); + mBoxWidth = mBackgroundImg->getWidth(); + } } ItemShortcutContainer::~ItemShortcutContainer() { - mBackgroundImg->decRef(); delete mItemPopup; } void ItemShortcutContainer::draw(gcn::Graphics *graphics) { - if (config.guiAlpha != mAlpha) - { - mAlpha = config.guiAlpha; - mBackgroundImg->setAlpha(mAlpha); - } + mBackgroundImg->setAlpha(config.guiAlpha); auto *g = static_cast<Graphics*>(graphics); diff --git a/src/gui/widgets/playerbox.cpp b/src/gui/widgets/playerbox.cpp index 3bdd6bd1..9858727d 100644 --- a/src/gui/widgets/playerbox.cpp +++ b/src/gui/widgets/playerbox.cpp @@ -42,7 +42,7 @@ PlayerBox::PlayerBox(const Being *being): if (instances == 0) { // Load the background skin - Image *textbox = Theme::getImageFromTheme("deepbox.png"); + auto textbox = Theme::getImageFromTheme("deepbox.png"); int bggridx[4] = {0, 3, 28, 31}; int bggridy[4] = {0, 3, 28, 31}; int a = 0; @@ -60,8 +60,6 @@ PlayerBox::PlayerBox(const Being *being): } background.setAlpha(config.guiAlpha); - - textbox->decRef(); } instances++; diff --git a/src/gui/widgets/progressbar.cpp b/src/gui/widgets/progressbar.cpp index 9d41d1af..ec9690ab 100644 --- a/src/gui/widgets/progressbar.cpp +++ b/src/gui/widgets/progressbar.cpp @@ -56,7 +56,7 @@ ProgressBar::ProgressBar(float progress, if (mInstances == 0) { - Image *dBorders = Theme::getImageFromTheme("vscroll_grey.png"); + auto dBorders = Theme::getImageFromTheme("vscroll_grey.png"); mBorder.grid[0] = dBorders->getSubImage(0, 0, 4, 4); mBorder.grid[1] = dBorders->getSubImage(4, 0, 3, 4); mBorder.grid[2] = dBorders->getSubImage(7, 0, 4, 4); @@ -68,8 +68,6 @@ ProgressBar::ProgressBar(float progress, mBorder.grid[8] = dBorders->getSubImage(7, 15, 4, 4); mBorder.setAlpha(mAlpha); - - dBorders->decRef(); } mInstances++; diff --git a/src/gui/widgets/progressindicator.cpp b/src/gui/widgets/progressindicator.cpp index 496bd8a1..a103c8b4 100644 --- a/src/gui/widgets/progressindicator.cpp +++ b/src/gui/widgets/progressindicator.cpp @@ -24,7 +24,6 @@ #include "simpleanimation.h" #include "resources/animation.h" -#include "resources/imageset.h" #include "resources/resourcemanager.h" #include "resources/theme.h" @@ -32,12 +31,12 @@ ProgressIndicator::ProgressIndicator() { - ImageSet *images = Theme::getImageSetFromTheme("progress-indicator.png", - 32, 32); + const std::string path = Theme::resolveThemePath("progress-indicator.png"); + mImageSet = ResourceManager::getInstance()->getImageSet(path, 32, 32); Animation anim; - for (size_t i = 0; i < images->size(); ++i) - anim.addFrame(images->get(i), 100, 0, 0); + for (size_t i = 0; i < mImageSet->size(); ++i) + anim.addFrame(mImageSet->get(i), 100, 0, 0); mIndicator = std::make_unique<SimpleAnimation>(std::move(anim)); diff --git a/src/gui/widgets/progressindicator.h b/src/gui/widgets/progressindicator.h index cb66a887..4a6ea339 100644 --- a/src/gui/widgets/progressindicator.h +++ b/src/gui/widgets/progressindicator.h @@ -20,6 +20,8 @@ #pragma once +#include "resources/imageset.h" + #include <guichan/widget.hpp> #include <memory> @@ -41,4 +43,5 @@ public: private: std::unique_ptr<SimpleAnimation> mIndicator; + ResourceRef<ImageSet> mImageSet; }; diff --git a/src/gui/widgets/radiobutton.cpp b/src/gui/widgets/radiobutton.cpp index 92cdacd1..9fcc34fa 100644 --- a/src/gui/widgets/radiobutton.cpp +++ b/src/gui/widgets/radiobutton.cpp @@ -29,12 +29,12 @@ int RadioButton::instances = 0; float RadioButton::mAlpha = 1.0; -Image *RadioButton::radioNormal; -Image *RadioButton::radioChecked; -Image *RadioButton::radioDisabled; -Image *RadioButton::radioDisabledChecked; -Image *RadioButton::radioNormalHi; -Image *RadioButton::radioCheckedHi; +ResourceRef<Image> RadioButton::radioNormal; +ResourceRef<Image> RadioButton::radioChecked; +ResourceRef<Image> RadioButton::radioDisabled; +ResourceRef<Image> RadioButton::radioDisabledChecked; +ResourceRef<Image> RadioButton::radioNormalHi; +ResourceRef<Image> RadioButton::radioCheckedHi; RadioButton::RadioButton(const std::string &caption, const std::string &group, bool marked): @@ -65,12 +65,12 @@ RadioButton::~RadioButton() if (instances == 0) { - radioNormal->decRef(); - radioChecked->decRef(); - radioDisabled->decRef(); - radioDisabledChecked->decRef(); - radioNormalHi->decRef(); - radioCheckedHi->decRef(); + radioNormal = nullptr; + radioChecked = nullptr; + radioDisabled = nullptr; + radioDisabledChecked = nullptr; + radioNormalHi = nullptr; + radioCheckedHi = nullptr; } } diff --git a/src/gui/widgets/radiobutton.h b/src/gui/widgets/radiobutton.h index b8026e56..4f6b5a49 100644 --- a/src/gui/widgets/radiobutton.h +++ b/src/gui/widgets/radiobutton.h @@ -21,6 +21,8 @@ #pragma once +#include "resources/resource.h" + #include <guichan/widgets/radiobutton.hpp> class Image; @@ -61,10 +63,10 @@ class RadioButton : public gcn::RadioButton static int instances; static float mAlpha; bool mHasMouse = false; - static Image *radioNormal; - static Image *radioChecked; - static Image *radioDisabled; - static Image *radioDisabledChecked; - static Image *radioNormalHi; - static Image *radioCheckedHi; + static ResourceRef<Image> radioNormal; + static ResourceRef<Image> radioChecked; + static ResourceRef<Image> radioDisabled; + static ResourceRef<Image> radioDisabledChecked; + static ResourceRef<Image> radioNormalHi; + static ResourceRef<Image> radioCheckedHi; }; diff --git a/src/gui/widgets/resizegrip.cpp b/src/gui/widgets/resizegrip.cpp index dd29a977..b7d2ec16 100644 --- a/src/gui/widgets/resizegrip.cpp +++ b/src/gui/widgets/resizegrip.cpp @@ -29,7 +29,7 @@ #include <guichan/graphics.hpp> -Image *ResizeGrip::gripImage = nullptr; +ResourceRef<Image> ResizeGrip::gripImage; int ResizeGrip::mInstances = 0; float ResizeGrip::mAlpha = 1.0; @@ -53,7 +53,7 @@ ResizeGrip::~ResizeGrip() mInstances--; if (mInstances == 0) - gripImage->decRef(); + gripImage = nullptr; } void ResizeGrip::draw(gcn::Graphics *graphics) diff --git a/src/gui/widgets/resizegrip.h b/src/gui/widgets/resizegrip.h index 7d496950..5638eff4 100644 --- a/src/gui/widgets/resizegrip.h +++ b/src/gui/widgets/resizegrip.h @@ -21,6 +21,8 @@ #pragma once +#include "resources/resource.h" + #include <guichan/widget.hpp> class Image; @@ -36,7 +38,6 @@ class ResizeGrip : public gcn::Widget { public: ResizeGrip(const std::string &image = "resize.png"); - ~ResizeGrip() override; /** @@ -45,7 +46,7 @@ class ResizeGrip : public gcn::Widget void draw(gcn::Graphics *graphics) override; private: - static Image *gripImage; /**< Resize grip image */ + static ResourceRef<Image> gripImage; /**< Resize grip image */ static int mInstances; /**< Number of resize grip instances */ static float mAlpha; }; diff --git a/src/gui/widgets/scrollarea.cpp b/src/gui/widgets/scrollarea.cpp index 225a231d..44065580 100644 --- a/src/gui/widgets/scrollarea.cpp +++ b/src/gui/widgets/scrollarea.cpp @@ -34,7 +34,7 @@ float ScrollArea::mAlpha = 1.0; ImageRect ScrollArea::background; ImageRect ScrollArea::vMarker; ImageRect ScrollArea::vMarkerHi; -Image *ScrollArea::buttons[4][2]; +ResourceRef<Image> ScrollArea::buttons[4][2]; ScrollArea::ScrollArea() { @@ -60,14 +60,14 @@ ScrollArea::~ScrollArea() std::for_each(vMarker.grid, vMarker.grid + 9, dtor<Image*>()); std::for_each(vMarkerHi.grid, vMarkerHi.grid + 9, dtor<Image*>()); - buttons[UP][0]->decRef(); - buttons[UP][1]->decRef(); - buttons[DOWN][0]->decRef(); - buttons[DOWN][1]->decRef(); - buttons[LEFT][0]->decRef(); - buttons[LEFT][1]->decRef(); - buttons[RIGHT][0]->decRef(); - buttons[RIGHT][1]->decRef(); + buttons[UP][0] = nullptr; + buttons[UP][1] = nullptr; + buttons[DOWN][0] = nullptr; + buttons[DOWN][1] = nullptr; + buttons[LEFT][0] = nullptr; + buttons[LEFT][1] = nullptr; + buttons[RIGHT][0] = nullptr; + buttons[RIGHT][1] = nullptr; } } @@ -84,7 +84,7 @@ void ScrollArea::init() if (instances == 0) { // Load the background skin - Image *textbox = Theme::getImageFromTheme("deepbox.png"); + auto textbox = Theme::getImageFromTheme("deepbox.png"); const int bggridx[4] = {0, 3, 28, 31}; const int bggridy[4] = {0, 3, 28, 31}; int a = 0; @@ -102,11 +102,9 @@ void ScrollArea::init() } background.setAlpha(config.guiAlpha); - textbox->decRef(); - // Load vertical scrollbar skin - Image *vscroll = Theme::getImageFromTheme("vscroll_grey.png"); - Image *vscrollHi = Theme::getImageFromTheme("vscroll_highlight.png"); + auto vscroll = Theme::getImageFromTheme("vscroll_grey.png"); + auto vscrollHi = Theme::getImageFromTheme("vscroll_highlight.png"); int vsgridx[4] = {0, 4, 7, 11}; int vsgridy[4] = {0, 4, 15, 19}; @@ -131,9 +129,6 @@ void ScrollArea::init() vMarker.setAlpha(config.guiAlpha); vMarkerHi.setAlpha(config.guiAlpha); - vscroll->decRef(); - vscrollHi->decRef(); - buttons[UP][0] = Theme::getImageFromTheme("vscroll_up_default.png"); buttons[DOWN][0] = diff --git a/src/gui/widgets/scrollarea.h b/src/gui/widgets/scrollarea.h index ef20610b..faec0e8f 100644 --- a/src/gui/widgets/scrollarea.h +++ b/src/gui/widgets/scrollarea.h @@ -21,8 +21,10 @@ #pragma once -#include <guichan/widgets/scrollarea.hpp> +#include "resources/resource.h" + #include <guichan/widgetlistener.hpp> +#include <guichan/widgets/scrollarea.hpp> class Image; class ImageRect; @@ -133,7 +135,7 @@ class ScrollArea : public gcn::ScrollArea, public gcn::WidgetListener static ImageRect background; static ImageRect vMarker; static ImageRect vMarkerHi; - static Image *buttons[4][2]; + static ResourceRef<Image> buttons[4][2]; int mX = 0; int mY = 0; diff --git a/src/gui/widgets/shortcutcontainer.cpp b/src/gui/widgets/shortcutcontainer.cpp index 5925752e..4bcf3860 100644 --- a/src/gui/widgets/shortcutcontainer.cpp +++ b/src/gui/widgets/shortcutcontainer.cpp @@ -21,11 +21,7 @@ #include "gui/widgets/shortcutcontainer.h" -float ShortcutContainer::mAlpha = 1.0; - -ShortcutContainer::ShortcutContainer() -{ -} +ShortcutContainer::~ShortcutContainer() = default; void ShortcutContainer::widgetResized(const gcn::Event &event) { diff --git a/src/gui/widgets/shortcutcontainer.h b/src/gui/widgets/shortcutcontainer.h index dd88bded..9998e188 100644 --- a/src/gui/widgets/shortcutcontainer.h +++ b/src/gui/widgets/shortcutcontainer.h @@ -21,12 +21,12 @@ #pragma once +#include "resources/image.h" + #include <guichan/mouselistener.hpp> #include <guichan/widget.hpp> #include <guichan/widgetlistener.hpp> -class Image; - /** * A generic shortcut container. * @@ -37,7 +37,8 @@ class ShortcutContainer : public gcn::Widget, public gcn::MouseListener { public: - ShortcutContainer(); + ShortcutContainer() = default; + ~ShortcutContainer(); /** * Draws the shortcuts @@ -69,9 +70,7 @@ class ShortcutContainer : public gcn::Widget, */ int getIndexFromGrid(int pointX, int pointY) const; - Image *mBackgroundImg; - - static float mAlpha; + ResourceRef<Image> mBackgroundImg; int mMaxItems = 0; int mBoxWidth = 0; diff --git a/src/gui/widgets/slider.cpp b/src/gui/widgets/slider.cpp index a7ba37e8..f0b61667 100644 --- a/src/gui/widgets/slider.cpp +++ b/src/gui/widgets/slider.cpp @@ -79,8 +79,8 @@ void Slider::init() // Load resources if (mInstances == 0) { - Image *slider = Theme::getImageFromTheme("slider.png"); - Image *sliderHi = Theme::getImageFromTheme("slider_hilight.png"); + auto slider = Theme::getImageFromTheme("slider.png"); + auto sliderHi = Theme::getImageFromTheme("slider_hilight.png"); x = 0; y = 0; w = 15; h = 6; @@ -111,9 +111,6 @@ void Slider::init() w = 9; h = 10; vGrip = slider->getSubImage(x, y, w, h); vGripHi = sliderHi->getSubImage(x, y, w, h); - - slider->decRef(); - sliderHi->decRef(); } mInstances++; @@ -205,4 +202,3 @@ void Slider::mouseExited(gcn::MouseEvent& event) { mHasMouse = false; } - diff --git a/src/gui/widgets/tab.cpp b/src/gui/widgets/tab.cpp index 64f49eac..d20e7993 100644 --- a/src/gui/widgets/tab.cpp +++ b/src/gui/widgets/tab.cpp @@ -88,17 +88,15 @@ void Tab::init() if (mInstances == 0) { // Load the skin - Image *tab[TAB_COUNT]; - for (int mode = 0; mode < TAB_COUNT; mode++) { - tab[mode] = Theme::getImageFromTheme(data[mode].file); + auto tabImage = Theme::getImageFromTheme(data[mode].file); int a = 0; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { - tabImg[mode].grid[a] = tab[mode]->getSubImage( + tabImg[mode].grid[a] = tabImage->getSubImage( data[mode].gridX[x], data[mode].gridY[y], data[mode].gridX[x + 1] - data[mode].gridX[x] + 1, data[mode].gridY[y + 1] - data[mode].gridY[y] + 1); @@ -106,7 +104,6 @@ void Tab::init() } } tabImg[mode].setAlpha(mAlpha); - tab[mode]->decRef(); } } mInstances++; diff --git a/src/gui/widgets/textfield.cpp b/src/gui/widgets/textfield.cpp index dd0adecd..be75a96f 100644 --- a/src/gui/widgets/textfield.cpp +++ b/src/gui/widgets/textfield.cpp @@ -53,7 +53,7 @@ TextField::TextField(const std::string &text, bool loseFocusOnTab): if (instances == 0) { // Load the skin - Image *textbox = Theme::getImageFromTheme("deepbox.png"); + auto textbox = Theme::getImageFromTheme("deepbox.png"); int gridx[4] = {0, 3, 28, 31}; int gridy[4] = {0, 3, 28, 31}; int a = 0; @@ -70,8 +70,6 @@ TextField::TextField(const std::string &text, bool loseFocusOnTab): } } skin.setAlpha(config.guiAlpha); - - textbox->decRef(); } instances++; |