From 0e925e97554aae573e895afa4e3d8450f01df342 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Thu, 15 Nov 2007 23:44:01 +0000 Subject: Moved item icon from ItemInfo class to the Item class, so that it can be loaded on demand. Results in faster startup time and reduced memory usage. --- src/gui/buy.cpp | 14 ++++++++------ src/gui/equipmentwindow.cpp | 9 ++++----- src/gui/itemcontainer.cpp | 9 ++++----- src/gui/itemshortcutcontainer.cpp | 4 ++-- src/gui/sell.cpp | 14 +++++++------- src/gui/shop.cpp | 35 +++++++++++++---------------------- src/gui/shop.h | 27 ++++++++++----------------- src/gui/shoplistbox.cpp | 4 ++-- 8 files changed, 50 insertions(+), 66 deletions(-) (limited to 'src/gui') diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp index a21435f8..d2e12a85 100644 --- a/src/gui/buy.cpp +++ b/src/gui/buy.cpp @@ -35,7 +35,6 @@ #include "../npc.h" #include "../net/gameserver/player.h" -#include "../resources/itemdb.h" #include "../utils/gettext.h" #include "../utils/strprintf.h" @@ -165,7 +164,7 @@ void BuyDialog::action(const gcn::ActionEvent &event) mAmountItems <= mMaxItems) { Net::GameServer::Player::tradeWithNPC - (mShopItems->at(selectedItem).id, mAmountItems); + (mShopItems->at(selectedItem)->getId(), mAmountItems); // Reset selection mAmountItems = 1; @@ -174,7 +173,8 @@ void BuyDialog::action(const gcn::ActionEvent &event) // Update money and adjust the max number of items that can be bought mMaxItems -= mAmountItems; - setMoney(mMoney - mAmountItems * mShopItems->at(selectedItem).price); + setMoney(mMoney - + mAmountItems * mShopItems->at(selectedItem)->getPrice()); } } @@ -197,22 +197,24 @@ BuyDialog::updateButtonsAndLabels() if (selectedItem > -1) { - const ItemInfo &info = ItemDB::get(mShopItems->at(selectedItem).id); + const ItemInfo &info = mShopItems->at(selectedItem)->getInfo(); mItemDescLabel->setCaption (strprintf(_("Description: %s"), info.getDescription().c_str())); mItemEffectLabel->setCaption (strprintf(_("Effect: %s"), info.getEffect().c_str())); + int itemPrice = mShopItems->at(selectedItem)->getPrice(); + // Calculate how many the player can afford - mMaxItems = mMoney / mShopItems->at(selectedItem).price; + mMaxItems = mMoney / itemPrice; if (mAmountItems > mMaxItems) { mAmountItems = mMaxItems; } // Calculate price of pending purchase - price = mAmountItems * mShopItems->at(selectedItem).price; + price = mAmountItems * itemPrice; } else { diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp index 20cc86e7..9a96b4d6 100644 --- a/src/gui/equipmentwindow.cpp +++ b/src/gui/equipmentwindow.cpp @@ -102,11 +102,11 @@ void EquipmentWindow::draw(gcn::Graphics *graphics) for (int i = 0; i < EQUIPMENT_SIZE; i++) { - int itemId = mEquipment->getEquipment(i); - if (itemId) + Item *item = mEquipment->getEquipment(i); + if (item) { // Draw Item. - Image *image = Item(itemId).getInfo().getImage(); + Image *image = item->getImage(); g->drawImage(image, mEquipBox[i].posX, mEquipBox[i].posY); } @@ -149,8 +149,7 @@ void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent) BOX_WIDTH, BOX_HEIGHT); if (tRect.isPointInRect(x, y)) { - int itemId = mEquipment->getEquipment(i); - if (itemId) + if (mEquipment->getEquipment(i)) { mSelected = i; } diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index c51204a7..1abb8f0b 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -104,11 +104,10 @@ ItemContainer::draw(gcn::Graphics *graphics) Item *item = mInventory->getItem((j * mGridColumns) + i); - if (!item) - return; - if (item->getId() == 0) + if (!item || item->getId() == 0) continue; - Image *image = item->getInfo().getImage(); + + Image *image = item->getImage(); if (image) { if (item == mSelectedItem) @@ -232,7 +231,7 @@ ItemContainer::mousePressed(gcn::MouseEvent &event) { mSelectionStatus = SEL_DESELECTING; } - else if (item->getId()) + else if (item && item->getId()) { setSelectedItem(item); mSelectionStatus = SEL_SELECTING; diff --git a/src/gui/itemshortcutcontainer.cpp b/src/gui/itemshortcutcontainer.cpp index 1943ef93..6a5d94fc 100644 --- a/src/gui/itemshortcutcontainer.cpp +++ b/src/gui/itemshortcutcontainer.cpp @@ -88,7 +88,7 @@ ItemShortcutContainer::draw(gcn::Graphics *graphics) Item *item = itemShortcut->getItem(i); if (item) { // Draw item icon. - Image* image = item->getInfo().getImage(); + Image* image = item->getImage(); if (image) { g->drawImage(image, itemX, itemY); g->drawText( @@ -102,7 +102,7 @@ ItemShortcutContainer::draw(gcn::Graphics *graphics) if (mItemMoved) { // Draw the item image being dragged by the cursor. - Image* image = mItemMoved->getInfo().getImage(); + Image* image = mItemMoved->getImage(); if (image) { const int tPosX = mCursorPosX - (image->getWidth() / 2); diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index 08565654..bc683c0b 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -38,7 +38,6 @@ #include "../item.h" #include "../npc.h" #include "../net/gameserver/player.h" -#include "../resources/itemdb.h" #include "../resources/iteminfo.h" #include "../utils/gettext.h" #include "../utils/strprintf.h" @@ -158,11 +157,12 @@ void SellDialog::action(const gcn::ActionEvent &event) && mAmountItems <= mMaxItems) { Net::GameServer::Player::tradeWithNPC - (mShopItems->at(selectedItem).id, mAmountItems); + (mShopItems->at(selectedItem)->getId(), mAmountItems); mMaxItems -= mAmountItems; - mShopItems->getShop()->at(selectedItem).quantity = mMaxItems; - mPlayerMoney += (mAmountItems * mShopItems->at(selectedItem).price); + mShopItems->getShop()->at(selectedItem)->setQuantity(mMaxItems); + mPlayerMoney += + mAmountItems * mShopItems->at(selectedItem)->getPrice(); mAmountItems = 1; if (!mMaxItems) @@ -206,20 +206,20 @@ SellDialog::updateButtonsAndLabels() if (selectedItem > -1) { - const ItemInfo &info = ItemDB::get(mShopItems->at(selectedItem).id); + const ItemInfo &info = mShopItems->at(selectedItem)->getInfo(); mItemDescLabel->setCaption (strprintf(_("Description: %s"), info.getDescription().c_str())); mItemEffectLabel->setCaption (strprintf(_("Effect: %s"), info.getEffect().c_str())); - mMaxItems = mShopItems->at(selectedItem).quantity; + mMaxItems = mShopItems->at(selectedItem)->getQuantity(); if (mAmountItems > mMaxItems) { mAmountItems = mMaxItems; } - income = mAmountItems * mShopItems->at(selectedItem).price; + income = mAmountItems * mShopItems->at(selectedItem)->getPrice(); } else { diff --git a/src/gui/shop.cpp b/src/gui/shop.cpp index e2fd54f2..1731ae4c 100644 --- a/src/gui/shop.cpp +++ b/src/gui/shop.cpp @@ -22,8 +22,8 @@ */ #include "shop.h" -#include "../utils/tostring.h" -#include "../resources/itemdb.h" + +#include "../utils/dtor.h" ShopItems::~ShopItems() { @@ -32,45 +32,36 @@ ShopItems::~ShopItems() int ShopItems::getNumberOfElements() { - return mItemsShop.size(); + return mShopItems.size(); } std::string ShopItems::getElementAt(int i) { - return mItemsShop.at(i).name; + return mShopItems.at(i)->getDisplayName(); } void ShopItems::addItem(int id, int amount, int price) { - ITEM_SHOP item_shop; - ItemInfo const &item = ItemDB::get(id); - - item_shop.name = item.getName() - + " (" + toString(price) + " GP)"; - item_shop.price = price; - item_shop.id = id; - item_shop.quantity = amount; - item_shop.image = item.getImage(); - - mItemsShop.push_back(item_shop); + mShopItems.push_back(new ShopItem(id, amount, price)); } -ITEM_SHOP ShopItems::at(int i) +void ShopItems::addItem(ShopItem* shopItem) { - return mItemsShop.at(i); + mShopItems.push_back(shopItem); } -void ShopItems::push_back(ITEM_SHOP item_shop) +ShopItem* ShopItems::at(int i) { - mItemsShop.push_back(item_shop); + return mShopItems.at(i); } void ShopItems::clear() { - mItemsShop.clear(); + std::for_each(mShopItems.begin(), mShopItems.end(), make_dtor(mShopItems)); + mShopItems.clear(); } -std::vector* ShopItems::getShop() +std::vector* ShopItems::getShop() { - return &mItemsShop; + return &mShopItems; } diff --git a/src/gui/shop.h b/src/gui/shop.h index 665e92cb..281f4c6d 100644 --- a/src/gui/shop.h +++ b/src/gui/shop.h @@ -28,34 +28,28 @@ #include #include + #include "../resources/image.h" -struct ITEM_SHOP { - short id; - std::string name; - Image *image; - int price; - int index; - int quantity; -}; +#include "../shopitem.h" class ShopItems : public gcn::ListModel { public: /** - * Destructor + * Destructor. */ ~ShopItems(); /** - * Adds an item and its associated picture + * Adds an item and its associated picture. */ void addItem(int id, int amount, int price); /** - * Convenience function for adding items + * Convenience function for adding items. */ - void push_back(ITEM_SHOP item_shop); + void addItem(ShopItem* shopItem); /** * Returns the number of items in the shop. @@ -70,7 +64,7 @@ class ShopItems : public gcn::ListModel /** * Returns the item number i in the shop. */ - ITEM_SHOP at(int i); + ShopItem* at(int i); /** * Clear the vector. @@ -78,13 +72,12 @@ class ShopItems : public gcn::ListModel void clear(); /** - * Direct access to the vector + * Direct access to the vector. */ - std::vector* getShop(); + std::vector* getShop(); private: - std::vector mItemsShop; - + std::vector mShopItems; }; #endif diff --git a/src/gui/shoplistbox.cpp b/src/gui/shoplistbox.cpp index e4f6e6f9..ffa4d116 100644 --- a/src/gui/shoplistbox.cpp +++ b/src/gui/shoplistbox.cpp @@ -79,7 +79,7 @@ void ShopListBox::draw(gcn::Graphics *gcnGraphics) backgroundColor = gcn::Color(110, 160, 255); } else if (mShopItems && - mPlayerMoney < mShopItems->at(i).price && mPriceCheck) + mPlayerMoney < mShopItems->at(i)->getPrice() && mPriceCheck) { backgroundColor = gcn::Color(0x919191); } @@ -89,7 +89,7 @@ void ShopListBox::draw(gcn::Graphics *gcnGraphics) if (mShopItems) { - Image *icon = mShopItems->at(i).image; + Image *icon = mShopItems->at(i)->getImage(); if (icon) { graphics->drawImage(icon, 1, y); -- cgit v1.2.3-70-g09d2