Skip to content

Commit

Permalink
LAB: Avoid manual memory management of CloseData
Browse files Browse the repository at this point in the history
  • Loading branch information
wjp committed Dec 24, 2015
1 parent 1a1525a commit 0d97d1a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 115 deletions.
34 changes: 14 additions & 20 deletions engines/lab/engine.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions engines/lab/lab.cpp
Expand Up @@ -141,8 +141,6 @@ LabEngine::~LabEngine() {
DebugMan.clearAllDebugChannels();

freeMapData();
for (int i = 1; i <= _manyRooms; i++)
_resource->freeViews(i);
delete[] _rooms;
delete[] _inventory;

Expand Down
4 changes: 2 additions & 2 deletions engines/lab/lab.h
Expand Up @@ -336,7 +336,7 @@ class LabEngine : public Engine {
* 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.
Expand Down Expand Up @@ -441,7 +441,7 @@ class LabEngine : public Engine {
/**
* 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.
Expand Down
74 changes: 39 additions & 35 deletions engines/lab/processroom.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions engines/lab/processroom.h
Expand Up @@ -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 {
Expand Down
63 changes: 15 additions & 48 deletions engines/lab/resource.cpp
Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
6 changes: 1 addition & 5 deletions engines/lab/resource.h
Expand Up @@ -101,7 +101,6 @@ class Resource {
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]; }
Expand All @@ -112,11 +111,8 @@ class Resource {
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);

Expand Down

0 comments on commit 0d97d1a

Please sign in to comment.