From 306ad2effe4d0897453e61ad787e01dc47c33076 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Tue, 12 Mar 2024 13:24:16 +0100 Subject: General code cleanups * Use default member initializers * Use range-based for loops * Avoid needless pointer references for ShopItem::mDuplicates * Removed type aliases that are only used once or twice * Removed more unused includes * Removed some unused functions * Removed superfluous .c_str() * Rely on default copy and assignment operators for Vector class * Use std::unique_ptr in some places * Removed duplicated mPlayerMoney updating in SellDialog * Removed duplicated Game::handleInput call * Removed unused SDLInput::mMouseInWindow * Removed remnant of manual widget positioning in HelpWindow * Removed superfluous initialization of static pointers --- src/client.cpp | 10 +++------ src/compoundsprite.cpp | 35 +++++++++++++++++------------- src/compoundsprite.h | 3 --- src/configuration.cpp | 47 ++++++++++++++++------------------------- src/configuration.h | 17 ++++++--------- src/event.cpp | 42 ------------------------------------ src/event.h | 37 ++------------------------------ src/game.cpp | 24 ++++++++++----------- src/gui/chatwindow.cpp | 25 ++++++++-------------- src/gui/chatwindow.h | 3 +-- src/gui/confirmdialog.cpp | 2 -- src/gui/helpwindow.cpp | 5 ----- src/gui/itempopup.cpp | 2 +- src/gui/npcdialog.cpp | 4 +--- src/gui/okdialog.cpp | 2 -- src/gui/outfitwindow.cpp | 7 ------ src/gui/palette.cpp | 2 +- src/gui/palette.h | 9 ++++---- src/gui/sdlinput.cpp | 12 +---------- src/gui/sdlinput.h | 15 ++++++------- src/gui/selldialog.cpp | 2 -- src/gui/serverdialog.h | 4 +--- src/gui/setup_video.cpp | 2 +- src/gui/skilldialog.cpp | 6 ++---- src/gui/skilldialog.h | 3 +-- src/gui/socialwindow.cpp | 45 +++++++++++++-------------------------- src/gui/socialwindow.h | 19 +++++++---------- src/gui/statuswindow.h | 3 +-- src/gui/tradewindow.h | 5 ++--- src/gui/truetypefont.cpp | 5 +---- src/gui/viewport.h | 3 +-- src/gui/widgets/button.cpp | 3 +-- src/gui/widgets/icon.cpp | 4 +--- src/gui/widgets/icon.h | 2 +- src/gui/widgets/layout.cpp | 21 ++++++++++++------ src/gui/widgets/linkhandler.h | 2 +- src/gui/widgets/shopitems.cpp | 12 ++++------- src/gui/widgets/tabbedarea.cpp | 28 +++++++++++------------- src/gui/widgets/tabbedarea.h | 3 +-- src/gui/widgets/textfield.h | 7 ++---- src/gui/worldselectdialog.cpp | 15 +++++-------- src/gui/worldselectdialog.h | 4 ++-- src/inventory.cpp | 6 ++---- src/inventory.h | 5 ++--- src/localplayer.cpp | 8 +++---- src/localplayer.h | 3 +-- src/net/tmwa/gui/guildtab.cpp | 4 ---- src/net/tmwa/gui/guildtab.h | 1 - src/net/tmwa/gui/partytab.cpp | 8 +------ src/net/tmwa/gui/partytab.h | 1 - src/particle.cpp | 21 ++---------------- src/particle.h | 42 ++++++++++++++++++------------------ src/particlecontainer.cpp | 6 ++---- src/particlecontainer.h | 8 +++---- src/playerinfo.cpp | 24 ++++++++++----------- src/playerinfo.h | 7 ++---- src/resources/beinginfo.h | 2 -- src/resources/monsterdb.cpp | 2 +- src/resources/npcdb.cpp | 2 +- src/resources/resourcemanager.h | 6 ++---- src/resources/specialdb.cpp | 3 ++- src/resources/specialdb.h | 3 --- src/resources/spritedef.h | 2 -- src/resources/theme.cpp | 2 +- src/resources/theme.h | 10 +++------ src/resources/wallpaper.cpp | 15 +++---------- src/shopitem.cpp | 46 ++++++++-------------------------------- src/shopitem.h | 25 +++++----------------- src/textmanager.cpp | 6 ++---- src/textmanager.h | 3 +-- src/vector.h | 24 ++++----------------- 71 files changed, 250 insertions(+), 546 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index 18d95ac8..1348112d 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -252,7 +252,7 @@ Client::Client(const Options &options): if (!resman->setWriteDir(mLocalDataDir)) { - logger->error(strprintf("%s couldn't be set as home directory! " + logger->error(strprintf("%s couldn't be set as write directory! " "Exiting.", mLocalDataDir.c_str())); } @@ -505,9 +505,6 @@ int Client::exec() mState = STATE_EXIT; break; - case SDL_KEYDOWN: - break; - case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_SIZE_CHANGED: @@ -663,8 +660,7 @@ int Client::exec() // lower than the default value Theme::instance()->setMinimumOpacity(0.8f); - if (mOptions.username.empty() - || mOptions.password.empty()) + if (mOptions.username.empty() || mOptions.password.empty()) { mCurrentDialog = new LoginDialog(&loginData); } @@ -701,7 +697,7 @@ int Client::exec() } else { - mCurrentDialog = new WorldSelectDialog(worlds); + mCurrentDialog = new WorldSelectDialog(std::move(worlds)); if (mOptions.chooseDefault) { ((WorldSelectDialog*) mCurrentDialog)->action( diff --git a/src/compoundsprite.cpp b/src/compoundsprite.cpp index 68cdc095..f2621384 100644 --- a/src/compoundsprite.cpp +++ b/src/compoundsprite.cpp @@ -247,21 +247,24 @@ void CompoundSprite::redraw() const #endif mWidth = mHeight = mOffsetX = mOffsetY = 0; - Sprite *s = nullptr; - SpriteConstIterator it, it_end = mSprites.end(); int posX = 0; int posY = 0; - for (it = mSprites.begin(); it != it_end; ++it) + for (auto sprite : mSprites) { - s = *it; - - if (s) - { - updateValues(mWidth, posX, s->getWidth() / 2, s->getWidth() / 2, s->getOffsetX()); - updateValues(mHeight, posY, s->getHeight(), 0, s->getOffsetY()); - } + if (!sprite) + continue; + + updateValues(mWidth, posX, + sprite->getWidth() / 2, + sprite->getWidth() / 2, + sprite->getOffsetX()); + + updateValues(mHeight, posY, + sprite->getHeight(), + 0, + sprite->getOffsetY()); } if (mWidth == 0 && mHeight == 0) @@ -296,12 +299,14 @@ void CompoundSprite::redraw() const graphics->setTarget(surface); graphics->_beginDraw(); - for (it = mSprites.begin(); it != it_end; ++it) + for (auto sprite : mSprites) { - s = *it; + if (!sprite) + continue; - if (s) - s->draw(graphics, posX - s->getWidth() / 2, posY - s->getHeight()); + sprite->draw(graphics, + posX - sprite->getWidth() / 2, + posY - sprite->getHeight()); } // Uncomment to see buffer sizes @@ -316,7 +321,7 @@ void CompoundSprite::redraw() const 32, rmask, gmask, bmask, amask); SDL_SetAlpha(surface, 0, SDL_ALPHA_OPAQUE); - SDL_BlitSurface(surface, NULL, surfaceA, NULL); + SDL_BlitSurface(surface, nullptr, surfaceA, nullptr); delete mImage; delete mAlphaImage; diff --git a/src/compoundsprite.h b/src/compoundsprite.h index 7c91eb73..a309ad3d 100644 --- a/src/compoundsprite.h +++ b/src/compoundsprite.h @@ -90,9 +90,6 @@ public: { mNeedsRedraw = true; } private: - using SpriteIterator = std::vector::iterator; - using SpriteConstIterator = std::vector::const_iterator; - void redraw() const; mutable Image *mImage = nullptr; diff --git a/src/configuration.cpp b/src/configuration.cpp index ca1f4438..7d2b496f 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -47,48 +47,41 @@ std::string ConfigurationObject::getValue(const std::string &key, const std::string &deflt) const { auto iter = mOptions.find(key); - return ((iter != mOptions.end()) ? iter->second : deflt); + return iter != mOptions.end() ? iter->second : deflt; } int ConfigurationObject::getValue(const std::string &key, int deflt) const { auto iter = mOptions.find(key); - return (iter != mOptions.end()) ? atoi(iter->second.c_str()) : deflt; + return iter != mOptions.end() ? atoi(iter->second.c_str()) : deflt; } unsigned ConfigurationObject::getValue(const std::string &key, unsigned deflt) const { auto iter = mOptions.find(key); - return (iter != mOptions.end()) ? atol(iter->second.c_str()) : deflt; + return iter != mOptions.end() ? atol(iter->second.c_str()) : deflt; } double ConfigurationObject::getValue(const std::string &key, double deflt) const { auto iter = mOptions.find(key); - return (iter != mOptions.end()) ? atof(iter->second.c_str()) : deflt; + return iter != mOptions.end() ? atof(iter->second.c_str()) : deflt; } -void ConfigurationObject::deleteList(const std::string &name) +void ConfigurationObject::deleteList(std::list &list) { - for (ConfigurationList::const_iterator - it = mContainerOptions[name].begin(); - it != mContainerOptions[name].end(); it++) - { - delete *it; - } + for (auto element : list) + delete element; - mContainerOptions[name].clear(); + list.clear(); } void ConfigurationObject::clear() { - for (std::map::const_iterator - it = mContainerOptions.begin(); it != mContainerOptions.end(); it++) - { - deleteList(it->first); - } + for (auto &[_, list] : mContainerOptions) + deleteList(list); mOptions.clear(); } @@ -150,7 +143,7 @@ int Configuration::getIntValue(const std::string &key) const auto iter = mOptions.find(key); if (iter == mOptions.end()) { - VariableData* vd = getDefault(key, VariableData::DATA_INT); + VariableData *vd = getDefault(key, VariableData::DATA_INT); if (vd) defaultValue = ((IntData*)vd)->getData(); } @@ -289,18 +282,15 @@ void ConfigurationObject::writeToXML(xmlTextWriterPtr writer) xmlTextWriterEndElement(writer); } - for (std::map::const_iterator - it = mContainerOptions.begin(); it != mContainerOptions.end(); it++) + for (auto &[name, list] : mContainerOptions) { - const char *name = it->first.c_str(); - xmlTextWriterStartElement(writer, BAD_CAST "list"); - xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST name); + xmlTextWriterWriteAttribute(writer, BAD_CAST "name", BAD_CAST name.c_str()); // Recurse on all elements - for (auto element : it->second) + for (auto element : list) { - xmlTextWriterStartElement(writer, BAD_CAST name); + xmlTextWriterStartElement(writer, BAD_CAST name.c_str()); element->writeToXML(writer); xmlTextWriterEndElement(writer); } @@ -319,10 +309,9 @@ void Configuration::write() mConfigPath.c_str()); return; } - else - { - fclose(testFile); - } + + fclose(testFile); + xmlTextWriterPtr writer = xmlNewTextWriterFilename(mConfigPath.c_str(), 0); diff --git a/src/configuration.h b/src/configuration.h index 5864829e..81806ad9 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -65,7 +65,7 @@ class ConfigurationListManager virtual CONT readConfigItem(ConfigurationObject *obj, CONT container) = 0; - virtual ~ConfigurationListManager() {} + virtual ~ConfigurationListManager() = default; }; /** @@ -127,8 +127,8 @@ class ConfigurationObject ConfigurationListManager *manager) { auto *nextobj = new ConfigurationObject; - deleteList(name); - ConfigurationList *list = &(mContainerOptions[name]); + std::list &list = mContainerOptions[name]; + deleteList(list); for (IT it = begin; it != end; it++) { @@ -137,7 +137,7 @@ class ConfigurationObject { // wrote something assert (wrobj == nextobj); nextobj = new ConfigurationObject; - list->push_back(wrobj); + list.push_back(wrobj); } else { @@ -174,13 +174,10 @@ class ConfigurationObject virtual void initFromXML(xmlNodePtr node); virtual void writeToXML(xmlTextWriterPtr writer); - void deleteList(const std::string &name); + void deleteList(std::list &list); - using Options = std::map; - Options mOptions; - - using ConfigurationList = std::list; - std::map mContainerOptions; + std::map mOptions; + std::map> mContainerOptions; }; /** diff --git a/src/event.cpp b/src/event.cpp index aba2996f..7c9b6f45 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -57,13 +57,6 @@ int Event::getInt(const std::string &key) const return static_cast(it->second)->getData(); } -bool Event::hasInt(const std::string &key) const -{ - auto it = mData.find(key); - return !(it == mData.end() - || it->second->getType() != VariableData::DATA_INT); -} - // Strings void Event::setString(const std::string &key, const std::string &value) @@ -87,13 +80,6 @@ const std::string &Event::getString(const std::string &key) const } -bool Event::hasString(const std::string &key) const -{ - auto it = mData.find(key); - return !(it == mData.end() - || it->second->getType() != VariableData::DATA_STRING); -} - // Floats void Event::setFloat(const std::string &key, double value) @@ -116,13 +102,6 @@ double Event::getFloat(const std::string &key) const return static_cast(it->second)->getData(); } -bool Event::hasFloat(const std::string &key) const -{ - auto it = mData.find(key); - return !(it == mData.end() - || it->second->getType() != VariableData::DATA_FLOAT); -} - // Booleans void Event::setBool(const std::string &key, bool value) @@ -145,13 +124,6 @@ bool Event::getBool(const std::string &key) const return static_cast(it->second)->getData(); } -bool Event::hasBool(const std::string &key) const -{ - auto it = mData.find(key); - return !(it == mData.end() - || it->second->getType() != VariableData::DATA_BOOL); -} - // Items void Event::setItem(const std::string &key, Item *value) @@ -174,13 +146,6 @@ Item *Event::getItem(const std::string &key) const return static_cast(it->second)->getData(); } -bool Event::hasItem(const std::string &key) const -{ - auto it = mData.find(key); - return !(it == mData.end() - || it->second->getType() != VariableData::DATA_ITEM); -} - // Actors void Event::setActor(const std::string &key, ActorSprite *value) @@ -203,13 +168,6 @@ ActorSprite *Event::getActor(const std::string &key) const return static_cast(it->second)->getData(); } -bool Event::hasActor(const std::string &key) const -{ - auto it = mData.find(key); - return !(it == mData.end() - || it->second->getType() != VariableData::DATA_ACTOR); -} - // Triggers void Event::trigger(Channel channel, const Event &event) diff --git a/src/event.h b/src/event.h index 0fc0c1ef..5f053724 100644 --- a/src/event.h +++ b/src/event.h @@ -36,10 +36,7 @@ enum BadEvent { }; class EventListener; - -using ListenerSet = std::set; class VariableData; -using VariableMap = std::map; class Event { @@ -148,11 +145,6 @@ public: int getInt(const std::string &key, int defaultValue) const { try { return getInt(key); } catch (BadEvent) { return defaultValue; }} - /** - * Returns true if the given variable exists and is an integer. - */ - bool hasInt(const std::string &key) const; - // Strings /** @@ -173,11 +165,6 @@ public: const std::string &defaultValue) const { try { return getString(key); } catch (BadEvent) { return defaultValue; }} - /** - * Returns true if the given variable exists and is a string. - */ - bool hasString(const std::string &key) const; - // Floats /** @@ -198,11 +185,6 @@ public: double getFloat(const std::string &key, float defaultValue) const { try { return getFloat(key); } catch (BadEvent) { return defaultValue; }} - /** - * Returns true if the given variable exists and is a floating-point. - */ - bool hasFloat(const std::string &key) const; - // Booleans /** @@ -222,11 +204,6 @@ public: bool getBool(const std::string &key, bool defaultValue) const { try { return getBool(key); } catch (BadEvent) { return defaultValue; }} - /** - * Returns true if the given variable exists and is a boolean. - */ - bool hasBool(const std::string &key) const; - // Items /** @@ -246,11 +223,6 @@ public: Item *getItem(const std::string &key, Item *defaultValue) const { try { return getItem(key); } catch (BadEvent) { return defaultValue; }} - /** - * Returns true if the given variable exists and is an Item. - */ - bool hasItem(const std::string &key) const; - // ActorSprites /** @@ -271,11 +243,6 @@ public: ActorSprite *defaultValue) const { try { return getActor(key); } catch (BadEvent) { return defaultValue; }} - /** - * Returns true if the given variable exists and is an actor. - */ - bool hasActor(const std::string &key) const; - // Triggers /** @@ -317,11 +284,11 @@ protected: static void remove(EventListener *listener); private: - using ListenMap = std::map; + using ListenMap = std::map>; static ListenMap mBindings; Type mType; - VariableMap mData; + std::map mData; }; #define SERVER_NOTICE(message) { \ diff --git a/src/game.cpp b/src/game.cpp index e27e6f5d..b7cc02fe 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -88,11 +88,11 @@ #include #include -Joystick *joystick = nullptr; +Joystick *joystick; -OkDialog *weightNotice = nullptr; -OkDialog *deathNotice = nullptr; -QuitDialog *quitDialog = nullptr; +OkDialog *weightNotice; +OkDialog *deathNotice; +QuitDialog *quitDialog; ChatWindow *chatWindow; StatusWindow *statusWindow; @@ -110,14 +110,14 @@ OutfitWindow *outfitWindow; SpecialsWindow *specialsWindow; SocialWindow *socialWindow; -ActorSpriteManager *actorSpriteManager = nullptr; -ChannelManager *channelManager = nullptr; -CommandHandler *commandHandler = nullptr; -Particle *particleEngine = nullptr; -EffectManager *effectManager = nullptr; -Viewport *viewport = nullptr; /**< Viewport on the map. */ +ActorSpriteManager *actorSpriteManager; +ChannelManager *channelManager; +CommandHandler *commandHandler; +Particle *particleEngine; +EffectManager *effectManager; +Viewport *viewport; /**< Viewport on the map. */ -ChatTab *localChatTab = nullptr; +ChatTab *localChatTab; /** * Initialize every game sub-engines in the right order @@ -338,8 +338,6 @@ static bool saveScreenshot() void Game::logic() { - handleInput(); - // Handle all necessary game logic ActorSprite::actorLogic(); actorSpriteManager->logic(); diff --git a/src/gui/chatwindow.cpp b/src/gui/chatwindow.cpp index 041e08e9..a3d9a1f4 100644 --- a/src/gui/chatwindow.cpp +++ b/src/gui/chatwindow.cpp @@ -265,20 +265,14 @@ void ChatWindow::removeWhisper(const std::string &nick) void ChatWindow::removeAllWhispers() { - TabMap::iterator iter; - std::list tabs; + // Swap with empty container before deleting, because each tab will try to + // remove itself from mWhispers when it gets deleted, possibly invalidating + // our iterator. + std::map whispers; + mWhispers.swap(whispers); - for (iter = mWhispers.begin(); iter != mWhispers.end(); ++iter) - { - tabs.push_back(iter->second); - } - - for (auto &tab : tabs) - { + for (auto &[_, tab] : whispers) delete tab; - } - - mWhispers.clear(); } void ChatWindow::chatInput(const std::string &msg) @@ -460,11 +454,11 @@ void ChatWindow::whisper(const std::string &nick, toLower(playerName); toLower(tempNick); - if (tempNick.compare(playerName) == 0) + if (tempNick == playerName) return; ChatTab *tab = nullptr; - TabMap::const_iterator i = mWhispers.find(tempNick); + auto i = mWhispers.find(tempNick); if (i != mWhispers.end()) tab = i->second; @@ -511,8 +505,7 @@ ChatTab *ChatWindow::addWhisperTab(const std::string &nick, bool switchTo) toLower(playerName); toLower(tempNick); - if (mWhispers.find(tempNick) != mWhispers.end() - || tempNick.compare(playerName) == 0) + if (mWhispers.find(tempNick) != mWhispers.end() || tempNick == playerName) return nullptr; ChatTab *ret = new WhisperTab(nick); diff --git a/src/gui/chatwindow.h b/src/gui/chatwindow.h index b3072d4a..9b46d7ad 100644 --- a/src/gui/chatwindow.h +++ b/src/gui/chatwindow.h @@ -214,9 +214,8 @@ class ChatWindow : public Window, /** Tabbed area for holding each channel. */ TabbedArea *mChatTabs; - using TabMap = std::map; /** Manage whisper tabs */ - TabMap mWhispers; + std::map mWhispers; bool mReturnToggles; /**< Marks whether toggles the chat log or not */ diff --git a/src/gui/confirmdialog.cpp b/src/gui/confirmdialog.cpp index 4d3f0f02..c0b471c1 100644 --- a/src/gui/confirmdialog.cpp +++ b/src/gui/confirmdialog.cpp @@ -21,8 +21,6 @@ #include "gui/confirmdialog.h" -#include "gui/gui.h" - #include "gui/widgets/button.h" #include "gui/widgets/textbox.h" diff --git a/src/gui/helpwindow.cpp b/src/gui/helpwindow.cpp index 3a528f27..1dfc109e 100644 --- a/src/gui/helpwindow.cpp +++ b/src/gui/helpwindow.cpp @@ -50,11 +50,6 @@ HelpWindow::HelpWindow(): mScrollArea = new ScrollArea(mBrowserBox); auto *okButton = new Button(_("Close"), "close", this); - mScrollArea->setDimension(gcn::Rectangle(5, 5, 445, - 335 - okButton->getHeight())); - okButton->setPosition(450 - okButton->getWidth(), - 345 - okButton->getHeight()); - mBrowserBox->setLinkHandler(this); mBrowserBox->setFont(monoFont); diff --git a/src/gui/itempopup.cpp b/src/gui/itempopup.cpp index 30d5c5c4..7790a3c0 100644 --- a/src/gui/itempopup.cpp +++ b/src/gui/itempopup.cpp @@ -45,7 +45,7 @@ #define ITEMPOPUP_WRAP_WIDTH 196 -static gcn::Color getColorFromItemType(ItemType type) +static const gcn::Color &getColorFromItemType(ItemType type) { switch (type) { diff --git a/src/gui/npcdialog.cpp b/src/gui/npcdialog.cpp index 4efa29eb..8150a01a 100644 --- a/src/gui/npcdialog.cpp +++ b/src/gui/npcdialog.cpp @@ -51,8 +51,6 @@ #define CAPTION_CLOSE _("Close") #define CAPTION_SUBMIT _("Submit") -using NpcDialogs = std::map; - class NpcEventListener : public EventListener { public: @@ -63,7 +61,7 @@ public: void removeDialog(int id); private: - NpcDialogs mNpcDialogs; + std::map mNpcDialogs; }; static NpcEventListener *npcListener = nullptr; diff --git a/src/gui/okdialog.cpp b/src/gui/okdialog.cpp index b2622d76..5a710890 100644 --- a/src/gui/okdialog.cpp +++ b/src/gui/okdialog.cpp @@ -21,8 +21,6 @@ #include "gui/okdialog.h" -#include "gui/gui.h" - #include "gui/widgets/button.h" #include "gui/widgets/textbox.h" diff --git a/src/gui/outfitwindow.cpp b/src/gui/outfitwindow.cpp index 0c37af19..5dc05790 100644 --- a/src/gui/outfitwindow.cpp +++ b/src/gui/outfitwindow.cpp @@ -26,21 +26,14 @@ #include "graphics.h" #include "inventory.h" #include "item.h" -#include "log.h" #include "playerinfo.h" -#include "gui/chatwindow.h" - #include "gui/widgets/button.h" #include "gui/widgets/checkbox.h" #include "gui/widgets/label.h" #include "gui/widgets/layout.h" -#include "net/inventoryhandler.h" -#include "net/net.h" - #include "resources/image.h" -#include "resources/iteminfo.h" #include "utils/gettext.h" #include "utils/stringutils.h" diff --git a/src/gui/palette.cpp b/src/gui/palette.cpp index aee78b29..aaa3165c 100644 --- a/src/gui/palette.cpp +++ b/src/gui/palette.cpp @@ -50,7 +50,7 @@ const int Palette::RAINBOW_COLOR_COUNT = 7; Palette::Palette(int size) : mRainbowTime(tick_time), - mColors(Colors(size)) + mColors(size) { mInstances.insert(this); } diff --git a/src/gui/palette.h b/src/gui/palette.h index 1ea7c467..41378a43 100644 --- a/src/gui/palette.h +++ b/src/gui/palette.h @@ -72,9 +72,9 @@ class Palette */ const gcn::Color &getColor(int type, int alpha = 255) { - gcn::Color *col = &mColors[type].color; - col->a = alpha; - return *col; + gcn::Color &col = mColors[type].color; + col.a = alpha; + return col; } /** @@ -163,9 +163,8 @@ class Palette committedColor.b; } }; - using Colors = std::vector; /** Vector containing the colors. */ - Colors mColors; + std::vector mColors; std::vector mGradVector; }; diff --git a/src/gui/sdlinput.cpp b/src/gui/sdlinput.cpp index bcc53ccf..8396418c 100644 --- a/src/gui/sdlinput.cpp +++ b/src/gui/sdlinput.cpp @@ -60,9 +60,7 @@ #include -SDLInput::SDLInput() -{ -} +SDLInput::SDLInput() = default; bool SDLInput::isKeyQueueEmpty() { @@ -220,8 +218,6 @@ void SDLInput::pushInput(SDL_Event event) if ((event.active.state & SDL_APPMOUSEFOCUS) && !event.active.gain) { - mMouseInWindow = false; - if (!mMouseDown) { mouseInput.setX(-1); @@ -231,12 +227,6 @@ void SDLInput::pushInput(SDL_Event event) mMouseInputQueue.push(mouseInput); } } - - if ((event.active.state & SDL_APPMOUSEFOCUS) - && event.active.gain) - { - mMouseInWindow = true; - } break; #endif } // end switch diff --git a/src/gui/sdlinput.h b/src/gui/sdlinput.h index 4fe5be44..a29f17ca 100644 --- a/src/gui/sdlinput.h +++ b/src/gui/sdlinput.h @@ -162,18 +162,18 @@ public: * only use SDL and plan sticking with SDL you can safely ignore this * function as it in the SDL case does nothing. */ - virtual void _pollInput() { } + void _pollInput() override { } // Inherited from Input - virtual bool isKeyQueueEmpty(); + bool isKeyQueueEmpty() override; - virtual gcn::KeyInput dequeueKeyInput(); + gcn::KeyInput dequeueKeyInput() override; - virtual bool isMouseQueueEmpty(); + bool isMouseQueueEmpty() override; - virtual gcn::MouseInput dequeueMouseInput(); + gcn::MouseInput dequeueMouseInput() override; bool isTextQueueEmpty() const; @@ -187,7 +187,7 @@ protected: * @param button an SDL mouse button. * @return a Guichan mouse button. */ - int convertMouseButton(int button); + static int convertMouseButton(int button); /** * Converts an SDL event key to a key value. @@ -196,14 +196,13 @@ protected: * @return a key value. * @see Key */ - int convertKeyCharacter(SDL_Event event); + static int convertKeyCharacter(SDL_Event event); std::queue mKeyInputQueue; std::queue mMouseInputQueue; std::queue mTextInputQueue; bool mMouseDown = false; - bool mMouseInWindow = true; }; #endif diff --git a/src/gui/selldialog.cpp b/src/gui/selldialog.cpp index ef694f96..5f499982 100644 --- a/src/gui/selldialog.cpp +++ b/src/gui/selldialog.cpp @@ -212,8 +212,6 @@ void SellDialog::action(const gcn::ActionEvent &event) mAmountItems -= sellCount; } - mPlayerMoney += - mAmountItems * mShopItems->at(selectedItem)->getPrice(); mAmountItems = 1; mSlider->setValue(0); diff --git a/src/gui/serverdialog.h b/src/gui/serverdialog.h index 4bb201a5..4db36462 100644 --- a/src/gui/serverdialog.h +++ b/src/gui/serverdialog.h @@ -73,10 +73,8 @@ class ServersListModel : public gcn::ListModel void setVersionString(int index, const std::string &version); private: - using VersionStrings = std::vector; - ServerInfos *mServers; - VersionStrings mVersionStrings; + std::vector mVersionStrings; ServerDialog *mParent; }; diff --git a/src/gui/setup_video.cpp b/src/gui/setup_video.cpp index de32fcb1..6f2f0d26 100644 --- a/src/gui/setup_video.cpp +++ b/src/gui/setup_video.cpp @@ -131,7 +131,7 @@ public: /** * Returns the index corresponding to the given video resolution - * or -1 if not found. + * or 0 ("Custom") if not found. */ int getIndexOf(int width, int height) const { diff --git a/src/gui/skilldialog.cpp b/src/gui/skilldialog.cpp index c43183a2..7639f1d6 100644 --- a/src/gui/skilldialog.cpp +++ b/src/gui/skilldialog.cpp @@ -99,8 +99,6 @@ struct SkillInfo void draw(Graphics *graphics, int y, int width); }; -using SkillList = std::vector; - class SkillModel : public gcn::ListModel { public: @@ -119,8 +117,8 @@ public: { mSkills.push_back(info); } private: - SkillList mSkills; - SkillList mVisibleSkills; + std::vector mSkills; + std::vector mVisibleSkills; }; class SkillListBox : public ListBox diff --git a/src/gui/skilldialog.h b/src/gui/skilldialog.h index ebf29ff8..e88c279f 100644 --- a/src/gui/skilldialog.h +++ b/src/gui/skilldialog.h @@ -75,8 +75,7 @@ class SkillDialog : public Window, public gcn::ActionListener, public EventListe bool hasSkills() { return !mSkills.empty(); } private: - using SkillMap = std::map; - SkillMap mSkills; + std::map mSkills; TabbedArea *mTabs; Label *mPointsLabel; Button *mIncreaseButton; diff --git a/src/gui/socialwindow.cpp b/src/gui/socialwindow.cpp index 96a9c389..00211e02 100644 --- a/src/gui/socialwindow.cpp +++ b/src/gui/socialwindow.cpp @@ -48,6 +48,8 @@ #include "utils/gettext.h" #include "utils/stringutils.h" +#include + class SocialTab : public Tab { protected: @@ -79,8 +81,8 @@ protected: TextDialog *mInviteDialog = nullptr; ConfirmDialog *mConfirmDialog = nullptr; - ScrollArea *mScroll; - AvatarListBox *mList; + std::unique_ptr mScroll; + std::unique_ptr mList; }; class GuildTab : public SocialTab, public gcn::ActionListener @@ -93,21 +95,13 @@ public: setTabColor(&Theme::getThemeColor(Theme::GUILD)); - mList = new AvatarListBox(guild); - mScroll = new ScrollArea(mList); + mList = std::make_unique(guild); + mScroll = std::make_unique(mList.get()); mScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_AUTO); mScroll->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_ALWAYS); } - ~GuildTab() override - { - delete mList; - mList = nullptr; - delete mScroll; - mScroll = nullptr; - } - void action(const gcn::ActionEvent &event) override { if (event.getId() == "do invite") @@ -176,21 +170,13 @@ public: setTabColor(&Theme::getThemeColor(Theme::PARTY_SOCIAL_TAB)); - mList = new AvatarListBox(party); - mScroll = new ScrollArea(mList); + mList = std::make_unique(party); + mScroll = std::make_unique(mList.get()); mScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_AUTO); mScroll->setVerticalScrollPolicy(gcn::ScrollArea::SHOW_AUTO); } - ~PartyTab() override - { - delete mList; - mList = nullptr; - delete mScroll; - mScroll = nullptr; - } - void action(const gcn::ActionEvent &event) override { if (event.getId() == "do invite") @@ -302,10 +288,7 @@ private: }; SocialWindow::SocialWindow() : - Window(_("Social")), - mGuildInvited(0), - mGuildAcceptDialog(nullptr), - mPartyAcceptDialog(nullptr) + Window(_("Social")) { setWindowName("Social"); setVisible(false); @@ -373,7 +356,7 @@ bool SocialWindow::addTab(Guild *guild) auto *tab = new GuildTab(guild); mGuilds[guild] = tab; - mTabs->addTab(tab, tab->mScroll); + mTabs->addTab(tab, tab->mScroll.get()); updateButtons(); @@ -403,7 +386,7 @@ bool SocialWindow::addTab(Party *party) auto *tab = new PartyTab(party); mParties[party] = tab; - mTabs->addTab(tab, tab->mScroll); + mTabs->addTab(tab, tab->mScroll.get()); updateButtons(); @@ -490,7 +473,8 @@ void SocialWindow::action(const gcn::ActionEvent &event) "shorter name.")); return; } - else if (!name.empty()) + + if (!name.empty()) { Net::getGuildHandler()->create(name); SERVER_NOTICE(strprintf(_("Creating guild called %s."), @@ -513,7 +497,8 @@ void SocialWindow::action(const gcn::ActionEvent &event) "shorter name.")); return; } - else if (!name.empty()) + + if (!name.empty()) { Net::getPartyHandler()->create(name); SERVER_NOTICE(strprintf(_("Creating party called %s."), diff --git a/src/gui/socialwindow.h b/src/gui/socialwindow.h index 7ffcec78..8575f3b2 100644 --- a/src/gui/socialwindow.h +++ b/src/gui/socialwindow.h @@ -64,7 +64,7 @@ public: */ void action(const gcn::ActionEvent &event) override; - void showGuildInvite(const std::string &guildName, const int guildId, + void showGuildInvite(const std::string &guildName, int guildId, const std::string &inviterName); void showGuildCreate(); @@ -79,19 +79,16 @@ protected: void updateButtons(); - int mGuildInvited; - ConfirmDialog *mGuildAcceptDialog; - TextDialog *mGuildCreateDialog; + int mGuildInvited = 0; + ConfirmDialog *mGuildAcceptDialog = nullptr; + TextDialog *mGuildCreateDialog = nullptr; std::string mPartyInviter; - ConfirmDialog *mPartyAcceptDialog; - TextDialog *mPartyCreateDialog; + ConfirmDialog *mPartyAcceptDialog = nullptr; + TextDialog *mPartyCreateDialog = nullptr; - using GuildMap = std::map; - GuildMap mGuilds; - - using PartyMap = std::map; - PartyMap mParties; + std::map mGuilds; + std::map mParties; CreatePopup *mCreatePopup; diff --git a/src/gui/statuswindow.h b/src/gui/statuswindow.h index d1350bec..99c3b46a 100644 --- a/src/gui/statuswindow.h +++ b/src/gui/statuswindow.h @@ -81,8 +81,7 @@ class StatusWindow : public Window, public EventListener gcn::Label *mCharacterPointsLabel; gcn::Label *mCorrectionPointsLabel; - using Attrs = std::map; - Attrs mAttrs; + std::map mAttrs; }; extern StatusWindow *statusWindow; diff --git a/src/gui/tradewindow.h b/src/gui/tradewindow.h index cf2bb70e..e9d8d4f1 100644 --- a/src/gui/tradewindow.h +++ b/src/gui/tradewindow.h @@ -117,9 +117,8 @@ class TradeWindow : public Window, gcn::ActionListener, gcn::SelectionListener */ void setStatus(Status s); - using InventoryPtr = const std::unique_ptr; - InventoryPtr mMyInventory; - InventoryPtr mPartnerInventory; + const std::unique_ptr mMyInventory; + const std::unique_ptr mPartnerInventory; ItemContainer *mMyItemContainer; ItemContainer *mPartnerItemContainer; diff --git a/src/gui/truetypefont.cpp b/src/gui/truetypefont.cpp index 1fd76bdf..167a66b2 100644 --- a/src/gui/truetypefont.cpp +++ b/src/gui/truetypefont.cpp @@ -80,8 +80,6 @@ class TextChunk gcn::Color color; }; -using CacheIterator = std::list::iterator; - static int fontCounter; TrueTypeFont::TrueTypeFont(const std::string &filename, int size, int style) @@ -175,8 +173,7 @@ int TrueTypeFont::getWidth(const std::string &text) const mCache.splice(mCache.begin(), mCache, i); if (i->img) return i->img->getWidth(); - else - return 0; + return 0; } } diff --git a/src/gui/viewport.h b/src/gui/viewport.h index 96c8afd3..6ea74b5e 100644 --- a/src/gui/viewport.h +++ b/src/gui/viewport.h @@ -209,8 +209,7 @@ class Viewport : public WindowContainer, public gcn::MouseListener, float decay; unsigned duration; }; - using ShakeEffects = std::list; - ShakeEffects mShakeEffects; + std::list mShakeEffects; bool mPlayerFollowMouse = false; diff --git a/src/gui/widgets/button.cpp b/src/gui/widgets/button.cpp index d40a54fb..28d16b72 100644 --- a/src/gui/widgets/button.cpp +++ b/src/gui/widgets/button.cpp @@ -61,8 +61,7 @@ static ButtonData const data[BUTTON_COUNT] = { { "button_disabled.png", 25, 23 } }; -Button::Button(): - mButtonIcon(nullptr) +Button::Button() { init(); adjustSize(); diff --git a/src/gui/widgets/icon.cpp b/src/gui/widgets/icon.cpp index eaf5be1c..5becadd1 100644 --- a/src/gui/widgets/icon.cpp +++ b/src/gui/widgets/icon.cpp @@ -31,10 +31,8 @@ Icon::Icon(const std::string &file) {} Icon::Icon(Image *image) - : mImage(image) { - if (mImage) - setSize(mImage->getWidth(), mImage->getHeight()); + setImage(image); } void Icon::setImage(Image *image) diff --git a/src/gui/widgets/icon.h b/src/gui/widgets/icon.h index 896b26c1..508d2095 100644 --- a/src/gui/widgets/icon.h +++ b/src/gui/widgets/icon.h @@ -62,7 +62,7 @@ class Icon : public gcn::Widget void draw(gcn::Graphics *g) override; private: - Image *mImage; + Image *mImage = nullptr; }; #endif // ICON_H diff --git a/src/gui/widgets/layout.cpp b/src/gui/widgets/layout.cpp index f7941889..c40a3b2c 100644 --- a/src/gui/widgets/layout.cpp +++ b/src/gui/widgets/layout.cpp @@ -37,13 +37,15 @@ LayoutCell &ContainerPlacer::operator() LayoutCell::~LayoutCell() { - if (mType == ARRAY) delete mArray; + if (mType == ARRAY) + delete mArray; } LayoutArray &LayoutCell::getArray() { assert(mType != WIDGET); - if (mType == ARRAY) return *mArray; + if (mType == ARRAY) + return *mArray; mArray = new LayoutArray; mType = ARRAY; mExtent[0] = 1; @@ -114,7 +116,8 @@ void LayoutArray::resizeGrid(int w, int h) { bool extW = w && w > (int)mSizes[0].size(), extH = h && h > (int)mSizes[1].size(); - if (!extW && !extH) return; + if (!extW && !extH) + return; if (extH) { @@ -177,8 +180,10 @@ LayoutCell &LayoutArray::place(gcn::Widget *widget, int x, int y, int w, int h) cell.mAlign[1] = LayoutCell::FILL; short &cs = mSizes[0][x]; short &rs = mSizes[1][y]; - if (cs == Layout::AUTO_DEF && w == 1) cs = 0; - if (rs == Layout::AUTO_DEF && h == 1) rs = 0; + if (cs == Layout::AUTO_DEF && w == 1) + cs = 0; + if (rs == Layout::AUTO_DEF && h == 1) + rs = 0; return cell; } @@ -218,7 +223,8 @@ std::vector< short > LayoutArray::getSizes(int dim, int upp) const for (int gridX = 0; gridX < gridW; ++gridX) { LayoutCell const *cell = mCells[gridY][gridX]; - if (!cell || cell->mType == LayoutCell::NONE) continue; + if (!cell || cell->mType == LayoutCell::NONE) + continue; if (cell->mExtent[dim] == 1) { @@ -249,7 +255,8 @@ std::vector< short > LayoutArray::getSizes(int dim, int upp) const } upp = upp + mSpacing; - if (nbFill == 0) return sizes; + if (nbFill == 0) + return sizes; for (int i = 0; i < nb; ++i) { diff --git a/src/gui/widgets/linkhandler.h b/src/gui/widgets/linkhandler.h index dab42a24..33263a25 100644 --- a/src/gui/widgets/linkhandler.h +++ b/src/gui/widgets/linkhandler.h @@ -31,7 +31,7 @@ class LinkHandler { public: - virtual ~LinkHandler() { } + virtual ~LinkHandler() = default; virtual void handleLink(const std::string &link) = 0; }; diff --git a/src/gui/widgets/shopitems.cpp b/src/gui/widgets/shopitems.cpp index 91829131..031416e0 100644 --- a/src/gui/widgets/shopitems.cpp +++ b/src/gui/widgets/shopitems.cpp @@ -60,7 +60,7 @@ void ShopItems::addItem(int inventoryIndex, int id, int quantity, int price) if (item) { - item->addDuplicate (inventoryIndex, quantity); + item->addDuplicate(inventoryIndex, quantity); } else { @@ -87,15 +87,11 @@ void ShopItems::clear() ShopItem *ShopItems::findItem(int id) { - ShopItem *item; - - std::vector::iterator it; - for (it = mShopItems.begin(); it != mShopItems.end(); it++) + for (auto shopItem : mShopItems) { - item = *(it); - if (item->getId() == id) + if (shopItem->getId() == id) { - return item; + return shopItem; } } diff --git a/src/gui/widgets/tabbedarea.cpp b/src/gui/widgets/tabbedarea.cpp index 644adf07..772a8e22 100644 --- a/src/gui/widgets/tabbedarea.cpp +++ b/src/gui/widgets/tabbedarea.cpp @@ -48,10 +48,10 @@ int TabbedArea::getNumberOfTabs() const Tab *TabbedArea::getTab(const std::string &name) const { - for (auto itr = mTabs.begin(); itr != mTabs.end(); ++itr) + for (const auto &[tab, _] : mTabs) { - if ((*itr).first->getCaption() == name) - return static_cast((*itr).first); + if (tab->getCaption() == name) + return static_cast(tab); } return nullptr; } @@ -113,8 +113,7 @@ void TabbedArea::removeTab(Tab *tab) mSelectedTab = nullptr; } - TabContainer::iterator iter; - for (iter = mTabs.begin(); iter != mTabs.end(); iter++) + for (auto iter = mTabs.begin(); iter != mTabs.end(); iter++) { if (iter->first == tab) { @@ -124,12 +123,11 @@ void TabbedArea::removeTab(Tab *tab) } } - std::vector::iterator iter2; - for (iter2 = mTabsToDelete.begin(); iter2 != mTabsToDelete.end(); iter2++) + for (auto iter = mTabsToDelete.begin(); iter != mTabsToDelete.end(); iter++) { - if (*iter2 == tab) + if (*iter == tab) { - mTabsToDelete.erase(iter2); + mTabsToDelete.erase(iter); delete tab; break; } @@ -154,9 +152,8 @@ void TabbedArea::mousePressed(gcn::MouseEvent &mouseEvent) { gcn::Widget *widget = mTabContainer->getWidgetAt(mouseEvent.getX(), mouseEvent.getY()); - auto *tab = dynamic_cast(widget); - if (tab) + if (auto *tab = dynamic_cast(widget)) { setSelectedTab(tab); requestFocus(); @@ -205,9 +202,9 @@ void TabbedArea::widgetResized(const gcn::Event &event) void TabbedArea::updateTabsWidth() { mTabsWidth = 0; - for (const auto &tab : mTabs) + for (const auto &[tab, _] : mTabs) { - mTabsWidth += tab.first->getWidth(); + mTabsWidth += tab->getWidth(); } updateVisibleTabsWidth(); } @@ -255,10 +252,9 @@ void TabbedArea::adjustTabPositions() void TabbedArea::action(const gcn::ActionEvent& actionEvent) { - Widget* source = actionEvent.getSource(); - Tab* tab = dynamic_cast(source); + Widget *source = actionEvent.getSource(); - if (tab) + if (Tab *tab = dynamic_cast(source)) { setSelectedTab(tab); } diff --git a/src/gui/widgets/tabbedarea.h b/src/gui/widgets/tabbedarea.h index 18e923bf..8e6dcb5f 100644 --- a/src/gui/widgets/tabbedarea.h +++ b/src/gui/widgets/tabbedarea.h @@ -112,9 +112,8 @@ class TabbedArea : public gcn::TabbedArea, public gcn::WidgetListener // Inherited from MouseListener void mousePressed(gcn::MouseEvent &mouseEvent) override; - private: - using TabContainer = std::vector>; + private: /** The tab arrows */ Button *mArrowButton[2]; diff --git a/src/gui/widgets/textfield.h b/src/gui/widgets/textfield.h index 3b997ba8..9235f4b8 100644 --- a/src/gui/widgets/textfield.h +++ b/src/gui/widgets/textfield.h @@ -30,13 +30,10 @@ class TextInput; class ImageRect; class TextField; -using TextHistoryList = std::list; -using TextHistoryIterator = TextHistoryList::iterator; - struct TextHistory { - TextHistoryList history; /**< Command history. */ - TextHistoryIterator current; /**< History iterator. */ + std::list history; /**< Command history. */ + std::list::iterator current; /**< History iterator. */ TextHistory() { current = history.end(); } diff --git a/src/gui/worldselectdialog.cpp b/src/gui/worldselectdialog.cpp index cab978de..a5b91de1 100644 --- a/src/gui/worldselectdialog.cpp +++ b/src/gui/worldselectdialog.cpp @@ -46,12 +46,10 @@ class WorldListModel : public gcn::ListModel { public: WorldListModel(Worlds worlds): - mWorlds(worlds) + mWorlds(std::move(worlds)) { } - ~WorldListModel() override {} - int getNumberOfElements() override { return mWorlds.size(); @@ -69,8 +67,8 @@ class WorldListModel : public gcn::ListModel WorldSelectDialog::WorldSelectDialog(Worlds worlds): Window(_("Select World")) { - mWorldListModel = new WorldListModel(worlds); - mWorldList = new ListBox(mWorldListModel); + mWorldListModel = std::make_unique(worlds); + mWorldList = new ListBox(mWorldListModel.get()); auto *worldsScroll = new ScrollArea(mWorldList); mChangeLoginButton = new Button(_("Change Login"), "login", this); mChooseWorld = new Button(_("Choose World"), "world", this); @@ -86,7 +84,7 @@ WorldSelectDialog::WorldSelectDialog(Worlds worlds): reflowLayout(0, 0); - if (worlds.size() == 0) + if (worlds.empty()) // Disable Ok button mChooseWorld->setEnabled(false); else @@ -100,10 +98,7 @@ WorldSelectDialog::WorldSelectDialog(Worlds worlds): mChooseWorld->requestFocus(); } -WorldSelectDialog::~WorldSelectDialog() -{ - delete mWorldListModel; -} +WorldSelectDialog::~WorldSelectDialog() = default; void WorldSelectDialog::action(const gcn::ActionEvent &event) { diff --git a/src/gui/worldselectdialog.h b/src/gui/worldselectdialog.h index f2b74582..a1150cb8 100644 --- a/src/gui/worldselectdialog.h +++ b/src/gui/worldselectdialog.h @@ -30,7 +30,7 @@ #include #include -#include +#include class LoginData; class WorldListModel; @@ -57,7 +57,7 @@ class WorldSelectDialog : public Window, public gcn::ActionListener, void mouseClicked(gcn::MouseEvent &mouseEvent) override; private: - WorldListModel *mWorldListModel; + std::unique_ptr mWorldListModel; gcn::ListBox *mWorldList; gcn::Button *mChangeLoginButton; gcn::Button *mChooseWorld; diff --git a/src/inventory.cpp b/src/inventory.cpp index a92a4377..2de03431 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -159,10 +159,8 @@ void Inventory::removeInventoryListener(InventoryListener* listener) void Inventory::distributeSlotsChangedEvent() { - InventoryListenerList::const_iterator i = mInventoryListeners.begin(); - InventoryListenerList::const_iterator i_end = mInventoryListeners.end(); - for (; i != i_end; i++) + for (auto inventoryListener : mInventoryListeners) { - (*i)->slotsChanged(this); + inventoryListener->slotsChanged(this); } } diff --git a/src/inventory.h b/src/inventory.h index b4253b72..891a0d7b 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -30,7 +30,7 @@ class Item; class InventoryListener { public: - virtual ~InventoryListener() {} + virtual ~InventoryListener() = default; virtual void slotsChanged(Inventory* inventory) = 0; @@ -135,8 +135,7 @@ class Inventory { return mType == INVENTORY; } protected: - using InventoryListenerList = std::list; - InventoryListenerList mInventoryListeners; + std::list mInventoryListeners; void distributeSlotsChangedEvent(); diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 92224c03..3bc41b01 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -86,13 +86,13 @@ void LocalPlayer::logic() { if (mMessageTime == 0) { - MessagePair info = mMessages.front(); + const auto &[message, color] = mMessages.front(); particleEngine->addTextRiseFadeOutEffect( - info.first, + message, getPixelX(), getPixelY() - 32 - 16, - &userPalette->getColor(info.second), + &userPalette->getColor(color), gui->getInfoParticleFont(), true); mMessages.pop_front(); @@ -978,7 +978,7 @@ void LocalPlayer::setGotoTarget(Being *target) void LocalPlayer::addMessageToQueue(const std::string &message, int color) { - mMessages.push_back(MessagePair(message, color)); + mMessages.emplace_back(message, color); } void LocalPlayer::event(Event::Channel channel, const Event &event) diff --git a/src/localplayer.h b/src/localplayer.h index 43a1fe6d..0013a471 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -249,9 +249,8 @@ class LocalPlayer final : public Being int mWalkingDir = 0; /**< The direction the player is walking in. */ bool mPathSetByMouse = false; /**< Tells if the path was set using mouse */ - using MessagePair = std::pair; /** Queued messages*/ - std::list mMessages; + std::list> mMessages; int mMessageTime = 0; bool mShowIp = false; diff --git a/src/net/tmwa/gui/guildtab.cpp b/src/net/tmwa/gui/guildtab.cpp index 6014e507..9858028e 100644 --- a/src/net/tmwa/gui/guildtab.cpp +++ b/src/net/tmwa/gui/guildtab.cpp @@ -43,10 +43,6 @@ GuildTab::GuildTab() : setTabColor(&Theme::getThemeColor(Theme::GUILD)); } -GuildTab::~GuildTab() -{ -} - void GuildTab::handleInput(const std::string &msg) { Net::getGuildHandler()->chat(taGuild->getId(), msg); diff --git a/src/net/tmwa/gui/guildtab.h b/src/net/tmwa/gui/guildtab.h index e7d7b7cc..3f864748 100644 --- a/src/net/tmwa/gui/guildtab.h +++ b/src/net/tmwa/gui/guildtab.h @@ -33,7 +33,6 @@ class GuildTab : public ChatTab { public: GuildTab(); - ~GuildTab() override; void showHelp() override; diff --git a/src/net/tmwa/gui/partytab.cpp b/src/net/tmwa/gui/partytab.cpp index 89f3a769..6f1e3b68 100644 --- a/src/net/tmwa/gui/partytab.cpp +++ b/src/net/tmwa/gui/partytab.cpp @@ -42,10 +42,6 @@ PartyTab::PartyTab() : setTabColor(&Theme::getThemeColor(Theme::PARTY_CHAT_TAB)); } -PartyTab::~PartyTab() -{ -} - void PartyTab::handleInput(const std::string &msg) { Net::getPartyHandler()->chat(msg); @@ -197,9 +193,7 @@ bool PartyTab::handleCommand(const std::string &type, const std::string &args) void PartyTab::getAutoCompleteList(std::vector &names) const { - Party *p = local_player->getParty(); - - if (p) + if (Party *p = local_player->getParty()) p->getNames(names); } diff --git a/src/net/tmwa/gui/partytab.h b/src/net/tmwa/gui/partytab.h index 61ccf37a..a6c52f58 100644 --- a/src/net/tmwa/gui/partytab.h +++ b/src/net/tmwa/gui/partytab.h @@ -33,7 +33,6 @@ class PartyTab : public ChatTab { public: PartyTab(); - ~PartyTab() override; void showHelp() override; diff --git a/src/particle.cpp b/src/particle.cpp index 3062de51..953e4160 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -52,24 +52,7 @@ int Particle::emitterSkip = 1; bool Particle::enabled = true; const float Particle::PARTICLE_SKY = 800.0f; -Particle::Particle(Map *map): - mAlpha(1.0f), - mLifetimeLeft(-1), - mLifetimePast(0), - mFadeOut(0), - mFadeIn(0), - mAlive(ALIVE), - mAutoDelete(true), - mAllowSizeAdjust(false), - mDeathEffectConditions(0x00), - mGravity(0.0f), - mRandomness(0), - mBounce(0.0f), - mFollow(false), - mTarget(nullptr), - mAcceleration(0.0f), - mInvDieDistance(-1.0f), - mMomentum(1.0f) +Particle::Particle(Map *map) { setMap(map); Particle::particleCount++; @@ -238,7 +221,7 @@ bool Particle::update() } } - return !(mAlive != ALIVE && mChildParticles.empty() && mAutoDelete); + return mAlive == ALIVE || !mChildParticles.empty() || !mAutoDelete; } void Particle::moveBy(const Vector &change) diff --git a/src/particle.h b/src/particle.h index 68b1efd9..eece1f85 100644 --- a/src/particle.h +++ b/src/particle.h @@ -262,38 +262,38 @@ class Particle : public Actor protected: /** Opacity of the graphical representation of the particle */ - float mAlpha; + float mAlpha = 1.0f; /** Calculates the current alpha transparency taking current fade status into account*/ float getCurrentAlpha() const; - int mLifetimeLeft; /**< Lifetime left in game ticks*/ - int mLifetimePast; /**< Age of the particle in game ticks*/ - int mFadeOut; /**< Lifetime in game ticks left where fading out begins*/ - int mFadeIn; /**< Age in game ticks where fading in is finished*/ - Vector mVelocity; /**< Speed in pixels per game-tick. */ + int mLifetimeLeft = -1; /**< Lifetime left in game ticks*/ + int mLifetimePast = 0; /**< Age of the particle in game ticks*/ + int mFadeOut = 0; /**< Lifetime in game ticks left where fading out begins*/ + int mFadeIn = 0; /**< Age in game ticks where fading in is finished*/ + Vector mVelocity; /**< Speed in pixels per game-tick. */ private: - AliveStatus mAlive; /**< Is the particle supposed to be drawn and updated?*/ + AliveStatus mAlive = ALIVE; /**< Is the particle supposed to be drawn and updated?*/ // generic properties - bool mAutoDelete; /**< May the particle request its deletion by the parent particle? */ - Emitters mChildEmitters; /**< List of child emitters. */ - Particles mChildParticles; /**< List of particles controlled by this particle */ - bool mAllowSizeAdjust; /**< Can the effect size be adjusted by the object props in the map file? */ - std::string mDeathEffect; /**< Particle effect file to be spawned when the particle dies */ - char mDeathEffectConditions;/**< Bitfield of death conditions which trigger spawning of the death particle */ + bool mAutoDelete = true; /**< May the particle request its deletion by the parent particle? */ + Emitters mChildEmitters; /**< List of child emitters. */ + Particles mChildParticles; /**< List of particles controlled by this particle */ + bool mAllowSizeAdjust = false; /**< Can the effect size be adjusted by the object props in the map file? */ + std::string mDeathEffect; /**< Particle effect file to be spawned when the particle dies */ + char mDeathEffectConditions = 0;/**< Bitfield of death conditions which trigger spawning of the death particle */ // dynamic particle - float mGravity; /**< Downward acceleration in pixels per game-tick. */ - int mRandomness; /**< Ammount of random vector change */ - float mBounce; /**< How much the particle bounces off when hitting the ground */ - bool mFollow; /**< is this particle moved when its parent particle moves? */ + float mGravity = 0.0f; /**< Downward acceleration in pixels per game-tick. */ + int mRandomness = 0; /**< Ammount of random vector change */ + float mBounce = 0.0f; /**< How much the particle bounces off when hitting the ground */ + bool mFollow = false; /**< is this particle moved when its parent particle moves? */ // follow-point particles - Particle *mTarget; /**< The particle that attracts this particle*/ - float mAcceleration; /**< Acceleration towards the target particle in pixels per game-tick*/ - float mInvDieDistance; /**< Distance in pixels from the target particle that causes the destruction of the particle*/ - float mMomentum; /**< How much speed the particle retains after each game tick*/ + Particle *mTarget = nullptr; /**< The particle that attracts this particle*/ + float mAcceleration = 0.0f; /**< Acceleration towards the target particle in pixels per game-tick*/ + float mInvDieDistance = -1.0f; /**< Distance in pixels from the target particle that causes the destruction of the particle*/ + float mMomentum = 1.0f; /**< How much speed the particle retains after each game tick*/ }; extern Particle *particleEngine; diff --git a/src/particlecontainer.cpp b/src/particlecontainer.cpp index f19ad6e3..9dfb8585 100644 --- a/src/particlecontainer.cpp +++ b/src/particlecontainer.cpp @@ -56,8 +56,7 @@ ParticleList::ParticleList(ParticleContainer *parent, bool delParent): ParticleContainer(parent, delParent) {} -ParticleList::~ParticleList() -{} +ParticleList::~ParticleList() = default; void ParticleList::addLocally(Particle *particle) { @@ -117,8 +116,7 @@ ParticleVector::ParticleVector(ParticleContainer *parent, bool delParent): ParticleContainer(parent, delParent) {} -ParticleVector::~ParticleVector() -{} +ParticleVector::~ParticleVector() = default; void ParticleVector::setLocally(int index, Particle *particle) { diff --git a/src/particlecontainer.h b/src/particlecontainer.h index f01618e1..3905ee49 100644 --- a/src/particlecontainer.h +++ b/src/particlecontainer.h @@ -67,7 +67,7 @@ protected: /** * Linked list of particle effects. */ -class ParticleList : public ParticleContainer +class ParticleList final : public ParticleContainer { public: ParticleList(ParticleContainer *parent = nullptr, bool delParent = true); @@ -94,7 +94,7 @@ protected: /** * Particle container with indexing facilities */ -class ParticleVector : public ParticleContainer +class ParticleVector final : public ParticleContainer { public: ParticleVector(ParticleContainer *parent = nullptr, bool delParent = true); @@ -104,12 +104,12 @@ public: * Sets a particle at a specified index. Kills the previous particle * there, if needed. */ - virtual void setLocally(int index, Particle *particle); + void setLocally(int index, Particle *particle); /** * Removes a particle at a specified index */ - virtual void delLocally(int index); + void delLocally(int index); void clearLocally() override; void moveTo(float x, float y) override; diff --git a/src/playerinfo.cpp b/src/playerinfo.cpp index a937d71e..433f4f6b 100644 --- a/src/playerinfo.cpp +++ b/src/playerinfo.cpp @@ -31,17 +31,17 @@ namespace PlayerInfo { class PlayerLogic; -static PlayerLogic *mListener = nullptr; +static PlayerLogic *mListener; static PlayerInfoBackend mData; -static Inventory *mInventory = nullptr; -static Equipment *mEquipment = nullptr; +static Inventory *mInventory; +static Equipment *mEquipment; -static bool mStorageCount = 0; +static bool mStorageCount = false; -static bool mNPCCount = 0; -static bool mNPCPostCount = 0; +static bool mNPCCount = false; +static bool mNPCPostCount = false; static BuySellState mBuySellState = BUYSELL_NONE; @@ -78,7 +78,7 @@ void triggerStat(int id, const std::string &changed, int old1, int old2 = 0) int getAttribute(int id) { - IntMap::const_iterator it = mData.mAttributes.find(id); + auto it = mData.mAttributes.find(id); if (it != mData.mAttributes.end()) return it->second; @@ -97,7 +97,7 @@ void setAttribute(int id, int value, bool notify) int getStatBase(int id) { - StatMap::const_iterator it = mData.mStats.find(id); + auto it = mData.mStats.find(id); if (it != mData.mStats.end()) return it->second.base; @@ -114,7 +114,7 @@ void setStatBase(int id, int value, bool notify) int getStatMod(int id) { - StatMap::const_iterator it = mData.mStats.find(id); + auto it = mData.mStats.find(id); if (it != mData.mStats.end()) return it->second.mod; @@ -131,7 +131,7 @@ void setStatMod(int id, int value, bool notify) int getStatEffective(int id) { - StatMap::const_iterator it = mData.mStats.find(id); + auto it = mData.mStats.find(id); if (it != mData.mStats.end()) return it->second.base + it->second.mod; @@ -140,7 +140,7 @@ int getStatEffective(int id) std::pair getStatExperience(int id) { - StatMap::const_iterator it = mData.mStats.find(id); + auto it = mData.mStats.find(id); int a, b; if (it != mData.mStats.end()) { @@ -152,7 +152,7 @@ std::pair getStatExperience(int id) a = 0; b = 0; } - return std::pair(a, b); + return { a, b }; } void setStatExperience(int id, int have, int need, bool notify) diff --git a/src/playerinfo.h b/src/playerinfo.h index 925c5078..cbb6c748 100644 --- a/src/playerinfo.h +++ b/src/playerinfo.h @@ -49,16 +49,13 @@ struct Stat int expNeed; }; -using IntMap = std::map; -using StatMap = std::map; - /** * Backend for core player information. */ struct PlayerInfoBackend { - IntMap mAttributes; - StatMap mStats; + std::map mAttributes; + std::map mStats; }; class Equipment; diff --git a/src/resources/beinginfo.h b/src/resources/beinginfo.h index a4736439..d65feb5e 100644 --- a/src/resources/beinginfo.h +++ b/src/resources/beinginfo.h @@ -124,6 +124,4 @@ class BeingInfo Map::BlockType mBlockType = Map::BLOCKTYPE_CHARACTER; }; -using BeingInfos = std::map; - #endif // BEINGINFO_H diff --git a/src/resources/monsterdb.cpp b/src/resources/monsterdb.cpp index 49ee2174..e28fe75e 100644 --- a/src/resources/monsterdb.cpp +++ b/src/resources/monsterdb.cpp @@ -37,7 +37,7 @@ namespace { - BeingInfos mMonsterInfos; + std::map mMonsterInfos; bool mLoaded = false; int mMonsterIdOffset; } diff --git a/src/resources/npcdb.cpp b/src/resources/npcdb.cpp index 83d91b20..ce1bf14c 100644 --- a/src/resources/npcdb.cpp +++ b/src/resources/npcdb.cpp @@ -30,7 +30,7 @@ namespace { - BeingInfos mNPCInfos; + std::map mNPCInfos; bool mLoaded = false; } diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 9a427ba0..2377eea1 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -237,10 +237,8 @@ class ResourceManager void cleanOrphans(); static ResourceManager *instance; - using Resources = std::map; - using ResourceIterator = Resources::iterator; - Resources mResources; - Resources mOrphanedResources; + std::map mResources; + std::map mOrphanedResources; time_t mOldestOrphan; }; diff --git a/src/resources/specialdb.cpp b/src/resources/specialdb.cpp index 89bd4d8d..c2ff16c2 100644 --- a/src/resources/specialdb.cpp +++ b/src/resources/specialdb.cpp @@ -24,10 +24,11 @@ #include "utils/dtor.h" +#include namespace { - SpecialInfos mSpecialInfos; + std::map mSpecialInfos; bool mLoaded = false; } diff --git a/src/resources/specialdb.h b/src/resources/specialdb.h index 71aae0ba..50aebf1f 100644 --- a/src/resources/specialdb.h +++ b/src/resources/specialdb.h @@ -22,7 +22,6 @@ #define SPECIAL_DB_H #include -#include #include "utils/xml.h" struct SpecialInfo @@ -65,6 +64,4 @@ namespace SpecialDB SpecialInfo::TargetMode targetModeFromString(const std::string& str); } -using SpecialInfos = std::map; - #endif diff --git a/src/resources/spritedef.h b/src/resources/spritedef.h index fafd6a41..49798afc 100644 --- a/src/resources/spritedef.h +++ b/src/resources/spritedef.h @@ -53,8 +53,6 @@ struct SpriteDisplay std::list particles; }; -using SpriteRefs = std::list::const_iterator; - /* * Remember those are the main action. * Action subtypes, e.g.: "attack_bow" are to be passed by items.xml after diff --git a/src/resources/theme.cpp b/src/resources/theme.cpp index 4686edf1..1db92feb 100644 --- a/src/resources/theme.cpp +++ b/src/resources/theme.cpp @@ -105,7 +105,7 @@ int Skin::getMinHeight() const Theme::Theme(): Palette(THEME_COLORS_END), mMinimumOpacity(-1.0f), - mProgressColors(ProgressColors(THEME_PROG_END)) + mProgressColors(THEME_PROG_END) { initDefaultThemePath(); diff --git a/src/resources/theme.h b/src/resources/theme.h index ae795ed4..434bd6d8 100644 --- a/src/resources/theme.h +++ b/src/resources/theme.h @@ -107,7 +107,7 @@ class Theme : public Palette, public EventListener static void deleteInstance(); static void prepareThemePath(); - static std::string getThemePath() { return mThemePath; } + static const std::string &getThemePath() { return mThemePath; } /** * Returns the patch to the given gui resource relative to the theme @@ -227,10 +227,7 @@ class Theme : public Palette, public EventListener Skin *readSkin(const std::string &filename); // Map containing all window skins - using Skins = std::map; - using SkinIterator = Skins::iterator; - - Skins mSkins; + std::map mSkins; static std::string mThemePath; static Theme *mInstance; @@ -245,8 +242,7 @@ class Theme : public Palette, public EventListener */ float mMinimumOpacity; - using ProgressColors = std::vector; - ProgressColors mProgressColors; + std::vector mProgressColors; }; #endif diff --git a/src/resources/wallpaper.cpp b/src/resources/wallpaper.cpp index a1990ed0..e8167b6b 100644 --- a/src/resources/wallpaper.cpp +++ b/src/resources/wallpaper.cpp @@ -21,17 +21,12 @@ #include "resources/wallpaper.h" -#include "resources/resourcemanager.h" -#include "log.h" - -#include "utils/stringutils.h" #include "configuration.h" #include #include #include -#include #include struct WallpaperData @@ -144,16 +139,12 @@ std::string Wallpaper::getWallpaper(int width, int height) WallpaperData wallpaper; // Search for the smallest wallpaper at least as large as the screen - std::vector::iterator iter; - for (iter = wallpaperData.begin(); iter != wallpaperData.end(); iter++) + for (auto &wp : wallpaperData) { - const WallpaperData &wp = *iter; - if (wp.width >= width && wp.height >= height) { - if (wallpaper.filename.empty() || - (wallpaper.width < wp.width && - wallpaper.height < wp.height)) + if (wallpaper.filename.empty() || (wallpaper.width < wp.width && + wallpaper.height < wp.height)) { wallpaper = wp; } diff --git a/src/shopitem.cpp b/src/shopitem.cpp index 6eb52d18..e966dbbf 100644 --- a/src/shopitem.cpp +++ b/src/shopitem.cpp @@ -31,56 +31,28 @@ ShopItem::ShopItem(int inventoryIndex, int id, mPrice(price) { mDisplayName = getInfo().getName() + - " (" + Units::formatCurrency(mPrice).c_str() + ")"; + " (" + Units::formatCurrency(mPrice) + ")"; setInvIndex(inventoryIndex); addDuplicate(inventoryIndex, quantity); } -ShopItem::ShopItem (int id, int price) : Item (id, 0), mPrice(price) -{ - mDisplayName = getInfo().getName() + - " (" + Units::formatCurrency(mPrice).c_str() + ")"; - setInvIndex(-1); - addDuplicate(-1, 0); -} - -ShopItem::~ShopItem() -{ - /** Clear all remaining duplicates on Object destruction. */ - while (!mDuplicates.empty()) - { - delete mDuplicates.top(); - mDuplicates.pop(); - } -} +ShopItem::~ShopItem() = default; void ShopItem::addDuplicate(int inventoryIndex, int quantity) { - auto* di = new DuplicateItem; - di->inventoryIndex = inventoryIndex; - di->quantity = quantity; - mDuplicates.push(di); + DuplicateItem &di = mDuplicates.emplace(); + di.inventoryIndex = inventoryIndex; + di.quantity = quantity; mQuantity += quantity; } -void ShopItem::addDuplicate() -{ - auto* di = new DuplicateItem; - di->inventoryIndex = -1; - di->quantity = 0; - mDuplicates.push(di); -} - int ShopItem::sellCurrentDuplicate(int quantity) { - DuplicateItem* dupl = mDuplicates.top(); - int sellCount = quantity <= dupl->quantity ? quantity : dupl->quantity; - dupl->quantity -= sellCount; + DuplicateItem &dupl = mDuplicates.top(); + int sellCount = quantity <= dupl.quantity ? quantity : dupl.quantity; + dupl.quantity -= sellCount; mQuantity -= sellCount; - if (dupl->quantity == 0) - { - delete dupl; + if (dupl.quantity == 0) mDuplicates.pop(); - } return sellCount; } diff --git a/src/shopitem.h b/src/shopitem.h index 996e04ec..dc1188e0 100644 --- a/src/shopitem.h +++ b/src/shopitem.h @@ -43,15 +43,6 @@ class ShopItem : public Item */ ShopItem(int inventoryIndex, int id, int quantity, int price); - /** - * Constructor. Creates a new ShopItem. Inventory index will be set to - * -1 and quantity to 0. - * - * @param id the id of the item - * @param price price of the item - */ - ShopItem(int id, int price); - ~ShopItem() override; /** @@ -62,13 +53,6 @@ class ShopItem : public Item */ void addDuplicate(int inventoryIndex, int quantity); - /** - * Add a duplicate. Id and price will be taken from this item. - * Needed for compatibility with ShopDuplicateItems (see) class - * documentation). - */ - void addDuplicate(); - /** * Gets the quantity of the currently topmost duplicate. * @@ -76,7 +60,7 @@ class ShopItem : public Item */ int getCurrentQuantity() const { - return mDuplicates.empty() ? 0 : mDuplicates.top()->quantity; + return mDuplicates.empty() ? 0 : mDuplicates.top().quantity; } /** @@ -87,7 +71,7 @@ class ShopItem : public Item int getCurrentInvIndex() const { return mDuplicates.empty() ? mInvIndex : - mDuplicates.top()->inventoryIndex; + mDuplicates.top().inventoryIndex; } /** @@ -126,11 +110,12 @@ class ShopItem : public Item /** * Struct to keep track of duplicates. */ - using DuplicateItem = struct { + struct DuplicateItem + { int inventoryIndex; int quantity; }; - std::stack mDuplicates; /** <-- Stores duplicates */ + std::stack mDuplicates; /** <-- Stores duplicates */ }; #endif diff --git a/src/textmanager.cpp b/src/textmanager.cpp index 35a20781..fff95f05 100644 --- a/src/textmanager.cpp +++ b/src/textmanager.cpp @@ -57,13 +57,11 @@ void TextManager::removeText(const Text *text) } } -TextManager::~TextManager() -{ -} +TextManager::~TextManager() = default; void TextManager::draw(gcn::Graphics *graphics, int xOff, int yOff) { - for (auto &text : mTextList) + for (auto text : mTextList) { text->draw(graphics, xOff, yOff); } diff --git a/src/textmanager.h b/src/textmanager.h index 9043e583..f736e87c 100644 --- a/src/textmanager.h +++ b/src/textmanager.h @@ -65,8 +65,7 @@ class TextManager void place(const Text *textObj, const Text *omit, int &x, int &y, int h); - using TextList = std::list; /**< The container type */ - TextList mTextList; /**< The container */ + std::list mTextList; }; extern TextManager *textManager; diff --git a/src/vector.h b/src/vector.h index 9ff98aa1..5d7d63d7 100644 --- a/src/vector.h +++ b/src/vector.h @@ -33,11 +33,7 @@ class Vector { public: - Vector(): - x(0.0f), - y(0.0f), - z(0.0f) - {} + Vector() = default; Vector(float x, float y, float z = 0.0f): x(x), @@ -45,12 +41,6 @@ class Vector z(z) {} - Vector(const Vector &v): - x(v.x), - y(v.y), - z(v.z) - {} - /** * Returns true if all coordinates are set to 0, otherwise returns * false. @@ -60,14 +50,6 @@ class Vector return x == 0.0f && y == 0.0f && z == 0.0f; } - Vector &operator=(const Vector &v) - { - x = v.x; - y = v.y; - z = v.z; - return *this; - } - /** * Scale vector operator. */ @@ -187,7 +169,9 @@ class Vector return Vector(x / l, y / l, z / l); } - float x, y, z; + float x = 0.0f; + float y = 0.0f; + float z = 0.0f; }; inline bool operator == (const Vector &a, const Vector &b) -- cgit v1.2.3-60-g2f50