diff options
Diffstat (limited to 'engines/parallaction/inventory.cpp')
-rw-r--r-- | engines/parallaction/inventory.cpp | 59 |
1 files changed, 22 insertions, 37 deletions
diff --git a/engines/parallaction/inventory.cpp b/engines/parallaction/inventory.cpp index 3581b956a6..44fdc2a1ba 100644 --- a/engines/parallaction/inventory.cpp +++ b/engines/parallaction/inventory.cpp @@ -91,31 +91,34 @@ InventoryItem _inventory[INVENTORY_MAX_ITEMS] = { void drawInventoryItem(uint16 pos, InventoryItem *item); +int16 getNumUsedSlots() { + int16 num = 0; + while (num < INVENTORY_MAX_ITEMS && _inventory[num]._id != 0) + num++; + return num; +} + // get inventory item index at position (x,y) // in screen coordinates // int16 Parallaction::getHoverInventoryItem(int16 x, int16 y) { - int16 slot = -1; - do { - slot++; - } while (_inventory[slot]._id != 0); - + int16 slot = getNumUsedSlots(); slot = (slot + 4) / INVENTORY_ITEMS_PER_LINE; - if (_invPosition.x >= x) return -1; - if ((_invPosition.x + INVENTORY_WIDTH) <= x) return -1; + Common::Rect r(INVENTORY_WIDTH, _numInvLines * INVENTORYITEM_HEIGHT); + r.moveTo(_invPosition); - if (_invPosition.y >= y) return -1; - if ((slot * INVENTORYITEM_HEIGHT + _invPosition.y) <= y) return -1; + if (!r.contains(Common::Point(x,y))) + return -1; return ((x - _invPosition.x) / INVENTORYITEM_WIDTH) + (INVENTORY_ITEMS_PER_LINE * ((y - _invPosition.y) / INVENTORYITEM_HEIGHT)); } -void refreshInventory(const char *character) { +void refreshInventory() { for (uint16 i = 0; i < INVENTORY_MAX_ITEMS; i++) { drawInventoryItem(i, &_inventory[i]); } @@ -123,24 +126,21 @@ void refreshInventory(const char *character) { } -void refreshInventoryItem(const char *character, uint16 index) { +void refreshInventoryItem(uint16 index) { drawInventoryItem(index, &_inventory[index]); return; } int Parallaction::addInventoryItem(uint16 item) { - uint16 slot = 0; - while (_inventory[slot]._id != 0) - slot++; - + int16 slot = getNumUsedSlots(); if (slot == INVENTORY_MAX_ITEMS) return -1; _inventory[slot]._id = MAKE_INVENTORY_ID(item); _inventory[slot]._index = item; - refreshInventoryItem(_characterName, slot); + refreshInventoryItem(slot); return 0; } @@ -160,7 +160,7 @@ void Parallaction::dropItem(uint16 v) { memcpy(&_inventory[slot], &_inventory[slot+1], sizeof(InventoryItem)); } - refreshInventory(_characterName); + refreshInventory(); return; } @@ -261,9 +261,8 @@ void extractInventoryGraphics(int16 pos, byte *dst) { void jobShowInventory(void *parm, Job *j) { // printf("job_showInventory()..."); - _numInvLines = 0; - while (_inventory[_numInvLines]._id != 0) _numInvLines++; - _numInvLines = (_numInvLines + 4) / INVENTORY_ITEMS_PER_LINE; + int16 slot = getNumUsedSlots(); + _numInvLines = (slot + 4) / INVENTORY_ITEMS_PER_LINE; Common::Rect r(INVENTORY_WIDTH, _numInvLines * INVENTORYITEM_HEIGHT); @@ -308,25 +307,11 @@ void jobHideInventory(void *parm, Job *j) { void openInventory() { _engineFlags |= kEngineInventory; - uint16 slot = 0; - while (_inventory[slot]._id != 0) - slot++; - + int16 slot = getNumUsedSlots(); uint16 lines = (slot + 4) / INVENTORY_ITEMS_PER_LINE; - _invPosition.x = _vm->_mousePos.x - (INVENTORY_WIDTH / 2); - if (_invPosition.x < 0) - _invPosition.x = 0; - - if ((_invPosition.x + INVENTORY_WIDTH) > SCREEN_WIDTH) - _invPosition.x = SCREEN_WIDTH - INVENTORY_WIDTH; - - _invPosition.y = _vm->_mousePos.y - 2 - (lines * INVENTORYITEM_HEIGHT); - if (_invPosition.y < 0) - _invPosition.y = 0; - - if (_invPosition.y > SCREEN_HEIGHT - lines * INVENTORYITEM_HEIGHT) - _invPosition.y = SCREEN_HEIGHT - lines * INVENTORYITEM_HEIGHT; + _invPosition.x = CLIP(_vm->_mousePos.x - (INVENTORY_WIDTH / 2), 0, SCREEN_WIDTH - INVENTORY_WIDTH); + _invPosition.y = CLIP(_vm->_mousePos.y - 2 - (lines * INVENTORYITEM_HEIGHT), 0, SCREEN_HEIGHT - lines * INVENTORYITEM_HEIGHT); return; |