[Scummvm-cvs-logs] SF.net SVN: scummvm:[45594] scummvm/trunk/engines/draci

spalek at users.sourceforge.net spalek at users.sourceforge.net
Sun Nov 1 11:03:38 CET 2009


Revision: 45594
          http://scummvm.svn.sourceforge.net/scummvm/?rev=45594&view=rev
Author:   spalek
Date:     2009-11-01 10:03:37 +0000 (Sun, 01 Nov 2009)

Log Message:
-----------
Get rid of doubling memory allocation and a lot of copying.

The Sprite class points to the original buffer (which is cached in the memory
thanks to BArchive machinery) instead of allocating its own buffer and
copying the source there.

Modified Paths:
--------------
    scummvm/trunk/engines/draci/sprite.cpp
    scummvm/trunk/engines/draci/sprite.h
    scummvm/trunk/engines/draci/walking.cpp

Modified: scummvm/trunk/engines/draci/sprite.cpp
===================================================================
--- scummvm/trunk/engines/draci/sprite.cpp	2009-11-01 09:53:04 UTC (rev 45593)
+++ scummvm/trunk/engines/draci/sprite.cpp	2009-11-01 10:03:37 UTC (rev 45594)
@@ -58,8 +58,8 @@
 /**
  *  Constructor for loading sprites from a raw data buffer, one byte per pixel.
  */
-Sprite::Sprite(const byte *raw_data, uint16 width, uint16 height, int x, int y,
-    bool columnwise) : _data(NULL) {
+Sprite::Sprite(uint16 width, uint16 height, byte *raw_data, int x, int y, bool columnwise)
+    : _ownsData(true), _data(raw_data), _mirror(false) {
 
 	 _width = width;
 	 _height = height;
@@ -72,17 +72,9 @@
 
 	_delay = 0;
 
-	_mirror = false;
-
-	byte *data = new byte[width * height];
-
-	memcpy(data, raw_data, width * height);
-
-	// If the sprite is stored column-wise, transform it to row-wise
 	if (columnwise) {
-		transformToRows(data, width, height);
+		transformToRows(raw_data, _width, _height);
 	}
-	_data = data;
 }
 
 /**
@@ -90,36 +82,36 @@
  *  pixel.
  */
 Sprite::Sprite(const byte *sprite_data, uint16 length, int x, int y, bool columnwise)
-    : _data(NULL) {
+    : _ownsData(false), _data(NULL), _mirror(false) {
 
-	 _x = x;
-	 _y = y;
-
-	_delay = 0;
-
-	_mirror = false;
-
 	Common::MemoryReadStream reader(sprite_data, length);
-
 	_width = reader.readSint16LE();
 	_height = reader.readSint16LE();
 
 	_scaledWidth = _width;
 	_scaledHeight = _height;
 
-	byte *data = new byte[_width * _height];
+	 _x = x;
+	 _y = y;
 
-	reader.read(data, _width * _height);
+	_delay = 0;
 
-	// If the sprite is stored column-wise, transform it to row-wise
-	if (columnwise) {
+	if (!columnwise) {
+		_ownsData = false;
+		_data = sprite_data + 4;
+	} else {
+		_ownsData = true;
+		byte *data = new byte[_width * _height];
+		memcpy(data, sprite_data + 4, _width * _height);
 		transformToRows(data, _width, _height);
+		_data = data;
 	}
-	_data = data;
 }
 
 Sprite::~Sprite() {
-	delete[] _data;
+	if (_ownsData) {
+		delete[] _data;
+	}
 }
 
 // Macro to simulate lround() for non-C99 compilers

Modified: scummvm/trunk/engines/draci/sprite.h
===================================================================
--- scummvm/trunk/engines/draci/sprite.h	2009-11-01 09:53:04 UTC (rev 45593)
+++ scummvm/trunk/engines/draci/sprite.h	2009-11-01 10:03:37 UTC (rev 45594)
@@ -101,7 +101,14 @@
 
 class Sprite : public Drawable {
 public:
-	Sprite(const byte *raw_data, uint16 width, uint16 height, int x, int y, bool columnwise);
+	// Takes ownership of raw_data.
+	Sprite(uint16 width, uint16 height, byte *raw_data, int x, int y, bool columnwise);
+
+	// It doesn't take ownership of sprite_data.  If columnwise is false,
+	// it internally points to the original buffer (which has lifetime at
+	// least as long as this sprite, assumming that it comes from a cached
+	// BArchive file), otherwise it allocates its own buffer with the
+	// transposed image.
 	Sprite(const byte *sprite_data, uint16 length, int x, int y, bool columnwise);
 
 	~Sprite();
@@ -120,6 +127,7 @@
 	DrawableType getType() const { return kDrawableSprite; }
 
 private:
+	bool _ownsData;
 	const byte *_data;  ///< Pointer to a buffer containing raw sprite data (row-wise)
 	bool _mirror;
 };

Modified: scummvm/trunk/engines/draci/walking.cpp
===================================================================
--- scummvm/trunk/engines/draci/walking.cpp	2009-11-01 09:53:04 UTC (rev 45593)
+++ scummvm/trunk/engines/draci/walking.cpp	2009-11-01 10:03:37 UTC (rev 45594)
@@ -71,8 +71,8 @@
 		}
 	}
 
-	Sprite *ov = new Sprite(wlk, kScreenWidth, kScreenHeight, 0, 0, false);
-        delete[] wlk;
+	Sprite *ov = new Sprite(kScreenWidth, kScreenHeight, wlk, 0, 0, false);
+	// ov has taken the ownership of wlk.
 
 	return ov;
 }


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