[Scummvm-cvs-logs] SF.net SVN: scummvm:[53719] scummvm/trunk/engines/saga

h00ligan at users.sourceforge.net h00ligan at users.sourceforge.net
Sat Oct 23 01:13:18 CEST 2010


Revision: 53719
          http://scummvm.svn.sourceforge.net/scummvm/?rev=53719&view=rev
Author:   h00ligan
Date:     2010-10-22 23:13:17 +0000 (Fri, 22 Oct 2010)

Log Message:
-----------
SAGA: replace Actor::_pathCell, Anim::*, Converse::text, IsoMap::*, Music::_songTable, ObjectMap::*, PalAnim::*, Scene::sceneLut, SndRes::_fxTable*  malloc based arrays with Common::Array implementation
add ByteArray type
fix debug Tile Hittest frame drawing
debug 0x%x => 0x%X

Modified Paths:
--------------
    scummvm/trunk/engines/saga/actor.cpp
    scummvm/trunk/engines/saga/actor.h
    scummvm/trunk/engines/saga/actor_walk.cpp
    scummvm/trunk/engines/saga/animation.cpp
    scummvm/trunk/engines/saga/animation.h
    scummvm/trunk/engines/saga/detection.cpp
    scummvm/trunk/engines/saga/font.cpp
    scummvm/trunk/engines/saga/font.h
    scummvm/trunk/engines/saga/gfx.cpp
    scummvm/trunk/engines/saga/gfx.h
    scummvm/trunk/engines/saga/image.cpp
    scummvm/trunk/engines/saga/interface.cpp
    scummvm/trunk/engines/saga/interface.h
    scummvm/trunk/engines/saga/introproc_ihnm.cpp
    scummvm/trunk/engines/saga/isomap.cpp
    scummvm/trunk/engines/saga/isomap.h
    scummvm/trunk/engines/saga/itedata.cpp
    scummvm/trunk/engines/saga/itedata.h
    scummvm/trunk/engines/saga/music.cpp
    scummvm/trunk/engines/saga/music.h
    scummvm/trunk/engines/saga/objectmap.cpp
    scummvm/trunk/engines/saga/objectmap.h
    scummvm/trunk/engines/saga/palanim.cpp
    scummvm/trunk/engines/saga/palanim.h
    scummvm/trunk/engines/saga/resource.cpp
    scummvm/trunk/engines/saga/resource_res.cpp
    scummvm/trunk/engines/saga/saga.cpp
    scummvm/trunk/engines/saga/saga.h
    scummvm/trunk/engines/saga/saveload.cpp
    scummvm/trunk/engines/saga/scene.cpp
    scummvm/trunk/engines/saga/scene.h
    scummvm/trunk/engines/saga/script.cpp
    scummvm/trunk/engines/saga/script.h
    scummvm/trunk/engines/saga/sfuncs.cpp
    scummvm/trunk/engines/saga/sfuncs_ihnm.cpp
    scummvm/trunk/engines/saga/sndres.cpp
    scummvm/trunk/engines/saga/sndres.h
    scummvm/trunk/engines/saga/sprite.cpp
    scummvm/trunk/engines/saga/sprite.h
    scummvm/trunk/engines/saga/sthread.cpp

Modified: scummvm/trunk/engines/saga/actor.cpp
===================================================================
--- scummvm/trunk/engines/saga/actor.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/actor.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -218,7 +218,7 @@
 	_yCellCount = _vm->_scene->getHeight();
 	_xCellCount = _vm->getDisplayInfo().width;
 
-	_pathCell = (int8 *)malloc(_yCellCount * _xCellCount * sizeof(*_pathCell));
+	_pathCell.resize(_yCellCount * _xCellCount);
 
 	_pathRect.left = 0;
 	_pathRect.right = _vm->getDisplayInfo().width;
@@ -295,7 +295,6 @@
 Actor::~Actor() {
 	debug(9, "Actor::~Actor()");
 
-	free(_pathCell);
 	//release resources
 	freeProtagStates();
 	freeActorList();
@@ -445,7 +444,7 @@
 		actor = _actors[i] = new ActorData();
 		actor->_id = objectIndexToId(kGameObjectActor, i); //actorIndexToId(i);
 		actor->_index = i;
-		debug(4, "init actor id=0x%x index=%d", actor->_id, actor->_index);
+		debug(4, "init actor id=0x%X index=%d", actor->_id, actor->_index);
 		actorS.readUint32LE(); //next displayed
 		actorS.readByte(); //type
 		actor->_flags = actorS.readByte();
@@ -1162,7 +1161,7 @@
 		return;
 	}
 
-	if (_vm->_scene->_entryList.entryListCount == 0) {
+	if (_vm->_scene->_entryList.empty()) {
 		return;
 	}
 

Modified: scummvm/trunk/engines/saga/actor.h
===================================================================
--- scummvm/trunk/engines/saga/actor.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/actor.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -196,6 +196,8 @@
 	ActorFrameRange directions[ACTOR_DIRECTIONS_COUNT];
 };
 
+//typedef Common::Array<ActorFrameSequence> ActorFrameSequences;
+
 uint pathLine(PointList &pointList, uint idx, const Point &point1, const Point &point2);
 
 struct Location {
@@ -376,7 +378,7 @@
 
 	int32 _frameNumber;			// current frame number
 
-	Common::Array<byte> _tileDirections;
+	ByteArray _tileDirections;
 
 	Common::Array<Point> _walkStepsPoints;
 
@@ -624,7 +626,7 @@
 
 	Rect _barrierList[ACTOR_BARRIERS_MAX];
 	int _barrierCount;
-	int8 *_pathCell;
+	Common::Array<int8> _pathCell;
 
 	int _xCellCount;
 	int _yCellCount;

Modified: scummvm/trunk/engines/saga/actor_walk.cpp
===================================================================
--- scummvm/trunk/engines/saga/actor_walk.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/actor_walk.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -227,12 +227,12 @@
 	if (_protagonist == NULL)
 		return;
 
-	if ((actorsEntrance >= 0) && (_vm->_scene->_entryList.entryListCount > 0)) {
-		if (_vm->_scene->_entryList.entryListCount <= actorsEntrance) {
+	if ((actorsEntrance >= 0) && (!_vm->_scene->_entryList.empty())) {
+		if (_vm->_scene->_entryList.size() <= uint(actorsEntrance)) {
 			actorsEntrance = 0; //OCEAN bug
 		}
 
-		sceneEntry = _vm->_scene->_entryList.getEntry(actorsEntrance);
+		sceneEntry = &_vm->_scene->_entryList[actorsEntrance];
 		if (_vm->_scene->getFlags() & kSceneFlagISO) {
 			_protagonist->_location = sceneEntry->location;
 		} else {
@@ -722,7 +722,7 @@
 
 void Actor::direct(int msec) {
 
-	if (_vm->_scene->_entryList.entryListCount == 0) {
+	if (_vm->_scene->_entryList.empty()) {
 		return;
 	}
 

Modified: scummvm/trunk/engines/saga/animation.cpp
===================================================================
--- scummvm/trunk/engines/saga/animation.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/animation.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -42,8 +42,6 @@
 Anim::Anim(SagaEngine *vm) : _vm(vm) {
 	uint16 i;
 
-	_cutawayList = NULL;
-	_cutawayListLength = 0;
 	_cutawayActive = false;
 
 	for (i = 0; i < MAX_ANIMATIONS; i++)
@@ -55,21 +53,16 @@
 
 Anim::~Anim() {
 	reset();
-#ifdef ENABLE_IHNM
-	freeCutawayList();
-#endif
 }
 
 #ifdef ENABLE_IHNM
 
 void Anim::loadCutawayList(const byte *resourcePointer, size_t resourceLength) {
-	free(_cutawayList);
-	_cutawayListLength = resourceLength / 8;
-	_cutawayList = (Cutaway *)malloc(_cutawayListLength * sizeof(Cutaway));
+	_cutawayList.resize(resourceLength / 8);
 
 	MemoryReadStream cutawayS(resourcePointer, resourceLength);
 
-	for (int i = 0; i < _cutawayListLength; i++) {
+	for (uint i = 0; i < _cutawayList.size(); i++) {
 		_cutawayList[i].backgroundResourceId = cutawayS.readUint16LE();
 		_cutawayList[i].animResourceId = cutawayS.readUint16LE();
 		_cutawayList[i].cycles = cutawayS.readSint16LE();
@@ -77,10 +70,8 @@
 	}
 }
 
-void Anim::freeCutawayList() {
-	free(_cutawayList);
-	_cutawayList = NULL;
-	_cutawayListLength = 0;
+void Anim::clearCutawayList() {
+	_cutawayList.clear();
 }
 
 int Anim::playCutaway(int cut, bool fade) {
@@ -404,11 +395,11 @@
 	if (animId >= MAX_ANIMATIONS) {
 		if (animId >= MAX_ANIMATIONS + ARRAYSIZE(_cutawayAnimations))
 			error("Anim::load could not find unused animation slot");
-		anim = _cutawayAnimations[animId - MAX_ANIMATIONS] = new AnimationData(animResourceData, animResourceLength);
+		anim = _cutawayAnimations[animId - MAX_ANIMATIONS] = new AnimationData();
 	} else
-		anim = _animations[animId] = new AnimationData(animResourceData, animResourceLength);
+		anim = _animations[animId] = new AnimationData();
 
-	MemoryReadStreamEndian headerReadS(anim->resourceData, anim->resourceLength, _vm->isBigEndian());
+	MemoryReadStreamEndian headerReadS(animResourceData, animResourceLength, _vm->isBigEndian());
 	anim->magic = headerReadS.readUint16LE(); // cause ALWAYS LE
 	anim->screenWidth = headerReadS.readUint16();
 	anim->screenHeight = headerReadS.readUint16();
@@ -418,23 +409,30 @@
 	anim->maxFrame = headerReadS.readByte() - 1;
 	anim->loopFrame = headerReadS.readByte() - 1;
 	temp = headerReadS.readUint16BE();
-	anim->start = headerReadS.pos();
+	size_t start;
+
+	start = headerReadS.pos();
 	if (temp == (uint16)(-1)) {
 		temp = 0;
 	}
-	anim->start += temp;
+	start += temp;
 
+	size_t dataOffset = headerReadS.pos();
+	if (dataOffset != start) {
+		warning("Anim::load animId=%d start != dataOffset 0x%X 0x%X", animId, start, dataOffset);
+	}
+
+	anim->resourceData.resize(animResourceLength - dataOffset);
+	
+	memcpy(anim->resourceData.getBuffer(), animResourceData + dataOffset, anim->resourceData.size());
 	// Cache frame offsets
 
 	// WORKAROUND: Cutaway with background resource ID 37 (loaded as cutaway #4) is ending credits.
 	// For some reason it has wrong number of frames specified in its header. So we calculate it here:
-	if (animId > MAX_ANIMATIONS && _cutawayListLength > 4 && _cutawayList[4].backgroundResourceId == 37 && anim->maxFrame == 143)
+	if (animId > MAX_ANIMATIONS && _cutawayList.size() > 4 && _cutawayList[4].backgroundResourceId == 37 && anim->maxFrame == 143)
 		anim->maxFrame = fillFrameOffsets(anim, false);
 
-	anim->frameOffsets = (size_t *)malloc((anim->maxFrame + 1) * sizeof(*anim->frameOffsets));
-	if (anim->frameOffsets == NULL) {
-		memoryError("Anim::load");
-	}
+	anim->frameOffsets.resize(anim->maxFrame + 1);
 
 	fillFrameOffsets(anim);
 
@@ -688,7 +686,7 @@
 		error("decodeFrame() Buffer size inadequate");
 	}
 
-	MemoryReadStream readS(anim->resourceData + frameOffset, anim->resourceLength - frameOffset);
+	MemoryReadStream readS(&anim->resourceData[frameOffset], anim->resourceData.size() - frameOffset);
 
 // FIXME: This is thrown when the first video of the IHNM end sequence is shown (the "turn off screen"
 // video), however the video is played correctly and the rest of the end sequence continues normally
@@ -825,10 +823,8 @@
 	int i;
 	bool longData = isLongData();
 
-	MemoryReadStreamEndian readS(anim->resourceData, anim->resourceLength, !_vm->isBigEndian()); // RLE has inversion BE<>LE
+	MemoryReadStreamEndian readS(&anim->resourceData.front(), anim->resourceData.size(), !_vm->isBigEndian()); // RLE has inversion BE<>LE
 
-	readS.seek(12);
-
 	while (readS.pos() != readS.size()) {
 		if (reallyFill) {
 			anim->frameOffsets[currentFrame] = readS.pos();
@@ -843,7 +839,7 @@
 		// including the frame header, is in big endian format
 		do {
 			markByte = readS.readByte();
-//			debug(7, "_pos=%x currentFrame=%i markByte=%x", readS.pos(), currentFrame, markByte);
+//			debug(7, "_pos=%X currentFrame=%i markByte=%X", readS.pos(), currentFrame, markByte);
 
 			switch (markByte) {
 			case SAGA_FRAME_START: // Start of frame
@@ -942,9 +938,9 @@
 void Anim::cutawayInfo() {
 	uint16 i;
 
-	_vm->_console->DebugPrintf("There are %d cutaways loaded:\n", _cutawayListLength);
+	_vm->_console->DebugPrintf("There are %d cutaways loaded:\n", _cutawayList.size());
 
-	for (i = 0; i < _cutawayListLength; i++) {
+	for (i = 0; i < _cutawayList.size(); i++) {
 		_vm->_console->DebugPrintf("%02d: Bg res: %u Anim res: %u Cycles: %u Framerate: %u\n", i,
 			_cutawayList[i].backgroundResourceId, _cutawayList[i].animResourceId,
 			_cutawayList[i].cycles, _cutawayList[i].frameRate);

Modified: scummvm/trunk/engines/saga/animation.h
===================================================================
--- scummvm/trunk/engines/saga/animation.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/animation.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -66,8 +66,7 @@
 
 // Animation info array member
 struct AnimationData {
-	byte *resourceData;
-	size_t resourceLength;
+	ByteArray resourceData;
 
 	uint16 magic;
 
@@ -80,10 +79,8 @@
 	int16 maxFrame;
 	int16 loopFrame;
 
-	int16 start;
-
 	int16 currentFrame;
-	size_t *frameOffsets;
+	Common::Array<size_t> frameOffsets;
 
 	uint16 completed;
 	uint16 cycles;
@@ -93,17 +90,6 @@
 	AnimationState state;
 	int16 linkId;
 	uint16 flags;
-
-	AnimationData(const byte *animResourceData, size_t animResourceLength) {
-		memset(this, 0, sizeof(*this));
-		resourceLength = animResourceLength;
-		resourceData = (byte*)malloc(animResourceLength);
-		memcpy(resourceData, animResourceData, animResourceLength);
-	}
-	~AnimationData() {
-		free(frameOffsets);
-		free(resourceData);
-	}
 };
 
 class Anim {
@@ -112,7 +98,7 @@
 	~Anim();
 
 	void loadCutawayList(const byte *resourcePointer, size_t resourceLength);
-	void freeCutawayList();
+	void clearCutawayList();
 	int playCutaway(int cut, bool fade);
 	void endCutaway();
 	void returnFromCutaway();
@@ -154,9 +140,9 @@
 
 	bool hasCutaway() { return _cutawayActive; }
 	void setCutAwayMode(int mode) { _cutAwayMode = mode; }
-	int cutawayListLength() { return _cutawayListLength; }
-	int cutawayBgResourceID(int cutaway) { return _cutawayList[cutaway].backgroundResourceId; }
-	int cutawayAnimResourceID(int cutaway) { return _cutawayList[cutaway].animResourceId; }
+//	int cutawayListLength() { return _cutawayListLength; }
+//	int cutawayBgResourceID(int cutaway) { return _cutawayList[cutaway].backgroundResourceId; }
+//	int cutawayAnimResourceID(int cutaway) { return _cutawayList[cutaway].animResourceId; }
 
 private:
 	void decodeFrame(AnimationData *anim, size_t frameOffset, byte *buf, size_t bufLength);
@@ -205,9 +191,8 @@
 	SagaEngine *_vm;
 	AnimationData *_animations[MAX_ANIMATIONS];
 	AnimationData *_cutawayAnimations[2];
-	Cutaway *_cutawayList;
+	Common::Array<Cutaway> _cutawayList;
 	PalEntry saved_pal[PAL_ENTRIES];
-	int _cutawayListLength;
 	bool _cutawayActive;
 	int _cutAwayMode;
 	bool _cutAwayFade;

Modified: scummvm/trunk/engines/saga/detection.cpp
===================================================================
--- scummvm/trunk/engines/saga/detection.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/detection.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -259,7 +259,7 @@
 			version = SWAP_BYTES_32(version);
 		}
 
-		debug(2, "Save version: %x", version);
+		debug(2, "Save version: 0x%X", version);
 
 		if (version < 4)
 			warning("This savegame is not endian-safe. There may be problems");

Modified: scummvm/trunk/engines/saga/font.cpp
===================================================================
--- scummvm/trunk/engines/saga/font.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/font.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -109,7 +109,7 @@
 	}
 
 	font->normal.font.resize(fontResourceLength - FONT_DESCSIZE);
-	memcpy(&font->normal.font.front(), fontResourcePointer + FONT_DESCSIZE, fontResourceLength - FONT_DESCSIZE);
+	memcpy(font->normal.font.getBuffer(), fontResourcePointer + FONT_DESCSIZE, fontResourceLength - FONT_DESCSIZE);
 
 	free(fontResourcePointer);
 

Modified: scummvm/trunk/engines/saga/font.h
===================================================================
--- scummvm/trunk/engines/saga/font.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/font.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -120,7 +120,7 @@
 struct FontStyle {
 	FontHeader header;
 	FontCharEntry fontCharEntry[256];
-	Common::Array<byte> font;
+	ByteArray font;
 };
 
 struct FontData {

Modified: scummvm/trunk/engines/saga/gfx.cpp
===================================================================
--- scummvm/trunk/engines/saga/gfx.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/gfx.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -564,8 +564,9 @@
 
 // This method adds a dirty rectangle automatically
 void Gfx::drawFrame(const Common::Point &p1, const Common::Point &p2, int color) {
-	_backBuffer.drawFrame(p1, p2, color);
-	_vm->_render->addDirtyRect(Common::Rect(p1.x, p1.y, p2.x + 1, p2.y + 1));
+	Common::Rect rect(MIN(p1.x, p2.x), MIN(p1.y, p2.y), MAX(p1.x, p2.x) + 1, MAX(p1.y, p2.y) + 1);
+	_backBuffer.frameRect(rect, color);
+	_vm->_render->addDirtyRect(rect);
 }
 
 // This method adds a dirty rectangle automatically

Modified: scummvm/trunk/engines/saga/gfx.h
===================================================================
--- scummvm/trunk/engines/saga/gfx.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/gfx.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -108,10 +108,7 @@
 		rect.right = w;
 		rect.bottom = h;
 	}
-	void drawFrame(const Common::Point &p1, const Common::Point &p2, int color) {
-		Common::Rect rect(MIN(p1.x, p2.x), MIN(p1.y, p2.y), MAX(p1.x, p2.x) + 1, MAX(p1.y, p2.y) + 1);
-		frameRect(rect, color);
-	}
+
 	void drawRect(const Common::Rect &destRect, int color) {
 		Common::Rect rect(w , h);
 		rect.clip(destRect);
@@ -198,7 +195,7 @@
 	// WARNING: This method does not add a dirty rectangle automatically.
 	// Whenever it gets called, the corresponding caller must take care
 	// to add the corresponding dirty rectangle itself
-	void drawPolyLine(Common::Point *points, int count, int color) {
+	void drawPolyLine(const Common::Point *points, int count, int color) {
 		_backBuffer.drawPolyLine(points, count, color);
 	}
 

Modified: scummvm/trunk/engines/saga/image.cpp
===================================================================
--- scummvm/trunk/engines/saga/image.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/image.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -53,8 +53,7 @@
 	int modex_height;
 	const byte *RLE_data_ptr;
 	size_t RLE_data_len;
-	byte *decode_buf;
-	size_t decode_buf_len;
+	ByteArray decodeBuffer;
 	byte *out_buf;
 	size_t out_buf_len;
 
@@ -75,28 +74,23 @@
 
 	modex_height = granulate(hdr.height, 4);
 
-	decode_buf_len = hdr.width * modex_height;
-	decode_buf = (byte *)malloc(decode_buf_len);
+	decodeBuffer.resize(hdr.width * modex_height);
 
 	out_buf_len = hdr.width * hdr.height;
 	out_buf = (byte *)malloc(out_buf_len);
 
-	if (decodeBGImageRLE(RLE_data_ptr,
-		RLE_data_len, decode_buf, decode_buf_len) != SUCCESS) {
-		free(decode_buf);
+	if (decodeBGImageRLE(RLE_data_ptr, RLE_data_len, decodeBuffer) != SUCCESS) {
 		free(out_buf);
 		return FAILURE;
 	}
 
-	unbankBGImage(out_buf, decode_buf, hdr.width, hdr.height);
+	unbankBGImage(out_buf, decodeBuffer.getBuffer(), hdr.width, hdr.height);
 
 	// For some reason bg images in IHNM are upside down
 	if (getGameId() == GID_IHNM && !flip) {
 		flipImage(out_buf, hdr.width, hdr.height);
 	}
 
-	free(decode_buf);
-
 	*output_buf_len = out_buf_len;
 	*output_buf = out_buf;
 
@@ -106,9 +100,10 @@
 	return SUCCESS;
 }
 
-int SagaEngine::decodeBGImageRLE(const byte *inbuf, size_t inbuf_len, byte *outbuf, size_t outbuf_len) {
+int SagaEngine::decodeBGImageRLE(const byte *inbuf, size_t inbuf_len, ByteArray &outbuf) {
 	const byte *inbuf_ptr;
 	byte *outbuf_ptr;
+	byte *outbuf_start;
 	uint32 inbuf_remain;
 
 	const byte *inbuf_end;
@@ -134,13 +129,13 @@
 	inbuf_ptr = inbuf;
 	inbuf_remain = inbuf_len;
 
-	outbuf_ptr = outbuf;
-	outbuf_remain = outbuf_len;
+	outbuf_start = outbuf_ptr = outbuf.getBuffer();
+	outbuf_remain = outbuf.size();
+	outbuf_end = (outbuf_start + outbuf_remain) - 1;
+	memset(outbuf_start, 0, outbuf_remain);
 
 	inbuf_end = (inbuf + inbuf_len) - 1;
-	outbuf_end = (outbuf + outbuf_len) - 1;
 
-	memset(outbuf, 0, outbuf_len);
 
 	while ((inbuf_remain > 1) && (outbuf_remain > 0) && !decode_err) {
 
@@ -194,7 +189,7 @@
 			runcount = ((mark_byte >> 3) & 0x07U) + 3;
 			backtrack_amount = *inbuf_ptr;
 
-			if (!inbuf_remain || (backtrack_amount > (outbuf_ptr - outbuf)) || (runcount > outbuf_remain)) {
+			if (!inbuf_remain || (backtrack_amount > (outbuf_ptr - outbuf_start)) || (runcount > outbuf_remain)) {
 				return FAILURE;
 			}
 
@@ -277,7 +272,7 @@
 			inbuf_ptr++;
 			runcount = *inbuf_ptr++;
 
-			if ((backtrack_amount > (outbuf_ptr - outbuf)) || (outbuf_remain < runcount)) {
+			if ((backtrack_amount > (outbuf_ptr - outbuf_start)) || (outbuf_remain < runcount)) {
 				return FAILURE;
 			}
 
@@ -301,15 +296,17 @@
 
 int SagaEngine::flipImage(byte *img_buf, int columns, int scanlines) {
 	int line;
-	byte *tmp_scan;
+	ByteArray tmp_scan;
 
 	byte *flip_p1;
 	byte *flip_p2;
+	byte *flip_tmp;
 
 	int flipcount = scanlines / 2;
 
-	tmp_scan = (byte *)malloc(columns);
-	if (tmp_scan == NULL) {
+	tmp_scan.resize(columns);
+	flip_tmp = tmp_scan.getBuffer();
+	if (flip_tmp == NULL) {
 		return FAILURE;
 	}
 
@@ -317,15 +314,13 @@
 	flip_p2 = img_buf + (columns * (scanlines - 1));
 
 	for (line = 0; line < flipcount; line++) {
-		memcpy(tmp_scan, flip_p1, columns);
+		memcpy(flip_tmp, flip_p1, columns);
 		memcpy(flip_p1, flip_p2, columns);
-		memcpy(flip_p2, tmp_scan, columns);
+		memcpy(flip_p2, flip_tmp, columns);
 		flip_p1 += columns;
 		flip_p2 -= columns;
 	}
 
-	free(tmp_scan);
-
 	return SUCCESS;
 }
 

Modified: scummvm/trunk/engines/saga/interface.cpp
===================================================================
--- scummvm/trunk/engines/saga/interface.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/interface.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -2420,18 +2420,9 @@
 
 
 // Converse stuff
-void Interface::converseInit() {
-	for (int i = 0; i < CONVERSE_MAX_TEXTS; i++)
-		_converseText[i].text = NULL;
-	converseClear();
-}
-
 void Interface::converseClear() {
 	for (int i = 0; i < CONVERSE_MAX_TEXTS; i++) {
-		if (_converseText[i].text != NULL) {
-			free(_converseText[i].text);
-			_converseText[i].text = NULL;
-		}
+		_converseText[i].text.clear();
 		_converseText[i].stringNum = -1;
 		_converseText[i].replyId = 0;
 		_converseText[i].replyFlags = 0;
@@ -2477,8 +2468,8 @@
 			return true;
 		}
 
-		_converseText[_converseTextCount].text = (char *)malloc(i + 1);
-		strncpy(_converseText[_converseTextCount].text, _converseWorkString, i);
+		_converseText[_converseTextCount].text.resize(i + 1);
+		strncpy(&_converseText[_converseTextCount].text.front(), _converseWorkString, i);
 
 		_converseText[_converseTextCount].strId = strId;
 		_converseText[_converseTextCount].text[i] = 0;
@@ -2588,7 +2579,7 @@
 		rect.left += 8;
 		_vm->_gfx->drawRect(rect, backgnd);
 
-		str = _converseText[relPos].text;
+		str = &_converseText[relPos].text.front();
 
 		if (_converseText[relPos].textNum == 0) { // first entry
 			textPoint.x = rect.left - 6;

Modified: scummvm/trunk/engines/saga/interface.h
===================================================================
--- scummvm/trunk/engines/saga/interface.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/interface.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -164,7 +164,7 @@
 };
 
 struct Converse {
-	char *text;
+	Common::Array<char> text;
 	int strId;
 	int stringNum;
 	int textNum;
@@ -356,7 +356,6 @@
 	void processStatusTextInput(Common::KeyState keystate);
 
 public:
-	void converseInit();
 	void converseClear();
 	bool converseAddText(const char *text, int strId, int replyId, byte replyFlags, int replyBit);
 	void converseDisplayText();

Modified: scummvm/trunk/engines/saga/introproc_ihnm.cpp
===================================================================
--- scummvm/trunk/engines/saga/introproc_ihnm.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/introproc_ihnm.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -83,7 +83,7 @@
 	}
 
 	_vm->_music->setVolume(0, 1000);
-	_vm->_anim->freeCutawayList();
+	_vm->_anim->clearCutawayList();
 
 	// Queue first scene
 	firstScene.loadFlag = kLoadBySceneNumber;
@@ -114,7 +114,7 @@
 	}
 
 	_vm->_music->setVolume(0, 1000);
-	_vm->_anim->freeCutawayList();
+	_vm->_anim->clearCutawayList();
 
 	return SUCCESS;
 }

Modified: scummvm/trunk/engines/saga/isomap.cpp
===================================================================
--- scummvm/trunk/engines/saga/isomap.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/isomap.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -85,16 +85,21 @@
 	{ 0, 1, 0, SAGA_STRAIGHT_HARD_COST},
 };
 
-IsoMap::IsoMap(SagaEngine *vm) : _vm(vm) {
-	_tileData = NULL;
-	_tileDataLength = 0;
+static const int16 directions[8][2] = {
+	{	16,		16},
+	{	16,		0},
+	{	16,		-16},
+	{	0,		-16},
+	{	-16,	-16},
+	{	-16,	0},
+	{	-16,	16},
+	{	0,		16}
+};
 
-	_multiTableData = NULL;
-	_multiDataCount = 0;
+IsoMap::IsoMap(SagaEngine *vm) : _vm(vm) {
 	_viewScroll.x = (128 - 8) * 16;
 	_viewScroll.x = (128 - 8) * 16 - 64;
 	_viewDiff = 1;
-
 }
 
 void IsoMap::loadImages(const byte *resourcePointer, size_t resourceLength) {
@@ -112,7 +117,8 @@
 	i = readS.readUint16();
 	i = i / SAGA_ISOTILEDATA_LEN;
 	_tilesTable.resize(i);
-
+	Common::Array<size_t> tempOffsets;
+	tempOffsets.resize(_tilesTable.size());
 	readS.seek(0);
 
 
@@ -120,7 +126,7 @@
 		tileData = &_tilesTable[i];
 		tileData->height = readS.readByte();
 		tileData->attributes = readS.readSByte();
-		tileData->offset = readS.readUint16();
+		tempOffsets[i] = readS.readUint16();
 		tileData->terrainMask = readS.readUint16();
 		tileData->FGDBGDAttr = readS.readByte();
 		readS.readByte(); //skip
@@ -128,12 +134,11 @@
 
 	offsetDiff = readS.pos();
 
-	_tileDataLength = resourceLength - offsetDiff;
-	_tileData = (byte*)malloc(_tileDataLength);
-	memcpy(_tileData, resourcePointer + offsetDiff, _tileDataLength);
+	_tileData.resize(resourceLength - offsetDiff);
+	memcpy(_tileData.getBuffer(), resourcePointer + offsetDiff, _tileData.size());
 
 	for (i = 0; i < _tilesTable.size(); i++) {
-		_tilesTable[i].offset -= offsetDiff;
+		_tilesTable[i].tilePointer = _tileData.getBuffer() + tempOffsets[i] - offsetDiff;
 	}
 }
 
@@ -240,26 +245,21 @@
 		_multiTable[i].offset -= offsetDiff;
 	}
 
-	_multiDataCount = (readS.size() - readS.pos()) / 2;
+	uint16 multiDataCount = (readS.size() - readS.pos()) / 2;
 
-	_multiTableData = (int16 *)malloc(_multiDataCount * sizeof(*_multiTableData));
-	for (i = 0; i < _multiDataCount; i++) {
+	_multiTableData.resize(multiDataCount);
+	for (i = 0; i < _multiTableData.size(); i++) {
 		_multiTableData[i] = readS.readSint16();
 	}
 }
 
-void IsoMap::freeMem() {
+void IsoMap::clear() {
 	_tilesTable.clear();
 	_tilePlatformList.clear();
 	_metaTileList.clear();
 	_multiTable.clear();
-
-	free(_tileData);
-	_tileData = NULL;
-
-	free(_multiTableData);
-	_multiTableData = NULL;
-	_multiDataCount = 0;
+	_tileData.clear();
+	_multiTableData.clear();
 }
 
 void IsoMap::adjustScroll(bool jump) {
@@ -344,12 +344,12 @@
 			state = multiTileEntryData->currentState;
 
 			offset = (ru + state * multiTileEntryData->uSize) * multiTileEntryData->vSize + rv;
-			offset *= sizeof(*_multiTableData);
+			offset *= sizeof(int16);
 			offset += multiTileEntryData->offset;
-			if (offset + sizeof(*_multiTableData) - 1 >= _multiDataCount * sizeof(*_multiTableData)) {
+			if (offset + sizeof(int16) > _multiTableData.size() * sizeof(int16)) {
 				error("wrong multiTileEntryData->offset");
 			}
-			tiles = (int16*)((byte*)_multiTableData + offset);
+			tiles = (int16*)((byte*)&_multiTableData.front() + offset);
 			tileIndex = *tiles;
 			if (tileIndex >= 256) {
 				warning("something terrible happened");
@@ -707,7 +707,7 @@
 		return;
 	}
 
-	tilePointer = _tileData + _tilesTable[tileIndex].offset;
+	tilePointer = _tilesTable[tileIndex].tilePointer;
 	height = _tilesTable[tileIndex].height;
 
 	if ((height <= 8) || (height > 64)) {
@@ -1613,19 +1613,6 @@
 	multiTileEntryData->currentState = doorState;
 }
 
-static const int16 directions[8][2] = {
-	{	16,		16},
-	{	16,		0},
-	{	16,		-16},
-	{	0,		-16},
-	{	-16,	-16},
-	{	-16,	0},
-	{	-16,	16},
-	{	0,		16}
-};
-
-
-
 bool IsoMap::nextTileTarget(ActorData* actor) {
 	uint16 dir;
 

Modified: scummvm/trunk/engines/saga/isomap.h
===================================================================
--- scummvm/trunk/engines/saga/isomap.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/isomap.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -95,7 +95,7 @@
 struct IsoTileData {
 	byte height;
 	int8 attributes;
-	size_t offset;
+	byte *tilePointer;
 	uint16 terrainMask;
 	byte FGDBGDAttr;
 	int8 GetMaskRule() const {
@@ -154,14 +154,13 @@
 public:
 	IsoMap(SagaEngine *vm);
 	~IsoMap() {
-		freeMem();
 	}
 	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 freeMem();
+	void clear();
 	void draw();
 	void drawSprite(SpriteList &spriteList, int spriteNumber, const Location &location, const Point &screenPosition, int scale);
 	void adjustScroll(bool jump);
@@ -213,16 +212,14 @@
 	IsoTileData *getTile(int16 u, int16 v, int16 z);
 
 
-	byte *_tileData;
-	size_t _tileDataLength;
+	ByteArray _tileData;
 	Common::Array<IsoTileData> _tilesTable;
 
 	Common::Array<TilePlatformData> _tilePlatformList;
 	Common::Array<MetaTileData> _metaTileList;
 
 	Common::Array<MultiTileEntryData> _multiTable;
-	uint16 _multiDataCount;
-	int16 *_multiTableData;
+	Common::Array<int16> _multiTableData;
 
 	TileMapData _tileMap;
 

Modified: scummvm/trunk/engines/saga/itedata.cpp
===================================================================
--- scummvm/trunk/engines/saga/itedata.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/itedata.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -269,7 +269,7 @@
 	{ 54, 281,  620,  352,  0, 80, 46, 0           }  // Orb of Storms in Dam Lab
 };
 
-FxTable ITE_SfxTable[ITE_SFXCOUNT] = {
+IteFxTable ITE_SfxTable[ITE_SFXCOUNT] = {
 	{ 14,	127 },	// Door open
 	{ 15,   127 },	// Door close
 	{ 16,    63 },	// Rush water (floppy volume: 127)

Modified: scummvm/trunk/engines/saga/itedata.h
===================================================================
--- scummvm/trunk/engines/saga/itedata.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/itedata.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -77,16 +77,16 @@
 	uint16 interactBits;
 };
 
-struct FxTable {
-	int res;
-	int vol;
+struct IteFxTable {
+	byte res;
+	byte vol;
 };
 
 #define ITE_OBJECTCOUNT 39
 #define ITE_SFXCOUNT 63
 
 extern ObjectTableData ITE_ObjectTable[ITE_OBJECTCOUNT];
-extern FxTable ITE_SfxTable[ITE_SFXCOUNT];
+extern IteFxTable ITE_SfxTable[ITE_SFXCOUNT];
 
 extern const char *ITEinterfaceTextStrings[][53];
 

Modified: scummvm/trunk/engines/saga/music.cpp
===================================================================
--- scummvm/trunk/engines/saga/music.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/music.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -182,9 +182,6 @@
 	_parser->setTimerRate(_driver->getBaseTempo());
 	_parser->property(MidiParser::mpCenterPitchWheelOnUnload, 1);
 
-	_songTableLen = 0;
-	_songTable = 0;
-
 	_midiMusicData = NULL;
 	_digitalMusic = false;
 }
@@ -197,7 +194,6 @@
 	_parser->setMidiDriver(NULL);
 	delete _parser;
 
-	free(_songTable);
 	free(_midiMusicData);
 }
 
@@ -260,7 +256,7 @@
 	Audio::SeekableAudioStream *audioStream = NULL;
 	byte *resourceData;
 	size_t resourceSize;
-	uint32 loopStart;
+	uint32 loopStart = 0;
 
 	debug(2, "Music::play %d, %d", resourceId, flags);
 

Modified: scummvm/trunk/engines/saga/music.h
===================================================================
--- scummvm/trunk/engines/saga/music.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/music.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -106,8 +106,7 @@
 	void setVolume(int volume, int time = 1);
 	int getVolume() { return _currentVolume; }
 
-	int32 *_songTable;
-	int _songTableLen;
+	Common::Array<int32> _songTable;
 
 private:
 	SagaEngine *_vm;

Modified: scummvm/trunk/engines/saga/objectmap.cpp
===================================================================
--- scummvm/trunk/engines/saga/objectmap.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/objectmap.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -45,84 +45,58 @@
 
 namespace Saga {
 
-HitZone::HitZone(MemoryReadStreamEndian *readStream, int index, int sceneNumber): _index(index) {
-	int i, j;
-	HitZone::ClickArea *clickArea;
-	Point *point;
-
+void HitZone::load(SagaEngine *vm, MemoryReadStreamEndian *readStream, int index, int sceneNumber) {
+	_index = index;
 	_flags = readStream->readByte();
-	_clickAreasCount = readStream->readByte();
+	_clickAreas.resize(readStream->readByte());
 	_rightButtonVerb = readStream->readByte();
 	readStream->readByte(); // pad
 	_nameIndex = readStream->readUint16();
 	_scriptNumber = readStream->readUint16();
 
-	_clickAreas = (HitZone::ClickArea *)malloc(_clickAreasCount * sizeof(*_clickAreas));
+	for (ClickAreas::iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
+		i->resize(readStream->readUint16LE());
 
-	if (_clickAreas == NULL) {
-		memoryError("HitZone::HitZone");
-	}
+		assert(!i->empty());
 
-	for (i = 0; i < _clickAreasCount; i++) {
-		clickArea = &_clickAreas[i];
-		clickArea->pointsCount = readStream->readUint16LE();
+		for (ClickArea::iterator j = i->begin(); j != i->end(); ++j) {
+			j->x = readStream->readSint16();
+			j->y = readStream->readSint16();
 
-		assert(clickArea->pointsCount);
-
-		clickArea->points = (Point *)malloc(clickArea->pointsCount * sizeof(*(clickArea->points)));
-		if (clickArea->points == NULL) {
-			memoryError("HitZone::HitZone");
-		}
-
-		for (j = 0; j < clickArea->pointsCount; j++) {
-			point = &clickArea->points[j];
-			point->x = readStream->readSint16();
-			point->y = readStream->readSint16();
-
 			// WORKAROUND: bug #1259608: "ITE: Riff ignores command in Ferret merchant center"
 			// Apparently ITE Mac version has bug in game data. Both ObjectMap and ActionMap
 			// for exit area are little taller (y = 123) and thus Riff goes to exit
 			// when clicked on barrel of nails.
-			if (sceneNumber == 18 && index == 0 && i == 0 && j == 0 && point->y == 123)
-				point->y = 129;
+			if (vm->getGameId() == GID_ITE) {
+				if (sceneNumber == 18 && index == 0 && (i == _clickAreas.begin()) && (j == i->begin()) && j->y == 123) {
+					j->y = 129;
+				}
+			}
 		}
 	}
 }
 
-HitZone::~HitZone() {
-	for (int i = 0; i < _clickAreasCount; i++) {
-		free(_clickAreas[i].points);
-	}
-	free(_clickAreas);
-}
-
 bool HitZone::getSpecialPoint(Point &specialPoint) const {
-	int i, pointsCount;
-	HitZone::ClickArea *clickArea;
-	Point *points;
-
-	for (i = 0; i < _clickAreasCount; i++) {
-		clickArea = &_clickAreas[i];
-		pointsCount = clickArea->pointsCount;
-		points = clickArea->points;
-		if (pointsCount == 1) {
-			specialPoint = points[0];
+	for (ClickAreas::const_iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
+		if (i->size() == 1) {
+			specialPoint = (*i)[0];
 			return true;
 		}
 	}
 	return false;
 }
+
 bool HitZone::hitTest(const Point &testPoint) {
-	int i, pointsCount;
-	HitZone::ClickArea *clickArea;
-	Point *points;
+	const Point *points;
+	uint pointsCount;
 
 	if (_flags & kHitZoneEnabled) {
-		for (i = 0; i < _clickAreasCount; i++) {
-			clickArea = &_clickAreas[i];
-			pointsCount = clickArea->pointsCount;
-			points = clickArea->points;
-
+		for (ClickAreas::const_iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
+			pointsCount = i->size();
+			if (pointsCount < 2) {
+				continue;
+			}
+			points = &i->front();
 			if (pointsCount == 2) {
 				// Hit-test a box region
 				if ((testPoint.x >= points[0].x) &&
@@ -132,11 +106,9 @@
 						return true;
 					}
 			} else {
-				if (pointsCount > 2) {
-					// Hit-test a polygon
-					if (hitTestPoly(points, pointsCount, testPoint)) {
-						return true;
-					}
+				// Hit-test a polygon
+				if (hitTestPoly(points, pointsCount, testPoint)) {
+					return true;
 				}
 			}
 		}
@@ -146,26 +118,25 @@
 
 #ifdef SAGA_DEBUG
 void HitZone::draw(SagaEngine *vm, int color) {
-	int i, pointsCount, j;
+	int pointsCount, j;
 	Location location;
-	HitZone::ClickArea *clickArea;
-	Point *points;
+	HitZone::ClickArea tmpPoints;
+	const Point *points;
 	Point specialPoint1;
 	Point specialPoint2;
 
-	for (i = 0; i < _clickAreasCount; i++) {
-		clickArea = &_clickAreas[i];
-		pointsCount = clickArea->pointsCount;
+	for (ClickAreas::const_iterator i = _clickAreas.begin(); i != _clickAreas.end(); ++i) {
+		pointsCount = i->size();
+		points = &i->front();
 		if (vm->_scene->getFlags() & kSceneFlagISO) {
-			points = (Point*)malloc(sizeof(Point) * pointsCount);
+			tmpPoints.resize(pointsCount);
 			for (j = 0; j < pointsCount; j++) {
-				location.u() = clickArea->points[j].x;
-				location.v() = clickArea->points[j].y;
+				location.u() = points[j].x;
+				location.v() = points[j].y;
 				location.z = 0;
-				vm->_isoMap->tileCoordsToScreenPoint(location, points[j]);
+				vm->_isoMap->tileCoordsToScreenPoint(location, tmpPoints[j]);
 			}
-		} else {
-			points = clickArea->points;
+			points = &tmpPoints.front();
 		}
 
 		if (pointsCount == 2) {
@@ -179,10 +150,6 @@
 				vm->_gfx->drawPolyLine(points, pointsCount, color);
 			}
 		}
-		if (vm->_scene->getFlags() & kSceneFlagISO) {
-			free(points);
-		}
-
 	}
 	if (getSpecialPoint(specialPoint1)) {
 		specialPoint2 = specialPoint1;
@@ -197,8 +164,12 @@
 
 // Loads an object map resource ( objects ( clickareas ( points ) ) )
 void ObjectMap::load(const byte *resourcePointer, size_t resourceLength) {
-	int i;
+	uint i;
 
+	if (!_hitZoneList.empty()) {
+		error("ObjectMap::load _hitZoneList not empty");
+	}
+
 	if (resourceLength == 0) {
 		return;
 	}
@@ -209,43 +180,21 @@
 
 	MemoryReadStreamEndian readS(resourcePointer, resourceLength, _vm->isBigEndian());
 
-	_hitZoneListCount = readS.readSint16();
-	if (_hitZoneListCount < 0) {
-		error("ObjectMap::load _hitZoneListCount < 0");
-	}
+	_hitZoneList.resize(readS.readUint16());
 
-	if (_hitZoneList)
-		error("ObjectMap::load _hitZoneList != NULL");
-
-	_hitZoneList = (HitZone **) malloc(_hitZoneListCount * sizeof(HitZone *));
-	if (_hitZoneList == NULL) {
-		memoryError("ObjectMap::load");
+	for (i = 0; i < _hitZoneList.size(); i++) {
+		_hitZoneList[i].load(_vm, &readS, i, _vm->_scene->currentSceneNumber());
 	}
-
-	for (i = 0; i < _hitZoneListCount; i++) {
-		_hitZoneList[i] = new HitZone(&readS, i, _vm->_scene->currentSceneNumber());
-	}
 }
 
-void ObjectMap::freeMem() {
-	int i;
-
-	if (_hitZoneList) {
-		for (i = 0; i < _hitZoneListCount; i++) {
-			delete _hitZoneList[i];
-		}
-
-		free(_hitZoneList);
-		_hitZoneList = NULL;
-	}
-	_hitZoneListCount = 0;
+void ObjectMap::clear() {
+	_hitZoneList.clear();
 }
 
-
 #ifdef SAGA_DEBUG
 void ObjectMap::draw(const Point& testPoint, int color, int color2) {
-	int i;
-	int hitZoneIndex;
+	uint i;
+	uint hitZoneIndex;
 	char txtBuf[32];
 	Point pickPoint;
 	Point textPoint;
@@ -260,8 +209,8 @@
 
 	hitZoneIndex = hitTest(pickPoint);
 
-	for (i = 0; i < _hitZoneListCount; i++) {
-		_hitZoneList[i]->draw(_vm, (hitZoneIndex == i) ? color2 : color);
+	for (i = 0; i < _hitZoneList.size(); i++) {
+		_hitZoneList[i].draw(_vm, (hitZoneIndex == i) ? color2 : color);
 	}
 
 	if (hitZoneIndex != -1) {
@@ -274,11 +223,11 @@
 #endif
 
 int ObjectMap::hitTest(const Point& testPoint) {
-	int i;
+	uint i;
 
 	// Loop through all scene objects
-	for (i = 0; i < _hitZoneListCount; i++) {
-		if (_hitZoneList[i]->hitTest(testPoint)) {
+	for (i = 0; i < _hitZoneList.size(); i++) {
+		if (_hitZoneList[i].hitTest(testPoint)) {
 			return i;
 		}
 	}
@@ -287,7 +236,7 @@
 }
 
 void ObjectMap::cmdInfo() {
-	_vm->_console->DebugPrintf("%d zone(s) loaded.\n\n", _hitZoneListCount);
+	_vm->_console->DebugPrintf("%d zone(s) loaded.\n\n", _hitZoneList.size());
 }
 
 } // End of namespace Saga

Modified: scummvm/trunk/engines/saga/objectmap.h
===================================================================
--- scummvm/trunk/engines/saga/objectmap.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/objectmap.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -33,14 +33,10 @@
 
 class HitZone {
 private:
-	struct ClickArea {
-		int pointsCount;
-		Point *points;
-	};
-
+	typedef Common::Array<Point> ClickArea;
+	typedef Common::Array<ClickArea> ClickAreas;
 public:
-	HitZone(MemoryReadStreamEndian *readStream, int index, int sceneNumber);
-	~HitZone();
+	void load(SagaEngine *vm, MemoryReadStreamEndian *readStream, int index, int sceneNumber);
 
 	int getNameIndex() const {
 		return _nameIndex;
@@ -76,40 +72,37 @@
 		return objectIndexToId(kGameObjectStepZone, _index);
 	}
 	bool getSpecialPoint(Point &specialPoint) const;
+#ifdef SAGA_DEBUG
 	void draw(SagaEngine *vm, int color);	// for debugging
+#endif
 	bool hitTest(const Point &testPoint);
 
 private:
 	int _flags;				// Saga::HitZoneFlags
-	int _clickAreasCount;
 	int _rightButtonVerb;
 	int _nameIndex;
 	int _scriptNumber;
 	int _index;
 
-	ClickArea *_clickAreas;
+	ClickAreas _clickAreas;
 };
 
 
 class ObjectMap {
 public:
 	ObjectMap(SagaEngine *vm) : _vm(vm) {
-		_hitZoneList = NULL;
-		_hitZoneListCount = 0;
-
 	}
-	~ObjectMap() {
-		freeMem();
-	}
 	void load(const byte *resourcePointer, size_t resourceLength);
-	void freeMem();
+	void clear();
+#ifdef SAGA_DEBUG
 	void draw(const Point& testPoint, int color, int color2);	// for debugging
+#endif
 	int hitTest(const Point& testPoint);
 	HitZone *getHitZone(int16 index) {
-		if ((index < 0) || (index >= _hitZoneListCount)) {
+		if (uint(index) >= _hitZoneList.size()) {
 			return NULL;
 		}
-		return _hitZoneList[index];
+		return &_hitZoneList[index];
 	}
 
 	void cmdInfo();
@@ -117,8 +110,7 @@
 private:
 	SagaEngine *_vm;
 
-	int _hitZoneListCount;
-	HitZone **_hitZoneList;
+	Common::Array<HitZone> _hitZoneList;
 };
 
 } // End of namespace Saga

Modified: scummvm/trunk/engines/saga/palanim.cpp
===================================================================
--- scummvm/trunk/engines/saga/palanim.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/palanim.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -35,83 +35,54 @@
 namespace Saga {
 
 PalAnim::PalAnim(SagaEngine *vm) : _vm(vm) {
-	_loaded = false;
-	_entryCount = 0;
-	_entries = NULL;
 }
 
-PalAnim::~PalAnim() {
-}
+void PalAnim::loadPalAnim(const byte *resdata, size_t resdata_len) {
 
-int PalAnim::loadPalAnim(const byte *resdata, size_t resdata_len) {
-	void *test_p;
+	clear();
 
-	uint16 i;
-
-	if (_loaded) {
-		freePalAnim();
-	}
-
 	if (resdata == NULL) {
-		return FAILURE;
+		return;
 	}
 
 	MemoryReadStreamEndian readS(resdata, resdata_len, _vm->isBigEndian());
 
 	if (_vm->getGameId() == GID_IHNM) {
-		return SUCCESS;
+		return;
 	}
 
-	_entryCount = readS.readUint16();
+	_entries.resize(readS.readUint16());
 
-	debug(3, "PalAnim::loadPalAnim(): Loading %d PALANIM entries.", _entryCount);
+	debug(3, "PalAnim::loadPalAnim(): Loading %d PALANIM entries.", _entries.size());
 
-	test_p = malloc(_entryCount * sizeof(PalanimEntry));
-	_entries = (PalanimEntry *)test_p;
+	for (Common::Array<PalanimEntry>::iterator i = _entries.begin(); i != _entries.end(); ++i) {
+		
+		i->cycle = 0;
 
-	for (i = 0; i < _entryCount; i++) {
-		int color_count;
-		int pal_count;
-		int p, c;
+		i->colors.resize(readS.readUint16());
+		debug(2, "PalAnim::loadPalAnim(): Loading %d SAGA_COLOR structures.", i->colors.size());
 
-		_entries[i].cycle = 0;
+		i->palIndex.resize(readS.readUint16());
+		debug(2, "PalAnim::loadPalAnim(): Loading %d palette indices.\n", i->palIndex.size());
 
-		color_count = readS.readUint16();
-		pal_count = readS.readUint16();
 
-		_entries[i].pal_count = pal_count;
-		_entries[i].color_count = color_count;
-
-		debug(2, "PalAnim::loadPalAnim(): Entry %d: Loading %d palette indices.\n", i, pal_count);
-
-		test_p = malloc(sizeof(char) * pal_count);
-		_entries[i].pal_index = (byte *)test_p;
-
-		debug(2, "PalAnim::loadPalAnim(): Entry %d: Loading %d SAGA_COLOR structures.", i, color_count);
-
-		test_p = malloc(sizeof(Color) * color_count);
-		_entries[i].colors = (Color *)test_p;
-
-		for (p = 0; p < pal_count; p++) {
-			_entries[i].pal_index[p] = readS.readByte();
+		for (uint j = 0; j < i->palIndex.size(); j++) {
+			i->palIndex[j] = readS.readByte();
 		}
 
-		for (c = 0; c < color_count; c++) {
-			_entries[i].colors[c].red = readS.readByte();
-			_entries[i].colors[c].green = readS.readByte();
-			_entries[i].colors[c].blue = readS.readByte();
+		for (Common::Array<Color>::iterator j = i->colors.begin(); j != i->colors.end(); ++j) {
+			j->red = readS.readByte();
+			j->green = readS.readByte();
+			j->blue = readS.readByte();
 		}
 	}
-
-	_loaded = true;
-	return SUCCESS;
 }
 
-int PalAnim::cycleStart() {
+void PalAnim::cycleStart() {
 	Event event;
 
-	if (!_loaded) {
-		return FAILURE;
+	if (_entries.empty()) {
+		return;
 	}
 
 	event.type = kEvTOneshot;
@@ -119,42 +90,40 @@
 	event.op = kEventCycleStep;
 	event.time = PALANIM_CYCLETIME;
 	_vm->_events->queue(&event);
-
-	return SUCCESS;
 }
 
-int PalAnim::cycleStep(int vectortime) {
+void PalAnim::cycleStep(int vectortime) {
 	static PalEntry pal[256];
-	uint16 pal_index;
-	uint16 col_index;
+	uint16 palIndex;
+	uint16 colIndex;
 
-	uint16 i, j;
+	uint16 j;
 	uint16 cycle;
-	uint16 cycle_limit;
+	uint16 cycleLimit;
 
 	Event event;
 
-	if (!_loaded) {
-		return FAILURE;
+	if (_entries.empty()) {
+		return;
 	}
 
 	_vm->_gfx->getCurrentPal(pal);
 
-	for (i = 0; i < _entryCount; i++) {
-		cycle = _entries[i].cycle;
-		cycle_limit = _entries[i].color_count;
-		for (j = 0; j < _entries[i].pal_count; j++) {
-			pal_index = (unsigned char)_entries[i].pal_index[j];
-			col_index = (cycle + j) % cycle_limit;
-			pal[pal_index].red = (byte) _entries[i].colors[col_index].red;
-			pal[pal_index].green = (byte) _entries[i].colors[col_index].green;
-			pal[pal_index].blue = (byte) _entries[i].colors[col_index].blue;
+	for (Common::Array<PalanimEntry>::iterator i = _entries.begin(); i != _entries.end(); ++i) {
+		cycle = i->cycle;
+		cycleLimit = i->colors.size();
+		for (j = 0; j < i->palIndex.size(); j++) {
+			palIndex = i->palIndex[j];
+			colIndex = (cycle + j) % cycleLimit;
+			pal[palIndex].red = (byte) i->colors[colIndex].red;
+			pal[palIndex].green = (byte) i->colors[colIndex].green;
+			pal[palIndex].blue = (byte) i->colors[colIndex].blue;
 		}
 
-		_entries[i].cycle++;
+		i->cycle++;
 
-		if (_entries[i].cycle == cycle_limit) {
-			_entries[i].cycle = 0;
+		if (i->cycle == cycleLimit) {
+			i->cycle = 0;
 		}
 	}
 
@@ -169,30 +138,12 @@
 	event.time = vectortime + PALANIM_CYCLETIME;
 	_vm->_events->queue(&event);
 
-	return SUCCESS;
 }
 
-int PalAnim::freePalAnim() {
-	uint16 i;
-
-	if (!_loaded) {
-		return FAILURE;
-	}
-
-	for (i = 0; i < _entryCount; i++) {
-		debug(2, "PalAnim::freePalAnim(): Entry %d: Freeing colors.", i);
-		free(_entries[i].colors);
-		debug(2, "PalAnim::freePalAnim(): Entry %d: Freeing indices.", i);
-		free(_entries[i].pal_index);
-	}
-
-	debug(3, "PalAnim::freePalAnim(): Freeing entries.");
-
-	free(_entries);
-
-	_loaded = false;
-
-	return SUCCESS;
+void PalAnim::clear() {
+	debug(3, "PalAnim::clear()");
+	
+	_entries.clear();
 }
 
 } // End of namespace Saga

Modified: scummvm/trunk/engines/saga/palanim.h
===================================================================
--- scummvm/trunk/engines/saga/palanim.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/palanim.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -33,29 +33,24 @@
 #define PALANIM_CYCLETIME 100
 
 struct PalanimEntry {
-	uint16 pal_count;
-	uint16 color_count;
 	uint16 cycle;
-	byte *pal_index;
-	Color *colors;
+	ByteArray palIndex;
+	Common::Array<Color> colors;
 };
 
 class PalAnim {
  public:
 	PalAnim(SagaEngine *vm);
-	~PalAnim();
 
-	int loadPalAnim(const byte *, size_t);
-	int cycleStart();
-	int cycleStep(int vectortime);
-	int freePalAnim();
+	void loadPalAnim(const byte *, size_t);
+	void cycleStart();
+	void cycleStep(int vectortime);
+	void clear();
 
  private:
 	SagaEngine *_vm;
 
-	bool _loaded;
-	uint16 _entryCount;
-	PalanimEntry *_entries;
+	Common::Array<PalanimEntry> _entries;
 };
 
 } // End of namespace Saga

Modified: scummvm/trunk/engines/saga/resource.cpp
===================================================================
--- scummvm/trunk/engines/saga/resource.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/resource.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -43,8 +43,7 @@
 	size_t i;
 	bool result;
 	byte tableInfo[RSC_TABLEINFO_SIZE];
-	byte *tableBuffer;
-	size_t tableSize;
+	ByteArray tableBuffer;
 	uint32 count;
 	uint32 resourceTableOffset;
 	ResourceData *resourceData;
@@ -70,17 +69,15 @@
 	}
 
 	// Load resource table
-	tableSize = RSC_TABLEENTRY_SIZE * count;
+	tableBuffer.resize(RSC_TABLEENTRY_SIZE * count);
 
-	tableBuffer = (byte *)malloc(tableSize);
-
 	_file.seek(resourceTableOffset + contextOffset, SEEK_SET);
 
-	result = (_file.read(tableBuffer, tableSize) == tableSize);
+	result = (_file.read(tableBuffer.getBuffer(), tableBuffer.size()) == tableBuffer.size());
 	if (result) {
 		_table.resize(count);
 
-		MemoryReadStreamEndian readS1(tableBuffer, tableSize, _isBigEndian);
+		MemoryReadStreamEndian readS1(tableBuffer.getBuffer(), tableBuffer.size(), _isBigEndian);
 
 		for (i = 0; i < count; i++) {
 			resourceData = &_table[i];
@@ -94,7 +91,6 @@
 		}
 	}
 
-	free(tableBuffer);
 	return result;
 }
 
@@ -375,7 +371,6 @@
 	uint32 resourceOffset;
 	ResourceData *resourceData;
 
-	debug(8, "loadResource %d", resourceId);
 
 	resourceData = context->getResourceData(resourceId);
 
@@ -384,6 +379,8 @@
 	resourceOffset = resourceData->offset;
 	resourceSize = resourceData->size;
 
+	debug(8, "loadResource %d 0x%X:0x%X", resourceId, resourceOffset, resourceSize);
+
 	resourceBuffer = (byte*)malloc(resourceSize);
 
 	file->seek((long)resourceOffset, SEEK_SET);

Modified: scummvm/trunk/engines/saga/resource_res.cpp
===================================================================
--- scummvm/trunk/engines/saga/resource_res.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/resource_res.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -54,7 +54,7 @@
 
 	ResourceContext *resourceContext;
 	ResourceContext *soundContext;
-	int i;
+	uint i;
 
 	resourceContext = _vm->_resource->getContext(GAME_RESOURCEFILE);
 	if (resourceContext == NULL) {
@@ -114,7 +114,7 @@
 	_vm->loadStrings(_vm->_actor->_objectsStrings, resourcePointer, resourceLength);
 	free(resourcePointer);
 
-	if (chapter >= _vm->_sndRes->_fxTableIDsLen) {
+	if (uint(chapter) >= _vm->_sndRes->_fxTableIDs.size()) {
 		error("Chapter ID exceeds fxTableIDs length");
 	}
 
@@ -126,14 +126,11 @@
 		error("Resource::loadGlobalResources Can't load sound effects for current track");
 	}
 
-	free(_vm->_sndRes->_fxTable);
-
-	_vm->_sndRes->_fxTableLen = resourceLength / 4;
-	_vm->_sndRes->_fxTable = (FxTable *)malloc(sizeof(FxTable) * _vm->_sndRes->_fxTableLen);
-
+	_vm->_sndRes->_fxTable.resize(resourceLength / 4);
+	
 	MemoryReadStream fxS(resourcePointer, resourceLength);
 
-	for (i = 0; i < _vm->_sndRes->_fxTableLen; i++) {
+	for (i = 0; i < _vm->_sndRes->_fxTable.size(); i++) {
 		_vm->_sndRes->_fxTable[i].res = fxS.readSint16LE();
 		_vm->_sndRes->_fxTable[i].vol = fxS.readSint16LE();
 	}
@@ -177,14 +174,11 @@
 			error("Resource::loadGlobalResources Can't load songs list for current track");
 		}
 
-		free(_vm->_music->_songTable);
+		_vm->_music->_songTable.resize(resourceLength / 4);
 
-		_vm->_music->_songTableLen = resourceLength / 4;
-		_vm->_music->_songTable = (int32 *)malloc(sizeof(int32) * _vm->_music->_songTableLen);
-
 		MemoryReadStream songS(resourcePointer, resourceLength);
 
-		for (i = 0; i < _vm->_music->_songTableLen; i++)
+		for (i = 0; i < _vm->_music->_songTable.size(); i++)
 			_vm->_music->_songTable[i] = songS.readSint32LE();
 		free(resourcePointer);
 	} else {

Modified: scummvm/trunk/engines/saga/saga.cpp
===================================================================
--- scummvm/trunk/engines/saga/saga.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/saga.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -291,7 +291,7 @@
 	_sound = new Sound(this, _mixer);
 
 	if (!isSaga2()) {
-		_interface->converseInit();
+		_interface->converseClear();
 		_script->setVerb(_script->getVerbType(kVerbWalkTo));
 	}
 

Modified: scummvm/trunk/engines/saga/saga.h
===================================================================
--- scummvm/trunk/engines/saga/saga.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/saga.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -86,7 +86,7 @@
 using Common::MemoryReadStream;
 using Common::MemoryReadStreamEndian;
 
-//#define SAGA_DEBUG 1		// define for test functions
+// #define SAGA_DEBUG 1		// define for test functions
 #define SAGA_IMAGE_DATA_OFFSET 776
 #define SAGA_IMAGE_HEADER_LEN  8
 
@@ -458,6 +458,14 @@
 	return (type << OBJECT_TYPE_SHIFT) | (OBJECT_TYPE_MASK & index);
 }
 
+class ByteArray : public Common::Array<byte> {
+public:
+
+	byte * getBuffer() { // call this method instead of  "&front()" if you insure of array emptyness state
+		return empty() ? NULL : &front();
+	}
+};
+
 class SagaEngine : public Engine {
 	friend class Scene;
 
@@ -537,7 +545,7 @@
 	Common::RandomSource _rnd;
 
 private:
-	int decodeBGImageRLE(const byte *inbuf, size_t inbuf_len, byte *outbuf, size_t outbuf_len);
+	int decodeBGImageRLE(const byte *inbuf, size_t inbuf_len, ByteArray &outbuf);
 	int flipImage(byte *img_buf, int columns, int scanlines);
 	int unbankBGImage(byte *dest_buf, const byte *src_buf, int columns, int scanlines);
 	uint32 _previousTicks;

Modified: scummvm/trunk/engines/saga/saveload.cpp
===================================================================
--- scummvm/trunk/engines/saga/saveload.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/saveload.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -242,7 +242,7 @@
 
 	out->writeSint16LE(_script->_commonBuffer.size());
 
-	out->write(&_script->_commonBuffer.front(), _script->_commonBuffer.size());
+	out->write(_script->_commonBuffer.getBuffer(), _script->_commonBuffer.size());
 
 	// ISO map x, y coordinates for ITE
 	if (getGameId() == GID_ITE) {
@@ -282,7 +282,7 @@
 		_saveHeader.version = SWAP_BYTES_32(_saveHeader.version);
 	}
 
-	debug(2, "Save version: %x", _saveHeader.version);
+	debug(2, "Save version: 0x%X", _saveHeader.version);
 
 	if (_saveHeader.version < 4)
 		warning("This savegame is not endian-safe. There may be problems");
@@ -352,7 +352,7 @@
 
 	commonBufferSize = in->readSint16LE();
 	_script->_commonBuffer.resize(commonBufferSize);
-	in->read(&_script->_commonBuffer.front(), commonBufferSize);
+	in->read(_script->_commonBuffer.getBuffer(), commonBufferSize);
 
 	if (getGameId() == GID_ITE) {
 		mapx = in->readSint16LE();

Modified: scummvm/trunk/engines/saga/scene.cpp
===================================================================
--- scummvm/trunk/engines/saga/scene.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/scene.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -140,7 +140,7 @@
 	byte *sceneLUTPointer;
 	size_t sceneLUTLength;
 	uint32 resourceId;
-	int i;
+	uint i;
 
 	// Do nothing for SAGA2 games for now
 	if (_vm->isSaga2()) {
@@ -162,15 +162,11 @@
 	if (sceneLUTLength == 0) {
 		error("Scene::Scene() sceneLUTLength == 0");
 	}
-	_sceneCount = sceneLUTLength / 2;
-	_sceneLUT = (int *)malloc(_sceneCount * sizeof(*_sceneLUT));
-	if (_sceneLUT == NULL) {
-		memoryError("Scene::Scene()");
-	}
+	_sceneLUT.resize(sceneLUTLength / 2);
 
 	MemoryReadStreamEndian readS(sceneLUTPointer, sceneLUTLength, _sceneContext->isBigEndian());
 
-	for (i = 0; i < _sceneCount; i++) {
+	for (i = 0; i < _sceneLUT.size(); i++) {
 		_sceneLUT[i] = readS.readUint16();
 		debug(8, "sceneNumber %i has resourceId %i", i, _sceneLUT[i]);
 	}
@@ -190,7 +186,7 @@
 
 		getResourceTypes(types, typesCount);
 
-		for (i = 0; i < _sceneCount; i++) {
+		for (i = 0; i < _sceneLUT.size(); i++) {
 			gDebugLevel = -1;
 			loadSceneDescriptor(_sceneLUT[i]);
 			loadSceneResourceList(_sceneDescription.resourceListResourceId);
@@ -210,7 +206,7 @@
 	}
 #endif
 
-	debug(3, "LUT has %d entries.", _sceneCount);
+	debug(3, "LUT has %d entries.", _sceneLUT.size());
 
 	_sceneLoaded = false;
 	_sceneNumber = 0;
@@ -236,7 +232,6 @@
 
 	delete _actionMap;
 	delete _objectMap;
-	free(_sceneLUT);
 }
 
 void Scene::getResourceTypes(SAGAResourceTypes *&types, int &typesCount) {
@@ -623,7 +618,7 @@
 		if (loadSceneParams.chapter == 6 || loadSceneParams.chapter == 8)
 			_vm->_interface->setLeftPortrait(0);
 
-		_vm->_anim->freeCutawayList();
+		_vm->_anim->clearCutawayList();
 		_vm->_script->clearModules();
 
 		// deleteAllScenes();
@@ -1244,15 +1239,15 @@
 	// Free animation info list
 	_vm->_anim->reset();
 
-	_vm->_palanim->freePalAnim();
+	_vm->_palanim->clear();
 
-	_objectMap->freeMem();
-	_actionMap->freeMem();
-	_entryList.freeMem();
+	_objectMap->clear();
+	_actionMap->clear();
+	_entryList.clear();
 	_sceneStrings.clear();
 
 	if (_vm->getGameId() == GID_ITE)
-		_vm->_isoMap->freeMem();
+		_vm->_isoMap->clear();
 
 	_vm->_events->clearList();
 	_textList.clear();
@@ -1285,7 +1280,7 @@
 
 	scene_num = atoi(argv[1]);
 
-	if ((scene_num < 1) || (scene_num >= _sceneCount)) {
+	if ((scene_num < 1) || (uint(scene_num) >= _sceneLUT.size())) {
 		_vm->_console->DebugPrintf("Invalid scene number.\n");
 		return;
 	}
@@ -1304,27 +1299,22 @@
 }
 
 void Scene::loadSceneEntryList(const byte* resourcePointer, size_t resourceLength) {
-	int i;
+	uint i;
 
-	_entryList.entryListCount = resourceLength / 8;
+	if (!_entryList.empty()) {
+		error("Scene::loadSceneEntryList entryList not empty");
+	}
 
+	_entryList.resize(resourceLength / 8);
+
 	MemoryReadStreamEndian readS(resourcePointer, resourceLength, _sceneContext->isBigEndian());
 
-
-	if (_entryList.entryList)
-		error("Scene::loadSceneEntryList entryList != NULL");
-
-	_entryList.entryList = (SceneEntry *) malloc(_entryList.entryListCount * sizeof(*_entryList.entryList));
-	if (_entryList.entryList == NULL) {
-		memoryError("Scene::loadSceneEntryList");
+	for (i = 0; i < _entryList.size(); i++) {
+		_entryList[i].location.x = readS.readSint16();
+		_entryList[i].location.y = readS.readSint16();
+		_entryList[i].location.z = readS.readSint16();
+		_entryList[i].facing = readS.readUint16();
 	}
-
-	for (i = 0; i < _entryList.entryListCount; i++) {
-		_entryList.entryList[i].location.x = readS.readSint16();
-		_entryList.entryList[i].location.y = readS.readSint16();
-		_entryList.entryList[i].location.z = readS.readSint16();
-		_entryList.entryList[i].facing = readS.readUint16();
-	}
 }
 
 void Scene::clearPlacard() {

Modified: scummvm/trunk/engines/saga/scene.h
===================================================================
--- scummvm/trunk/engines/saga/scene.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/scene.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -134,29 +134,10 @@
 
 struct SceneEntry {
 	Location location;
-	int facing;
+	uint16 facing;
 };
 
-struct SceneEntryList {
-	SceneEntry *entryList;
-	int entryListCount;
-
-	const SceneEntry * getEntry(int index) {
-		if ((index < 0) || (index >= entryListCount)) {
-			error("SceneEntryList::getEntry wrong index (%d)", index);
-		}
-		return &entryList[index];
-	}
-	void freeMem() {
-		free(entryList);
-		memset(this, 0, sizeof(*this));
-	}
-	SceneEntryList() {
-		memset(this, 0, sizeof(*this));
-	}
-	~SceneEntryList() {
-		freeMem();
-	}
+class SceneEntryList : public Common::Array<SceneEntry> {
 };
 
 struct SceneImage {
@@ -325,7 +306,7 @@
 
 	bool isSceneLoaded() const { return _sceneLoaded; }
 
-	int getSceneResourceId(int sceneNumber) {
+	uint16 getSceneResourceId(int sceneNumber) {
 	#ifdef SCENE_DEBUG
 		if ((sceneNumber < 0) || (sceneNumber >= _sceneCount)) {
 			error("getSceneResourceId: wrong sceneNumber %i", sceneNumber);
@@ -385,8 +366,7 @@
 	SagaEngine *_vm;
 
 	ResourceContext *_sceneContext;
-	int *_sceneLUT;
-	int _sceneCount;
+	Common::Array<uint16> _sceneLUT;
 	SceneQueueList _sceneQueue;
 	bool _sceneLoaded;
 	int _currentProtag;

Modified: scummvm/trunk/engines/saga/script.cpp
===================================================================
--- scummvm/trunk/engines/saga/script.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/script.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -1112,9 +1112,9 @@
 
 	module.moduleBase.resize(resourceLength);
 	
-	memcpy(&module.moduleBase.front(), resourcePointer, resourceLength);
+	memcpy(module.moduleBase.getBuffer(), resourcePointer, resourceLength);
 
-	MemoryReadStreamEndian scriptS(&module.moduleBase.front(), module.moduleBase.size(), _scriptContext->isBigEndian());
+	MemoryReadStreamEndian scriptS(module.moduleBase.getBuffer(), module.moduleBase.size(), _scriptContext->isBigEndian());
 
 	uint entryPointsCount = scriptS.readUint16();
 	scriptS.readUint16(); //skip

Modified: scummvm/trunk/engines/saga/script.h
===================================================================
--- scummvm/trunk/engines/saga/script.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/script.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -138,7 +138,7 @@
 	int stringsResourceId;
 	int voicesResourceId;
 
-	Common::Array<byte> moduleBase;	// all base module
+	ByteArray moduleBase;	// all base module
 	uint16 staticSize;				// size of static data
 	uint staticOffset;				// offset of static data begining in _commonBuffer
 	Common::Array<EntryPoint> entryPoints;
@@ -354,7 +354,7 @@
 	TextListEntry *_placardTextEntry;
 
 	friend class SagaEngine;
-	Common::Array<byte> _commonBuffer;
+	ByteArray _commonBuffer;
 
 	uint _staticSize;
 	ScriptThreadList _threadList;

Modified: scummvm/trunk/engines/saga/sfuncs.cpp
===================================================================
--- scummvm/trunk/engines/saga/sfuncs.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/sfuncs.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -953,7 +953,7 @@
 	int frameOffset = thread->pop();
 	ActorFrameRange *frameRange;
 
-	debug(1, "sfPlaceActor(id = 0x%x, x=%d, y=%d, dir=%d, frameType=%d, frameOffset=%d)", actorId, actor->_location.x,
+	debug(1, "sfPlaceActor(id = 0x%X, x=%d, y=%d, dir=%d, frameType=%d, frameOffset=%d)", actorId, actor->_location.x,
 		  actor->_location.y, actor->_facingDirection, frameType, frameOffset);
 
 	if (frameType >= 0) {
@@ -1349,8 +1349,8 @@
 			return;
 		}
 
-		if (param1 >= _vm->_music->_songTableLen) {
-			warning("sfPlayMusic: Wrong song number (%d > %d)", param1, _vm->_music->_songTableLen - 1);
+		if (uint(param1) >= _vm->_music->_songTable.size()) {
+			warning("sfPlayMusic: Wrong song number (%d > %d)", param1, _vm->_music->_songTable.size() - 1);
 		} else {
 			_vm->_music->setVolume(_vm->_musicVolume, 1);
 			_vm->_music->play(_vm->_music->_songTable[param1], param2 ? MUSIC_LOOP : MUSIC_NORMAL);
@@ -1440,7 +1440,7 @@
 	int16 param = thread->pop();
 	int res;
 
-	if (param >= 0 && param < _vm->_sndRes->_fxTableLen) {
+	if (uint(param) < _vm->_sndRes->_fxTable.size()) {
 		res = _vm->_sndRes->_fxTable[param].res;
 		if (_vm->getGameId() == GID_ITE && !(_vm->getFeatures() & GF_ITE_FLOPPY))
 			res -= 14;
@@ -1455,7 +1455,7 @@
 	int16 param = thread->pop();
 	int res;
 
-	if (param >= 0 && param < _vm->_sndRes->_fxTableLen) {
+	if (uint(param) < _vm->_sndRes->_fxTable.size()) {
 		res = _vm->_sndRes->_fxTable[param].res;
 		if (_vm->getGameId() == GID_ITE && !(_vm->getFeatures() & GF_ITE_FLOPPY))
 			res -= 14;

Modified: scummvm/trunk/engines/saga/sfuncs_ihnm.cpp
===================================================================
--- scummvm/trunk/engines/saga/sfuncs_ihnm.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/sfuncs_ihnm.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -413,8 +413,8 @@
 		return;
 	}
 
-	if (param1 >= _vm->_music->_songTableLen) {
-		warning("sfQueueMusic: Wrong song number (%d > %d)", param1, _vm->_music->_songTableLen - 1);
+	if (uint(param1) >= _vm->_music->_songTable.size()) {
+		warning("sfQueueMusic: Wrong song number (%d > %d)", param1, _vm->_music->_songTable.size() - 1);
 	} else {
 		_vm->_music->setVolume(_vm->_musicVolume, 1);
 		event.type = kEvTOneshot;

Modified: scummvm/trunk/engines/saga/sndres.cpp
===================================================================
--- scummvm/trunk/engines/saga/sndres.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/sndres.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -60,8 +60,11 @@
 	setVoiceBank(0);
 
 	if (_vm->getGameId() == GID_ITE) {
-		_fxTable = ITE_SfxTable;
-		_fxTableLen = ITE_SFXCOUNT;
+		_fxTable.resize(ITE_SFXCOUNT);
+		for (uint i = 0; i < _fxTable.size(); i++) {
+			_fxTable[i].res = ITE_SfxTable[i].res;
+			_fxTable[i].vol = ITE_SfxTable[i].vol;
+		}
 #ifdef ENABLE_IHNM
 	} else if (_vm->getGameId() == GID_IHNM) {
 		ResourceContext *resourceContext;
@@ -86,17 +89,14 @@
 			error("Sndres::SndRes can't read SfxIDs table");
 		}
 
-		_fxTableIDsLen = resourceLength / 2;
-		_fxTableIDs = (int16 *)malloc(_fxTableIDsLen * sizeof(int16));
+		_fxTableIDs.resize(resourceLength / 2);
 
 		MemoryReadStream metaS(resourcePointer, resourceLength);
-		for (int i = 0; i < _fxTableIDsLen; i++)
+		for (uint i = 0; i < _fxTableIDs.size(); i++) {
 			_fxTableIDs[i] = metaS.readSint16LE();
+		}
 
 		free(resourcePointer);
-
-		_fxTable = 0;
-		_fxTableLen = 0;
 #endif
 #ifdef ENABLE_SAGA2
 	} else if (_vm->getGameId() == GID_DINO) {
@@ -108,12 +108,6 @@
 }
 
 SndRes::~SndRes() {
-#ifdef ENABLE_IHNM
-	if (_vm->getGameId() == GID_IHNM) {
-		free(_fxTable);
-		free(_fxTableIDs);
-	}
-#endif
 }
 
 void SndRes::setVoiceBank(int serial) {

Modified: scummvm/trunk/engines/saga/sndres.h
===================================================================
--- scummvm/trunk/engines/saga/sndres.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/sndres.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -33,6 +33,11 @@
 
 namespace Saga {
 
+struct FxTable {
+	int16 res;
+	int16 vol;
+};
+
 class SndRes {
 public:
 
@@ -44,11 +49,9 @@
 	int getVoiceLength(uint32 resourceId);
 	void setVoiceBank(int serial);
 
-	FxTable *_fxTable;
-	int _fxTableLen;
+	Common::Array<FxTable> _fxTable;
 
-	int16 *_fxTableIDs;
-	int _fxTableIDsLen;
+	Common::Array<int16> _fxTableIDs;
 
  private:
 	bool load(ResourceContext *context, uint32 resourceId, SoundBuffer &buffer, bool onlyHeader);

Modified: scummvm/trunk/engines/saga/sprite.cpp
===================================================================
--- scummvm/trunk/engines/saga/sprite.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/sprite.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -148,7 +148,7 @@
 		spriteInfo->decodedBuffer.resize(outputLength);
 		if (outputLength > 0) {
 			decodeRLEBuffer(spriteDataPointer, inputLength, outputLength);
-			byte *dst = spriteInfo->getBuffer();
+			byte *dst = &spriteInfo->decodedBuffer.front();
 #ifdef ENABLE_IHNM
 			// IHNM sprites are upside-down, for reasons which i can only
 			// assume are perverse. To simplify things, flip them now. Not
@@ -183,13 +183,13 @@
 	spriteInfo = &spriteList[spriteNumber];
 
 	if (scale < 256) {
-		xAlign = (spriteInfo->xAlign * scale) >> 8;
-		yAlign = (spriteInfo->yAlign * scale) >> 8;
+		xAlign = (spriteInfo->xAlign * scale) >> 8; //TODO: do we need to take in account sprite x&y aligns ?
+		yAlign = (spriteInfo->yAlign * scale) >> 8; // ????
 		height = (spriteInfo->height * scale + 0x7f) >> 8;
 		width = (spriteInfo->width * scale + 0x7f) >> 8;
 		size_t outLength = width * height;
 		if (outLength > 0) {
-			scaleBuffer(spriteInfo->getBuffer(), spriteInfo->width, spriteInfo->height, scale, outLength);
+			scaleBuffer(&spriteInfo->decodedBuffer.front(), spriteInfo->width, spriteInfo->height, scale, outLength);
 			buffer = &_decodeBuf.front();
 		} else {
 			buffer = NULL;
@@ -199,7 +199,7 @@
 		yAlign = spriteInfo->yAlign;
 		height = spriteInfo->height;
 		width = spriteInfo->width;
-		buffer = spriteInfo->getBuffer();
+		buffer = spriteInfo->decodedBuffer.getBuffer();
 	}
 }
 

Modified: scummvm/trunk/engines/saga/sprite.h
===================================================================
--- scummvm/trunk/engines/saga/sprite.h	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/sprite.h	2010-10-22 23:13:17 UTC (rev 53719)
@@ -34,20 +34,12 @@
 #define SPRITE_ZMASK 0x0F
 
 struct SpriteInfo {
-	Common::Array<byte> decodedBuffer;
+	ByteArray decodedBuffer;
 	int width;
 	int height;
 	int xAlign;
 	int yAlign;
 
-	byte * getBuffer() {
-		if (decodedBuffer.empty()) {
-			return NULL;
-		} else {
-			return &decodedBuffer.front();
-		}
-	}
-
 	SpriteInfo() : width(0), height(0), xAlign(0), yAlign(0) {
 	}
 };
@@ -88,7 +80,7 @@
 
 	SagaEngine *_vm;
 	ResourceContext *_spriteContext;
-	Common::Array<byte> _decodeBuf;
+	ByteArray _decodeBuf;
 };
 
 } // End of namespace Saga

Modified: scummvm/trunk/engines/saga/sthread.cpp
===================================================================
--- scummvm/trunk/engines/saga/sthread.cpp	2010-10-22 22:41:37 UTC (rev 53718)
+++ scummvm/trunk/engines/saga/sthread.cpp	2010-10-22 23:13:17 UTC (rev 53719)
@@ -47,9 +47,9 @@
 	_threadList.push_front(tmp);
 	ScriptThread &newThread = _threadList.front();
 	newThread._instructionOffset = _modules[scriptModuleNumber].entryPoints[scriptEntryPointNumber].offset;
-	newThread._commonBase = &_commonBuffer.front();
-	newThread._staticBase = &_commonBuffer.front() + _modules[scriptModuleNumber].staticOffset;
-	newThread._moduleBase = &_modules[scriptModuleNumber].moduleBase.front();
+	newThread._commonBase = _commonBuffer.getBuffer();
+	newThread._staticBase = _commonBuffer.getBuffer() + _modules[scriptModuleNumber].staticOffset;
+	newThread._moduleBase = _modules[scriptModuleNumber].moduleBase.getBuffer();
 	newThread._moduleBaseSize = _modules[scriptModuleNumber].moduleBase.size();
 	newThread._strings = &_modules[scriptModuleNumber].strings;
 
@@ -209,7 +209,7 @@
 		savedInstructionOffset = thread._instructionOffset;
 		operandChar = scriptS.readByte();
 
-		debug(8, "Executing thread offset: %u (%x) stack: %d", thread._instructionOffset, operandChar, thread.pushedSize());
+		debug(8, "Executing thread offset: %u (0x%X) stack: %d", thread._instructionOffset, operandChar, thread.pushedSize());
 
 		stopParsing = false;
 		debug(4, "Calling op %s", this->_scriptOpsList[operandChar].scriptOpName);


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list