summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-11-15 23:44:01 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-11-15 23:44:01 +0000
commit0e925e97554aae573e895afa4e3d8450f01df342 (patch)
treebbc30319410aecb9fec7c005e667cd7fb628bb17
parentffa0fae492d954c0aed35a0acbd7b856778d7328 (diff)
downloadmana-0e925e97554aae573e895afa4e3d8450f01df342.tar.gz
mana-0e925e97554aae573e895afa4e3d8450f01df342.tar.bz2
mana-0e925e97554aae573e895afa4e3d8450f01df342.tar.xz
mana-0e925e97554aae573e895afa4e3d8450f01df342.zip
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.
-rw-r--r--ChangeLog15
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/Makefile.am2
-rw-r--r--src/equipment.cpp24
-rw-r--r--src/equipment.h17
-rw-r--r--src/floor_item.cpp11
-rw-r--r--src/floor_item.h8
-rw-r--r--src/gui/buy.cpp14
-rw-r--r--src/gui/equipmentwindow.cpp9
-rw-r--r--src/gui/itemcontainer.cpp9
-rw-r--r--src/gui/itemshortcutcontainer.cpp4
-rw-r--r--src/gui/sell.cpp14
-rw-r--r--src/gui/shop.cpp35
-rw-r--r--src/gui/shop.h27
-rw-r--r--src/gui/shoplistbox.cpp4
-rw-r--r--src/inventory.cpp48
-rw-r--r--src/inventory.h8
-rw-r--r--src/item.cpp22
-rw-r--r--src/item.h13
-rw-r--r--src/localplayer.cpp6
-rw-r--r--src/localplayer.h2
-rw-r--r--src/net/inventoryhandler.cpp4
-rw-r--r--src/resources/itemdb.cpp4
-rw-r--r--src/resources/iteminfo.cpp28
-rw-r--r--src/resources/iteminfo.h22
-rw-r--r--src/shopitem.cpp33
-rw-r--r--src/shopitem.h58
27 files changed, 279 insertions, 164 deletions
diff --git a/ChangeLog b/ChangeLog
index 6e20921e..628d7cac 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2007-11-16 Bjørn Lindeijer <bjorn@lindeijer.nl>
+
+ * src/floor_item.cpp, src/localplayer.cpp, src/item.cpp,
+ src/inventory.h, src/gui/sell.cpp, src/gui/equipmentwindow.cpp,
+ src/gui/shop.cpp, src/gui/shoplistbox.cpp, src/gui/shop.h,
+ src/gui/itemshortcutcontainer.cpp, src/gui/buy.cpp,
+ src/gui/itemcontainer.cpp, src/inventory.cpp, src/item.h,
+ src/equipment.h, src/shopitem.cpp, src/CMakeLists.txt,
+ src/floor_item.h, src/net/inventoryhandler.cpp, src/equipment.cpp,
+ src/localplayer.h, src/Makefile.am, src/resources/iteminfo.h,
+ src/resources/itemdb.cpp, src/resources/iteminfo.cpp, src/shopitem.h:
+ 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.
+
2007-11-15 Guillaume Melquiond <guillaume.melquiond@gmail.com>
* src/main.cpp: Disabled update loading.
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 21ae1a25..84310fa0 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -358,6 +358,8 @@ SET(SRCS
player.h
properties.h
serverinfo.h
+ shopitem.cpp
+ shopitem.h
simpleanimation.cpp
simpleanimation.h
sound.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 5e2b62fb..31c17edb 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -312,6 +312,8 @@ tmw_SOURCES = gui/widgets/dropdown.cpp \
player.h \
properties.h \
serverinfo.h \
+ shopitem.cpp \
+ shopitem.h \
simpleanimation.cpp \
simpleanimation.h \
sound.cpp \
diff --git a/src/equipment.cpp b/src/equipment.cpp
index 9de8c26e..265f230a 100644
--- a/src/equipment.cpp
+++ b/src/equipment.cpp
@@ -24,9 +24,31 @@
#include <algorithm>
#include "equipment.h"
+#include "item.h"
+
+Equipment::Equipment()
+{
+ std::fill_n(mEquipment, EQUIPMENT_SIZE, (Item*) 0);
+}
+
+Equipment::~Equipment()
+{
+ clear();
+}
void Equipment::clear()
{
- std::fill_n(mEquipment, EQUIPMENT_SIZE, 0);
+ for (int i = 0; i < EQUIPMENT_SIZE; ++i)
+ delete mEquipment[i];
+
+ std::fill_n(mEquipment, EQUIPMENT_SIZE, (Item*) 0);
}
+void Equipment::setEquipment(int index, int id)
+{
+ if (mEquipment[index] && mEquipment[index]->getId() == id)
+ return;
+
+ delete mEquipment[index];
+ mEquipment[index] = (id > 0) ? new Item(id) : 0;
+}
diff --git a/src/equipment.h b/src/equipment.h
index f820dcf0..7a0c8238 100644
--- a/src/equipment.h
+++ b/src/equipment.h
@@ -26,14 +26,20 @@
#define EQUIPMENT_SIZE 11
+class Item;
+
class Equipment
{
public:
/**
* Constructor.
*/
- Equipment()
- { clear(); }
+ Equipment();
+
+ /**
+ * Destructor.
+ */
+ ~Equipment();
/**
* Clears equipment.
@@ -43,17 +49,16 @@ class Equipment
/**
* Get equipment at the given slot.
*/
- int getEquipment(int index)
+ Item* getEquipment(int index)
{ return mEquipment[index]; }
/**
* Set equipment at the given slot.
*/
- void setEquipment(int index, int id)
- { mEquipment[index] = id; }
+ void setEquipment(int index, int id);
private:
- int mEquipment[EQUIPMENT_SIZE];
+ Item* mEquipment[EQUIPMENT_SIZE];
};
#endif
diff --git a/src/floor_item.cpp b/src/floor_item.cpp
index 5d83e1dd..9727093f 100644
--- a/src/floor_item.cpp
+++ b/src/floor_item.cpp
@@ -25,23 +25,18 @@
#include "map.h"
-#include "resources/itemdb.h"
-#include "resources/iteminfo.h"
-
-
FloorItem::FloorItem(unsigned int id,
unsigned int itemId,
unsigned short x,
unsigned short y,
Map *map):
mId(id),
- mItemId(itemId),
mX(x),
mY(y),
mMap(map)
{
- // Retrieve item image from item info
- mImage = ItemDB::get(itemId).getImage();
+ // Create a corresponding item instance
+ mItem = new Item(itemId);
// Add ourselves to the map
mSpriteIterator = mMap->addSprite(this);
@@ -51,4 +46,6 @@ FloorItem::~FloorItem()
{
// Remove ourselves from the map
mMap->removeSprite(mSpriteIterator);
+
+ delete mItem;
}
diff --git a/src/floor_item.h b/src/floor_item.h
index 36f81585..a87e3f79 100644
--- a/src/floor_item.h
+++ b/src/floor_item.h
@@ -25,6 +25,7 @@
#define _TMW_FLOORITEM_H_
#include "graphics.h"
+#include "item.h"
#include "map.h"
#include "sprite.h"
#include "resources/image.h"
@@ -59,7 +60,7 @@ class FloorItem : public Sprite
* Returns the item id.
*/
unsigned int
- getItemId() const { return mItemId; }
+ getItemId() const { return mItem->getId(); }
/**
* Returns the x coordinate.
@@ -89,16 +90,15 @@ class FloorItem : public Sprite
void
draw(Graphics *graphics, int offsetX, int offsetY) const
{
- graphics->drawImage(mImage,
+ graphics->drawImage(mItem->getImage(),
mX * 32 + offsetX,
mY * 32 + offsetY);
}
private:
unsigned int mId;
- unsigned int mItemId;
unsigned short mX, mY;
- Image *mImage;
+ Item *mItem;
Sprites::iterator mSpriteIterator;
Map *mMap;
};
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<ITEM_SHOP>* ShopItems::getShop()
+std::vector<ShopItem*>* 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 <vector>
#include <guichan/listmodel.hpp>
+
#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<ITEM_SHOP>* getShop();
+ std::vector<ShopItem*>* getShop();
private:
- std::vector<ITEM_SHOP> mItemsShop;
-
+ std::vector<ShopItem*> 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);
diff --git a/src/inventory.cpp b/src/inventory.cpp
index f6cf04f4..bc4bd1c0 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -27,48 +27,54 @@
#include "item.h"
-struct SlotUsed : public std::unary_function<Item, bool>
+struct SlotUsed : public std::unary_function<Item*, bool>
{
- bool operator()(const Item &item) const {
- return (item.getId() && item.getQuantity());
+ bool operator()(const Item *item) const {
+ return item && item->getId() && item->getQuantity();
}
};
Inventory::Inventory()
{
- mItems = new Item[INVENTORY_SIZE];
- for (int i = 0; i < INVENTORY_SIZE; i++) {
- mItems[i].setInvIndex(i);
- }
+ mItems = new Item*[INVENTORY_SIZE];
+ std::fill_n(mItems, INVENTORY_SIZE, (Item*) 0);
}
Inventory::~Inventory()
{
+ for (int i = 0; i < INVENTORY_SIZE; i++)
+ delete mItems[i];
+
delete [] mItems;
}
-Item* Inventory::getItem(int index)
+Item* Inventory::getItem(int index) const
{
if (index < 0 || index >= INVENTORY_SIZE)
- {
return 0;
- }
- return &mItems[index];
+ return mItems[index];
}
+
void Inventory::addItem(int id, int quantity)
{
- addItem(getFreeSlot(), id, quantity);
+ setItem(getFreeSlot(), id, quantity);
}
-void Inventory::addItem(int index, int id, int quantity)
+void Inventory::setItem(int index, int id, int quantity)
{
- mItems[index].setId(id);
- mItems[index].increaseQuantity(quantity);
+ if (!mItems[index] && id > 0) {
+ mItems[index] = new Item(id, quantity);
+ mItems[index]->setInvIndex(index);
+ } else if (id > 0) {
+ mItems[index]->setId(id);
+ mItems[index]->setQuantity(quantity);
+ } else if (mItems[index]) {
+ removeItemIndex(index);
+ }
}
-
void Inventory::clear()
{
for (int i = 0; i < INVENTORY_SIZE; i++) {
@@ -79,7 +85,7 @@ void Inventory::clear()
void Inventory::removeItem(int id)
{
for (int i = 0; i < INVENTORY_SIZE; i++) {
- if (mItems[i].getId() == id) {
+ if (mItems[i] && mItems[i]->getId() == id) {
removeItemIndex(i);
}
}
@@ -87,14 +93,14 @@ void Inventory::removeItem(int id)
void Inventory::removeItemIndex(int index)
{
- mItems[index].setId(0);
- mItems[index].setQuantity(0);
+ delete mItems[index];
+ mItems[index] = 0;
}
bool Inventory::contains(Item *item) const
{
for (int i = 0; i < INVENTORY_SIZE; i++) {
- if (mItems[i].getId() == item->getId()) {
+ if (mItems[i] && mItems[i]->getId() == item->getId()) {
return true;
}
}
@@ -104,7 +110,7 @@ bool Inventory::contains(Item *item) const
int Inventory::getFreeSlot() const
{
- Item *i = std::find_if(mItems, mItems + INVENTORY_SIZE,
+ Item **i = std::find_if(mItems, mItems + INVENTORY_SIZE,
std::not1(SlotUsed()));
return (i == mItems + INVENTORY_SIZE) ? -1 : (i - mItems);
}
diff --git a/src/inventory.h b/src/inventory.h
index 7a9e6ad2..48ace29c 100644
--- a/src/inventory.h
+++ b/src/inventory.h
@@ -44,7 +44,7 @@ class Inventory
/**
* Returns the item at the specified index.
*/
- Item* getItem(int index);
+ Item* getItem(int index) const;
/**
* Adds a new item in a free slot.
@@ -52,9 +52,9 @@ class Inventory
void addItem(int id, int quantity);
/**
- * Adds a new item at a given position.
+ * Sets the item at the given position.
*/
- void addItem(int index, int id, int quantity);
+ void setItem(int index, int id, int quantity);
/**
* Remove a item from the inventory.
@@ -93,7 +93,7 @@ class Inventory
static const int NO_SLOT_INDEX = -1; /**< Slot has no index. */
protected:
- Item *mItems; /**< The holder of items */
+ Item **mItems; /**< The holder of items */
};
#endif
diff --git a/src/item.cpp b/src/item.cpp
index 66f638a5..210589e9 100644
--- a/src/item.cpp
+++ b/src/item.cpp
@@ -23,16 +23,38 @@
#include "item.h"
+#include "resources/image.h"
+#include "resources/resourcemanager.h"
+
Item::Item(int id, int quantity) :
+ mImage(0),
mQuantity(quantity)
{
setId(id);
}
+Item::~Item()
+{
+ if (mImage)
+ mImage->decRef();
+}
+
void Item::setId(int id)
{
mId = id;
+
// Types 0 and 1 are not equippable items.
mEquipment = id && getInfo().getType() >= 2;
+
+ // Load the associated image
+ if (mImage)
+ mImage->decRef();
+
+ ResourceManager *resman = ResourceManager::getInstance();
+ std::string imagePath = "graphics/items/" + getInfo().getImageName();
+ mImage = resman->getImage(imagePath);
+
+ if (!mImage)
+ mImage = resman->getImage("graphics/gui/unknown-item.png");
}
diff --git a/src/item.h b/src/item.h
index f5d0fc77..fc71e53d 100644
--- a/src/item.h
+++ b/src/item.h
@@ -26,6 +26,8 @@
#include "resources/itemdb.h"
+class Image;
+
/**
* Represents one or more instances of a certain item type.
*/
@@ -38,6 +40,11 @@ class Item
Item(int id = 0, int quantity = 0);
/**
+ * Destructor.
+ */
+ ~Item();
+
+ /**
* Sets the item id, identifying the item type.
*/
void setId(int id);
@@ -49,6 +56,11 @@ class Item
getId() const { return mId; }
/**
+ * Returns the item image.
+ */
+ Image* getImage() { return mImage; }
+
+ /**
* Sets the number of items.
*/
void
@@ -92,6 +104,7 @@ class Item
protected:
int mId; /**< Item type id. */
+ Image *mImage; /**< Item image. */
int mQuantity; /**< Number of items. */
bool mEquipment; /**< Item is equipment. */
int mInvIndex; /**< Inventory index. */
diff --git a/src/localplayer.cpp b/src/localplayer.cpp
index 346597a8..059bd0f4 100644
--- a/src/localplayer.cpp
+++ b/src/localplayer.cpp
@@ -101,9 +101,9 @@ void LocalPlayer::clearInventory()
mInventory->clear();
}
-Item* LocalPlayer::getInvItem(int index)
+void LocalPlayer::setInvItem(int index, int id, int amount)
{
- return mInventory->getItem(index);
+ mInventory->setItem(index, id, amount);
}
void
@@ -137,7 +137,7 @@ void LocalPlayer::unequipItem(int slot)
{
Net::GameServer::Player::unequip(slot);
- // Tidy equipment directly to avoid weapon still shown bug, by instance
+ // Tidy equipment directly to avoid weapon still shown bug, for instance
mEquipment->setEquipment(slot, 0);
}
diff --git a/src/localplayer.h b/src/localplayer.h
index d6cb11ba..ddf5878e 100644
--- a/src/localplayer.h
+++ b/src/localplayer.h
@@ -142,7 +142,7 @@ class LocalPlayer : public Player
drawName(Graphics *, int, int) {};
void clearInventory();
- Item* getInvItem(int index);
+ void setInvItem(int index, int id, int amount);
/**
* Move the Inventory item from the old slot to the new slot.
diff --git a/src/net/inventoryhandler.cpp b/src/net/inventoryhandler.cpp
index de74e8f5..6fdb827d 100644
--- a/src/net/inventoryhandler.cpp
+++ b/src/net/inventoryhandler.cpp
@@ -73,9 +73,7 @@ void InventoryHandler::handleMessage(MessageIn &msg)
else if (slot >= 32 && slot < 32 + INVENTORY_SIZE)
{
int amount = id ? msg.readByte() : 0;
- Item *it = player_node->getInvItem(slot - 32);
- it->setId(id);
- it->setQuantity(amount);
+ player_node->setInvItem(slot - 32, id, amount);
}
};
itemShortcut->load();
diff --git a/src/resources/itemdb.cpp b/src/resources/itemdb.cpp
index a83da342..ff537bc3 100644
--- a/src/resources/itemdb.cpp
+++ b/src/resources/itemdb.cpp
@@ -65,7 +65,7 @@ void ItemDB::load()
mUnknown = new ItemInfo();
mUnknown->setName("Unknown item");
- mUnknown->setImage("");
+ mUnknown->setImageName("");
mUnknown->setSprite("error.xml", 0);
mUnknown->setSprite("error.xml", 1);
@@ -118,7 +118,7 @@ void ItemDB::load()
int weaponType = XML::getProperty(node, "weapon_type", 0);
ItemInfo *itemInfo = new ItemInfo;
- itemInfo->setImage(image);
+ itemInfo->setImageName(image);
itemInfo->setName(name.empty() ? "Unnamed" : name);
itemInfo->setDescription(description);
itemInfo->setType(type);
diff --git a/src/resources/iteminfo.cpp b/src/resources/iteminfo.cpp
index 82c46e3c..f1ebd0a9 100644
--- a/src/resources/iteminfo.cpp
+++ b/src/resources/iteminfo.cpp
@@ -23,36 +23,8 @@
#include "iteminfo.h"
-#include "resourcemanager.h"
-#include "image.h"
#include "itemdb.h"
-ItemInfo::~ItemInfo()
-{
- if (mImage)
- {
- mImage->decRef();
- }
-}
-
-void
-ItemInfo::setImage(const std::string &image)
-{
- if (mImage)
- {
- mImage->decRef();
- }
-
- ResourceManager *resman = ResourceManager::getInstance();
- mImageName = "graphics/items/" + image;
- mImage = ResourceManager::getInstance()->getImage(mImageName);
-
- if (!mImage)
- {
- mImage = resman->getImage("graphics/gui/unknown-item.png");
- }
-}
-
const std::string&
ItemInfo::getSprite(int gender) const
{
diff --git a/src/resources/iteminfo.h b/src/resources/iteminfo.h
index 2726a012..680c8d61 100644
--- a/src/resources/iteminfo.h
+++ b/src/resources/iteminfo.h
@@ -30,8 +30,6 @@
#include "spritedef.h"
-class Image;
-
enum EquipmentSoundEvent
{
EQUIP_EVENT_STRIKE,
@@ -49,7 +47,6 @@ class ItemInfo
* Constructor.
*/
ItemInfo():
- mImage(NULL),
mType(0),
mWeight(0),
mView(0),
@@ -57,21 +54,17 @@ class ItemInfo
{
}
- /**
- * Destructor.
- */
- ~ItemInfo();
-
void setName(const std::string &name)
{ mName = name; }
const std::string& getName() const
{ return mName; }
- void setImage(const std::string &image);
+ void setImageName(const std::string &imageName)
+ { mImageName = imageName; }
- Image* getImage() const
- { return mImage; }
+ const std::string& getImageName() const
+ { return mImageName; }
void setDescription(const std::string &description)
{ mDescription = description; }
@@ -116,13 +109,6 @@ class ItemInfo
protected:
std::string mImageName; /**< The filename of the icon image. */
-
- /* TODO (BL): I do not think the item info should keep a reference to
- * the item icon. It would probably be better if this was kept in the
- * Item class, so that the images can be lazily instantiated and also
- * unloaded when no longer used.
- */
- Image *mImage; /**< The loaded icon image. */
std::string mName;
std::string mDescription; /**< Short description. */
std::string mEffect; /**< Description of effects. */
diff --git a/src/shopitem.cpp b/src/shopitem.cpp
new file mode 100644
index 00000000..ed5d30a9
--- /dev/null
+++ b/src/shopitem.cpp
@@ -0,0 +1,33 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#include "shopitem.h"
+
+#include "utils/tostring.h"
+
+ShopItem::ShopItem(int id, int quantity, int price):
+ Item(id, quantity),
+ mPrice(price)
+{
+ mDisplayName = getInfo().getName() + " (" + toString(mPrice) + " GP)";
+}
diff --git a/src/shopitem.h b/src/shopitem.h
new file mode 100644
index 00000000..ffafbebe
--- /dev/null
+++ b/src/shopitem.h
@@ -0,0 +1,58 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#ifndef _SHOPITEM_H_
+#define _SHOPITEM_H_
+
+#include "item.h"
+
+/**
+ * Represents an item in a shop inventory.
+ */
+class ShopItem : public Item
+{
+ public:
+ /**
+ * Constructor.
+ */
+ ShopItem(int id, int quantity, int price);
+
+ /**
+ * Gets the price of the item.
+ */
+ int getPrice() const
+ { return mPrice; }
+
+ /**
+ * Gets the display name for in the shop list.
+ */
+ const std::string& getDisplayName() const
+ { return mDisplayName; }
+
+ protected:
+ int mPrice;
+ int mIndex;
+ std::string mDisplayName;
+};
+
+#endif