diff options
author | Willem Jan Palenstijn | 2015-12-24 19:27:00 +0100 |
---|---|---|
committer | Willem Jan Palenstijn | 2015-12-24 20:28:57 +0100 |
commit | 0d97d1af820a55ab62ce259113f9ba1305f76c9f (patch) | |
tree | b093fd6ff7b30c87cd573fec50e74f3c2c4be7fa /engines/lab | |
parent | 1a1525a15501072da93ec0a7c1ef356886bd13a8 (diff) | |
download | scummvm-rg350-0d97d1af820a55ab62ce259113f9ba1305f76c9f.tar.gz scummvm-rg350-0d97d1af820a55ab62ce259113f9ba1305f76c9f.tar.bz2 scummvm-rg350-0d97d1af820a55ab62ce259113f9ba1305f76c9f.zip |
LAB: Avoid manual memory management of CloseData
Diffstat (limited to 'engines/lab')
-rw-r--r-- | engines/lab/engine.cpp | 34 | ||||
-rw-r--r-- | engines/lab/lab.cpp | 2 | ||||
-rw-r--r-- | engines/lab/lab.h | 4 | ||||
-rw-r--r-- | engines/lab/processroom.cpp | 74 | ||||
-rw-r--r-- | engines/lab/processroom.h | 5 | ||||
-rw-r--r-- | engines/lab/resource.cpp | 63 | ||||
-rw-r--r-- | engines/lab/resource.h | 6 |
7 files changed, 73 insertions, 115 deletions
diff --git a/engines/lab/engine.cpp b/engines/lab/engine.cpp index 8d859bc84e..d2333f8381 100644 --- a/engines/lab/engine.cpp +++ b/engines/lab/engine.cpp @@ -568,7 +568,6 @@ bool LabEngine::fromCrumbs(uint32 tmpClass, uint16 code, uint16 qualifier, Commo uint16 oldDirection = 0; uint16 lastInv = kItemMap; - CloseDataPtr wrkClosePtr = nullptr; bool leftButtonClick = false; bool rightButtonClick = false; @@ -640,29 +639,24 @@ bool LabEngine::fromCrumbs(uint32 tmpClass, uint16 code, uint16 qualifier, Commo mayShowCrumbIndicator(); _graphics->screenUpdate(); } else if (msgClass == kMessageDeltaMove) { - ViewData *vptr = getViewData(_roomNum, _direction); - CloseDataPtr oldClosePtr = vptr->_closeUps; CloseDataPtr tmpClosePtr = _closeDataPtr; - setCurrentClose(curPos, &tmpClosePtr, true); - - if (!tmpClosePtr || (tmpClosePtr == _closeDataPtr)) { - if (!_closeDataPtr) - wrkClosePtr = oldClosePtr; - else - wrkClosePtr = _closeDataPtr->_subCloseUps; - } else - wrkClosePtr = tmpClosePtr->_nextCloseUp; + // get next close-up in list after the one pointed to by curPos + setCurrentClose(curPos, &tmpClosePtr, true, true); - if (!wrkClosePtr) { - if (!_closeDataPtr) - wrkClosePtr = oldClosePtr; - else - wrkClosePtr = _closeDataPtr->_subCloseUps; + if (tmpClosePtr == _closeDataPtr) { + tmpClosePtr = nullptr; + if (!_closeDataPtr) { + ViewData *vptr = getViewData(_roomNum, _direction); + if (!vptr->_closeUps.empty()) + tmpClosePtr = &(*vptr->_closeUps.begin()); + } else { + if (!_closeDataPtr->_subCloseUps.empty()) + tmpClosePtr = &(*_closeDataPtr->_subCloseUps.begin()); + } } - - if (wrkClosePtr) - _event->setMousePos(Common::Point(_utils->scaleX((wrkClosePtr->_x1 + wrkClosePtr->_x2) / 2), _utils->scaleY((wrkClosePtr->_y1 + wrkClosePtr->_y2) / 2))); + if (tmpClosePtr) + _event->setMousePos(Common::Point(_utils->scaleX((tmpClosePtr->_x1 + tmpClosePtr->_x2) / 2), _utils->scaleY((tmpClosePtr->_y1 + tmpClosePtr->_y2) / 2))); } return true; diff --git a/engines/lab/lab.cpp b/engines/lab/lab.cpp index d21dc66a30..e942617a26 100644 --- a/engines/lab/lab.cpp +++ b/engines/lab/lab.cpp @@ -141,8 +141,6 @@ LabEngine::~LabEngine() { DebugMan.clearAllDebugChannels(); freeMapData(); - for (int i = 1; i <= _manyRooms; i++) - _resource->freeViews(i); delete[] _rooms; delete[] _inventory; diff --git a/engines/lab/lab.h b/engines/lab/lab.h index a8f5aa3926..76397b953c 100644 --- a/engines/lab/lab.h +++ b/engines/lab/lab.h @@ -336,7 +336,7 @@ private: * some of the closeups have the same hit boxes, then this returns the first * occurrence of the object with the same hit box. */ - CloseDataPtr findClosePtrMatch(CloseDataPtr closePtr, CloseDataPtr closePtrList); + CloseDataPtr findClosePtrMatch(CloseDataPtr closePtr, Common::List<CloseData> &list); /** * Checks if a floor has been visited. @@ -441,7 +441,7 @@ private: /** * Sets the current close up data. */ - void setCurrentClose(Common::Point pos, CloseDataPtr *closePtrList, bool useAbsoluteCoords); + void setCurrentClose(Common::Point pos, CloseDataPtr *closePtrList, bool useAbsoluteCoords, bool next=false); /** * Takes the currently selected item. diff --git a/engines/lab/processroom.cpp b/engines/lab/processroom.cpp index daf4f3ba84..ec07adde0a 100644 --- a/engines/lab/processroom.cpp +++ b/engines/lab/processroom.cpp @@ -69,39 +69,37 @@ ViewData *LabEngine::getViewData(uint16 roomNum, uint16 direction) { } CloseData *LabEngine::getObject(Common::Point pos, CloseDataPtr closePtr) { - CloseDataPtr wrkClosePtr; + Common::List<CloseData> *list; if (!closePtr) - wrkClosePtr = getViewData(_roomNum, _direction)->_closeUps; + list = &(getViewData(_roomNum, _direction)->_closeUps); else - wrkClosePtr = closePtr->_subCloseUps; + list = &(closePtr->_subCloseUps); - Common::Rect objRect; - while (wrkClosePtr) { + Common::List<CloseData>::iterator wrkClosePtr; + for (wrkClosePtr = list->begin(); wrkClosePtr != list->end(); ++wrkClosePtr) { + Common::Rect objRect; objRect = _utils->rectScale(wrkClosePtr->_x1, wrkClosePtr->_y1, wrkClosePtr->_x2, wrkClosePtr->_y2); if (objRect.contains(pos)) - return wrkClosePtr; - - wrkClosePtr = wrkClosePtr->_nextCloseUp; + return &(*wrkClosePtr); } return nullptr; } -CloseDataPtr LabEngine::findClosePtrMatch(CloseDataPtr closePtr, CloseDataPtr closePtrList) { - CloseDataPtr resClosePtr; +CloseDataPtr LabEngine::findClosePtrMatch(CloseDataPtr closePtr, Common::List<CloseData> &list) { + Common::List<CloseData>::iterator i; - while (closePtrList) { - if ((closePtr->_x1 == closePtrList->_x1) && (closePtr->_x2 == closePtrList->_x2) && - (closePtr->_y1 == closePtrList->_y1) && (closePtr->_y2 == closePtrList->_y2) && - (closePtr->_depth == closePtrList->_depth)) - return closePtrList; + for (i = list.begin(); i != list.end(); ++i) { + if ((closePtr->_x1 == i->_x1) && (closePtr->_x2 == i->_x2) && + (closePtr->_y1 == i->_y1) && (closePtr->_y2 == i->_y2) && + (closePtr->_depth == i->_depth)) + return &(*i); - resClosePtr = findClosePtrMatch(closePtr, closePtrList->_subCloseUps); + CloseDataPtr resClosePtr; + resClosePtr = findClosePtrMatch(closePtr, i->_subCloseUps); if (resClosePtr) return resClosePtr; - else - closePtrList = closePtrList->_nextCloseUp; } return nullptr; @@ -174,50 +172,56 @@ uint16 LabEngine::processArrow(uint16 curDirection, uint16 arrow) { return curDirection; } -void LabEngine::setCurrentClose(Common::Point pos, CloseDataPtr *closePtrList, bool useAbsoluteCoords) { - CloseDataPtr closePtr; +void LabEngine::setCurrentClose(Common::Point pos, CloseDataPtr *closePtrList, bool useAbsoluteCoords, bool next) { + + Common::List<CloseData> *list; if (!*closePtrList) - closePtr = getViewData(_roomNum, _direction)->_closeUps; + list = &(getViewData(_roomNum, _direction)->_closeUps); else - closePtr = (*closePtrList)->_subCloseUps; + list = &((*closePtrList)->_subCloseUps); - Common::Rect target; - while (closePtr) { + Common::List<CloseData>::iterator closePtr; + for (closePtr = list->begin(); closePtr != list->end(); ++closePtr) { + Common::Rect target; if (!useAbsoluteCoords) target = Common::Rect(closePtr->_x1, closePtr->_y1, closePtr->_x2, closePtr->_y2); else target = _utils->rectScale(closePtr->_x1, closePtr->_y1, closePtr->_x2, closePtr->_y2); if (target.contains(pos) && !closePtr->_graphicName.empty()) { - *closePtrList = closePtr; + + if (next) { + // cycle to the next one + ++closePtr; + if (closePtr == list->end()) + closePtr = list->begin(); + } + *closePtrList = &(*closePtr); + return; } - - closePtr = closePtr->_nextCloseUp; } } bool LabEngine::takeItem(Common::Point pos, CloseDataPtr *closePtrList) { - CloseDataPtr closePtr; - + Common::List<CloseData> *list; if (!*closePtrList) { - closePtr = getViewData(_roomNum, _direction)->_closeUps; + list = &(getViewData(_roomNum, _direction)->_closeUps); } else if ((*closePtrList)->_closeUpType < 0) { _conditions->inclElement(abs((*closePtrList)->_closeUpType)); return true; } else - closePtr = (*closePtrList)->_subCloseUps; + list = &((*closePtrList)->_subCloseUps); - Common::Rect objRect; - while (closePtr) { + Common::List<CloseData>::iterator closePtr; + for (closePtr = list->begin(); closePtr != list->end(); ++closePtr) { + Common::Rect objRect; objRect = _utils->rectScale(closePtr->_x1, closePtr->_y1, closePtr->_x2, closePtr->_y2); if (objRect.contains(pos) && (closePtr->_closeUpType < 0)) { _conditions->inclElement(abs(closePtr->_closeUpType)); return true; } - - closePtr = closePtr->_nextCloseUp; } return false; diff --git a/engines/lab/processroom.h b/engines/lab/processroom.h index 88dd544f7b..5051588ab1 100644 --- a/engines/lab/processroom.h +++ b/engines/lab/processroom.h @@ -143,14 +143,13 @@ struct CloseData { uint16 _depth; // Level of the closeup. Common::String _graphicName; Common::String _message; - CloseData *_nextCloseUp; - CloseData *_subCloseUps; + Common::List<CloseData> _subCloseUps; }; struct ViewData { Common::Array<int16> _condition; Common::String _graphicName; - CloseDataPtr _closeUps; + Common::List<CloseData> _closeUps; }; struct Action { diff --git a/engines/lab/resource.cpp b/engines/lab/resource.cpp index 5de2df802d..b2258ba25c 100644 --- a/engines/lab/resource.cpp +++ b/engines/lab/resource.cpp @@ -133,7 +133,6 @@ void Resource::readViews(uint16 roomNum) { Common::String fileName = "LAB:Rooms/" + Common::String::format("%d", roomNum); Common::File *dataFile = openDataFile(fileName, MKTAG('R', 'O', 'M', '4')); - freeViews(roomNum); RoomData *curRoom = &_vm->_rooms[roomNum]; curRoom->_roomMsg = readString(dataFile); @@ -147,14 +146,6 @@ void Resource::readViews(uint16 roomNum) { delete dataFile; } -void Resource::freeViews(uint16 roomNum) { - if (!_vm->_rooms) - return; - - for (int i = 0; i < 4; i++) - freeView(_vm->_rooms[roomNum]._view[i]); -} - Common::String Resource::translateFileName(const Common::String filename) { Common::String upperFilename = filename; upperFilename.toUppercase(); @@ -285,39 +276,21 @@ Common::List<Action> Resource::readAction(Common::File *file) { return list; } -CloseData *Resource::readCloseUps(uint16 depth, Common::File *file) { - CloseData *closeup = nullptr; - CloseData *prev = nullptr; - CloseData *head = nullptr; - +void Resource::readCloseUps(uint16 depth, Common::File *file, Common::List<CloseData> &list) { + list.clear(); while (file->readByte() != '\0') { - closeup = new CloseData(); - if (!head) - head = closeup; - if (prev) - prev->_nextCloseUp = closeup; - closeup->_x1 = file->readUint16LE(); - closeup->_y1 = file->readUint16LE(); - closeup->_x2 = file->readUint16LE(); - closeup->_y2 = file->readUint16LE(); - closeup->_closeUpType = file->readSint16LE(); - closeup->_depth = depth; - closeup->_graphicName = readString(file); - closeup->_message = readString(file); - closeup->_subCloseUps = readCloseUps(depth + 1, file); - closeup->_nextCloseUp = nullptr; - prev = closeup; - } - - return head; -} - -void Resource::freeCloseUps(CloseData *closeUps) { - while (closeUps) { - CloseData *nextCloseUp = closeUps->_nextCloseUp; - freeCloseUps(closeUps->_subCloseUps); - delete closeUps; - closeUps = nextCloseUp; + list.push_back(CloseData()); + CloseData &closeup = list.back(); + + closeup._x1 = file->readUint16LE(); + closeup._y1 = file->readUint16LE(); + closeup._x2 = file->readUint16LE(); + closeup._y2 = file->readUint16LE(); + closeup._closeUpType = file->readSint16LE(); + closeup._depth = depth; + closeup._graphicName = readString(file); + closeup._message = readString(file); + readCloseUps(depth + 1, file, closeup._subCloseUps); } } @@ -329,14 +302,8 @@ void Resource::readView(Common::File *file, Common::List<ViewData> &list) { view._condition = readConditions(file); view._graphicName = readString(file); - view._closeUps = readCloseUps(0, file); + readCloseUps(0, file, view._closeUps); } } -void Resource::freeView(Common::List<ViewData> &view) { - Common::List<ViewData>::iterator i; - for (i = view.begin(); i != view.end(); ++i) - freeCloseUps(i->_closeUps); -} - } // End of namespace Lab diff --git a/engines/lab/resource.h b/engines/lab/resource.h index 39672d1e32..a5732587ec 100644 --- a/engines/lab/resource.h +++ b/engines/lab/resource.h @@ -101,7 +101,6 @@ public: void readRoomData(const Common::String fileName); InventoryData *readInventory(const Common::String fileName); void readViews(uint16 roomNum); - void freeViews(uint16 roomNum); TextFont *getFont(const Common::String fileName); Common::String getText(const Common::String fileName); Common::String getStaticText(byte index) const { return _staticText[index]; } @@ -112,11 +111,8 @@ private: Common::Array<int16> readConditions(Common::File *file); void readRule(Common::File *file, RuleList &rules); Common::List<Action> readAction(Common::File *file); - void freeAction(Action *action); - CloseData *readCloseUps(uint16 depth, Common::File *file); - void freeCloseUps(CloseData *closeUps); + void readCloseUps(uint16 depth, Common::File *file, Common::List<CloseData> &close); void readView(Common::File *file, Common::List<ViewData> &view); - void freeView(Common::List<ViewData> &view); void readStaticText(); Common::String translateFileName(const Common::String filename); |