Started to move some resource related code into a new class ResourceManager (hypothetic goal for the future: use ResourceManager in ScummEx)

svn-id: r17311
This commit is contained in:
Max Horn 2005-03-31 21:39:31 +00:00
parent 59331fe535
commit ac247c9b94
20 changed files with 211 additions and 193 deletions

View file

@ -739,7 +739,7 @@ byte AkosRenderer::codec1(int xmoveCur, int ymoveCur) {
/* implement custom scale table */
v1.scaletable = (_vm->_features & GF_HUMONGOUS) ? oldScaleTable : defaultScaleTable;
if (_vm->VAR_CUSTOMSCALETABLE != 0xFF && _vm->isResourceLoaded(rtString, _vm->VAR(_vm->VAR_CUSTOMSCALETABLE))) {
if (_vm->VAR_CUSTOMSCALETABLE != 0xFF && _vm->res.isResourceLoaded(rtString, _vm->VAR(_vm->VAR_CUSTOMSCALETABLE))) {
v1.scaletable = _vm->getStringAddressVar(_vm->VAR_CUSTOMSCALETABLE);
}

View file

@ -254,8 +254,8 @@ void ScummEngine::initScreens(int b, int h) {
int adj = 0;
for (i = 0; i < 3; i++) {
nukeResource(rtBuffer, i + 1);
nukeResource(rtBuffer, i + 5);
res.nukeResource(rtBuffer, i + 1);
res.nukeResource(rtBuffer, i + 5);
}
if (!getResourceAddress(rtBuffer, 4)) {

View file

@ -342,7 +342,7 @@ ImuseDigiSndMgr::soundStruct *ImuseDigiSndMgr::openSound(int32 soundId, const ch
assert(soundName[0] == 0); // Paranoia check
_vm->ensureResourceLoaded(rtSound, soundId);
_vm->lock(rtSound, soundId);
_vm->res.lock(rtSound, soundId);
ptr = _vm->getResourceAddress(rtSound, soundId);
if (ptr == NULL) {
closeSound(sound);
@ -413,7 +413,7 @@ void ImuseDigiSndMgr::closeSound(soundStruct *soundHandle) {
found = true;
}
if (!found)
_vm->unlock(rtSound, soundHandle->soundId);
_vm->res.unlock(rtSound, soundHandle->soundId);
}
if (soundHandle->compressedStream)

View file

@ -1119,7 +1119,7 @@ int Insane::smlayer_loadSound(int id, int flag, int phase) {
if (phase == 2)
_vm->ensureResourceLoaded(rtSound, resid);
_vm->setResourceCounter(rtSound, resid, 1);
_vm->res.setResourceCounter(rtSound, resid, 1);
if (phase == 1) {
_objArray2Idx2++;
@ -1141,7 +1141,7 @@ int Insane::smlayer_loadCostume(int id, int phase) {
return 0;
_vm->ensureResourceLoaded(rtCostume, resid);
_vm->setResourceCounter(rtCostume, resid, 1);
_vm->res.setResourceCounter(rtCostume, resid, 1);
// smlayer_lock(rtCostume, resid); // FIXME

View file

@ -521,7 +521,7 @@ void ScummEngine::clearRoomObjects() {
} else {
// Nuke all unlocked flObjects
if (!(res.flags[rtFlObject][_objs[i].fl_object_index] & RF_LOCK)) {
nukeResource(rtFlObject, _objs[i].fl_object_index);
res.nukeResource(rtFlObject, _objs[i].fl_object_index);
_objs[i].obj_nr = 0;
_objs[i].fl_object_index = 0;
}
@ -896,7 +896,7 @@ void ScummEngine::clearOwnerOf(int obj) {
if (_objs[i].obj_nr == obj) {
if (!_objs[i].fl_object_index)
return;
nukeResource(rtFlObject, _objs[i].fl_object_index);
res.nukeResource(rtFlObject, _objs[i].fl_object_index);
_objs[i].obj_nr = 0;
_objs[i].fl_object_index = 0;
}
@ -908,7 +908,7 @@ void ScummEngine::clearOwnerOf(int obj) {
if (_inventory[i] == obj) {
j = whereIsObject(obj);
if (j == WIO_INVENTORY) {
nukeResource(rtInventory, i);
res.nukeResource(rtInventory, i);
_inventory[i] = 0;
}
a = _inventory;
@ -990,7 +990,7 @@ void ScummEngine::setObjectName(int obj) {
for (i = 0; i < _numNewNames; i++) {
if (_newNames[i] == obj) {
nukeResource(rtObjectName, i);
res.nukeResource(rtObjectName, i);
_newNames[i] = 0;
break;
}
@ -1420,7 +1420,7 @@ void ScummEngine::nukeFlObjects(int min, int max) {
for (i = (_numLocalObjects-1), od = _objs; --i >= 0; od++)
if (od->fl_object_index && od->obj_nr >= min && od->obj_nr <= max) {
nukeResource(rtFlObject, od->fl_object_index);
res.nukeResource(rtFlObject, od->fl_object_index);
od->obj_nr = 0;
od->fl_object_index = 0;
}
@ -1638,9 +1638,9 @@ void ScummEngine::loadFlObject(uint object, uint room) {
isRoomLocked = ((res.flags[rtRoom][room] & RF_LOCK) != 0);
isRoomScriptsLocked = ((res.flags[rtRoomScripts][room] & RF_LOCK) != 0);
if (!isRoomLocked)
lock(rtRoom, room);
res.lock(rtRoom, room);
if (_version == 8 && !isRoomScriptsLocked)
lock(rtRoomScripts, room);
res.lock(rtRoomScripts, room);
// Allocate slot & memory for floating object
slot = findFlObjectSlot();
@ -1656,9 +1656,9 @@ void ScummEngine::loadFlObject(uint object, uint room) {
// Unlock room/roomScripts
if (!isRoomLocked)
unlock(rtRoom, room);
res.unlock(rtRoom, room);
if (_version == 8 && !isRoomScriptsLocked)
unlock(rtRoomScripts, room);
res.unlock(rtRoomScripts, room);
// Setup local object flags
setupRoomObject(od, flob, flob);

View file

@ -631,7 +631,7 @@ void ScummEngine::loadCharset(int no) {
void ScummEngine::nukeCharset(int i) {
checkRange(_numCharsets - 1, 1, i, "Nuking illegal charset %d");
nukeResource(rtCharset, i);
res.nukeResource(rtCharset, i);
}
void ScummEngine::ensureResourceLoaded(int type, int i) {
@ -749,7 +749,7 @@ int ScummEngine::loadResource(int type, int idx) {
return 1;
}
nukeResource(type, idx);
res.nukeResource(type, idx);
error("Cannot read resource");
}
@ -771,7 +771,7 @@ byte *ScummEngine::getResourceAddress(int type, int idx) {
byte *ptr;
CHECK_HEAP
if (!validateResource("getResourceAddress", type, idx))
if (!res.validateResource("getResourceAddress", type, idx))
return NULL;
if (!res.address[type]) {
@ -788,7 +788,7 @@ byte *ScummEngine::getResourceAddress(int type, int idx) {
return NULL;
}
setResourceCounter(type, idx, 1);
res.setResourceCounter(type, idx, 1);
debugC(DEBUG_RESOURCE, "getResourceAddress(%s,%d) == %p", resTypeFromId(type), idx, ptr + sizeof(MemBlkHeader));
return ptr + sizeof(MemBlkHeader);
@ -811,9 +811,23 @@ byte *ScummEngine::getStringAddressVar(int i) {
return getStringAddress(_scummVars[i]);
}
void ScummEngine::setResourceCounter(int type, int idx, byte flag) {
res.flags[type][idx] &= ~RF_USAGE;
res.flags[type][idx] |= flag;
void ResourceManager::increaseResourceCounter() {
int i, j;
byte counter;
for (i = rtFirst; i <= rtLast; i++) {
for (j = num[i]; --j >= 0;) {
counter = flags[i][j] & RF_USAGE;
if (counter && counter < RF_USAGE_MAX) {
setResourceCounter(i, j, counter + 1);
}
}
}
}
void ResourceManager::setResourceCounter(int type, int idx, byte flag) {
flags[type][idx] &= ~RF_USAGE;
flags[type][idx] |= flag;
}
/* 2 bytes safety area to make "precaching" of bytes in the gdi drawer easier */
@ -825,9 +839,9 @@ byte *ScummEngine::createResource(int type, int idx, uint32 size) {
CHECK_HEAP
debugC(DEBUG_RESOURCE, "createResource(%s,%d,%d)", resTypeFromId(type), idx, size);
if (!validateResource("allocating", type, idx))
if (!res.validateResource("allocating", type, idx))
return NULL;
nukeResource(type, idx);
res.nukeResource(type, idx);
expireResources(size);
@ -837,35 +851,41 @@ byte *ScummEngine::createResource(int type, int idx, uint32 size) {
error("Out of memory while allocating %d", size);
}
_allocatedSize += size;
res._allocatedSize += size;
res.address[type][idx] = ptr;
((MemBlkHeader *)ptr)->size = size;
setResourceCounter(type, idx, 1);
res.setResourceCounter(type, idx, 1);
return ptr + sizeof(MemBlkHeader); /* skip header */
}
bool ScummEngine::validateResource(const char *str, int type, int idx) const {
if (type < rtFirst || type > rtLast || (uint) idx >= (uint) res.num[type]) {
ResourceManager::ResourceManager(ScummEngine *vm) {
memset(this, 0, sizeof(ResourceManager));
_vm = vm;
// _allocatedSize = 0;
}
bool ResourceManager::validateResource(const char *str, int type, int idx) const {
if (type < rtFirst || type > rtLast || (uint) idx >= (uint)num[type]) {
warning("%s Illegal Glob type %s (%d) num %d", str, resTypeFromId(type), type, idx);
return false;
}
return true;
}
void ScummEngine::nukeResource(int type, int idx) {
void ResourceManager::nukeResource(int type, int idx) {
byte *ptr;
CHECK_HEAP
if (!res.address[type])
if (!address[type])
return;
assert(idx >= 0 && idx < res.num[type]);
assert(idx >= 0 && idx < num[type]);
if ((ptr = res.address[type][idx]) != NULL) {
if ((ptr = address[type][idx]) != NULL) {
debugC(DEBUG_RESOURCE, "nukeResource(%s,%d)", resTypeFromId(type), idx);
res.address[type][idx] = 0;
res.flags[type][idx] = 0;
address[type][idx] = 0;
flags[type][idx] = 0;
_allocatedSize -= ((MemBlkHeader *)ptr)->size;
free(ptr);
}
@ -896,20 +916,20 @@ int ScummEngine::getResourceDataSize(const byte *ptr) const {
return READ_BE_UINT32(ptr - 4) - 8;
}
void ScummEngine::lock(int type, int i) {
void ResourceManager::lock(int type, int i) {
if (!validateResource("Locking", type, i))
return;
res.flags[type][i] |= RF_LOCK;
flags[type][i] |= RF_LOCK;
}
void ScummEngine::unlock(int type, int i) {
void ResourceManager::unlock(int type, int i) {
if (!validateResource("Unlocking", type, i))
return;
res.flags[type][i] &= ~RF_LOCK;
flags[type][i] &= ~RF_LOCK;
}
bool ScummEngine::isResourceInUse(int type, int i) const {
if (!validateResource("isResourceInUse", type, i))
if (!res.validateResource("isResourceInUse", type, i))
return false;
switch (type) {
case rtRoom:
@ -927,20 +947,6 @@ bool ScummEngine::isResourceInUse(int type, int i) const {
}
}
void ScummEngine::increaseResourceCounter() {
int i, j;
byte counter;
for (i = rtFirst; i <= rtLast; i++) {
for (j = res.num[i]; --j >= 0;) {
counter = res.flags[i][j] & RF_USAGE;
if (counter && counter < RF_USAGE_MAX) {
setResourceCounter(i, j, counter + 1);
}
}
}
}
void ScummEngine::expireResources(uint32 size) {
int i, j;
byte flag;
@ -948,15 +954,15 @@ void ScummEngine::expireResources(uint32 size) {
int best_type, best_res = 0;
uint32 oldAllocatedSize;
if (_expire_counter != 0xFF) {
_expire_counter = 0xFF;
increaseResourceCounter();
if (res._expireCounter != 0xFF) {
res._expireCounter = 0xFF;
res.increaseResourceCounter();
}
if (size + _allocatedSize < _maxHeapThreshold)
if (size + res._allocatedSize < res._maxHeapThreshold)
return;
oldAllocatedSize = _allocatedSize;
oldAllocatedSize = res._allocatedSize;
do {
best_type = 0;
@ -976,32 +982,27 @@ void ScummEngine::expireResources(uint32 size) {
if (!best_type)
break;
nukeResource(best_type, best_res);
} while (size + _allocatedSize > _minHeapThreshold);
res.nukeResource(best_type, best_res);
} while (size + res._allocatedSize > res._minHeapThreshold);
increaseResourceCounter();
res.increaseResourceCounter();
debugC(DEBUG_RESOURCE, "Expired resources, mem %d -> %d", oldAllocatedSize, _allocatedSize);
debugC(DEBUG_RESOURCE, "Expired resources, mem %d -> %d", oldAllocatedSize, res._allocatedSize);
}
void ScummEngine::freeResources() {
void ResourceManager::freeResources() {
int i, j;
for (i = rtFirst; i <= rtLast; i++) {
for (j = res.num[i]; --j >= 0;) {
for (j = num[i]; --j >= 0;) {
if (isResourceLoaded(i, j))
nukeResource(i, j);
}
free(res.address[i]);
free(res.flags[i]);
free(res.roomno[i]);
free(res.roomoffs[i]);
free(address[i]);
free(flags[i]);
free(roomno[i]);
free(roomoffs[i]);
if (_heversion >= 70)
free(res.globsize[i]);
}
if (_heversion >= 70) {
free(_heV7RoomIntOffsets);
free(_heV7RoomOffsets);
free(globsize[i]);
}
}
@ -1009,7 +1010,7 @@ void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source)
byte *alloced;
int i, len;
nukeResource(type, resindex);
res.nukeResource(type, resindex);
len = resStrLen(source) + 1;
@ -1028,10 +1029,10 @@ void ScummEngine::loadPtrToResource(int type, int resindex, const byte *source)
}
}
bool ScummEngine::isResourceLoaded(int type, int idx) const {
bool ResourceManager::isResourceLoaded(int type, int idx) const {
if (!validateResource("isResourceLoaded", type, idx))
return false;
return res.address[type][idx] != NULL;
return address[type][idx] != NULL;
}
void ScummEngine::resourceStats() {
@ -1048,7 +1049,7 @@ void ScummEngine::resourceStats() {
}
}
debug(1, "Total allocated size=%d, locked=%d(%d)", _allocatedSize, lockedSize, lockedNum);
debug(1, "Total allocated size=%d, locked=%d(%d)", res._allocatedSize, lockedSize, lockedNum);
}
void ScummEngine::readMAXS(int blockSize) {

View file

@ -167,7 +167,7 @@ bool ScummEngine::loadState(int slot, bool compat) {
for (i = rtFirst; i <= rtLast; i++)
if (i != rtTemp && i != rtBuffer && (i != rtSound || _saveSound || !compat))
for (j = 0; j < res.num[i]; j++) {
nukeResource(i, j);
res.nukeResource(i, j);
res.flags[i][j] = 0;
}

View file

@ -364,7 +364,7 @@ void ScummEngine::nukeArrays(byte script) {
for (i = 1; i < _numArray; i++) {
if (_arraySlot[i] == script) {
nukeResource(rtString, i);
res.nukeResource(rtString, i);
_arraySlot[i] = 0;
}
}
@ -897,7 +897,7 @@ void ScummEngine::killScriptsAndResources() {
continue;
_newNames[i] = 0;
nukeResource(rtObjectName, i);
res.nukeResource(rtObjectName, i);
}
}
}

View file

@ -1128,16 +1128,16 @@ void ScummEngine_v100he::o100_resourceRoutines() {
if (_heResType == rtFlObject) {
objidx = getObjectIndex(_heResId);
//assert(objidx != -1);
//lock(_heResType, objidx);
//res.lock(_heResType, objidx);
} else {
lock(_heResType, _heResId);
res.lock(_heResType, _heResId);
}
break;
case 133:
if (_heResType == rtCharset)
nukeCharset(_heResId);
else
nukeResource(_heResType, _heResId);
res.nukeResource(_heResType, _heResId);
break;
case 134:
case 135:
@ -1153,9 +1153,9 @@ void ScummEngine_v100he::o100_resourceRoutines() {
if (_heResType == rtFlObject) {
objidx = getObjectIndex(_heResId);
//assert(objidx != -1);
//unlock(_heResType, objidx);
//res.unlock(_heResType, objidx);
} else {
unlock(_heResType, _heResId);
res.unlock(_heResType, _heResId);
}
break;
default:

View file

@ -782,9 +782,9 @@ void ScummEngine_v2::o2_resourceRoutines() {
ensureResourceLoaded(type, resid);
} else {
if (opcode & 1)
lock(type, resid);
res.lock(type, resid);
else
unlock(type, resid);
res.unlock(type, resid);
}
}

View file

@ -1732,40 +1732,40 @@ void ScummEngine_v5::o5_resourceRoutines() {
if (_gameId == GID_ZAK256)
warning("o5_resourceRoutines %d should not occur in Zak256", op);
else
setResourceCounter(resType[op-5], resid, 0x7F);
res.setResourceCounter(resType[op-5], resid, 0x7F);
break;
case 9: // SO_LOCK_SCRIPT
if (resid >= _numGlobalScripts)
break;
lock(rtScript, resid);
res.lock(rtScript, resid);
break;
case 10: // SO_LOCK_SOUND
lock(rtSound, resid);
res.lock(rtSound, resid);
break;
case 11: // SO_LOCK_COSTUME
lock(rtCostume, resid);
res.lock(rtCostume, resid);
break;
case 12: // SO_LOCK_ROOM
if (resid > 0x7F)
resid = _resourceMapper[resid & 0x7F];
lock(rtRoom, resid);
res.lock(rtRoom, resid);
break;
case 13: // SO_UNLOCK_SCRIPT
if (resid >= _numGlobalScripts)
break;
unlock(rtScript, resid);
res.unlock(rtScript, resid);
break;
case 14: // SO_UNLOCK_SOUND
unlock(rtSound, resid);
res.unlock(rtSound, resid);
break;
case 15: // SO_UNLOCK_COSTUME
unlock(rtCostume, resid);
res.unlock(rtCostume, resid);
break;
case 16: // SO_UNLOCK_ROOM
if (resid > 0x7F)
resid = _resourceMapper[resid & 0x7F];
unlock(rtRoom, resid);
res.unlock(rtRoom, resid);
break;
case 17: // SO_CLEAR_HEAP
@ -2256,7 +2256,7 @@ void ScummEngine_v5::o5_stringOps() {
case 2: /* copystring */
a = getVarOrDirectByte(PARAM_1);
b = getVarOrDirectByte(PARAM_2);
nukeResource(rtString, a);
res.nukeResource(rtString, a);
ptr = getResourceAddress(rtString, b);
if (ptr)
loadPtrToResource(rtString, a, ptr);
@ -2287,7 +2287,7 @@ void ScummEngine_v5::o5_stringOps() {
case 5: /* create empty string */
a = getVarOrDirectByte(PARAM_1);
b = getVarOrDirectByte(PARAM_2);
nukeResource(rtString, a);
res.nukeResource(rtString, a);
if (b) {
ptr = createResource(rtString, a, b);
if (ptr) {
@ -2332,7 +2332,7 @@ void ScummEngine_v5::o5_verbOps() {
case 2: // SO_VERB_NAME
loadPtrToResource(rtVerb, slot, NULL);
if (slot == 0)
nukeResource(rtVerb, slot);
res.nukeResource(rtVerb, slot);
vs->type = kTextVerbType;
vs->imgindex = 0;
break;
@ -2446,12 +2446,12 @@ void ScummEngine_v5::o5_verbOps() {
case 20: // SO_VERB_NAME_STR
ptr = getResourceAddress(rtString, getVarOrDirectWord(PARAM_1));
if (!ptr)
nukeResource(rtVerb, slot);
res.nukeResource(rtVerb, slot);
else {
loadPtrToResource(rtVerb, slot, ptr);
}
if (slot == 0)
nukeResource(rtVerb, slot);
res.nukeResource(rtVerb, slot);
vs->type = kTextVerbType;
vs->imgindex = 0;
break;

View file

@ -466,7 +466,7 @@ void ScummEngine_v6::nukeArray(int a) {
data = readVar(a);
if (data)
nukeResource(rtString, data);
res.nukeResource(rtString, data);
if (_features & GF_HUMONGOUS)
_arraySlot[data] = 0;
@ -1559,59 +1559,59 @@ void ScummEngine_v6::o6_resourceRoutines() {
if (_version >= 7)
if (resid >= _numGlobalScripts)
break;
setResourceCounter(rtScript, resid, 0x7F);
res.setResourceCounter(rtScript, resid, 0x7F);
break;
case 105: // SO_NUKE_SOUND
resid = pop();
setResourceCounter(rtSound, resid, 0x7F);
res.setResourceCounter(rtSound, resid, 0x7F);
break;
case 106: // SO_NUKE_COSTUME
resid = pop();
setResourceCounter(rtCostume, resid, 0x7F);
res.setResourceCounter(rtCostume, resid, 0x7F);
break;
case 107: // SO_NUKE_ROOM
resid = pop();
setResourceCounter(rtRoom, resid, 0x7F);
res.setResourceCounter(rtRoom, resid, 0x7F);
break;
case 108: // SO_LOCK_SCRIPT
resid = pop();
if (resid >= _numGlobalScripts)
break;
lock(rtScript, resid);
res.lock(rtScript, resid);
break;
case 109: // SO_LOCK_SOUND
resid = pop();
lock(rtSound, resid);
res.lock(rtSound, resid);
break;
case 110: // SO_LOCK_COSTUME
resid = pop();
lock(rtCostume, resid);
res.lock(rtCostume, resid);
break;
case 111: // SO_LOCK_ROOM
resid = pop();
if (resid > 0x7F)
resid = _resourceMapper[resid & 0x7F];
lock(rtRoom, resid);
res.lock(rtRoom, resid);
break;
case 112: // SO_UNLOCK_SCRIPT
resid = pop();
if (resid >= _numGlobalScripts)
break;
unlock(rtScript, resid);
res.unlock(rtScript, resid);
break;
case 113: // SO_UNLOCK_SOUND
resid = pop();
unlock(rtSound, resid);
res.unlock(rtSound, resid);
break;
case 114: // SO_UNLOCK_COSTUME
resid = pop();
unlock(rtCostume, resid);
res.unlock(rtCostume, resid);
break;
case 115: // SO_UNLOCK_ROOM
resid = pop();
if (resid > 0x7F)
resid = _resourceMapper[resid & 0x7F];
unlock(rtRoom, resid);
res.unlock(rtRoom, resid);
break;
case 116: // SO_CLEAR_HEAP
/* this is actually a scumm message */

View file

@ -532,59 +532,59 @@ void ScummEngine_v70he::o70_resourceRoutines() {
break;
case 104: // SO_NUKE_SCRIPT
resid = pop();
setResourceCounter(rtScript, resid, 0x7F);
res.setResourceCounter(rtScript, resid, 0x7F);
break;
case 105: // SO_NUKE_SOUND
resid = pop();
setResourceCounter(rtSound, resid, 0x7F);
res.setResourceCounter(rtSound, resid, 0x7F);
break;
case 106: // SO_NUKE_COSTUME
resid = pop();
setResourceCounter(rtCostume, resid, 0x7F);
res.setResourceCounter(rtCostume, resid, 0x7F);
break;
case 107: // SO_NUKE_ROOM
resid = pop();
setResourceCounter(rtRoom, resid, 0x7F);
res.setResourceCounter(rtRoom, resid, 0x7F);
break;
case 108: // SO_LOCK_SCRIPT
resid = pop();
if (resid >= _numGlobalScripts)
break;
lock(rtScript, resid);
res.lock(rtScript, resid);
break;
case 109: // SO_LOCK_SOUND
resid = pop();
lock(rtSound, resid);
res.lock(rtSound, resid);
break;
case 110: // SO_LOCK_COSTUME
resid = pop();
lock(rtCostume, resid);
res.lock(rtCostume, resid);
break;
case 111: // SO_LOCK_ROOM
resid = pop();
if (resid > 0x7F)
resid = _resourceMapper[resid & 0x7F];
lock(rtRoom, resid);
res.lock(rtRoom, resid);
break;
case 112: // SO_UNLOCK_SCRIPT
resid = pop();
if (resid >= _numGlobalScripts)
break;
unlock(rtScript, resid);
res.unlock(rtScript, resid);
break;
case 113: // SO_UNLOCK_SOUND
resid = pop();
unlock(rtSound, resid);
res.unlock(rtSound, resid);
break;
case 114: // SO_UNLOCK_COSTUME
resid = pop();
unlock(rtCostume, resid);
res.unlock(rtCostume, resid);
break;
case 115: // SO_UNLOCK_ROOM
resid = pop();
if (resid > 0x7F)
resid = _resourceMapper[resid & 0x7F];
unlock(rtRoom, resid);
res.unlock(rtRoom, resid);
break;
case 116:
break;
@ -611,11 +611,11 @@ void ScummEngine_v70he::o70_resourceRoutines() {
break;
case 159:
resid = pop();
unlock(rtImage, resid);
res.unlock(rtImage, resid);
break;
case 192:
resid = pop();
nukeResource(rtImage, resid);
res.nukeResource(rtImage, resid);
break;
case 201:
resid = pop();
@ -623,7 +623,7 @@ void ScummEngine_v70he::o70_resourceRoutines() {
break;
case 202:
resid = pop();
lock(rtImage, resid);
res.lock(rtImage, resid);
break;
case 233:
resid = pop();

View file

@ -779,40 +779,40 @@ void ScummEngine_v8::o8_resourceRoutines() {
break;
case 0x42: // SO_HEAP_LOCK_COSTUME Lock costume in heap
lock(rtCostume, resid);
res.lock(rtCostume, resid);
break;
case 0x43: // SO_HEAP_LOCK_ROOM Lock room in heap
lock(rtRoom, resid);
res.lock(rtRoom, resid);
break;
case 0x44: // SO_HEAP_LOCK_SCRIPT Lock script in heap
lock(rtScript, resid);
res.lock(rtScript, resid);
break;
case 0x45: // SO_HEAP_LOCK_SOUND Lock sound in heap
lock(rtSound, resid);
res.lock(rtSound, resid);
break;
case 0x46: // SO_HEAP_UNLOCK_COSTUME Unlock costume
unlock(rtCostume, resid);
res.unlock(rtCostume, resid);
break;
case 0x47: // SO_HEAP_UNLOCK_ROOM Unlock room
unlock(rtRoom, resid);
res.unlock(rtRoom, resid);
break;
case 0x48: // SO_HEAP_UNLOCK_SCRIPT Unlock script
unlock(rtScript, resid);
res.unlock(rtScript, resid);
break;
case 0x49: // SO_HEAP_UNLOCK_SOUND Unlock sound
unlock(rtSound, resid);
res.unlock(rtSound, resid);
break;
case 0x4A: // SO_HEAP_NUKE_COSTUME Remove costume from heap
setResourceCounter(rtCostume, resid, 0x7F);
res.setResourceCounter(rtCostume, resid, 0x7F);
break;
case 0x4B: // SO_HEAP_NUKE_ROOM Remove room from heap
setResourceCounter(rtRoom, resid, 0x7F);
res.setResourceCounter(rtRoom, resid, 0x7F);
break;
case 0x4C: // SO_HEAP_NUKE_SCRIPT Remove script from heap
setResourceCounter(rtScript, resid, 0x7F);
res.setResourceCounter(rtScript, resid, 0x7F);
break;
case 0x4D: // SO_HEAP_NUKE_SOUND Remove sound from heap
setResourceCounter(rtSound, resid, 0x7F);
res.setResourceCounter(rtSound, resid, 0x7F);
break;
default:
error("o8_resourceRoutines: default case 0x%x", subOp);
@ -1214,13 +1214,13 @@ void ScummEngine_v8::o8_kernelSetFunctions() {
case 11: { // lockObject
int objidx = getObjectIndex(args[1]);
assert(objidx != -1);
lock(rtFlObject, objidx);
res.lock(rtFlObject, objidx);
break;
}
case 12: { // unlockObject
int objidx = getObjectIndex(args[1]);
assert(objidx != -1);
unlock(rtFlObject, objidx);
res.unlock(rtFlObject, objidx);
break;
}
case 13: // remapCostume

View file

@ -594,7 +594,9 @@ ScummEngine::ScummEngine(GameDetector *detector, OSystem *syst, const ScummGameS
_heversion(gs.heversion),
_numActors(gs.numActors),
_features(gs.features),
gdi(this), _pauseDialog(0), _mainMenuDialog(0), _versionDialog(0),
gdi(this),
res(this),
_pauseDialog(0), _mainMenuDialog(0), _versionDialog(0),
_targetName(detector->_targetName) {
// Copy MD5 checksum
@ -758,8 +760,6 @@ ScummEngine::ScummEngine(GameDetector *detector, OSystem *syst, const ScummGameS
_lastSaveTime = 0;
_saveTemporaryState = false;
memset(_saveLoadName, 0, sizeof(_saveLoadName));
_maxHeapThreshold = 0;
_minHeapThreshold = 0;
memset(_localScriptOffsets, 0, sizeof(_localScriptOffsets));
_scriptPointer = NULL;
_scriptOrgPointer = NULL;
@ -776,8 +776,6 @@ ScummEngine::ScummEngine(GameDetector *detector, OSystem *syst, const ScummGameS
_fileOffset = 0;
_dynamicRoomOffsets = false;
memset(_resourceMapper, 0, sizeof(_resourceMapper));
_allocatedSize = 0;
_expire_counter = 0;
_lastLoadedRoom = 0;
_roomResource = 0;
_substResFileNameIndex = 0;
@ -1173,7 +1171,11 @@ ScummEngine::~ScummEngine() {
free(_shadowPalette);
freeResources();
res.freeResources();
if (_heversion >= 70) {
free(_heV7RoomIntOffsets);
free(_heV7RoomOffsets);
}
free(_objectStateTable);
free(_objectRoomTable);
@ -1309,18 +1311,18 @@ int ScummEngine::init(GameDetector &detector) {
#ifdef __PALM_OS__
if (_features & GF_NEW_COSTUMES)
_maxHeapThreshold = gVars->memory[kMemScummNewCostGames];
res._maxHeapThreshold = gVars->memory[kMemScummNewCostGames];
else
_maxHeapThreshold = gVars->memory[kMemScummOldCostGames];
res._maxHeapThreshold = gVars->memory[kMemScummOldCostGames];
#else
// Since the new costumes are very big, we increase the heap limit, to avoid having
// to constantly reload stuff from the data files.
if (_features & GF_NEW_COSTUMES)
_maxHeapThreshold = 2500000;
res._maxHeapThreshold = 2500000;
else
_maxHeapThreshold = 550000;
res._maxHeapThreshold = 550000;
#endif
_minHeapThreshold = 400000;
res._minHeapThreshold = 400000;
allocResTypeData(rtBuffer, MKID('NONE'), 10, "buffer", 0);
@ -2074,8 +2076,8 @@ load_game:
camera._last = camera._cur;
if (!(++_expire_counter)) {
increaseResourceCounter();
if (!(++res._expireCounter)) {
res.increaseResourceCounter();
}
animateCursor();
@ -2174,7 +2176,7 @@ void ScummEngine::startScene(int room, Actor *a, int objectNr) {
VAR(VAR_ROOM) = room;
_fullRedraw = true;
increaseResourceCounter();
res.increaseResourceCounter();
_currentRoom = room;
VAR(VAR_ROOM) = room;
@ -2391,8 +2393,8 @@ void ScummEngine::initRoomSubBlocks() {
//
// Load box data
//
nukeResource(rtMatrix, 1);
nukeResource(rtMatrix, 2);
res.nukeResource(rtMatrix, 1);
res.nukeResource(rtMatrix, 2);
if (_features & GF_SMALL_HEADER) {
if (_version <= 2)
ptr = roomptr + *(roomptr + 0x15);
@ -2451,7 +2453,7 @@ void ScummEngine::initRoomSubBlocks() {
// Load scale data
//
for (i = 1; i < res.num[rtScaleTable]; i++)
nukeResource(rtScaleTable, i);
res.nukeResource(rtScaleTable, i);
if (_features & GF_OLD_BUNDLE)
ptr = 0;

View file

@ -291,6 +291,44 @@ struct AuxEntry {
int subIndex;
};
class ResourceManager {
friend class ScummDebugger;
friend class ScummEngine;
protected:
ScummEngine *_vm;
public:
byte mode[rtNumTypes];
uint16 num[rtNumTypes];
uint32 tags[rtNumTypes];
const char *name[rtNumTypes];
byte **address[rtNumTypes];
byte *flags[rtNumTypes];
byte *roomno[rtNumTypes];
uint32 *roomoffs[rtNumTypes];
uint32 *globsize[rtNumTypes];
uint32 _allocatedSize;
uint32 _maxHeapThreshold, _minHeapThreshold;
byte _expireCounter;
public:
ResourceManager(ScummEngine *vm);
void nukeResource(int type, int i);
void freeResources();
bool validateResource(const char *str, int type, int index) const;
bool isResourceLoaded(int type, int index) const;
void lock(int type, int i);
void unlock(int type, int i);
void setResourceCounter(int type, int index, byte flag);
void increaseResourceCounter();
};
class ScummEngine : public Engine {
friend class ScummDebugger;
friend class SmushPlayer;
@ -325,20 +363,10 @@ public:
/** Graphics manager */
Gdi gdi;
protected:
/** Central resource data. */
struct {
byte mode[rtNumTypes];
uint16 num[rtNumTypes];
uint32 tags[rtNumTypes];
const char *name[rtNumTypes];
byte **address[rtNumTypes];
byte *flags[rtNumTypes];
byte *roomno[rtNumTypes];
uint32 *roomoffs[rtNumTypes];
uint32 *globsize[rtNumTypes];
} res;
ResourceManager res;
protected:
VirtualMachineState vm;
public:
@ -534,13 +562,7 @@ public:
void requestSave(int slot, const char *name, bool temporary = false);
void requestLoad(int slot);
void lock(int type, int i);
void unlock(int type, int i);
protected:
/* Heap and memory management */
uint32 _maxHeapThreshold, _minHeapThreshold;
/* Script VM - should be in Script class */
uint32 _localScriptOffsets[256];
const byte *_scriptPointer, *_scriptOrgPointer;
@ -631,8 +653,6 @@ protected:
Common::String _targetName; // This is the game the user calls it, so use for saving
bool _dynamicRoomOffsets;
byte _resourceMapper[128];
uint32 _allocatedSize;
byte _expire_counter;
byte *_heV7DiskOffsets;
byte *_heV7RoomOffsets;
uint32 *_heV7RoomIntOffsets;
@ -652,11 +672,10 @@ protected:
void allocResTypeData(int id, uint32 tag, int num, const char *name, int mode);
byte *createResource(int type, int index, uint32 size);
int loadResource(int type, int i);
void nukeResource(int type, int i);
// void nukeResource(int type, int i);
int getResourceSize(int type, int idx);
public:
bool isResourceLoaded(int type, int index) const;
byte *getResourceAddress(int type, int i);
byte *getStringAddress(int i);
byte *getStringAddressVar(int i);
@ -669,9 +688,6 @@ protected:
void convertMac0Resource(int type, int index, byte *ptr, int size);
void convertADResource(int type, int index, byte *ptr, int size);
int readSoundResourceSmallHeader(int type, int index);
void setResourceCounter(int type, int index, byte flag);
bool validateResource(const char *str, int type, int index) const;
void increaseResourceCounter();
bool isResourceInUse(int type, int i) const;
void initRoomSubBlocks();
void clearRoomObjects();
@ -695,7 +711,6 @@ public:
protected:
void resourceStats();
void expireResources(uint32 size);
void freeResources();
public:
/* Should be in Object class */

View file

@ -841,7 +841,7 @@ int Sound::isSoundRunning(int sound) const {
if (isSoundInQueue(sound))
return 1;
if (sound > _vm->_numSounds || !_vm->isResourceLoaded(rtSound, sound))
if (sound > _vm->_numSounds || !_vm->res.isResourceLoaded(rtSound, sound))
return 0;
if (_vm->_musicEngine)
@ -872,7 +872,7 @@ bool Sound::isSoundInUse(int sound) const {
if (isSoundInQueue(sound))
return true;
if (!_vm->isResourceLoaded(rtSound, sound))
if (!_vm->res.isResourceLoaded(rtSound, sound))
return false;
if (_vm->_imuse)

View file

@ -720,7 +720,7 @@ void ScummEngine::initCharset(int charsetno) {
int i;
if (_gameId == GID_FT) {
if (!isResourceLoaded(rtCharset, charsetno))
if (!res.isResourceLoaded(rtCharset, charsetno))
loadCharset(charsetno);
} else {
if (!getResourceAddress(rtCharset, charsetno))

View file

@ -613,7 +613,7 @@ void ScummEngine::killVerb(int slot) {
vs->verbid = 0;
vs->curmode = 0;
nukeResource(rtVerb, slot);
res.nukeResource(rtVerb, slot);
if (_version <= 6 && vs->saveid == 0) {
drawVerb(slot, 0);

View file

@ -1424,7 +1424,7 @@ void ScummEngine_v90he::processWizImage(const WizParameters *params) {
f.seek(0, SEEK_SET);
byte *p = createResource(rtImage, params->img.resNum, size);
if (f.read(p, size) != size) {
nukeResource(rtImage, params->img.resNum);
res.nukeResource(rtImage, params->img.resNum);
warning("i/o error when reading '%s'", params->filename);
VAR(VAR_GAME_LOADED) = -2;
VAR(119) = -2;