From 53542073b9d9516396b1de1d883dc0192a66043b Mon Sep 17 00:00:00 2001 From: Simei Yin Date: Tue, 18 Jul 2017 19:50:45 +0200 Subject: SLUDGE: Objectify object manager --- engines/sludge/builtin.cpp | 8 ++--- engines/sludge/main_loop.cpp | 2 +- engines/sludge/objtypes.cpp | 74 +++++++++++++++++++++----------------------- engines/sludge/objtypes.h | 35 ++++++++++++++------- engines/sludge/people.cpp | 13 ++++---- engines/sludge/people.h | 2 +- engines/sludge/region.cpp | 11 ++++--- engines/sludge/region.h | 2 +- engines/sludge/sludge.cpp | 3 ++ engines/sludge/sludge.h | 2 ++ engines/sludge/talk.cpp | 5 +-- engines/sludge/variable.cpp | 2 +- 12 files changed, 88 insertions(+), 71 deletions(-) (limited to 'engines') diff --git a/engines/sludge/builtin.cpp b/engines/sludge/builtin.cpp index 83589387a7..7d5dadda14 100644 --- a/engines/sludge/builtin.cpp +++ b/engines/sludge/builtin.cpp @@ -992,7 +992,7 @@ builtIn(callEvent) { return BR_ERROR; trimStack(fun->stack); - int fNum = getCombinationFunction(obj1, obj2); + int fNum = g_sludge->_objMan->getCombinationFunction(obj1, obj2); // Return value if (fNum) { @@ -1366,7 +1366,7 @@ builtIn(rename) { if (!getValueType(objT, SVT_OBJTYPE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - objectType *o = findObjectType(objT); + ObjectType *o = g_sludge->_objMan->findObjectType(objT); o->screenName.clear(); o->screenName = newText; return BR_CONTINUE; @@ -2124,7 +2124,7 @@ builtIn(fetchEvent) { return BR_ERROR; trimStack(fun->stack); - int fNum = getCombinationFunction(obj1, obj2); + int fNum = g_sludge->_objMan->getCombinationFunction(obj1, obj2); // Return value if (fNum) { @@ -2503,7 +2503,7 @@ builtIn(hasFlag) { if (!getValueType(objNum, SVT_OBJTYPE, fun->stack->thisVar)) return BR_ERROR; trimStack(fun->stack); - objectType *objT = findObjectType(objNum); + ObjectType *objT = g_sludge->_objMan->findObjectType(objNum); if (!objT) return BR_ERROR; setVariable(fun->reg, SVT_INT, objT->flags & (1 << flagIndex)); diff --git a/engines/sludge/main_loop.cpp b/engines/sludge/main_loop.cpp index 8f31167df3..bc29209ca4 100644 --- a/engines/sludge/main_loop.cpp +++ b/engines/sludge/main_loop.cpp @@ -173,7 +173,7 @@ int main_loop(const char *filename) return fatal("Couldn't initialise people stuff"); if (!initFloor()) return fatal("Couldn't initialise floor stuff"); - if (!initObjectTypes()) + if (!g_sludge->_objMan->initObjectTypes()) return fatal("Couldn't initialise object type stuff"); initSpeech(); initStatusBar(); diff --git a/engines/sludge/objtypes.cpp b/engines/sludge/objtypes.cpp index 57365e22ae..3bea21a943 100644 --- a/engines/sludge/objtypes.cpp +++ b/engines/sludge/objtypes.cpp @@ -31,31 +31,37 @@ namespace Sludge { -objectType *allObjectTypes = NULL; +ObjectManager::~ObjectManager() { + ObjectTypeList::iterator it; + for (it = _allObjectTypes.begin(); it != _allObjectTypes.end(); ++it) { + delete [](*it)->allCombis; + delete (*it); + (*it) = nullptr; + } +} -bool initObjectTypes() { +bool ObjectManager::initObjectTypes() { return true; } -objectType *findObjectType(int i) { - objectType *huntType = allObjectTypes; - - while (huntType) { - if (huntType->objectNum == i) - return huntType; - huntType = huntType->next; +ObjectType *ObjectManager::findObjectType(int i) { + ObjectTypeList::iterator it; + for (it = _allObjectTypes.begin(); it != _allObjectTypes.end(); ++it) { + if ((*it)->objectNum == i) { + return (*it); + } } - return loadObjectType(i); } -objectType *loadObjectType(int i) { +ObjectType *ObjectManager::loadObjectType(int i) { int a, nameNum; - objectType *newType = new objectType; + ObjectType *newType = new ObjectType; + ResourceManager *rm = _vm->_resMan; if (checkNew(newType)) { - if (g_sludge->_resMan->openObjectSlice(i)) { - Common::SeekableReadStream *readStream = g_sludge->_resMan->getData(); + if (rm->openObjectSlice(i)) { + Common::SeekableReadStream *readStream = rm->getData(); nameNum = readStream->readUint16BE(); newType->r = (byte)readStream->readByte(); newType->g = (byte)readStream->readByte(); @@ -79,7 +85,7 @@ objectType *loadObjectType(int i) { } newType->numCom = readStream->readUint16BE(); - newType->allCombis = (newType->numCom) ? new combination[newType->numCom] : NULL; + newType->allCombis = (newType->numCom) ? new Combination[newType->numCom] : nullptr; for (a = 0; a < newType->numCom; a++) { @@ -87,33 +93,32 @@ objectType *loadObjectType(int i) { newType->allCombis[a].funcNum = readStream->readUint16BE(); } - g_sludge->_resMan->finishAccess(); - newType->screenName = g_sludge->_resMan->getNumberedString(nameNum); + rm->finishAccess(); + newType->screenName = rm->getNumberedString(nameNum); newType->objectNum = i; - newType->next = allObjectTypes; - allObjectTypes = newType; + _allObjectTypes.push_back(newType); return newType; } } - return NULL; + return nullptr; } -objectType *loadObjectRef(Common::SeekableReadStream *stream) { - objectType *r = loadObjectType(stream->readUint16BE()); +ObjectType *ObjectManager::loadObjectRef(Common::SeekableReadStream *stream) { + ObjectType *r = loadObjectType(stream->readUint16BE()); r->screenName.clear(); r->screenName = readString(stream); return r; } -void saveObjectRef(objectType *r, Common::WriteStream *stream) { +void ObjectManager::saveObjectRef(ObjectType *r, Common::WriteStream *stream) { stream->writeUint16BE(r->objectNum); writeString(r->screenName, stream); } -int getCombinationFunction(int withThis, int thisObject) { +int ObjectManager::getCombinationFunction(int withThis, int thisObject) { int i, num = 0; - objectType *obj = findObjectType(thisObject); + ObjectType *obj = findObjectType(thisObject); for (i = 0; i < obj->numCom; i++) { if (obj->allCombis[i].withObj == withThis) { @@ -125,20 +130,11 @@ int getCombinationFunction(int withThis, int thisObject) { return num; } -void removeObjectType(objectType *oT) { - objectType **huntRegion = &allObjectTypes; - - while (*huntRegion) { - if ((*huntRegion) == oT) { - *huntRegion = oT->next; - delete []oT->allCombis; - delete oT; - return; - } else { - huntRegion = &((*huntRegion)->next); - } - } - fatal("Can't delete object type: bad pointer"); +void ObjectManager::removeObjectType(ObjectType *oT) { + _allObjectTypes.remove(oT); + delete []oT->allCombis; + delete oT; + oT = nullptr; } } // End of namespace Sludge diff --git a/engines/sludge/objtypes.h b/engines/sludge/objtypes.h index 3958301b35..f0f5125884 100644 --- a/engines/sludge/objtypes.h +++ b/engines/sludge/objtypes.h @@ -24,28 +24,41 @@ namespace Sludge { -struct combination { +class SludgeEngine; + +struct Combination { int withObj, funcNum; }; -struct objectType { +struct ObjectType { Common::String screenName; int objectNum; - objectType *next; byte r, g, b; int numCom; int speechGap, walkSpeed, wrapSpeech, spinSpeed; uint16 flags; - combination *allCombis; + Combination *allCombis; }; -bool initObjectTypes(); -objectType *findObjectType(int i); -objectType *loadObjectType(int i); -int getCombinationFunction(int a, int b); -void removeObjectType(objectType *oT); -void saveObjectRef(objectType *r, Common::WriteStream *stream); -objectType *loadObjectRef(Common::SeekableReadStream *stream); +typedef Common::List ObjectTypeList; + +class ObjectManager { +public: + ObjectManager(SludgeEngine *vm) : _vm(vm) {} + ~ObjectManager(); + + bool initObjectTypes(); + ObjectType *findObjectType(int i); + ObjectType *loadObjectType(int i); + int getCombinationFunction(int a, int b); + void removeObjectType(ObjectType *oT); + void saveObjectRef(ObjectType *r, Common::WriteStream *stream); + ObjectType *loadObjectRef(Common::SeekableReadStream *stream); + +private: + ObjectTypeList _allObjectTypes; + SludgeEngine *_vm; +}; } // End of namespace Sludge diff --git a/engines/sludge/people.cpp b/engines/sludge/people.cpp index 7d15086b29..ad5a234384 100644 --- a/engines/sludge/people.cpp +++ b/engines/sludge/people.cpp @@ -34,6 +34,7 @@ #include "sludge/loadsave.h" #include "sludge/floor.h" #include "sludge/zbuffer.h" +#include "sludge/sludge.h" #include "sludge/sound.h" #include "sludge/version.h" @@ -811,7 +812,7 @@ bool addPerson(int x, int y, int objNum, persona *p) { return false; // EASY STUFF - newPerson->thisType = loadObjectType(objNum); + newPerson->thisType = g_sludge->_objMan->loadObjectType(objNum); newPerson->scale = 1; newPerson->extra = 0; newPerson->continueAfterWalking = NULL; @@ -909,7 +910,7 @@ void killAllPeople() { allPeople->continueAfterWalking = NULL; killPeople = allPeople; allPeople = allPeople->next; - removeObjectType(killPeople->thisType); + g_sludge->_objMan->removeObjectType(killPeople->thisType); delete killPeople; } } @@ -931,7 +932,7 @@ void killMostPeople() { if (killPeople->continueAfterWalking) abortFunction(killPeople->continueAfterWalking); killPeople->continueAfterWalking = NULL; - removeObjectType(killPeople->thisType); + g_sludge->_objMan->removeObjectType(killPeople->thisType); delete killPeople; } } @@ -955,7 +956,7 @@ void removeOneCharacter(int i) { } *killPeople = p->next; - removeObjectType(p->thisType); + g_sludge->_objMan->removeObjectType(p->thisType); delete p; } } @@ -1089,7 +1090,7 @@ bool savePeople(Common::WriteStream *stream) { stream->writeByte(me->colourmix); stream->writeByte(me->transparency); - saveObjectRef(me->thisType, stream); + g_sludge->_objMan->saveObjectRef(me->thisType, stream); me = me->next; } @@ -1171,7 +1172,7 @@ bool loadPeople(Common::SeekableReadStream *stream) { } else { setMyDrawMode(me, stream->readUint16BE()); } - me->thisType = loadObjectRef(stream); + me->thisType = g_sludge->_objMan->loadObjectRef(stream); // Anti-aliasing settings if (ssgVersion >= VERSION(1, 6)) { diff --git a/engines/sludge/people.h b/engines/sludge/people.h index 87f6170e1f..f11ac2fbee 100644 --- a/engines/sludge/people.h +++ b/engines/sludge/people.h @@ -65,7 +65,7 @@ struct onScreenPerson { int frameNum, frameTick, angle, wantAngle, angleOffset; bool show; int direction, directionWhenDoneWalking; - struct objectType *thisType; + struct ObjectType *thisType; int extra, spinSpeed; byte r, g, b, colourmix, transparency; }; diff --git a/engines/sludge/region.cpp b/engines/sludge/region.cpp index e43cd579e9..9e81809025 100644 --- a/engines/sludge/region.cpp +++ b/engines/sludge/region.cpp @@ -26,6 +26,7 @@ #include "sludge/newfatal.h" #include "sludge/objtypes.h" #include "sludge/region.h" +#include "sludge/sludge.h" #include "sludge/sludger.h" namespace Sludge { @@ -55,7 +56,7 @@ void removeScreenRegion(int objectNum) { if ((*huntRegion)->thisType->objectNum == objectNum) { killMe = *huntRegion; *huntRegion = killMe->next; - removeObjectType(killMe->thisType); + g_sludge->_objMan->removeObjectType(killMe->thisType); if (killMe == overRegion) overRegion = NULL; delete killMe; @@ -83,7 +84,7 @@ void saveRegions(Common::WriteStream *stream) { stream->writeUint16BE(thisRegion->sX); stream->writeUint16BE(thisRegion->sY); stream->writeUint16BE(thisRegion->di); - saveObjectRef(thisRegion->thisType, stream); + g_sludge->_objMan->saveObjectRef(thisRegion->thisType, stream); thisRegion = thisRegion->next; } @@ -107,7 +108,7 @@ void loadRegions(Common::SeekableReadStream *stream) { newRegion->sX = stream->readUint16BE(); newRegion->sY = stream->readUint16BE(); newRegion->di = stream->readUint16BE(); - newRegion->thisType = loadObjectRef(stream); + newRegion->thisType = g_sludge->_objMan->loadObjectRef(stream); } *pointy = NULL; } @@ -117,7 +118,7 @@ void killAllRegions() { while (allScreenRegions) { killRegion = allScreenRegions; allScreenRegions = allScreenRegions->next; - removeObjectType(killRegion->thisType); + g_sludge->_objMan->removeObjectType(killRegion->thisType); delete killRegion; } overRegion = NULL; @@ -135,7 +136,7 @@ bool addScreenRegion(int x1, int y1, int x2, int y2, int sX, int sY, int di, newRegion->y2 = y2; newRegion->sX = sX; newRegion->sY = sY; - newRegion->thisType = loadObjectType(objectNum); + newRegion->thisType = g_sludge->_objMan->loadObjectType(objectNum); newRegion->next = allScreenRegions; allScreenRegions = newRegion; return (bool) (newRegion->thisType != NULL); diff --git a/engines/sludge/region.h b/engines/sludge/region.h index 4cefd8594d..ce4157fed7 100644 --- a/engines/sludge/region.h +++ b/engines/sludge/region.h @@ -26,7 +26,7 @@ namespace Sludge { struct screenRegion { int x1, y1, x2, y2, sX, sY, di; - objectType *thisType; + ObjectType *thisType; screenRegion *next; }; diff --git a/engines/sludge/sludge.cpp b/engines/sludge/sludge.cpp index ff62459451..34992ae33c 100644 --- a/engines/sludge/sludge.cpp +++ b/engines/sludge/sludge.cpp @@ -64,6 +64,7 @@ SludgeEngine::SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc) // Init managers _resMan = new ResourceManager(); _languageMan = new LanguageManager(); + _objMan = new ObjectManager(this); } SludgeEngine::~SludgeEngine() { @@ -86,6 +87,8 @@ SludgeEngine::~SludgeEngine() { _pixelFormat = nullptr; // Dispose managers + delete _objMan; + _objMan = nullptr; delete _languageMan; _languageMan = nullptr; delete _resMan; diff --git a/engines/sludge/sludge.h b/engines/sludge/sludge.h index 8d41b5c317..cb2a509f6a 100644 --- a/engines/sludge/sludge.h +++ b/engines/sludge/sludge.h @@ -31,6 +31,7 @@ #include "sludge/console.h" #include "sludge/fileset.h" #include "sludge/language.h" +#include "sludge/objtypes.h" #include "sludge/timing.h" namespace Sludge { @@ -70,6 +71,7 @@ public: // managers ResourceManager *_resMan; LanguageManager *_languageMan; + ObjectManager *_objMan; SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc); virtual ~SludgeEngine(); diff --git a/engines/sludge/talk.cpp b/engines/sludge/talk.cpp index e92cd00ef8..bdfe43100a 100644 --- a/engines/sludge/talk.cpp +++ b/engines/sludge/talk.cpp @@ -29,6 +29,7 @@ #include "sludge/sprbanks.h" #include "sludge/people.h" #include "sludge/talk.h" +#include "sludge/sludge.h" #include "sludge/sound.h" #include "sludge/fonttext.h" #include "sludge/newfatal.h" @@ -71,7 +72,7 @@ void killAllSpeech() { } } -inline void setObjFontColour(objectType *t) { +inline void setObjFontColour(ObjectType *t) { setFontColour(speech->talkCol, t->r, t->g, t->b); } @@ -188,7 +189,7 @@ int wrapSpeech(const Common::String &theText, int objT, int sampleFile, bool ani thisRegion->y1 - thisRegion->thisType->speechGap - cameraY, thisRegion->thisType->wrapSpeech, sampleFile); } else { - objectType *temp = findObjectType(objT); + ObjectType *temp = g_sludge->_objMan->findObjectType(objT); setObjFontColour(temp); i = wrapSpeechXY(theText, winWidth >> 1, 10, temp->wrapSpeech, sampleFile); diff --git a/engines/sludge/variable.cpp b/engines/sludge/variable.cpp index b445095630..5b03dadda8 100644 --- a/engines/sludge/variable.cpp +++ b/engines/sludge/variable.cpp @@ -310,7 +310,7 @@ Common::String getTextFromAnyVar(const variable &from) { } case SVT_OBJTYPE: { - objectType *thisType = findObjectType(from.varData.intValue); + ObjectType *thisType = g_sludge->_objMan->findObjectType(from.varData.intValue); if (thisType) return thisType->screenName; break; -- cgit v1.2.3