aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engines/saga/actor.cpp50
-rw-r--r--engines/saga/animation.cpp34
-rw-r--r--engines/saga/animation.h4
-rw-r--r--engines/saga/events.cpp11
-rw-r--r--engines/saga/font.cpp17
-rw-r--r--engines/saga/gfx.cpp20
-rw-r--r--engines/saga/image.cpp20
-rw-r--r--engines/saga/interface.cpp60
-rw-r--r--engines/saga/introproc_ihnm.cpp13
-rw-r--r--engines/saga/isomap.cpp40
-rw-r--r--engines/saga/isomap.h10
-rw-r--r--engines/saga/music.cpp33
-rw-r--r--engines/saga/music.h3
-rw-r--r--engines/saga/objectmap.cpp8
-rw-r--r--engines/saga/objectmap.h2
-rw-r--r--engines/saga/palanim.cpp6
-rw-r--r--engines/saga/palanim.h2
-rw-r--r--engines/saga/resource.cpp20
-rw-r--r--engines/saga/resource.h2
-rw-r--r--engines/saga/resource_res.cpp109
-rw-r--r--engines/saga/saga.cpp10
-rw-r--r--engines/saga/saga.h29
-rw-r--r--engines/saga/scene.cpp101
-rw-r--r--engines/saga/scene.h9
-rw-r--r--engines/saga/script.cpp64
-rw-r--r--engines/saga/script.h4
-rw-r--r--engines/saga/sndres.cpp17
-rw-r--r--engines/saga/sprite.cpp17
28 files changed, 314 insertions, 401 deletions
diff --git a/engines/saga/actor.cpp b/engines/saga/actor.cpp
index c4c05c7ae0..5111f1ae07 100644
--- a/engines/saga/actor.cpp
+++ b/engines/saga/actor.cpp
@@ -214,8 +214,7 @@ static int tileCommonObjectCompare(const CommonObjectDataPointer& obj1, const Co
Actor::Actor(SagaEngine *vm) : _vm(vm) {
int i;
- byte *stringsPointer;
- size_t stringsLength;
+ ByteArray stringsData;
debug(9, "Actor::Actor()");
_handleActionDiv = 15;
@@ -251,10 +250,9 @@ Actor::Actor(SagaEngine *vm) : _vm(vm) {
if (_vm->getGameId() == GID_ITE) {
- _vm->_resource->loadResource(_actorContext, _vm->getResourceDescription()->actorsStringsResourceId, stringsPointer, stringsLength);
+ _vm->_resource->loadResource(_actorContext, _vm->getResourceDescription()->actorsStringsResourceId, stringsData);
- _vm->loadStrings(_actorsStrings, stringsPointer, stringsLength);
- free(stringsPointer);
+ _vm->loadStrings(_actorsStrings, stringsData);
}
if (_vm->getGameId() == GID_ITE) {
@@ -310,16 +308,15 @@ Actor::~Actor() {
}
void Actor::loadFrameList(int frameListResourceId, ActorFrameSequences &frames) {
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
debug(9, "Loading frame resource id %d", frameListResourceId);
- _vm->_resource->loadResource(_actorContext, frameListResourceId, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(_actorContext, frameListResourceId, resourceData);
- frames.resize(resourceLength / 16);
- debug(9, "Frame resource contains %d frames (res length is %d)", frames.size(), (int)resourceLength);
+ frames.resize(resourceData.size() / 16);
+ debug(9, "Frame resource contains %d frames (res length is %d)", frames.size(), (int)resourceData.size());
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _actorContext->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _actorContext->isBigEndian());
for (ActorFrameSequences::iterator frame = frames.begin(); frame != frames.end(); ++frame) {
for (int orient = 0; orient < ACTOR_DIRECTIONS_COUNT; orient++) {
@@ -336,8 +333,6 @@ void Actor::loadFrameList(int frameListResourceId, ActorFrameSequences &frames)
debug(9, "frameIndex %d frameCount %d", frame->directions[orient].frameIndex, frame->directions[orient].frameCount);
}
}
-
- free(resourcePointer);
}
bool Actor::loadActorResources(ActorData *actor) {
@@ -398,8 +393,7 @@ void Actor::loadActorSpriteList(ActorData *actor) {
void Actor::loadActorList(int protagonistIdx, int actorCount, int actorsResourceID, int protagStatesCount, int protagStatesResourceID) {
int i, j;
- byte* actorListData;
- size_t actorListLength;
+ ByteArray actorListData;
byte walk[128];
byte acv[6];
int movementSpeed;
@@ -407,13 +401,13 @@ void Actor::loadActorList(int protagonistIdx, int actorCount, int actorsResource
int walkStepCount;
int stateResourceId;
- _vm->_resource->loadResource(_actorContext, actorsResourceID, actorListData, actorListLength);
+ _vm->_resource->loadResource(_actorContext, actorsResourceID, actorListData);
- if (actorListLength != (uint)actorCount * ACTOR_INHM_SIZE) {
+ if (actorListData.size() != (uint)actorCount * ACTOR_INHM_SIZE) {
error("Actor::loadActorList wrong actorlist length");
}
- MemoryReadStream actorS(actorListData, actorListLength);
+ ByteArrayReadStreamEndian actorS(actorListData);
_actors.resize(actorCount);
i = 0;
@@ -483,7 +477,6 @@ void Actor::loadActorList(int protagonistIdx, int actorCount, int actorsResource
}
// actorS.seek(6, SEEK_CUR); //action vars
}
- free(actorListData);
_actors[protagonistIdx]._flags |= kProtagonist | kExtended;
@@ -500,24 +493,21 @@ void Actor::loadActorList(int protagonistIdx, int actorCount, int actorsResource
if (protagStatesResourceID) {
_protagStates.resize(protagStatesCount);
- byte *idsResourcePointer;
- size_t idsResourceLength;
+ ByteArray idsResourceData;
- _vm->_resource->loadResource(_actorContext, protagStatesResourceID,
- idsResourcePointer, idsResourceLength);
+ _vm->_resource->loadResource(_actorContext, protagStatesResourceID, idsResourceData);
- if (idsResourceLength < (size_t)protagStatesCount * 4) {
+ if (idsResourceData.size() < (size_t)protagStatesCount * 4) {
error("Wrong protagonist states resource");
}
- MemoryReadStream statesIds(idsResourcePointer, idsResourceLength);
+ ByteArrayReadStreamEndian statesIds(idsResourceData);
for (i = 0; i < protagStatesCount; i++) {
stateResourceId = statesIds.readUint32LE();
loadFrameList(stateResourceId, _protagStates[i]._frames);
}
- free(idsResourcePointer);
_protagonist->_frames = &_protagStates[_protagState]._frames;
}
@@ -527,14 +517,13 @@ void Actor::loadActorList(int protagonistIdx, int actorCount, int actorsResource
void Actor::loadObjList(int objectCount, int objectsResourceID) {
uint i;
int frameListResourceId;
- byte* objectListData;
- size_t objectListLength;
+ ByteArray objectListData;
- _vm->_resource->loadResource(_actorContext, objectsResourceID, objectListData, objectListLength);
+ _vm->_resource->loadResource(_actorContext, objectsResourceID, objectListData);
_objs.resize(objectCount);
- MemoryReadStream objectS(objectListData, objectListLength);
+ ByteArrayReadStreamEndian objectS(objectListData);
i = 0;
for (ObjectDataArray::iterator object = _objs.begin(); object != _objs.end(); ++object, i++) {
@@ -564,7 +553,6 @@ void Actor::loadObjList(int objectCount, int objectsResourceID) {
objectS.readUint16LE(); //BOTTOM
object->_interactBits = objectS.readUint16LE();
}
- free(objectListData);
}
void Actor::takeExit(uint16 actorId, const HitZone *hitZone) {
diff --git a/engines/saga/animation.cpp b/engines/saga/animation.cpp
index 56201f62fc..aca29ed82e 100644
--- a/engines/saga/animation.cpp
+++ b/engines/saga/animation.cpp
@@ -57,10 +57,10 @@ Anim::~Anim() {
#ifdef ENABLE_IHNM
-void Anim::loadCutawayList(const byte *resourcePointer, size_t resourceLength) {
- _cutawayList.resize(resourceLength / 8);
+void Anim::loadCutawayList(const ByteArray &resourceData) {
+ _cutawayList.resize(resourceData.size() / 8);
- MemoryReadStream cutawayS(resourcePointer, resourceLength);
+ ByteArrayReadStreamEndian cutawayS(resourceData);
for (uint i = 0; i < _cutawayList.size(); i++) {
_cutawayList[i].backgroundResourceId = cutawayS.readUint16LE();
@@ -80,8 +80,6 @@ int Anim::playCutaway(int cut, bool fade) {
Event event;
EventColumns *eventColumns = NULL;
bool startImmediately = false;
- byte *resourceData;
- size_t resourceDataLength;
ResourceContext *context = _vm->_resource->getContext(GAME_RESOURCEFILE);
_cutAwayFade = fade;
@@ -171,9 +169,10 @@ int Anim::playCutaway(int cut, bool fade) {
// for the second from the left monitor in Ellen's chapter etc
// Therefore, skip the animation bit if animResourceId is 0 and only show the background
if (_cutawayList[cut].animResourceId != 0) {
- _vm->_resource->loadResource(context, _cutawayList[cut].animResourceId, resourceData, resourceDataLength);
- load(MAX_ANIMATIONS + cutawaySlot, resourceData, resourceDataLength);
- free(resourceData);
+ ByteArray resourceData;
+ _vm->_resource->loadResource(context, _cutawayList[cut].animResourceId, resourceData);
+ load(MAX_ANIMATIONS + cutawaySlot, resourceData);
+
setCycles(MAX_ANIMATIONS + cutawaySlot, _cutawayList[cut].cycles);
setFrameTime(MAX_ANIMATIONS + cutawaySlot, 1000 / _cutawayList[cut].frameRate);
@@ -329,18 +328,17 @@ void Anim::clearCutaway() {
void Anim::showCutawayBg(int bg) {
ResourceContext *context = _vm->_resource->getContext(GAME_RESOURCEFILE);
- byte *resourceData;
- size_t resourceDataLength;
+ ByteArray resourceData;
ByteArray image;
int width;
int height;
Event event;
static PalEntry pal[PAL_ENTRIES];
- _vm->_resource->loadResource(context, bg, resourceData, resourceDataLength);
- _vm->decodeBGImage(resourceData, resourceDataLength, image, &width, &height);
+ _vm->_resource->loadResource(context, bg, resourceData);
+ _vm->decodeBGImage(resourceData, image, &width, &height);
- const byte *palPointer = _vm->getImagePal(resourceData, resourceDataLength);
+ const byte *palPointer = _vm->getImagePal(resourceData);
memcpy(pal, palPointer, sizeof(pal));
const Rect rect(width, height);
_vm->_render->getBackGroundSurface()->blit(rect, image.getBuffer());
@@ -359,8 +357,6 @@ void Anim::showCutawayBg(int bg) {
} else {
_vm->_gfx->setPalette(pal);
}
-
- free(resourceData);
}
void Anim::startVideo(int vid, bool fade) {
@@ -386,7 +382,7 @@ void Anim::returnFromVideo() {
#endif
-void Anim::load(uint16 animId, const byte *animResourceData, size_t animResourceLength) {
+void Anim::load(uint16 animId, const ByteArray &resourceData) {
AnimationData *anim;
uint16 temp;
@@ -397,7 +393,7 @@ void Anim::load(uint16 animId, const byte *animResourceData, size_t animResource
} else
anim = _animations[animId] = new AnimationData();
- MemoryReadStreamEndian headerReadS(animResourceData, animResourceLength, _vm->isBigEndian());
+ ByteArrayReadStreamEndian headerReadS(resourceData, _vm->isBigEndian());
anim->magic = headerReadS.readUint16LE(); // cause ALWAYS LE
anim->screenWidth = headerReadS.readUint16();
anim->screenHeight = headerReadS.readUint16();
@@ -420,9 +416,9 @@ void Anim::load(uint16 animId, const byte *animResourceData, size_t animResource
warning("Anim::load animId=%d start != dataOffset 0x%X 0x%X", animId, uint(start), uint(dataOffset));
}
- anim->resourceData.resize(animResourceLength - dataOffset);
+ anim->resourceData.resize(resourceData.size() - dataOffset);
- memcpy(anim->resourceData.getBuffer(), animResourceData + dataOffset, anim->resourceData.size());
+ memcpy(anim->resourceData.getBuffer(), resourceData.getBuffer() + dataOffset, anim->resourceData.size());
// Cache frame offsets
// WORKAROUND: Cutaway with background resource ID 37 (loaded as cutaway #4) is ending credits.
diff --git a/engines/saga/animation.h b/engines/saga/animation.h
index 6a783dceb0..c27909115e 100644
--- a/engines/saga/animation.h
+++ b/engines/saga/animation.h
@@ -97,7 +97,7 @@ public:
Anim(SagaEngine *vm);
~Anim();
- void loadCutawayList(const byte *resourcePointer, size_t resourceLength);
+ void loadCutawayList(const ByteArray &resourceData);
void clearCutawayList();
int playCutaway(int cut, bool fade);
void endCutaway();
@@ -109,7 +109,7 @@ public:
void endVideo();
void returnFromVideo();
- void load(uint16 animId, const byte *animResourceData, size_t animResourceLength);
+ void load(uint16 animId, const ByteArray &resourceData);
void freeId(uint16 animId);
void play(uint16 animId, int vectorTime, bool playing = true);
void link(int16 animId1, int16 animId2);
diff --git a/engines/saga/events.cpp b/engines/saga/events.cpp
index b64defc26f..cf27ad7559 100644
--- a/engines/saga/events.cpp
+++ b/engines/saga/events.cpp
@@ -356,18 +356,17 @@ int Events::handleOneShot(Event *event) {
{
ResourceContext *context = _vm->_resource->getContext(GAME_RESOURCEFILE);
- byte *resourceData;
- size_t resourceDataLength;
+ ByteArray resourceData;
- _vm->_resource->loadResource(context, _vm->getResourceDescription()->psychicProfileResourceId, resourceData, resourceDataLength);
+ _vm->_resource->loadResource(context, _vm->getResourceDescription()->psychicProfileResourceId, resourceData);
ByteArray image;
int width;
int height;
- _vm->decodeBGImage(resourceData, resourceDataLength, image, &width, &height);
+ _vm->decodeBGImage(resourceData, image, &width, &height);
- const PalEntry *palette = (const PalEntry *)_vm->getImagePal(resourceData, resourceDataLength);
+ const PalEntry *palette = (const PalEntry *)_vm->getImagePal(resourceData);
const Rect profileRect(width, height);
@@ -377,8 +376,6 @@ int Events::handleOneShot(Event *event) {
_vm->_gfx->setPalette(palette);
- free(resourceData);
-
// Draw the scene. It won't be drawn by Render::drawScene(), as a placard is up
_vm->_scene->draw();
}
diff --git a/engines/saga/font.cpp b/engines/saga/font.cpp
index 3e82bf88e4..01e74d2984 100644
--- a/engines/saga/font.cpp
+++ b/engines/saga/font.cpp
@@ -55,8 +55,7 @@ Font::~Font() {
void Font::loadFont(FontData *font, uint32 fontResourceId) {
- byte *fontResourcePointer;
- size_t fontResourceLength;
+ ByteArray fontResourceData;
int numBits;
int c;
ResourceContext *fontContext;
@@ -69,13 +68,13 @@ void Font::loadFont(FontData *font, uint32 fontResourceId) {
}
// Load font resource
- _vm->_resource->loadResource(fontContext, fontResourceId, fontResourcePointer, fontResourceLength);
+ _vm->_resource->loadResource(fontContext, fontResourceId, fontResourceData);
- if (fontResourceLength < FONT_DESCSIZE) {
- error("Font::loadFont() Invalid font length (%i < %i)", (int)fontResourceLength, FONT_DESCSIZE);
+ if (fontResourceData.size() < FONT_DESCSIZE) {
+ error("Font::loadFont() Invalid font length (%i < %i)", (int)fontResourceData.size(), FONT_DESCSIZE);
}
- MemoryReadStreamEndian readS(fontResourcePointer, fontResourceLength, fontContext->isBigEndian());
+ ByteArrayReadStreamEndian readS(fontResourceData, fontContext->isBigEndian());
// Read font header
font->normal.header.charHeight = readS.readUint16();
@@ -108,10 +107,8 @@ void Font::loadFont(FontData *font, uint32 fontResourceId) {
error("Invalid font resource size");
}
- font->normal.font.resize(fontResourceLength - FONT_DESCSIZE);
- memcpy(font->normal.font.getBuffer(), fontResourcePointer + FONT_DESCSIZE, fontResourceLength - FONT_DESCSIZE);
-
- free(fontResourcePointer);
+ font->normal.font.resize(fontResourceData.size() - FONT_DESCSIZE);
+ memcpy(font->normal.font.getBuffer(), fontResourceData.getBuffer() + FONT_DESCSIZE, fontResourceData.size() - FONT_DESCSIZE);
// Create outline font style
diff --git a/engines/saga/gfx.cpp b/engines/saga/gfx.cpp
index a06179634b..77842acc7b 100644
--- a/engines/saga/gfx.cpp
+++ b/engines/saga/gfx.cpp
@@ -173,13 +173,11 @@ void Gfx::initPalette() {
error("Resource::loadGlobalResources() resource context not found");
}
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
- _vm->_resource->loadResource(resourceContext, RID_IHNM_DEFAULT_PALETTE,
- resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, RID_IHNM_DEFAULT_PALETTE, resourceData);
- MemoryReadStream metaS(resourcePointer, resourceLength);
+ ByteArrayReadStreamEndian metaS(resourceData);
for (int i = 0; i < 256; i++) {
_globalPalette[i].red = metaS.readByte();
@@ -187,8 +185,6 @@ void Gfx::initPalette() {
_globalPalette[i].blue = metaS.readByte();
}
- free(resourcePointer);
-
setPalette(_globalPalette, true);
}
@@ -504,19 +500,17 @@ void Gfx::setCursor(CursorType cursorType) {
break;
}
- byte *resource;
- size_t resourceLength;
+ ByteArray resourceData;
ByteArray image;
int width, height;
if (resourceId != (uint32)-1) {
ResourceContext *context = _vm->_resource->getContext(GAME_RESOURCEFILE);
- _vm->_resource->loadResource(context, resourceId, resource, resourceLength);
+ _vm->_resource->loadResource(context, resourceId, resourceData);
- _vm->decodeBGImage(resource, resourceLength, image, &width, &height);
+ _vm->decodeBGImage(resourceData, image, &width, &height);
} else {
- resource = NULL;
width = height = 31;
image.resize(width * height);
@@ -530,8 +524,6 @@ void Gfx::setCursor(CursorType cursorType) {
// Note: Hard-coded hotspot
CursorMan.replaceCursor(image.getBuffer(), width, height, 15, 15, 0);
-
- free(resource);
}
}
diff --git a/engines/saga/image.cpp b/engines/saga/image.cpp
index d83262ea0d..87d9e514c8 100644
--- a/engines/saga/image.cpp
+++ b/engines/saga/image.cpp
@@ -47,18 +47,18 @@ static int granulate(int value, int granularity) {
}
}
-bool SagaEngine::decodeBGImage(const byte *image_data, size_t image_size, ByteArray &outputBuffer, int *w, int *h, bool flip) {
+bool SagaEngine::decodeBGImage(const ByteArray &imageData, ByteArray &outputBuffer, int *w, int *h, bool flip) {
ImageHeader hdr;
int modex_height;
const byte *RLE_data_ptr;
size_t RLE_data_len;
ByteArray decodeBuffer;
- if (image_size <= SAGA_IMAGE_DATA_OFFSET) {
- error("decodeBGImage() Image size is way too small (%d)", (int)image_size);
+ if (imageData.size() <= SAGA_IMAGE_DATA_OFFSET) {
+ error("decodeBGImage() Image size is way too small (%d)", (int)imageData.size());
}
- MemoryReadStreamEndian readS(image_data, image_size, isBigEndian());
+ ByteArrayReadStreamEndian readS(imageData, isBigEndian());
hdr.width = readS.readUint16();
hdr.height = readS.readUint16();
@@ -66,8 +66,8 @@ bool SagaEngine::decodeBGImage(const byte *image_data, size_t image_size, ByteAr
readS.readUint16();
readS.readUint16();
- RLE_data_ptr = image_data + SAGA_IMAGE_DATA_OFFSET;
- RLE_data_len = image_size - SAGA_IMAGE_DATA_OFFSET;
+ RLE_data_ptr = &imageData.front() + SAGA_IMAGE_DATA_OFFSET;
+ RLE_data_len = imageData.size() - SAGA_IMAGE_DATA_OFFSET;
modex_height = granulate(hdr.height, 4);
@@ -411,12 +411,4 @@ void SagaEngine::unbankBGImage(byte *dst_buf, const byte *src_buf, int columns,
}
}
-const byte *SagaEngine::getImagePal(const byte *image_data, size_t image_size) {
- if (image_size <= SAGA_IMAGE_HEADER_LEN) {
- return NULL;
- }
-
- return image_data + SAGA_IMAGE_HEADER_LEN;
-}
-
} // End of namespace Saga
diff --git a/engines/saga/interface.cpp b/engines/saga/interface.cpp
index 5153c1890e..5b15fc9803 100644
--- a/engines/saga/interface.cpp
+++ b/engines/saga/interface.cpp
@@ -124,8 +124,7 @@ static const int IHNMTextStringIdsLUT[56] = {
#define buttonRes1 0x42544E01
Interface::Interface(SagaEngine *vm) : _vm(vm) {
- byte *resource;
- size_t resourceLength;
+ ByteArray resourceData;
int i;
#if 0
@@ -170,27 +169,23 @@ Interface::Interface(SagaEngine *vm) : _vm(vm) {
}
}
- _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->mainPanelResourceId, resource, resourceLength);
- _vm->decodeBGImage(resource, resourceLength, _mainPanel.image, &_mainPanel.imageWidth, &_mainPanel.imageHeight);
-
- free(resource);
+ _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->mainPanelResourceId, resourceData);
+ _vm->decodeBGImage(resourceData, _mainPanel.image, &_mainPanel.imageWidth, &_mainPanel.imageHeight);
// Converse panel
_conversePanel.buttons = _vm->getDisplayInfo().conversePanelButtons;
_conversePanel.buttonsCount = _vm->getDisplayInfo().conversePanelButtonsCount;
- _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->conversePanelResourceId, resource, resourceLength);
- _vm->decodeBGImage(resource, resourceLength, _conversePanel.image, &_conversePanel.imageWidth, &_conversePanel.imageHeight);
- free(resource);
+ _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->conversePanelResourceId, resourceData);
+ _vm->decodeBGImage(resourceData, _conversePanel.image, &_conversePanel.imageWidth, &_conversePanel.imageHeight);
// Option panel
if (!_vm->_script->isNonInteractiveDemo()) {
_optionPanel.buttons = _vm->getDisplayInfo().optionPanelButtons;
_optionPanel.buttonsCount = _vm->getDisplayInfo().optionPanelButtonsCount;
- _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->optionPanelResourceId, resource, resourceLength);
- _vm->decodeBGImage(resource, resourceLength, _optionPanel.image, &_optionPanel.imageWidth, &_optionPanel.imageHeight);
- free(resource);
+ _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->optionPanelResourceId, resourceData);
+ _vm->decodeBGImage(resourceData, _optionPanel.image, &_optionPanel.imageWidth, &_optionPanel.imageHeight);
} else {
_optionPanel.buttons = NULL;
_optionPanel.buttonsCount = 0;
@@ -203,9 +198,8 @@ Interface::Interface(SagaEngine *vm) : _vm(vm) {
_quitPanel.buttons = _vm->getDisplayInfo().quitPanelButtons;
_quitPanel.buttonsCount = _vm->getDisplayInfo().quitPanelButtonsCount;
- _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->warningPanelResourceId, resource, resourceLength);
- _vm->decodeBGImage(resource, resourceLength, _quitPanel.image, &_quitPanel.imageWidth, &_quitPanel.imageHeight);
- free(resource);
+ _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->warningPanelResourceId, resourceData);
+ _vm->decodeBGImage(resourceData, _quitPanel.image, &_quitPanel.imageWidth, &_quitPanel.imageHeight);
}
// Save panel
@@ -213,9 +207,8 @@ Interface::Interface(SagaEngine *vm) : _vm(vm) {
_savePanel.buttons = _vm->getDisplayInfo().savePanelButtons;
_savePanel.buttonsCount = _vm->getDisplayInfo().savePanelButtonsCount;
- _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->warningPanelResourceId, resource, resourceLength);
- _vm->decodeBGImage(resource, resourceLength, _savePanel.image, &_savePanel.imageWidth, &_savePanel.imageHeight);
- free(resource);
+ _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->warningPanelResourceId, resourceData);
+ _vm->decodeBGImage(resourceData, _savePanel.image, &_savePanel.imageWidth, &_savePanel.imageHeight);
}
// Load panel
@@ -223,9 +216,8 @@ Interface::Interface(SagaEngine *vm) : _vm(vm) {
_loadPanel.buttons = _vm->getDisplayInfo().loadPanelButtons;
_loadPanel.buttonsCount = _vm->getDisplayInfo().loadPanelButtonsCount;
- _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->warningPanelResourceId, resource, resourceLength);
- _vm->decodeBGImage(resource, resourceLength, _loadPanel.image, &_loadPanel.imageWidth, &_loadPanel.imageHeight);
- free(resource);
+ _vm->_resource->loadResource(_interfaceContext, _vm->getResourceDescription()->warningPanelResourceId, resourceData);
+ _vm->decodeBGImage(resourceData, _loadPanel.image, &_loadPanel.imageWidth, &_loadPanel.imageHeight);
}
#endif
@@ -2695,8 +2687,7 @@ void Interface::loadState(Common::InSaveFile *in) {
void Interface::mapPanelShow() {
int i;
- byte *resource;
- size_t resourceLength;
+ ByteArray resourceData;
Rect rect;
ByteArray image;
int imageWidth, imageHeight;
@@ -2707,9 +2698,8 @@ void Interface::mapPanelShow() {
rect.left = rect.top = 0;
- _vm->_resource->loadResource(_interfaceContext,
- _vm->_resource->convertResourceId(RID_ITE_TYCHO_MAP), resource, resourceLength);
- if (resourceLength == 0) {
+ _vm->_resource->loadResource(_interfaceContext, _vm->_resource->convertResourceId(RID_ITE_TYCHO_MAP), resourceData);
+ if (resourceData.empty()) {
error("Interface::mapPanelShow() unable to load Tycho map resource");
}
@@ -2723,8 +2713,8 @@ void Interface::mapPanelShow() {
_vm->_render->setFlag(RF_MAP);
- _vm->decodeBGImage(resource, resourceLength, image, &imageWidth, &imageHeight);
- pal = _vm->getImagePal(resource, resourceLength);
+ _vm->decodeBGImage(resourceData, image, &imageWidth, &imageHeight);
+ pal = _vm->getImagePal(resourceData);
for (i = 0; i < PAL_ENTRIES; i++) {
cPal[i].red = *pal++;
@@ -2744,7 +2734,6 @@ void Interface::mapPanelShow() {
_vm->_system->delayMillis(5);
}
- free(resource);
setSaveReminderState(false);
@@ -2801,8 +2790,7 @@ void Interface::keyBoss() {
_vm->_music->pause();
int i;
- byte *resource;
- size_t resourceLength;
+ ByteArray resourceData;
Rect rect;
ByteArray image;
int imageWidth, imageHeight;
@@ -2813,20 +2801,20 @@ void Interface::keyBoss() {
rect.left = rect.top = 0;
- _vm->_resource->loadResource(_interfaceContext, RID_IHNM_BOSS_SCREEN, resource, resourceLength);
- if (resourceLength == 0) {
+ _vm->_resource->loadResource(_interfaceContext, RID_IHNM_BOSS_SCREEN, resourceData);
+ if (resourceData.empty()) {
error("Interface::bossKey() unable to load Boss image resource");
}
_bossMode = _panelMode;
setMode(kPanelBoss);
- _vm->decodeBGImage(resource, resourceLength, image, &imageWidth, &imageHeight);
+ _vm->decodeBGImage(resourceData, image, &imageWidth, &imageHeight);
rect.setWidth(imageWidth);
rect.setHeight(imageHeight);
_vm->_gfx->getCurrentPal(_mapSavedPal);
- pal = _vm->getImagePal(resource, resourceLength);
+ pal = _vm->getImagePal(resourceData);
cPal[0].red = 0;
cPal[0].green = 0;
@@ -2841,8 +2829,6 @@ void Interface::keyBoss() {
_vm->_gfx->drawRegion(rect, image.getBuffer());
_vm->_gfx->setPalette(cPal);
-
- free(resource);
}
diff --git a/engines/saga/introproc_ihnm.cpp b/engines/saga/introproc_ihnm.cpp
index ac75bcf19d..cf3dd5cb4c 100644
--- a/engines/saga/introproc_ihnm.cpp
+++ b/engines/saga/introproc_ihnm.cpp
@@ -121,8 +121,7 @@ int Scene::IHNMCreditsProc() {
void Scene::IHNMLoadCutaways() {
ResourceContext *resourceContext;
//ResourceContext *soundContext;
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
resourceContext = _vm->_resource->getContext(GAME_RESOURCEFILE);
if (resourceContext == NULL) {
@@ -130,18 +129,16 @@ void Scene::IHNMLoadCutaways() {
}
if (!_vm->isIHNMDemo())
- _vm->_resource->loadResource(resourceContext, RID_IHNM_INTRO_CUTAWAYS, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, RID_IHNM_INTRO_CUTAWAYS, resourceData);
else
- _vm->_resource->loadResource(resourceContext, RID_IHNMDEMO_INTRO_CUTAWAYS, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, RID_IHNMDEMO_INTRO_CUTAWAYS, resourceData);
- if (resourceLength == 0) {
+ if (resourceData.empty() == 0) {
error("Scene::IHNMStartProc() Can't load cutaway list");
}
// Load the cutaways for the title screens
- _vm->_anim->loadCutawayList(resourcePointer, resourceLength);
-
- free(resourcePointer);
+ _vm->_anim->loadCutawayList(resourceData);
}
bool Scene::checkKey() {
diff --git a/engines/saga/isomap.cpp b/engines/saga/isomap.cpp
index baa1189939..6450af268a 100644
--- a/engines/saga/isomap.cpp
+++ b/engines/saga/isomap.cpp
@@ -102,17 +102,17 @@ IsoMap::IsoMap(SagaEngine *vm) : _vm(vm) {
_viewDiff = 1;
}
-void IsoMap::loadImages(const byte *resourcePointer, size_t resourceLength) {
+void IsoMap::loadImages(const ByteArray &resourceData) {
IsoTileData *tileData;
uint16 i;
size_t offsetDiff;
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("IsoMap::loadImages wrong resourceLength");
}
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());
readS.readUint16(); // skip
i = readS.readUint16();
i = i / SAGA_ISOTILEDATA_LEN;
@@ -134,25 +134,25 @@ void IsoMap::loadImages(const byte *resourcePointer, size_t resourceLength) {
offsetDiff = readS.pos();
- _tileData.resize(resourceLength - offsetDiff);
- memcpy(_tileData.getBuffer(), resourcePointer + offsetDiff, _tileData.size());
+ _tileData.resize(resourceData.size() - offsetDiff);
+ memcpy(_tileData.getBuffer(), resourceData.getBuffer() + offsetDiff, _tileData.size());
for (i = 0; i < _tilesTable.size(); i++) {
_tilesTable[i].tilePointer = _tileData.getBuffer() + tempOffsets[i] - offsetDiff;
}
}
-void IsoMap::loadPlatforms(const byte * resourcePointer, size_t resourceLength) {
+void IsoMap::loadPlatforms(const ByteArray &resourceData) {
TilePlatformData *tilePlatformData;
uint16 i, x, y;
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("IsoMap::loadPlatforms wrong resourceLength");
}
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());
- i = resourceLength / SAGA_TILEPLATFORMDATA_LEN;
+ i = resourceData.size() / SAGA_TILEPLATFORMDATA_LEN;
_tilePlatformList.resize(i);
for (i = 0; i < _tilePlatformList.size(); i++) {
@@ -171,14 +171,14 @@ void IsoMap::loadPlatforms(const byte * resourcePointer, size_t resourceLength)
}
-void IsoMap::loadMap(const byte * resourcePointer, size_t resourceLength) {
+void IsoMap::loadMap(const ByteArray &resourceData) {
uint16 x, y;
- if (resourceLength != SAGA_TILEMAP_LEN) {
- error("IsoMap::loadMap wrong resourceLength");
+ if (resourceData.size() != SAGA_TILEMAP_LEN) {
+ error("IsoMap::loadMap wrong resource length %d", resourceData.size());
}
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());
_tileMap.edgeType = readS.readByte();
readS.readByte(); //skip
@@ -190,16 +190,16 @@ void IsoMap::loadMap(const byte * resourcePointer, size_t resourceLength) {
}
-void IsoMap::loadMetaTiles(const byte * resourcePointer, size_t resourceLength) {
+void IsoMap::loadMetaTiles(const ByteArray &resourceData) {
MetaTileData *metaTileData;
uint16 i, j;
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("IsoMap::loadMetaTiles wrong resourceLength");
}
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
- i = resourceLength / SAGA_METATILEDATA_LEN;
+ ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());
+ i = resourceData.size() / SAGA_METATILEDATA_LEN;
_metaTileList.resize(i);
for (i = 0; i < _metaTileList.size(); i++) {
@@ -212,16 +212,16 @@ void IsoMap::loadMetaTiles(const byte * resourcePointer, size_t resourceLength)
}
}
-void IsoMap::loadMulti(const byte * resourcePointer, size_t resourceLength) {
+void IsoMap::loadMulti(const ByteArray &resourceData) {
MultiTileEntryData *multiTileEntryData;
uint16 i;
int16 offsetDiff;
- if (resourceLength < 2) {
+ if (resourceData.size() < 2) {
error("IsoMap::loadMetaTiles wrong resourceLength");
}
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());
i = readS.readUint16();
_multiTable.resize(i);
diff --git a/engines/saga/isomap.h b/engines/saga/isomap.h
index e2502d4a77..7dc7dff8cd 100644
--- a/engines/saga/isomap.h
+++ b/engines/saga/isomap.h
@@ -155,11 +155,11 @@ public:
IsoMap(SagaEngine *vm);
~IsoMap() {
}
- void loadImages(const byte * resourcePointer, size_t resourceLength);
- void loadMap(const byte * resourcePointer, size_t resourceLength);
- void loadPlatforms(const byte * resourcePointer, size_t resourceLength);
- void loadMetaTiles(const byte * resourcePointer, size_t resourceLength);
- void loadMulti(const byte * resourcePointer, size_t resourceLength);
+ void loadImages(const ByteArray &resourceData);
+ void loadMap(const ByteArray &resourceData);
+ void loadPlatforms(const ByteArray &resourceData);
+ void loadMetaTiles(const ByteArray &resourceData);
+ void loadMulti(const ByteArray &resourceData);
void clear();
void draw();
void drawSprite(SpriteList &spriteList, int spriteNumber, const Location &location, const Point &screenPosition, int scale);
diff --git a/engines/saga/music.cpp b/engines/saga/music.cpp
index 3fa38d30fb..f1cdcbce46 100644
--- a/engines/saga/music.cpp
+++ b/engines/saga/music.cpp
@@ -117,6 +117,7 @@ void MusicDriver::send(uint32 b) {
Music::Music(SagaEngine *vm, Audio::Mixer *mixer) : _vm(vm), _mixer(mixer) {
_currentVolume = 0;
+ _currentMusicBuffer = NULL;
_driver = new MusicDriver();
_digitalMusicContext = _vm->_resource->getContext(GAME_DIGITALMUSICFILE);
@@ -162,11 +163,13 @@ Music::Music(SagaEngine *vm, Audio::Mixer *mixer) : _vm(vm), _mixer(mixer) {
// Just set an XMIDI parser for Mac IHNM for now
_parser = MidiParser::createParser_XMIDI();
} else {
- byte *resourceData;
- size_t resourceSize;
+ ByteArray resourceData;
int resourceId = (_vm->getGameId() == GID_ITE ? 9 : 0);
- _vm->_resource->loadResource(_musicContext, resourceId, resourceData, resourceSize);
- if (!memcmp(resourceData, "FORM", 4)) {
+ _vm->_resource->loadResource(_musicContext, resourceId, resourceData);
+ if (resourceData.size() < 4) {
+ error("Music::Music Unable to load midi resource data");
+ }
+ if (!memcmp(resourceData.getBuffer(), "FORM", 4)) {
_parser = MidiParser::createParser_XMIDI();
// ITE had MT32 mapped instruments
_driver->setGM(_vm->getGameId() != GID_ITE);
@@ -175,14 +178,12 @@ Music::Music(SagaEngine *vm, Audio::Mixer *mixer) : _vm(vm), _mixer(mixer) {
// ITE with standalone MIDI files is General MIDI
_driver->setGM(_vm->getGameId() == GID_ITE);
}
- free(resourceData);
}
-
+
_parser->setMidiDriver(_driver);
_parser->setTimerRate(_driver->getBaseTempo());
_parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
- _midiMusicData = NULL;
_digitalMusic = false;
}
@@ -193,8 +194,6 @@ Music::~Music() {
delete _driver;
_parser->setMidiDriver(NULL);
delete _parser;
-
- free(_midiMusicData);
}
void Music::musicVolumeGaugeCallback(void *refCon) {
@@ -254,8 +253,6 @@ bool Music::isPlaying() {
void Music::play(uint32 resourceId, MusicFlags flags) {
Audio::SeekableAudioStream *audioStream = NULL;
- byte *resourceData;
- size_t resourceSize;
uint32 loopStart = 0;
debug(2, "Music::play %d, %d", resourceId, flags);
@@ -388,14 +385,19 @@ void Music::play(uint32 resourceId, MusicFlags flags) {
#endif
return;
} else {
- _vm->_resource->loadResource(_musicContext, resourceId, resourceData, resourceSize);
+ if (_currentMusicBuffer == &_musicBuffer[1]) {
+ _currentMusicBuffer = &_musicBuffer[0];
+ } else {
+ _currentMusicBuffer = &_musicBuffer[1];
+ }
+ _vm->_resource->loadResource(_musicContext, resourceId, *_currentMusicBuffer);
}
- if (resourceSize < 4) {
+ if (_currentMusicBuffer->size() < 4) {
error("Music::play() wrong music resource size");
}
- if (!_parser->loadMusic(resourceData, resourceSize))
+ if (!_parser->loadMusic(_currentMusicBuffer->getBuffer(), _currentMusicBuffer->size()))
error("Music::play() wrong music resource");
_parser->setTrack(0);
@@ -405,9 +407,6 @@ void Music::play(uint32 resourceId, MusicFlags flags) {
// Handle music looping
_parser->property(MidiParser::mpAutoLoop, (flags & MUSIC_LOOP) ? 1 : 0);
-
- free(_midiMusicData);
- _midiMusicData = resourceData;
}
void Music::pause() {
diff --git a/engines/saga/music.h b/engines/saga/music.h
index 3219cebc03..b4d868ed78 100644
--- a/engines/saga/music.h
+++ b/engines/saga/music.h
@@ -125,11 +125,12 @@ private:
ResourceContext *_digitalMusicContext;
MidiParser *_parser;
- byte *_midiMusicData;
static void musicVolumeGaugeCallback(void *refCon);
static void onTimer(void *refCon);
void musicVolumeGauge();
+ ByteArray *_currentMusicBuffer;
+ ByteArray _musicBuffer[2];
};
} // End of namespace Saga
diff --git a/engines/saga/objectmap.cpp b/engines/saga/objectmap.cpp
index a74a620ce7..4d7b78f7f6 100644
--- a/engines/saga/objectmap.cpp
+++ b/engines/saga/objectmap.cpp
@@ -163,22 +163,22 @@ void HitZone::draw(SagaEngine *vm, int color) {
#endif
// Loads an object map resource ( objects ( clickareas ( points ) ) )
-void ObjectMap::load(const byte *resourcePointer, size_t resourceLength) {
+void ObjectMap::load(const ByteArray &resourceData) {
uint i;
if (!_hitZoneList.empty()) {
error("ObjectMap::load _hitZoneList not empty");
}
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
return;
}
- if (resourceLength < 4) {
+ if (resourceData.size() < 4) {
error("ObjectMap::load wrong resourceLength");
}
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());
_hitZoneList.resize(readS.readUint16());
diff --git a/engines/saga/objectmap.h b/engines/saga/objectmap.h
index 727c5f1faa..264e197196 100644
--- a/engines/saga/objectmap.h
+++ b/engines/saga/objectmap.h
@@ -92,7 +92,7 @@ class ObjectMap {
public:
ObjectMap(SagaEngine *vm) : _vm(vm) {
}
- void load(const byte *resourcePointer, size_t resourceLength);
+ void load(const ByteArray &resourceData);
void clear();
#ifdef SAGA_DEBUG
void draw(const Point& testPoint, int color, int color2); // for debugging
diff --git a/engines/saga/palanim.cpp b/engines/saga/palanim.cpp
index bb27af75ff..b0b76fc947 100644
--- a/engines/saga/palanim.cpp
+++ b/engines/saga/palanim.cpp
@@ -37,15 +37,15 @@ namespace Saga {
PalAnim::PalAnim(SagaEngine *vm) : _vm(vm) {
}
-void PalAnim::loadPalAnim(const byte *resdata, size_t resdata_len) {
+void PalAnim::loadPalAnim(const ByteArray &resourceData) {
clear();
- if (resdata == NULL) {
+ if (resourceData.empty()) {
return;
}
- MemoryReadStreamEndian readS(resdata, resdata_len, _vm->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _vm->isBigEndian());
if (_vm->getGameId() == GID_IHNM) {
return;
diff --git a/engines/saga/palanim.h b/engines/saga/palanim.h
index 3767f0367d..2d2c3f1399 100644
--- a/engines/saga/palanim.h
+++ b/engines/saga/palanim.h
@@ -42,7 +42,7 @@ class PalAnim {
public:
PalAnim(SagaEngine *vm);
- void loadPalAnim(const byte *, size_t);
+ void loadPalAnim(const ByteArray &resourceData);
void cycleStart();
void cycleStep(int vectortime);
void clear();
diff --git a/engines/saga/resource.cpp b/engines/saga/resource.cpp
index 7b61676ffe..0fb7327f0d 100644
--- a/engines/saga/resource.cpp
+++ b/engines/saga/resource.cpp
@@ -103,8 +103,6 @@ bool ResourceContext::load(SagaEngine *vm, Resource *resource) {
uint32 subjectResourceId;
uint32 patchResourceId;
ResourceData *subjectResourceData;
- byte *tableBuffer;
- size_t tableSize;
bool isMacBinary;
if (_fileName == NULL) { // IHNM special case
@@ -141,10 +139,12 @@ bool ResourceContext::load(SagaEngine *vm, Resource *resource) {
if (subjectContext == NULL) {
error("ResourceContext::load() Subject context not found");
}
- resource->loadResource(this, _table.size() - 1, tableBuffer, tableSize);
+ ByteArray tableBuffer;
- MemoryReadStreamEndian readS2(tableBuffer, tableSize, _isBigEndian);
- for (i = 0; i < tableSize / 8; i++) {
+ resource->loadResource(this, _table.size() - 1, tableBuffer);
+
+ ByteArrayReadStreamEndian readS2(tableBuffer, _isBigEndian);
+ for (i = 0; i < tableBuffer.size() / 8; i++) {
subjectResourceId = readS2.readUint32();
patchResourceId = readS2.readUint32();
subjectResourceData = subjectContext->getResourceData(subjectResourceId);
@@ -153,7 +153,6 @@ bool ResourceContext::load(SagaEngine *vm, Resource *resource) {
subjectResourceData->offset = resourceData->offset;
subjectResourceData->size = resourceData->size;
}
- free(tableBuffer);
}
//process external patch files
@@ -366,7 +365,7 @@ void Resource::clearContexts() {
}
}
-void Resource::loadResource(ResourceContext *context, uint32 resourceId, byte*&resourceBuffer, size_t &resourceSize) {
+void Resource::loadResource(ResourceContext *context, uint32 resourceId, ByteArray &resourceBuffer) {
Common::File *file;
uint32 resourceOffset;
ResourceData *resourceData;
@@ -377,15 +376,14 @@ void Resource::loadResource(ResourceContext *context, uint32 resourceId, byte*&r
file = context->getFile(resourceData);
resourceOffset = resourceData->offset;
- resourceSize = resourceData->size;
- debug(8, "loadResource %d 0x%X:0x%X", resourceId, resourceOffset, uint(resourceSize));
+ debug(8, "loadResource %d 0x%X:0x%X", resourceId, resourceOffset, uint(resourceData->size));
+ resourceBuffer.resize(resourceData->size);
- resourceBuffer = (byte*)malloc(resourceSize);
file->seek((long)resourceOffset, SEEK_SET);
- if (file->read(resourceBuffer, resourceSize) != resourceSize) {
+ if (file->read(resourceBuffer.getBuffer(), resourceBuffer.size()) != resourceBuffer.size()) {
error("Resource::loadResource() failed to read");
}
diff --git a/engines/saga/resource.h b/engines/saga/resource.h
index e32d16c469..5009c862f4 100644
--- a/engines/saga/resource.h
+++ b/engines/saga/resource.h
@@ -206,7 +206,7 @@ public:
virtual ~Resource();
bool createContexts();
void clearContexts();
- void loadResource(ResourceContext *context, uint32 resourceId, byte*&resourceBuffer, size_t &resourceSize);
+ void loadResource(ResourceContext *context, uint32 resourceId, ByteArray &resourceBuffer);
virtual uint32 convertResourceId(uint32 resourceId) = 0;
virtual void loadGlobalResources(int chapter, int actorsEntrance) = 0;
diff --git a/engines/saga/resource_res.cpp b/engines/saga/resource_res.cpp
index 343f3b2ae2..646de8667b 100644
--- a/engines/saga/resource_res.cpp
+++ b/engines/saga/resource_res.cpp
@@ -66,41 +66,38 @@ void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) {
error("Resource::loadGlobalResources() sound context not found");
}
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
if (!_vm->isIHNMDemo()) {
- _vm->_resource->loadResource(resourceContext, metaResourceTable[chapter],
- resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, metaResourceTable[chapter], resourceData);
} else {
- _vm->_resource->loadResource(resourceContext, metaResourceTableDemo[chapter],
- resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, metaResourceTableDemo[chapter], resourceData);
}
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("Resource::loadGlobalResources wrong metaResource");
}
- MemoryReadStream metaS(resourcePointer, resourceLength);
-
- _metaResource.sceneIndex = metaS.readSint16LE();
- _metaResource.objectCount = metaS.readSint16LE();
- _metaResource.objectsStringsResourceID = metaS.readSint32LE();
- _metaResource.inventorySpritesID = metaS.readSint32LE();
- _metaResource.mainSpritesID = metaS.readSint32LE();
- _metaResource.objectsResourceID = metaS.readSint32LE();
- _metaResource.actorCount = metaS.readSint16LE();
- _metaResource.actorsStringsResourceID = metaS.readSint32LE();
- _metaResource.actorsResourceID = metaS.readSint32LE();
- _metaResource.protagFaceSpritesID = metaS.readSint32LE();
- _metaResource.field_22 = metaS.readSint32LE();
- _metaResource.field_26 = metaS.readSint16LE();
- _metaResource.protagStatesCount = metaS.readSint16LE();
- _metaResource.protagStatesResourceID = metaS.readSint32LE();
- _metaResource.cutawayListResourceID = metaS.readSint32LE();
- _metaResource.songTableID = metaS.readSint32LE();
-
- free(resourcePointer);
+ {
+ ByteArrayReadStreamEndian metaS(resourceData);
+
+ _metaResource.sceneIndex = metaS.readSint16LE();
+ _metaResource.objectCount = metaS.readSint16LE();
+ _metaResource.objectsStringsResourceID = metaS.readSint32LE();
+ _metaResource.inventorySpritesID = metaS.readSint32LE();
+ _metaResource.mainSpritesID = metaS.readSint32LE();
+ _metaResource.objectsResourceID = metaS.readSint32LE();
+ _metaResource.actorCount = metaS.readSint16LE();
+ _metaResource.actorsStringsResourceID = metaS.readSint32LE();
+ _metaResource.actorsResourceID = metaS.readSint32LE();
+ _metaResource.protagFaceSpritesID = metaS.readSint32LE();
+ _metaResource.field_22 = metaS.readSint32LE();
+ _metaResource.field_26 = metaS.readSint16LE();
+ _metaResource.protagStatesCount = metaS.readSint16LE();
+ _metaResource.protagStatesResourceID = metaS.readSint32LE();
+ _metaResource.cutawayListResourceID = metaS.readSint32LE();
+ _metaResource.songTableID = metaS.readSint32LE();
+ }
_vm->_actor->loadActorList(actorsEntrance, _metaResource.actorCount,
_metaResource.actorsResourceID, _metaResource.protagStatesCount,
@@ -110,9 +107,8 @@ void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) {
_vm->_actor->_objectsStrings.clear();
- _vm->_resource->loadResource(resourceContext, _metaResource.objectsStringsResourceID, resourcePointer, resourceLength);
- _vm->loadStrings(_vm->_actor->_objectsStrings, resourcePointer, resourceLength);
- free(resourcePointer);
+ _vm->_resource->loadResource(resourceContext, _metaResource.objectsStringsResourceID, resourceData);
+ _vm->loadStrings(_vm->_actor->_objectsStrings, resourceData);
if (uint(chapter) >= _vm->_sndRes->_fxTableIDs.size()) {
error("Chapter ID exceeds fxTableIDs length");
@@ -120,30 +116,30 @@ void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) {
debug(0, "Going to read %d of %d", chapter, _vm->_sndRes->_fxTableIDs[chapter]);
_vm->_resource->loadResource(soundContext, _vm->_sndRes->_fxTableIDs[chapter],
- resourcePointer, resourceLength);
+ resourceData);
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("Resource::loadGlobalResources Can't load sound effects for current track");
}
- _vm->_sndRes->_fxTable.resize(resourceLength / 4);
-
- MemoryReadStream fxS(resourcePointer, resourceLength);
+ _vm->_sndRes->_fxTable.resize(resourceData.size() / 4);
+
+ {
+ ByteArrayReadStreamEndian fxS(resourceData);
- for (i = 0; i < _vm->_sndRes->_fxTable.size(); i++) {
- _vm->_sndRes->_fxTable[i].res = fxS.readSint16LE();
- _vm->_sndRes->_fxTable[i].vol = fxS.readSint16LE();
+ for (i = 0; i < _vm->_sndRes->_fxTable.size(); i++) {
+ _vm->_sndRes->_fxTable[i].res = fxS.readSint16LE();
+ _vm->_sndRes->_fxTable[i].vol = fxS.readSint16LE();
+ }
}
- free(resourcePointer);
_vm->_interface->_defPortraits.clear();
_vm->_sprite->loadList(_metaResource.protagFaceSpritesID, _vm->_interface->_defPortraits);
_vm->_actor->_actorsStrings.clear();
- _vm->_resource->loadResource(resourceContext, _metaResource.actorsStringsResourceID, resourcePointer, resourceLength);
- _vm->loadStrings(_vm->_actor->_actorsStrings, resourcePointer, resourceLength);
- free(resourcePointer);
+ _vm->_resource->loadResource(resourceContext, _metaResource.actorsStringsResourceID, resourceData);
+ _vm->loadStrings(_vm->_actor->_actorsStrings, resourceData);
_vm->_sprite->_inventorySprites.clear();
_vm->_sprite->loadList(_metaResource.inventorySpritesID, _vm->_sprite->_inventorySprites);
@@ -153,39 +149,39 @@ void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) {
_vm->_actor->loadObjList(_metaResource.objectCount, _metaResource.objectsResourceID);
- _vm->_resource->loadResource(resourceContext, _metaResource.cutawayListResourceID, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, _metaResource.cutawayListResourceID, resourceData);
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("Resource::loadGlobalResources Can't load cutaway list");
}
- _vm->_anim->loadCutawayList(resourcePointer, resourceLength);
+ _vm->_anim->loadCutawayList(resourceData);
if (_metaResource.songTableID > 0) {
- _vm->_resource->loadResource(resourceContext, _metaResource.songTableID, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, _metaResource.songTableID, resourceData);
if (chapter == 6) {
- int32 id = READ_LE_UINT32(&resourcePointer[actorsEntrance * 4]);
- free(resourcePointer);
- _vm->_resource->loadResource(resourceContext, id, resourcePointer, resourceLength);
+ if (resourceData.size() < (uint(actorsEntrance) * 4 + 4)) {
+ error("Resource::loadGlobalResources chapter 6 has wrong resource");
+ }
+ int32 id = READ_LE_UINT32(&resourceData[actorsEntrance * 4]);
+ _vm->_resource->loadResource(resourceContext, id, resourceData);
}
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("Resource::loadGlobalResources Can't load songs list for current track");
}
- _vm->_music->_songTable.resize(resourceLength / 4);
+ _vm->_music->_songTable.resize(resourceData.size() / 4);
- MemoryReadStream songS(resourcePointer, resourceLength);
+ ByteArrayReadStreamEndian songS(resourceData);
for (i = 0; i < _vm->_music->_songTable.size(); i++)
_vm->_music->_songTable[i] = songS.readSint32LE();
- free(resourcePointer);
} else {
// The IHNM demo has a fixed music track and doesn't load a song table
_vm->_music->setVolume(_vm->_musicVolume, 1);
_vm->_music->play(3, MUSIC_LOOP);
- free(resourcePointer);
}
int voiceLUTResourceID = 0;
@@ -201,9 +197,8 @@ void Resource_RES::loadGlobalResources(int chapter, int actorsEntrance) {
}
if (voiceLUTResourceID) {
- _vm->_resource->loadResource(resourceContext, voiceLUTResourceID, resourcePointer, resourceLength);
- _vm->_script->loadVoiceLUT(_vm->_script->_globalVoiceLUT, resourcePointer, resourceLength);
- free(resourcePointer);
+ _vm->_resource->loadResource(resourceContext, voiceLUTResourceID, resourceData);
+ _vm->_script->loadVoiceLUT(_vm->_script->_globalVoiceLUT, resourceData);
}
_vm->_spiritualBarometer = 0;
diff --git a/engines/saga/saga.cpp b/engines/saga/saga.cpp
index 20c2ca2b1b..abd681ce87 100644
--- a/engines/saga/saga.cpp
+++ b/engines/saga/saga.cpp
@@ -393,19 +393,19 @@ Common::Error SagaEngine::run() {
return Common::kNoError;
}
-void SagaEngine::loadStrings(StringsTable &stringsTable, const byte *stringsPointer, size_t stringsLength) {
+void SagaEngine::loadStrings(StringsTable &stringsTable, const ByteArray &stringsData) {
uint16 stringsCount;
size_t offset;
size_t prevOffset = 0;
Common::Array<size_t> tempOffsets;
uint ui;
- if (stringsLength == 0) {
+ if (stringsData.empty()) {
error("SagaEngine::loadStrings() Error loading strings list resource");
}
- MemoryReadStreamEndian scriptS(stringsPointer, stringsLength, isBigEndian()); //TODO: get endianess from context
+ ByteArrayReadStreamEndian scriptS(stringsData, isBigEndian()); //TODO: get endianess from context
offset = scriptS.readUint16();
stringsCount = offset / 2;
@@ -421,12 +421,12 @@ void SagaEngine::loadStrings(StringsTable &stringsTable, const byte *stringsPoin
if (prevOffset > offset)
offset += 65536;
prevOffset = offset;
- if (offset == stringsLength) {
+ if (offset == stringsData.size()) {
stringsCount = ui;
tempOffsets.resize(stringsCount);
break;
}
- if (offset > stringsLength) {
+ if (offset > stringsData.size()) {
// This case should never occur, but apparently it does in the Italian fan
// translation of IHNM
warning("SagaEngine::loadStrings wrong strings table");
diff --git a/engines/saga/saga.h b/engines/saga/saga.h
index 74ebe1b23f..1db003d4a3 100644
--- a/engines/saga/saga.h
+++ b/engines/saga/saga.h
@@ -464,8 +464,21 @@ public:
* Return a pointer to the start of the buffer underlying this byte array,
* or NULL if the buffer is empty.
*/
- byte *getBuffer() {
- return empty() ? NULL : &front();
+ byte *getBuffer() const {
+ return empty() ? NULL : (byte *)&front();
+ }
+
+ void assign(const ByteArray &src) {
+ resize(src.size());
+ if (!empty()) {
+ memcpy(&front(), &src.front(), size());
+ }
+ }
+};
+
+class ByteArrayReadStreamEndian : public MemoryReadStreamEndian {
+public:
+ ByteArrayReadStreamEndian(const ByteArray & byteArray, bool bigEndian = false) : MemoryReadStreamEndian(byteArray.getBuffer(), byteArray.size(), bigEndian) {
}
};
@@ -553,9 +566,15 @@ private:
uint32 _previousTicks;
public:
- bool decodeBGImage(const byte *image_data, size_t image_size, ByteArray &outputBuffer, int *w, int *h, bool flip = false);
- const byte *getImagePal(const byte *image_data, size_t image_size);
- void loadStrings(StringsTable &stringsTable, const byte *stringsPointer, size_t stringsLength);
+ bool decodeBGImage(const ByteArray &imageData, ByteArray &outputBuffer, int *w, int *h, bool flip = false);
+ const byte *getImagePal(const ByteArray &imageData) {
+ if (imageData.size() <= SAGA_IMAGE_HEADER_LEN) {
+ return NULL;
+ }
+
+ return &imageData.front() + SAGA_IMAGE_HEADER_LEN;
+ }
+ void loadStrings(StringsTable &stringsTable, const ByteArray &stringsData);
const char *getObjectName(uint16 objectId) const;
public:
diff --git a/engines/saga/scene.cpp b/engines/saga/scene.cpp
index 2c419962dd..6631918b4c 100644
--- a/engines/saga/scene.cpp
+++ b/engines/saga/scene.cpp
@@ -137,8 +137,7 @@ const char *SAGAResourceTypesString[] = {
};
Scene::Scene(SagaEngine *vm) : _vm(vm) {
- byte *sceneLUTPointer;
- size_t sceneLUTLength;
+ ByteArray sceneLUTData;
uint32 resourceId;
uint i;
@@ -158,21 +157,19 @@ Scene::Scene(SagaEngine *vm) : _vm(vm) {
// Load scene lookup table
resourceId = _vm->_resource->convertResourceId(_vm->getResourceDescription()->sceneLUTResourceId);
debug(3, "Loading scene LUT from resource %i", resourceId);
- _vm->_resource->loadResource(_sceneContext, resourceId, sceneLUTPointer, sceneLUTLength);
- if (sceneLUTLength == 0) {
- error("Scene::Scene() sceneLUTLength == 0");
+ _vm->_resource->loadResource(_sceneContext, resourceId, sceneLUTData);
+ if (sceneLUTData.empty()) {
+ error("Scene::Scene() sceneLUT is empty");
}
- _sceneLUT.resize(sceneLUTLength / 2);
+ _sceneLUT.resize(sceneLUTData.size() / 2);
- MemoryReadStreamEndian readS(sceneLUTPointer, sceneLUTLength, _sceneContext->isBigEndian());
+ ByteArrayReadStreamEndian readS(sceneLUTData, _sceneContext->isBigEndian());
for (i = 0; i < _sceneLUT.size(); i++) {
_sceneLUT[i] = readS.readUint16();
debug(8, "sceneNumber %i has resourceId %i", i, _sceneLUT[i]);
}
- free(sceneLUTPointer);
-
#ifdef SAGA_DEBUG
#define DUMP_SCENES_LEVEL 10
@@ -686,11 +683,11 @@ void Scene::loadScene(LoadSceneParams &loadSceneParams) {
// Load resources from scene resource list
for (SceneResourceDataArray::iterator resource = _resourceList.begin(); resource != _resourceList.end(); ++resource) {
if (!resource->invalid) {
- _vm->_resource->loadResource(_sceneContext, resource->resourceId, resource->buffer, resource->size);
+ _vm->_resource->loadResource(_sceneContext, resource->resourceId, resource->buffer);
- if (resource->size >= 6) {
- if (!memcmp(resource->buffer, "DUMMY!", 6)) {
+ if (resource->buffer.size() >= 6) {
+ if (!memcmp(resource->buffer.getBuffer(), "DUMMY!", 6)) {
resource->invalid = true;
warning("DUMMY resource %i", resource->resourceId);
}
@@ -887,8 +884,7 @@ void Scene::loadScene(LoadSceneParams &loadSceneParams) {
}
void Scene::loadSceneDescriptor(uint32 resourceId) {
- byte *sceneDescriptorData;
- size_t sceneDescriptorDataLength;
+ ByteArray sceneDescriptorData;
_sceneDescription.reset();
@@ -896,10 +892,10 @@ void Scene::loadSceneDescriptor(uint32 resourceId) {
return;
}
- _vm->_resource->loadResource(_sceneContext, resourceId, sceneDescriptorData, sceneDescriptorDataLength);
+ _vm->_resource->loadResource(_sceneContext, resourceId, sceneDescriptorData);
- if (sceneDescriptorDataLength == 16) {
- MemoryReadStreamEndian readS(sceneDescriptorData, sceneDescriptorDataLength, _sceneContext->isBigEndian());
+ if (sceneDescriptorData.size() == 16) {
+ ByteArrayReadStreamEndian readS(sceneDescriptorData, _sceneContext->isBigEndian());
_sceneDescription.flags = readS.readSint16();
_sceneDescription.resourceListResourceId = readS.readSint16();
@@ -910,13 +906,10 @@ void Scene::loadSceneDescriptor(uint32 resourceId) {
_sceneDescription.startScriptEntrypointNumber = readS.readUint16();
_sceneDescription.musicResourceId = readS.readSint16();
}
-
- free(sceneDescriptorData);
}
void Scene::loadSceneResourceList(uint32 resourceId) {
- byte *resourceListData;
- size_t resourceListDataLength;
+ ByteArray resourceListData;
_resourceList.clear();
@@ -925,13 +918,13 @@ void Scene::loadSceneResourceList(uint32 resourceId) {
}
// Load the scene resource table
- _vm->_resource->loadResource(_sceneContext, resourceId, resourceListData, resourceListDataLength);
+ _vm->_resource->loadResource(_sceneContext, resourceId, resourceListData);
- if ((resourceListDataLength % SAGA_RESLIST_ENTRY_LEN) == 0) {
- MemoryReadStreamEndian readS(resourceListData, resourceListDataLength, _sceneContext->isBigEndian());
+ if ((resourceListData.size() % SAGA_RESLIST_ENTRY_LEN) == 0) {
+ ByteArrayReadStreamEndian readS(resourceListData, _sceneContext->isBigEndian());
// Allocate memory for scene resource list
- _resourceList.resize(resourceListDataLength / SAGA_RESLIST_ENTRY_LEN);
+ _resourceList.resize(resourceListData.size() / SAGA_RESLIST_ENTRY_LEN);
debug(3, "Scene resource list contains %i entries", (int)_resourceList.size());
// Load scene resource list from raw scene
@@ -946,12 +939,9 @@ void Scene::loadSceneResourceList(uint32 resourceId) {
}
}
- free(resourceListData);
}
void Scene::processSceneResources() {
- byte *resourceData;
- size_t resourceDataLength;
const byte *palPointer;
SAGAResourceTypes *types = 0;
int typesCount = 0;
@@ -964,8 +954,7 @@ void Scene::processSceneResources() {
if (resource->invalid) {
continue;
}
- resourceData = resource->buffer;
- resourceDataLength = resource->size;
+ ByteArray &resourceData = resource->buffer;
if (resource->resourceType >= typesCount) {
error("Scene::processSceneResources() wrong resource type %i", resource->resourceType);
@@ -993,19 +982,16 @@ void Scene::processSceneResources() {
}
debug(3, "Loading background resource.");
- _bg.res_buf = resourceData;
- _bg.res_len = resourceDataLength;
- _bg.loaded = true;
- if (!_vm->decodeBGImage(_bg.res_buf,
- _bg.res_len,
+ if (!_vm->decodeBGImage(resourceData,
_bg.buffer,
&_bg.w,
&_bg.h)) {
error("Scene::processSceneResources() Error loading background resource %i", resource->resourceId);
}
+ _bg.loaded = true;
- palPointer = _vm->getImagePal(_bg.res_buf, _bg.res_len);
+ palPointer = _vm->getImagePal(resourceData);
memcpy(_bg.pal, palPointer, sizeof(_bg.pal));
break;
case SAGA_BG_MASK: // Scene background mask resource
@@ -1013,10 +999,8 @@ void Scene::processSceneResources() {
error("Scene::ProcessSceneResources(): Duplicate background mask resource encountered");
}
debug(3, "Loading BACKGROUND MASK resource.");
- _bgMask.res_buf = resourceData;
- _bgMask.res_len = resourceDataLength;
+ _vm->decodeBGImage(resourceData, _bgMask.buffer, &_bgMask.w, &_bgMask.h, true);
_bgMask.loaded = true;
- _vm->decodeBGImage(_bgMask.res_buf, _bgMask.res_len, _bgMask.buffer, &_bgMask.w, &_bgMask.h, true);
// At least in ITE the mask needs to be clipped.
@@ -1027,15 +1011,15 @@ void Scene::processSceneResources() {
break;
case SAGA_STRINGS:
debug(3, "Loading scene strings resource...");
- _vm->loadStrings(_sceneStrings, resourceData, resourceDataLength);
+ _vm->loadStrings(_sceneStrings, resourceData);
break;
case SAGA_OBJECT_MAP:
debug(3, "Loading object map resource...");
- _objectMap->load(resourceData, resourceDataLength);
+ _objectMap->load(resourceData);
break;
case SAGA_ACTION_MAP:
debug(3, "Loading action map resource...");
- _actionMap->load(resourceData, resourceDataLength);
+ _actionMap->load(resourceData);
break;
case SAGA_ISO_IMAGES:
if (!(_sceneDescription.flags & kSceneFlagISO)) {
@@ -1044,7 +1028,7 @@ void Scene::processSceneResources() {
debug(3, "Loading isometric images resource.");
- _vm->_isoMap->loadImages(resourceData, resourceDataLength);
+ _vm->_isoMap->loadImages(resourceData);
break;
case SAGA_ISO_MAP:
if (!(_sceneDescription.flags & kSceneFlagISO)) {
@@ -1053,7 +1037,7 @@ void Scene::processSceneResources() {
debug(3, "Loading isometric map resource.");
- _vm->_isoMap->loadMap(resourceData, resourceDataLength);
+ _vm->_isoMap->loadMap(resourceData);
break;
case SAGA_ISO_PLATFORMS:
if (!(_sceneDescription.flags & kSceneFlagISO)) {
@@ -1062,7 +1046,7 @@ void Scene::processSceneResources() {
debug(3, "Loading isometric platforms resource.");
- _vm->_isoMap->loadPlatforms(resourceData, resourceDataLength);
+ _vm->_isoMap->loadPlatforms(resourceData);
break;
case SAGA_ISO_METATILES:
if (!(_sceneDescription.flags & kSceneFlagISO)) {
@@ -1071,7 +1055,7 @@ void Scene::processSceneResources() {
debug(3, "Loading isometric metatiles resource.");
- _vm->_isoMap->loadMetaTiles(resourceData, resourceDataLength);
+ _vm->_isoMap->loadMetaTiles(resourceData);
break;
case SAGA_ANIM:
{
@@ -1079,12 +1063,12 @@ void Scene::processSceneResources() {
debug(3, "Loading animation resource animId=%i", animId);
- _vm->_anim->load(animId, resourceData, resourceDataLength);
+ _vm->_anim->load(animId, resourceData);
}
break;
case SAGA_ENTRY:
debug(3, "Loading entry list resource...");
- loadSceneEntryList(resourceData, resourceDataLength);
+ loadSceneEntryList(resourceData);
break;
case SAGA_ISO_MULTI:
if (!(_sceneDescription.flags & kSceneFlagISO)) {
@@ -1093,11 +1077,11 @@ void Scene::processSceneResources() {
debug(3, "Loading isometric multi resource.");
- _vm->_isoMap->loadMulti(resourceData, resourceDataLength);
+ _vm->_isoMap->loadMulti(resourceData);
break;
case SAGA_PAL_ANIM:
debug(3, "Loading palette animation resource.");
- _vm->_palanim->loadPalAnim(resourceData, resourceDataLength);
+ _vm->_palanim->loadPalAnim(resourceData);
break;
case SAGA_FACES:
if (_vm->getGameId() == GID_ITE)
@@ -1106,10 +1090,10 @@ void Scene::processSceneResources() {
case SAGA_PALETTE:
{
PalEntry pal[PAL_ENTRIES];
- byte *palPtr = resourceData;
+ byte *palPtr = resourceData.getBuffer();
- if (resourceDataLength < 3 * PAL_ENTRIES)
- error("Too small scene palette %i", (int)resourceDataLength);
+ if (resourceData.size() < 3 * PAL_ENTRIES)
+ error("Too small scene palette %i", (int)resourceData.size());
for (uint16 c = 0; c < PAL_ENTRIES; c++) {
pal[c].red = *palPtr++;
@@ -1148,7 +1132,6 @@ void Scene::draw() {
void Scene::endScene() {
Rect rect;
- size_t i;
if (!_sceneLoaded)
return;
@@ -1197,10 +1180,6 @@ void Scene::endScene() {
}
// Free scene resource list
- for (i = 0; i < _resourceList.size(); i++) { //!!!!!!!!!!!!!!!!!!!
- free(_resourceList[i].buffer);
- }
-
_resourceList.clear();
// Free animation info list
@@ -1265,16 +1244,16 @@ void Scene::cmdObjectMapInfo() {
_objectMap->cmdInfo();
}
-void Scene::loadSceneEntryList(const byte* resourcePointer, size_t resourceLength) {
+void Scene::loadSceneEntryList(const ByteArray &resourceData) {
uint i;
if (!_entryList.empty()) {
error("Scene::loadSceneEntryList entryList not empty");
}
- _entryList.resize(resourceLength / 8);
+ _entryList.resize(resourceData.size() / 8);
- MemoryReadStreamEndian readS(resourcePointer, resourceLength, _sceneContext->isBigEndian());
+ ByteArrayReadStreamEndian readS(resourceData, _sceneContext->isBigEndian());
for (i = 0; i < _entryList.size(); i++) {
_entryList[i].location.x = readS.readSint16();
diff --git a/engines/saga/scene.h b/engines/saga/scene.h
index 8e55a15118..514e662a45 100644
--- a/engines/saga/scene.h
+++ b/engines/saga/scene.h
@@ -110,11 +110,10 @@ enum SAGAResourceTypes {
struct SceneResourceData {
uint32 resourceId;
int resourceType;
- byte *buffer;
- size_t size;
+ ByteArray buffer;
bool invalid;
- SceneResourceData() : resourceId(0), resourceType(0), buffer(NULL), size(0), invalid(false) {
+ SceneResourceData() : resourceId(0), resourceType(0), invalid(false) {
}
};
@@ -151,8 +150,6 @@ struct SceneImage {
int h;
int p;
ByteArray buffer;
- byte *res_buf;
- size_t res_len;
PalEntry pal[256];
SceneImage() : loaded(false), w(0), h(0), p(0) {
@@ -364,7 +361,7 @@ class Scene {
void loadScene(LoadSceneParams &loadSceneParams);
void loadSceneDescriptor(uint32 resourceId);
void loadSceneResourceList(uint32 resourceId);
- void loadSceneEntryList(const byte* resourcePointer, size_t resourceLength);
+ void loadSceneEntryList(const ByteArray &resourceData);
void processSceneResources();
void getResourceTypes(SAGAResourceTypes *&types, int &typesCount);
diff --git a/engines/saga/script.cpp b/engines/saga/script.cpp
index d7ddacabbe..3ae2f19e1b 100644
--- a/engines/saga/script.cpp
+++ b/engines/saga/script.cpp
@@ -46,13 +46,11 @@ namespace Saga {
SAGA1Script::SAGA1Script(SagaEngine *vm) : Script(vm) {
ResourceContext *resourceContext;
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
int prevTell;
uint ui;
int j;
- byte *stringsPointer;
- size_t stringsLength;
+ ByteArray stringsData;
//initialize member variables
_abortEnabled = true;
@@ -85,19 +83,19 @@ SAGA1Script::SAGA1Script(SagaEngine *vm) : Script(vm) {
uint32 scriptResourceId = 0;
scriptResourceId = _vm->getResourceDescription()->moduleLUTResourceId;
debug(3, "Loading module LUT from resource %i", scriptResourceId);
- _vm->_resource->loadResource(resourceContext, scriptResourceId, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, scriptResourceId, resourceData);
// Create logical script LUT from resource
- if (resourceLength % 22 == 0) { // ITE CD
+ if (resourceData.size() % 22 == 0) { // ITE CD
_modulesLUTEntryLen = 22;
- } else if (resourceLength % 16 == 0) { // ITE disk, IHNM
+ } else if (resourceData.size() % 16 == 0) { // ITE disk, IHNM
_modulesLUTEntryLen = 16;
} else {
- error("Script::Script() Invalid script lookup table length (%i)", (int)resourceLength);
+ error("Script::Script() Invalid script lookup table length (%i)", (int)resourceData.size());
}
// Calculate number of entries
- int modulesCount = resourceLength / _modulesLUTEntryLen;
+ int modulesCount = resourceData.size() / _modulesLUTEntryLen;
debug(3, "LUT has %i entries", modulesCount);
@@ -105,7 +103,7 @@ SAGA1Script::SAGA1Script(SagaEngine *vm) : Script(vm) {
_modules.resize(modulesCount);
// Convert LUT resource to logical LUT
- MemoryReadStreamEndian scriptS(resourcePointer, resourceLength, resourceContext->isBigEndian());
+ ByteArrayReadStreamEndian scriptS(resourceData, resourceContext->isBigEndian());
for (ui = 0; ui < _modules.size(); ui++) {
prevTell = scriptS.pos();
@@ -120,8 +118,6 @@ SAGA1Script::SAGA1Script(SagaEngine *vm) : Script(vm) {
}
}
- free(resourcePointer);
-
// TODO
//
// In ITE, the "main strings" resource contains both the verb strings
@@ -130,10 +126,9 @@ SAGA1Script::SAGA1Script(SagaEngine *vm) : Script(vm) {
// In IHNM, the "main strings" contains the verb strings, but not the
// object names. At least, I think that's the case.
- _vm->_resource->loadResource(resourceContext, _vm->getResourceDescription()->mainStringsResourceId, stringsPointer, stringsLength);
+ _vm->_resource->loadResource(resourceContext, _vm->getResourceDescription()->mainStringsResourceId, stringsData);
- _vm->loadStrings(_mainStrings, stringsPointer, stringsLength);
- free(stringsPointer);
+ _vm->loadStrings(_mainStrings, stringsData);
setupScriptOpcodeList();
@@ -155,8 +150,7 @@ SAGA1Script::~SAGA1Script() {
}
SAGA2Script::SAGA2Script(SagaEngine *vm) : Script(vm) {
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
debug(8, "Initializing scripting subsystem");
// Load script resource file context
@@ -171,12 +165,12 @@ SAGA2Script::SAGA2Script(SagaEngine *vm) : Script(vm) {
if (entryNum < 0)
error("Unable to locate the script's export segment");
debug(3, "Loading module LUT from resource %i", entryNum);
- _vm->_resource->loadResource(_scriptContext, (uint32)entryNum, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(_scriptContext, (uint32)entryNum, resourceData);
_modulesLUTEntryLen = sizeof(uint32);
// Calculate number of entries
- int modulesCount = resourceLength / _modulesLUTEntryLen + 1;
+ int modulesCount = resourceData.size() / _modulesLUTEntryLen + 1;
debug(3, "LUT has %i entries", modulesCount);
@@ -1055,8 +1049,7 @@ void Script::opJmpSeedRandom(SCRIPTOP_PARAMS) {
}
void Script::loadModule(uint scriptModuleNumber) {
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
// Validate script number
if (scriptModuleNumber >= _modules.size()) {
@@ -1070,21 +1063,18 @@ void Script::loadModule(uint scriptModuleNumber) {
// Initialize script data structure
debug(3, "Loading script module #%d", scriptModuleNumber);
- _vm->_resource->loadResource(_scriptContext, _modules[scriptModuleNumber].scriptResourceId, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(_scriptContext, _modules[scriptModuleNumber].scriptResourceId, resourceData);
- loadModuleBase(_modules[scriptModuleNumber], resourcePointer, resourceLength);
- free(resourcePointer);
+ loadModuleBase(_modules[scriptModuleNumber], resourceData);
- _vm->_resource->loadResource(_scriptContext, _modules[scriptModuleNumber].stringsResourceId, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(_scriptContext, _modules[scriptModuleNumber].stringsResourceId, resourceData);
- _vm->loadStrings(_modules[scriptModuleNumber].strings, resourcePointer, resourceLength);
- free(resourcePointer);
+ _vm->loadStrings(_modules[scriptModuleNumber].strings, resourceData);
if (_modules[scriptModuleNumber].voicesResourceId > 0) {
- _vm->_resource->loadResource(_scriptContext, _modules[scriptModuleNumber].voicesResourceId, resourcePointer, resourceLength);
+ _vm->_resource->loadResource(_scriptContext, _modules[scriptModuleNumber].voicesResourceId, resourceData);
- loadVoiceLUT(_modules[scriptModuleNumber].voiceLUT, resourcePointer, resourceLength);
- free(resourcePointer);
+ loadVoiceLUT(_modules[scriptModuleNumber].voiceLUT, resourceData);
}
_modules[scriptModuleNumber].staticOffset = _staticSize;
@@ -1105,16 +1095,14 @@ void Script::clearModules() {
_staticSize = 0;
}
-void Script::loadModuleBase(ModuleData &module, const byte *resourcePointer, size_t resourceLength) {
+void Script::loadModuleBase(ModuleData &module, const ByteArray &resourceData) {
uint i;
debug(3, "Loading module base...");
- module.moduleBase.resize(resourceLength);
-
- memcpy(module.moduleBase.getBuffer(), resourcePointer, resourceLength);
+ module.moduleBase.assign(resourceData);
- MemoryReadStreamEndian scriptS(module.moduleBase.getBuffer(), module.moduleBase.size(), _scriptContext->isBigEndian());
+ ByteArrayReadStreamEndian scriptS(module.moduleBase, _scriptContext->isBigEndian());
uint entryPointsCount = scriptS.readUint16();
scriptS.readUint16(); //skip
@@ -1152,12 +1140,12 @@ void Script::loadModuleBase(ModuleData &module, const byte *resourcePointer, siz
}
}
-void Script::loadVoiceLUT(VoiceLUT &voiceLUT, const byte *resourcePointer, size_t resourceLength) {
+void Script::loadVoiceLUT(VoiceLUT &voiceLUT, const ByteArray &resourceData) {
uint16 i;
- voiceLUT.resize(resourceLength / 2);
+ voiceLUT.resize(resourceData.size() / 2);
- MemoryReadStreamEndian scriptS(resourcePointer, resourceLength, _scriptContext->isBigEndian());
+ ByteArrayReadStreamEndian scriptS(resourceData, _scriptContext->isBigEndian());
for (i = 0; i < voiceLUT.size(); i++) {
voiceLUT[i] = scriptS.readUint16();
diff --git a/engines/saga/script.h b/engines/saga/script.h
index 3423ddd05c..e5054d5f4e 100644
--- a/engines/saga/script.h
+++ b/engines/saga/script.h
@@ -392,10 +392,10 @@ public:
void wakeUpThreads(int waitType);
void wakeUpThreadsDelayed(int waitType, int sleepTime);
- void loadVoiceLUT(VoiceLUT &voiceLUT, const byte *resourcePointer, size_t resourceLength);
+ void loadVoiceLUT(VoiceLUT &voiceLUT, const ByteArray &resourceData);
protected:
- void loadModuleBase(ModuleData &module, const byte *resourcePointer, size_t resourceLength);
+ void loadModuleBase(ModuleData &module, const ByteArray &resourceData);
// runThread returns true if we should break running of other threads
bool runThread(ScriptThread &thread);
diff --git a/engines/saga/sndres.cpp b/engines/saga/sndres.cpp
index 48072452df..74fde3e497 100644
--- a/engines/saga/sndres.cpp
+++ b/engines/saga/sndres.cpp
@@ -74,29 +74,24 @@ SndRes::SndRes(SagaEngine *vm) : _vm(vm), _sfxContext(NULL), _voiceContext(NULL)
error("Resource::loadGlobalResources() resource context not found");
}
- byte *resourcePointer;
- size_t resourceLength;
+ ByteArray resourceData;
if (_vm->isIHNMDemo()) {
- _vm->_resource->loadResource(resourceContext, RID_IHNMDEMO_SFX_LUT,
- resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, RID_IHNMDEMO_SFX_LUT, resourceData);
} else {
- _vm->_resource->loadResource(resourceContext, RID_IHNM_SFX_LUT,
- resourcePointer, resourceLength);
+ _vm->_resource->loadResource(resourceContext, RID_IHNM_SFX_LUT, resourceData);
}
- if (resourceLength == 0) {
+ if (resourceData.empty()) {
error("Sndres::SndRes can't read SfxIDs table");
}
- _fxTableIDs.resize(resourceLength / 2);
+ _fxTableIDs.resize(resourceData.size() / 2);
- MemoryReadStream metaS(resourcePointer, resourceLength);
+ ByteArrayReadStreamEndian metaS(resourceData);
for (uint i = 0; i < _fxTableIDs.size(); i++) {
_fxTableIDs[i] = metaS.readSint16LE();
}
-
- free(resourcePointer);
#endif
#ifdef ENABLE_SAGA2
} else if (_vm->getGameId() == GID_DINO) {
diff --git a/engines/saga/sprite.cpp b/engines/saga/sprite.cpp
index 4e0d9c6f75..eb62fb20ff 100644
--- a/engines/saga/sprite.cpp
+++ b/engines/saga/sprite.cpp
@@ -75,8 +75,7 @@ Sprite::~Sprite() {
void Sprite::loadList(int resourceId, SpriteList &spriteList) {
SpriteInfo *spriteInfo;
- byte *spriteListData = 0;
- size_t spriteListLength = 0;
+ ByteArray spriteListData;
uint16 oldSpriteCount;
uint16 newSpriteCount;
uint16 spriteCount;
@@ -86,13 +85,13 @@ void Sprite::loadList(int resourceId, SpriteList &spriteList) {
const byte *spritePointer;
const byte *spriteDataPointer;
- _vm->_resource->loadResource(_spriteContext, resourceId, spriteListData, spriteListLength);
+ _vm->_resource->loadResource(_spriteContext, resourceId, spriteListData);
- if (spriteListLength == 0) {
+ if (spriteListData.empty()) {
return;
}
- MemoryReadStreamEndian readS(spriteListData, spriteListLength, _spriteContext->isBigEndian());
+ ByteArrayReadStreamEndian readS(spriteListData, _spriteContext->isBigEndian());
spriteCount = readS.readUint16();
@@ -112,14 +111,14 @@ void Sprite::loadList(int resourceId, SpriteList &spriteList) {
else
offset = readS.readUint16();
- if (offset >= spriteListLength) {
+ if (offset >= spriteListData.size()) {
// ITE Mac demos throw this warning
warning("Sprite::loadList offset exceeded");
spriteList.resize(i);
return;
}
- spritePointer = spriteListData;
+ spritePointer = spriteListData.getBuffer();
spritePointer += offset;
if (bigHeader) {
@@ -144,7 +143,7 @@ void Sprite::loadList(int resourceId, SpriteList &spriteList) {
}
outputLength = spriteInfo->width * spriteInfo->height;
- inputLength = spriteListLength - (spriteDataPointer - spriteListData);
+ inputLength = spriteListData.size() - (spriteDataPointer - spriteListData.getBuffer());
spriteInfo->decodedBuffer.resize(outputLength);
if (outputLength > 0) {
decodeRLEBuffer(spriteDataPointer, inputLength, outputLength);
@@ -167,8 +166,6 @@ void Sprite::loadList(int resourceId, SpriteList &spriteList) {
memcpy(dst, &_decodeBuf.front(), outputLength);
}
}
-
- free(spriteListData);
}
void Sprite::getScaledSpriteBuffer(SpriteList &spriteList, uint spriteNumber, int scale, int &width, int &height, int &xAlign, int &yAlign, const byte *&buffer) {