[Scummvm-cvs-logs] scummvm master -> 90ac7a2bcb620eb29194eb94458f45d48e794c59

wjp wjp at usecode.org
Sat Dec 26 21:59:50 CET 2015


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
90ac7a2bcb LAB: Clean up Image._imageData memory handling


Commit: 90ac7a2bcb620eb29194eb94458f45d48e794c59
    https://github.com/scummvm/scummvm/commit/90ac7a2bcb620eb29194eb94458f45d48e794c59
Author: Willem Jan Palenstijn (wjp at usecode.org)
Date: 2015-12-26T21:59:10+01:00

Commit Message:
LAB: Clean up Image._imageData memory handling

Changed paths:
    engines/lab/dispman.cpp
    engines/lab/image.cpp
    engines/lab/image.h
    engines/lab/special.cpp
    engines/lab/tilepuzzle.cpp



diff --git a/engines/lab/dispman.cpp b/engines/lab/dispman.cpp
index fb25472..a332a57 100644
--- a/engines/lab/dispman.cpp
+++ b/engines/lab/dispman.cpp
@@ -782,12 +782,12 @@ void DisplayMan::doTransWipe(const Common::String filename) {
 	Image imgSource(_vm);
 	imgSource._width = _screenWidth;
 	imgSource._height = lastY;
-	imgSource._imageData = bitMapBuffer;
+	imgSource.setData(bitMapBuffer, true);
 
 	Image imgDest(_vm);
 	imgDest._width = _screenWidth;
 	imgDest._height = _screenHeight;
-	imgDest._imageData = getCurrentDrawingBuffer();
+	imgDest.setData(getCurrentDrawingBuffer(), false);
 
 	for (int j = 0; j < 2; j++) {
 		for (int i = 0; i < 2; i++) {
@@ -800,7 +800,7 @@ void DisplayMan::doTransWipe(const Common::String filename) {
 					linesDone = 0;
 				}
 
-				imgDest._imageData = getCurrentDrawingBuffer();
+				imgDest.setData(getCurrentDrawingBuffer(), false);
 
 				if (j == 0) {
 					imgSource.blitBitmap(0, curY, &imgDest, 0, curY, _screenWidth, 2, false);
@@ -815,9 +815,6 @@ void DisplayMan::doTransWipe(const Common::String filename) {
 		}	// for i
 	}	// for j
 
-	// Prevent the Image destructor from deleting the drawing buffer
-	imgDest._imageData = nullptr;
-
 	// bitMapBuffer will be deleted by the Image destructor
 }
 
@@ -872,7 +869,7 @@ void DisplayMan::blackAllScreen() {
 
 void DisplayMan::scrollDisplayX(int16 dx, uint16 x1, uint16 y1, uint16 x2, uint16 y2, byte *buffer) {
 	Image img(_vm);
-	img._imageData = buffer;
+	img.setData(buffer, false);
 
 	if (x1 > x2)
 		SWAP<uint16>(x1, x2);
@@ -897,14 +894,11 @@ void DisplayMan::scrollDisplayX(int16 dx, uint16 x1, uint16 y1, uint16 x2, uint1
 
 		rectFill(x2 + dx + 1, y1, x2, y2, 0);
 	}
-
-	// Prevent the Image destructor from deleting the external buffer
-	img._imageData = nullptr;
 }
 
 void DisplayMan::scrollDisplayY(int16 dy, uint16 x1, uint16 y1, uint16 x2, uint16 y2, byte *buffer) {
 	Image img(_vm);
-	img._imageData = buffer;
+	img.setData(buffer, false);
 
 	if (x1 > x2)
 		SWAP<uint16>(x1, x2);
@@ -929,9 +923,6 @@ void DisplayMan::scrollDisplayY(int16 dy, uint16 x1, uint16 y1, uint16 x2, uint1
 
 		rectFill(x1, y2 + dy + 1, x2, y2, 0);
 	}
-
-	// Prevent the Image destructor from deleting the external buffer
-	img._imageData = nullptr;
 }
 
 uint16 DisplayMan::fadeNumIn(uint16 num, uint16 res, uint16 counter) {
diff --git a/engines/lab/image.cpp b/engines/lab/image.cpp
index 08c4f6c..ec51671 100644
--- a/engines/lab/image.cpp
+++ b/engines/lab/image.cpp
@@ -48,10 +48,19 @@ Image::Image(Common::File *s, LabEngine *vm) : _vm(vm) {
 
 	_imageData = new byte[size];
 	s->read(_imageData, size);
+	_autoFree = true;
 }
 
 Image::~Image() {
-	delete[] _imageData;
+	if (_autoFree)
+		delete[] _imageData;
+}
+
+void Image::setData(byte *d, bool autoFree) {
+	if (_autoFree)
+		delete[] _imageData;
+	_imageData = d;
+	_autoFree = autoFree;
 }
 
 void Image::blitBitmap(uint16 srcX, uint16 srcY, Image *imgDest,
diff --git a/engines/lab/image.h b/engines/lab/image.h
index eaad751..0f985e0 100644
--- a/engines/lab/image.h
+++ b/engines/lab/image.h
@@ -47,11 +47,13 @@ public:
 	uint16 _height;
 	byte *_imageData;
 
-	Image(LabEngine *vm) : _width(0), _height(0), _imageData(nullptr), _vm(vm) {}
-	Image(int w, int h, byte *d, LabEngine *vm) : _width(w), _height(h), _imageData(d), _vm(vm) {}
+	Image(LabEngine *vm) : _width(0), _height(0), _imageData(nullptr), _vm(vm), _autoFree(true) {}
+	Image(int w, int h, byte *d, LabEngine *vm, bool autoFree = true) : _width(w), _height(h), _imageData(d), _vm(vm), _autoFree(autoFree) {}
 	Image(Common::File *s, LabEngine *vm);
 	~Image();
 
+	void setData(byte *d, bool autoFree = true);
+
 	/**
 	 * Draws an image to the screen.
 	 */
@@ -71,6 +73,9 @@ public:
 	 * Blits a piece of one image to another.
 	 */
 	void blitBitmap(uint16 srcX, uint16 srcY, Image *imgDest, uint16 destX, uint16 destY, uint16 width, uint16 height, byte masked);
+
+private:
+	bool _autoFree; ///< Free _imageData in destructor?
 };
 
 } // End of namespace Lab
diff --git a/engines/lab/special.cpp b/engines/lab/special.cpp
index e99c4df..62eeb75 100644
--- a/engines/lab/special.cpp
+++ b/engines/lab/special.cpp
@@ -134,7 +134,7 @@ void LabEngine::loadJournalData() {
 	delete journalFile;
 
 	_anim->_noPalChange = true;
-	_journalBackImage->_imageData = new byte[_graphics->_screenBytesPerPage];
+	_journalBackImage->setData(new byte[_graphics->_screenBytesPerPage]);
 	_graphics->readPict("P:Journal.pic", true, false, _journalBackImage->_imageData);
 	_anim->_noPalChange = false;
 
@@ -263,7 +263,7 @@ void LabEngine::doJournal() {
 
 	_journalBackImage->_width = _graphics->_screenWidth;
 	_journalBackImage->_height = _graphics->_screenHeight;
-	_journalBackImage->_imageData = nullptr;
+	_journalBackImage->setData(nullptr, true);
 
 	updateMusicAndEvents();
 	loadJournalData();
@@ -276,8 +276,8 @@ void LabEngine::doJournal() {
 	_event->mouseHide();
 
 	delete[] _blankJournal;
-	delete[] _journalBackImage->_imageData;
-	_blankJournal = _journalBackImage->_imageData = nullptr;
+	_blankJournal = nullptr;
+	_journalBackImage->setData(nullptr, true);
 
 	_event->freeButtonList(&_journalButtonList);
 	_graphics->freeFont(&_journalFont);
diff --git a/engines/lab/tilepuzzle.cpp b/engines/lab/tilepuzzle.cpp
index 8f21cee..792989a 100644
--- a/engines/lab/tilepuzzle.cpp
+++ b/engines/lab/tilepuzzle.cpp
@@ -308,7 +308,7 @@ void TilePuzzle::changeCombination(uint16 number) {
 
 	uint16 combnum = _combination[number];
 
-	display._imageData = _vm->_graphics->getCurrentDrawingBuffer();
+	display.setData(_vm->_graphics->getCurrentDrawingBuffer(), false);
 	display._width     = _vm->_graphics->_screenWidth;
 	display._height    = _vm->_graphics->_screenHeight;
 
@@ -321,14 +321,11 @@ void TilePuzzle::changeCombination(uint16 number) {
 		} else
 			_vm->waitTOF();
 
-		display._imageData = _vm->_graphics->getCurrentDrawingBuffer();
+		display.setData(_vm->_graphics->getCurrentDrawingBuffer(), false);
 		_vm->_graphics->scrollDisplayY(2, _vm->_utils->vgaScaleX(COMBINATION_X[number]), _vm->_utils->vgaScaleY(65), _vm->_utils->vgaScaleX(COMBINATION_X[number]) + (_numberImages[combnum])->_width - 1, _vm->_utils->vgaScaleY(65) + (_numberImages[combnum])->_height, buffer);
 		_numberImages[combnum]->blitBitmap(0, (_numberImages[combnum])->_height - (2 * i), &(display), _vm->_utils->vgaScaleX(COMBINATION_X[number]), _vm->_utils->vgaScaleY(65), (_numberImages[combnum])->_width, 2, false);
 	}
 
-	// Prevent the Image destructor from deleting the display buffer
-	display._imageData = nullptr;
-
 	delete[] buffer;
 
 	bool unlocked = true;






More information about the Scummvm-git-logs mailing list