From 77411ff0f9356036bb45720ff98df951492600d1 Mon Sep 17 00:00:00 2001 From: Vladimir Menshakov Date: Thu, 18 Mar 2010 22:35:58 +0000 Subject: added abstract Pack class, moved code into FilePack and MemoryPack svn-id: r48292 --- engines/teenagent/font.cpp | 7 ++-- engines/teenagent/font.h | 3 +- engines/teenagent/pack.cpp | 83 +++++++++++++++++++++++++++++++++-------- engines/teenagent/pack.h | 48 ++++++++++++++++++++++-- engines/teenagent/resources.cpp | 4 +- engines/teenagent/resources.h | 3 +- engines/teenagent/teenagent.cpp | 2 +- 7 files changed, 122 insertions(+), 28 deletions(-) (limited to 'engines') diff --git a/engines/teenagent/font.cpp b/engines/teenagent/font.cpp index c5d22cd2eb..d69db372b4 100644 --- a/engines/teenagent/font.cpp +++ b/engines/teenagent/font.cpp @@ -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); diff --git a/engines/teenagent/font.h b/engines/teenagent/font.h index 6961c21676..e08a3513af 100644 --- a/engines/teenagent/font.h +++ b/engines/teenagent/font.h @@ -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); diff --git a/engines/teenagent/pack.cpp b/engines/teenagent/pack.cpp index 1176bbc301..e0e560bbf4 100644 --- a/engines/teenagent/pack.cpp +++ b/engines/teenagent/pack.cpp @@ -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 @@ bool Pack::open(const Common::String &filename) { 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 @@ uint32 Pack::read(uint32 id, byte *dst, uint32 size) const { 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 diff --git a/engines/teenagent/pack.h b/engines/teenagent/pack.h index 0c9dccaceb..876e049901 100644 --- a/engines/teenagent/pack.h +++ b/engines/teenagent/pack.h @@ -27,21 +27,61 @@ #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(); + 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(this); + c->data = 0; + c->size = 0; + } + }; + Common::Array chunks; + +public: 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; diff --git a/engines/teenagent/resources.cpp b/engines/teenagent/resources.cpp index 114dec4b45..a995b129f2 100644 --- a/engines/teenagent/resources.cpp +++ b/engines/teenagent/resources.cpp @@ -87,10 +87,10 @@ bool Resources::loadArchives(const ADGameDescription *gd) { 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; diff --git a/engines/teenagent/resources.h b/engines/teenagent/resources.h index 42a5db6636..e32c2df284 100644 --- a/engines/teenagent/resources.h +++ b/engines/teenagent/resources.h @@ -47,7 +47,8 @@ public: //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; }; diff --git a/engines/teenagent/teenagent.cpp b/engines/teenagent/teenagent.cpp index 2475f21d42..8f16098ced 100644 --- a/engines/teenagent/teenagent.cpp +++ b/engines/teenagent/teenagent.cpp @@ -270,7 +270,7 @@ int TeenAgentEngine::skipEvents() const { } bool TeenAgentEngine::showLogo() { - Pack logo; + FilePack logo; if (!logo.open("unlogic.res")) return true; -- cgit v1.2.3