summaryrefslogtreecommitdiff
path: root/src/inventory.cpp
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 /src/inventory.cpp
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.
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r--src/inventory.cpp48
1 files changed, 27 insertions, 21 deletions
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);
}