[Scummvm-cvs-logs] SF.net SVN: scummvm:[48292] scummvm/trunk/engines/teenagent

megath at users.sourceforge.net megath at users.sourceforge.net
Thu Mar 18 23:35:59 CET 2010


Revision: 48292
          http://scummvm.svn.sourceforge.net/scummvm/?rev=48292&view=rev
Author:   megath
Date:     2010-03-18 22:35:58 +0000 (Thu, 18 Mar 2010)

Log Message:
-----------
added abstract Pack class, moved code into FilePack and MemoryPack

Modified Paths:
--------------
    scummvm/trunk/engines/teenagent/font.cpp
    scummvm/trunk/engines/teenagent/font.h
    scummvm/trunk/engines/teenagent/pack.cpp
    scummvm/trunk/engines/teenagent/pack.h
    scummvm/trunk/engines/teenagent/resources.cpp
    scummvm/trunk/engines/teenagent/resources.h
    scummvm/trunk/engines/teenagent/teenagent.cpp

Modified: scummvm/trunk/engines/teenagent/font.cpp
===================================================================
--- scummvm/trunk/engines/teenagent/font.cpp	2010-03-18 21:45:27 UTC (rev 48291)
+++ scummvm/trunk/engines/teenagent/font.cpp	2010-03-18 22:35:58 UTC (rev 48292)
@@ -23,18 +23,19 @@
  */
 
 #include "teenagent/font.h"
-#include "teenagent/resources.h"
+#include "teenagent/pack.h"
+#include "common/stream.h"
 
 namespace TeenAgent {
 
 Font::Font() : grid_color(0xd0), shadow_color(0), height(0), width_pack(0), data(0) {
 }
 
-void Font::load(int id) {
+void Font::load(const Pack &pack, int id) {
 	delete[] data;
 	data = NULL;
 
-	Common::SeekableReadStream *s = Resources::instance()->varia.getStream(id);
+	Common::SeekableReadStream *s = pack.getStream(id);
 	if (s == NULL)
 		error("loading font %d failed", id);
 

Modified: scummvm/trunk/engines/teenagent/font.h
===================================================================
--- scummvm/trunk/engines/teenagent/font.h	2010-03-18 21:45:27 UTC (rev 48291)
+++ scummvm/trunk/engines/teenagent/font.h	2010-03-18 22:35:58 UTC (rev 48292)
@@ -29,13 +29,14 @@
 
 namespace TeenAgent {
 
+class Pack;
 class Font {
 public:
 	byte grid_color, shadow_color;
 	byte height, width_pack;
 
 	Font();
-	void load(int id);
+	void load(const Pack &pack, int id);
 	uint render(Graphics::Surface *surface, int x, int y, const Common::String &str, byte color, bool grid = false);
 	uint render(Graphics::Surface *surface, int x, int y, char c, byte color);
 	static void grid(Graphics::Surface *surface, int x, int y, int w, int h, byte color);

Modified: scummvm/trunk/engines/teenagent/pack.cpp
===================================================================
--- scummvm/trunk/engines/teenagent/pack.cpp	2010-03-18 21:45:27 UTC (rev 48291)
+++ scummvm/trunk/engines/teenagent/pack.cpp	2010-03-18 22:35:58 UTC (rev 48292)
@@ -28,28 +28,26 @@
 
 namespace TeenAgent {
 
-Pack::Pack() : count(0), offsets(0) {}
+FilePack::FilePack() : offsets(0) {}
 
-Pack::~Pack() {
+FilePack::~FilePack() {
 	close();
 }
 
-
-void Pack::close() {
+void FilePack::close() {
 	delete[] offsets;
 	offsets = NULL;
 	file.close();
 }
 
-
-bool Pack::open(const Common::String &filename) {
+bool FilePack::open(const Common::String &filename) {
 	if (!file.open(filename))
 		return false;
 
-	count = file.readUint32LE();
-	debug(0, "opened %s, found %u entries", filename.c_str(), count);
-	offsets = new uint32[count + 1];
-	for (uint32 i = 0; i <= count; ++i) {
+	_files_count = file.readUint32LE();
+	debug(0, "opened %s, found %u entries", filename.c_str(), _files_count);
+	offsets = new uint32[_files_count + 1];
+	for (uint32 i = 0; i <= _files_count; ++i) {
 		offsets[i] = file.readUint32LE();
 		//debug(0, "%d: %06x", i, offsets[i]);
 	}
@@ -60,14 +58,14 @@
 	return true;
 }
 
-uint32 Pack::get_size(uint32 id) const {
-	if (id < 1 || id > count)
+uint32 FilePack::get_size(uint32 id) const {
+	if (id < 1 || id > _files_count)
 		return 0;
 	return offsets[id] - offsets[id - 1];
 }
 
-uint32 Pack::read(uint32 id, byte *dst, uint32 size) const {
-	if (id < 1 || id > count)
+uint32 FilePack::read(uint32 id, byte *dst, uint32 size) const {
+	if (id < 1 || id > _files_count)
 		return 0;
 
 	file.seek(offsets[id - 1]);
@@ -77,11 +75,64 @@
 	return r;
 }
 
-Common::SeekableReadStream *Pack::getStream(uint32 id) const {
-	if (id < 1 || id > count)
+Common::SeekableReadStream *FilePack::getStream(uint32 id) const {
+	if (id < 1 || id > _files_count)
 		return 0;
 	//debug(0, "stream: %04x-%04x", offsets[id - 1], offsets[id]);
 	return new Common::SeekableSubReadStream(&file, offsets[id - 1], offsets[id], DisposeAfterUse::NO);
 }
 
+void MemoryPack::close() {
+	chunks.clear();
+}
+
+bool MemoryPack::open(const Common::String &filename) {
+	Common::File file;
+	if (!file.open(filename))
+		return false;
+
+	uint32 count = file.readUint32LE();
+	debug(0, "opened %s, found %u entries [memory]", filename.c_str(), count);
+	for (uint32 i = 0; i <= count; ++i) {
+		uint32 offset = file.readUint32LE();
+		int32 pos = file.pos();
+		uint32 next_offset = file.readUint32LE();
+		uint32 size = next_offset - offset;
+		file.seek(offset);
+		Chunk chunk;
+		if (size != 0) {
+			chunk.data = new byte[size];
+			chunk.size = size;
+			file.read(chunk.data, size);
+		}
+		file.seek(pos);
+		chunks.push_back(chunk);
+	}
+	file.close();
+	return true;
+}
+
+uint32 MemoryPack::get_size(uint32 id) const {
+	--id;
+	return id < chunks.size()? chunks[id].size: 0;
+}
+
+uint32 MemoryPack::read(uint32 id, byte *dst, uint32 size) const {
+	--id;
+	if (id >= chunks.size())
+		return 0;
+	const Chunk &c = chunks[id];
+	memcpy(dst, c.data, c.size);
+	return c.size;
+}
+
+Common::SeekableReadStream *MemoryPack::getStream(uint32 id) const {
+	--id;
+	if (id >= chunks.size())
+		return 0;
+	const Chunk &c = chunks[id];
+	return new Common::MemoryReadStream(c.data, c.size, DisposeAfterUse::NO);
+}
+
+
 } // End of namespace TeenAgent

Modified: scummvm/trunk/engines/teenagent/pack.h
===================================================================
--- scummvm/trunk/engines/teenagent/pack.h	2010-03-18 21:45:27 UTC (rev 48291)
+++ scummvm/trunk/engines/teenagent/pack.h	2010-03-18 22:35:58 UTC (rev 48292)
@@ -27,26 +27,66 @@
 #define TEENAGENT_PACK_H
 
 #include "common/file.h"
+#include "common/array.h"
 
 namespace TeenAgent {
+
 class Pack {
+protected:
+	uint32 _files_count;
+public:
+	Pack(): _files_count(0) {}
+	virtual ~Pack() {};
+	virtual bool open(const Common::String &filename) = 0;
+	virtual void close() = 0;
+
+	virtual uint32 files_count() const { return _files_count; }
+	virtual uint32 get_size(uint32 id) const = 0;
+	virtual uint32 read(uint32 id, byte *dst, uint32 size) const = 0;
+	virtual Common::SeekableReadStream *getStream(uint32 id) const = 0;
+};
+
+class FilePack : public Pack {
 	mutable Common::File file;
-	uint32 count;
 	uint32 *offsets;
 
 public:
-	Pack();
-	~Pack();
+	FilePack();
+	~FilePack();
 
 	bool open(const Common::String &filename);
 	void close();
 
-	inline uint32 files_count() const { return count; }
 	uint32 get_size(uint32 id) const;
 	uint32 read(uint32 id, byte *dst, uint32 size) const;
 	Common::SeekableReadStream *getStream(uint32 id) const;
 };
 
+class MemoryPack : public Pack {
+	struct Chunk {
+		byte *data;
+		uint32 size;
+		inline Chunk(): data(0), size(0) {}
+		inline Chunk(const Chunk &c): data(c.data), size(c.size) { c.reset(); }
+		inline Chunk& operator=(const Chunk &c) { data = c.data; size = c.size; c.reset(); return *this; }
+		inline ~Chunk() { delete[] data; }
+		inline void reset() const {
+			Chunk *c = const_cast<Chunk *>(this);
+			c->data = 0;
+			c->size = 0;
+		}
+	};
+	Common::Array<Chunk> chunks;
+
+public:
+	bool open(const Common::String &filename);
+	void close();
+
+	uint32 get_size(uint32 id) const;
+	uint32 read(uint32 id, byte *dst, uint32 size) const;
+	Common::SeekableReadStream *getStream(uint32 id) const;
+};
+
 } // End of namespace TeenAgent
 
 #endif

Modified: scummvm/trunk/engines/teenagent/resources.cpp
===================================================================
--- scummvm/trunk/engines/teenagent/resources.cpp	2010-03-18 21:45:27 UTC (rev 48291)
+++ scummvm/trunk/engines/teenagent/resources.cpp	2010-03-18 22:35:58 UTC (rev 48292)
@@ -87,10 +87,10 @@
 	sam_mmm.open("sam_mmm.res");
 	sam_sam.open("sam_sam.res");
 
-	font7.load(7);
+	font7.load(varia, 7);
 	font7.width_pack = 1;
 	font7.height = 11;
-	font8.load(8);
+	font8.load(varia, 8);
 	font8.height = 31;
 
 	return true;

Modified: scummvm/trunk/engines/teenagent/resources.h
===================================================================
--- scummvm/trunk/engines/teenagent/resources.h	2010-03-18 21:45:27 UTC (rev 48291)
+++ scummvm/trunk/engines/teenagent/resources.h	2010-03-18 22:35:58 UTC (rev 48292)
@@ -47,7 +47,8 @@
 	//void loadOn(Graphics::Surface &surface, int id, uint16 &dst, uint16 *flags);
 	//void loadOns(Graphics::Surface &surface, int id, uint16 &dst);
 
-	Pack varia, off, on, ons, lan000, lan500, mmm, sam_mmm, sam_sam;
+	FilePack varia, off, on, ons, lan000, lan500, sam_mmm, sam_sam;
+	MemoryPack mmm;
 	Segment cseg, dseg, eseg;
 	Font font7, font8;
 };

Modified: scummvm/trunk/engines/teenagent/teenagent.cpp
===================================================================
--- scummvm/trunk/engines/teenagent/teenagent.cpp	2010-03-18 21:45:27 UTC (rev 48291)
+++ scummvm/trunk/engines/teenagent/teenagent.cpp	2010-03-18 22:35:58 UTC (rev 48292)
@@ -270,7 +270,7 @@
 }
 
 bool TeenAgentEngine::showLogo() {
-	Pack logo;
+	FilePack logo;
 	if (!logo.open("unlogic.res"))
 		return true;
 


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