diff options
Diffstat (limited to 'engines/cruise/overlay.cpp')
-rw-r--r-- | engines/cruise/overlay.cpp | 127 |
1 files changed, 88 insertions, 39 deletions
diff --git a/engines/cruise/overlay.cpp b/engines/cruise/overlay.cpp index 21332c58c5..4176337108 100644 --- a/engines/cruise/overlay.cpp +++ b/engines/cruise/overlay.cpp @@ -46,6 +46,82 @@ void initOverlayTable(void) { numOfLoadedOverlay = 1; } +void freeOverlayTable() { + for (int i = 0; i < 90; i++) { + if (overlayTable[i].alreadyLoaded) + freeOverlay(i); + } +} + +int freeOverlay(int overlayIdx) { + ovlDataStruct *ovlDataPtr; + int i; + + if (overlayTable[overlayIdx].alreadyLoaded == 0) + return -4; + + overlayTable[overlayIdx].alreadyLoaded = 0; + + ovlDataPtr = overlayTable[overlayIdx].ovlData; + + if (!ovlDataPtr) + return -4; + + + /* + if (overlayTable[overlayIdx].var1E) { + MemFree(overlayTable[overlayIdx].var1E); + overlayTable[overlayIdx].var1E = NULL; + } + + if (overlayTable[overlayIdx].var16) { + MemFree(overlayTable[overlayIdx].var16); + overlayTable[overlayIdx].var16 = NULL; + } */ + + removeScript(overlayIdx, -1, &procHead); + removeScript(overlayIdx, -1, &procHead); + + removeScript(overlayIdx, -1, &relHead); + removeScript(overlayIdx, -1, &relHead); + + if (ovlDataPtr->stringTable) { + for (i = 0; i < ovlDataPtr->numStrings; ++i) + MemFree(ovlDataPtr->stringTable[i].string); + MemFree(ovlDataPtr->stringTable); + } + if (ovlDataPtr->arrayProc) { + ovlData3Struct *tempPtr = tempPtr = ovlDataPtr->arrayProc; + for (i = 0; i < ovlDataPtr->numProc; ++i, ++tempPtr) + MemFree(tempPtr->dataPtr); + MemFree(ovlDataPtr->arrayProc); + } + if (ovlDataPtr->ptr1) { + ovlData3Struct *tempPtr = (ovlData3Struct *)ovlDataPtr->ptr1; + for (i = 0; i < ovlDataPtr->numRel; ++i, ++tempPtr) + MemFree(tempPtr->dataPtr); + MemFree(ovlDataPtr->ptr1); + } + + MemFree(ovlDataPtr->arraySymbGlob); + MemFree(ovlDataPtr->arrayNameSymbGlob); + MemFree(ovlDataPtr->data4Ptr); + MemFree(ovlDataPtr->arrayMsgRelHeader); + MemFree(ovlDataPtr->ptr8); + MemFree(ovlDataPtr->arrayObject); + MemFree(ovlDataPtr->arrayObjVar); + MemFree(ovlDataPtr->arrayStates); + MemFree(ovlDataPtr->nameVerbGlob); + MemFree(ovlDataPtr->arrayNameObj); + + MemFree(ovlDataPtr); + overlayTable[overlayIdx].ovlData = NULL; + + debug(1, "freeOverlay: finish !"); + + return 0; +} + int loadOverlay(const char *scriptName) { int newNumberOfScript; bool scriptNotLoadedBefore; @@ -107,8 +183,9 @@ int loadOverlay(const char *scriptName) { unpackedSize = volumePtrToFileDescriptor[fileIdx].extSize + 2; - // TODO: here, can unpack in gfx module buffer - unpackedBuffer = (byte *)mallocAndZero(unpackedSize); + // This memory block will be later passed to a MemoryReadStream, which will dispose of it + unpackedBuffer = (byte *)malloc(unpackedSize); + memset(unpackedBuffer, 0, unpackedSize); if (!unpackedBuffer) { return (-2); @@ -130,7 +207,8 @@ int loadOverlay(const char *scriptName) { debug(1, "OVL loading done..."); - Common::MemoryReadStream s(unpackedBuffer, unpackedSize); + Common::MemoryReadStream s(unpackedBuffer, unpackedSize, true); + unpackedBuffer = NULL; ovlData = overlayTable[scriptIdx].ovlData; @@ -486,8 +564,9 @@ int loadOverlay(const char *scriptName) { unpackedSize = volumePtrToFileDescriptor[fileIdx].extSize + 2; - // TODO: here, can unpack in gfx module buffer - unpackedBuffer = (byte *)mallocAndZero(unpackedSize); + // This memory block will be later passed to a MemoryReadStream, which will dispose of it + unpackedBuffer = (byte *)malloc(unpackedSize); + memset(unpackedBuffer, 0, unpackedSize); if (!unpackedBuffer) { return (-2); @@ -509,7 +588,8 @@ int loadOverlay(const char *scriptName) { loadPackedFileToMem(fileIdx, (uint8 *) unpackedBuffer); } - Common::MemoryReadStream s2(unpackedBuffer, unpackedSize); + Common::MemoryReadStream s2(unpackedBuffer, unpackedSize, true); + unpackedBuffer = NULL; ovlData->specialString1Length = s2.readUint16BE(); if (ovlData->specialString1Length) { @@ -612,43 +692,12 @@ int loadOverlay(const char *scriptName) { } int releaseOverlay(const char *name) { - int overlayIdx; - ovlDataStruct *ovlDataPtr; - - overlayIdx = findOverlayByName(name); + int overlayIdx = findOverlayByName(name); if (overlayIdx == -4) return -4; - if (overlayTable[overlayIdx].alreadyLoaded == 0) - return -4; - - overlayTable[overlayIdx].alreadyLoaded = 0; - - ovlDataPtr = overlayTable[overlayIdx].ovlData; - - if (!ovlDataPtr) - return -4; - /* - if (overlayTable[overlayIdx].var1E) { - MemFree(overlayTable[overlayIdx].var1E); - overlayTable[overlayIdx].var1E = NULL; - } - - if (overlayTable[overlayIdx].var16) { - MemFree(overlayTable[overlayIdx].var16); - overlayTable[overlayIdx].var16 = NULL; - } */ - - removeScript(overlayIdx, -1, &procHead); - removeScript(overlayIdx, -1, &procHead); - - removeScript(overlayIdx, -1, &relHead); - removeScript(overlayIdx, -1, &relHead); - - debug(1, "releaseOverlay: finish !"); - - return 0; + return freeOverlay(overlayIdx); } int32 findOverlayByName2(const char *name) { |