[Scummvm-cvs-logs] SF.net SVN: scummvm:[48080] scummvm/trunk/engines/mohawk

tdhs at users.sourceforge.net tdhs at users.sourceforge.net
Wed Feb 17 20:59:08 CET 2010


Revision: 48080
          http://scummvm.svn.sourceforge.net/scummvm/?rev=48080&view=rev
Author:   tdhs
Date:     2010-02-17 19:59:08 +0000 (Wed, 17 Feb 2010)

Log Message:
-----------
Mohawk : Adding resource cache class and integrating into Myst engine.
The resource caching trades increased memory usage against disk access.

This functionality can be disabled and enabled by the console "cache" command.

Modified Paths:
--------------
    scummvm/trunk/engines/mohawk/console.cpp
    scummvm/trunk/engines/mohawk/console.h
    scummvm/trunk/engines/mohawk/module.mk
    scummvm/trunk/engines/mohawk/mohawk.cpp
    scummvm/trunk/engines/mohawk/mohawk.h
    scummvm/trunk/engines/mohawk/myst.cpp
    scummvm/trunk/engines/mohawk/myst.h

Added Paths:
-----------
    scummvm/trunk/engines/mohawk/resource_cache.cpp
    scummvm/trunk/engines/mohawk/resource_cache.h

Modified: scummvm/trunk/engines/mohawk/console.cpp
===================================================================
--- scummvm/trunk/engines/mohawk/console.cpp	2010-02-17 16:18:23 UTC (rev 48079)
+++ scummvm/trunk/engines/mohawk/console.cpp	2010-02-17 19:59:08 UTC (rev 48080)
@@ -47,6 +47,7 @@
 	DCmd_Register("stopSound",			WRAP_METHOD(MystConsole, Cmd_StopSound));
 	DCmd_Register("playMovie",			WRAP_METHOD(MystConsole, Cmd_PlayMovie));
 	DCmd_Register("disableInitOpcodes",	WRAP_METHOD(MystConsole, Cmd_DisableInitOpcodes));
+	DCmd_Register("cache",				WRAP_METHOD(MystConsole, Cmd_Cache));
 }
 
 MystConsole::~MystConsole() {
@@ -270,6 +271,27 @@
 	return true;
 }
 
+bool MystConsole::Cmd_Cache(int argc, const char **argv) {
+	if (argc > 2) {
+		DebugPrintf("Usage: cache on/off - Omit parameter to get current state\n");
+		return true;
+	}
+
+	bool state = false;
+
+	if (argc == 1) {
+		state = _vm->getCacheState();
+	} else {
+		if (!scumm_stricmp(argv[1], "on"))
+			state = true;
+
+		_vm->setCacheState(state);
+	}
+
+	DebugPrintf("Cache: %s\n", state ? "Enabled" : "Disabled");
+	return true;
+}
+
 RivenConsole::RivenConsole(MohawkEngine_Riven *vm) : GUI::Debugger(), _vm(vm) {
 	DCmd_Register("changeCard",		WRAP_METHOD(RivenConsole, Cmd_ChangeCard));
 	DCmd_Register("curCard",		WRAP_METHOD(RivenConsole, Cmd_CurCard));

Modified: scummvm/trunk/engines/mohawk/console.h
===================================================================
--- scummvm/trunk/engines/mohawk/console.h	2010-02-17 16:18:23 UTC (rev 48079)
+++ scummvm/trunk/engines/mohawk/console.h	2010-02-17 19:59:08 UTC (rev 48080)
@@ -58,6 +58,7 @@
 	bool Cmd_StopSound(int argc, const char **argv);
 	bool Cmd_PlayMovie(int argc, const char **argv);
 	bool Cmd_DisableInitOpcodes(int argc, const char **argv);
+	bool Cmd_Cache(int argc, const char **argv);
 };
 
 class RivenConsole : public GUI::Debugger {

Modified: scummvm/trunk/engines/mohawk/module.mk
===================================================================
--- scummvm/trunk/engines/mohawk/module.mk	2010-02-17 16:18:23 UTC (rev 48079)
+++ scummvm/trunk/engines/mohawk/module.mk	2010-02-17 19:59:08 UTC (rev 48080)
@@ -15,6 +15,7 @@
 	myst_saveload.o \
 	myst_scripts.o \
 	resource.o \
+	resource_cache.o \
 	riven.o \
 	riven_external.o \
 	riven_saveload.o \

Modified: scummvm/trunk/engines/mohawk/mohawk.cpp
===================================================================
--- scummvm/trunk/engines/mohawk/mohawk.cpp	2010-02-17 16:18:23 UTC (rev 48079)
+++ scummvm/trunk/engines/mohawk/mohawk.cpp	2010-02-17 19:59:08 UTC (rev 48080)
@@ -90,7 +90,7 @@
 			return _mhk[i]->getRawData(tag, id);
 
 	error ("Could not find a \'%s\' resource with ID %04x", tag2str(tag), id);
-	return 0;
+	return NULL;
 }
 
 bool MohawkEngine::hasResource(uint32 tag, uint16 id) {

Modified: scummvm/trunk/engines/mohawk/mohawk.h
===================================================================
--- scummvm/trunk/engines/mohawk/mohawk.h	2010-02-17 16:18:23 UTC (rev 48079)
+++ scummvm/trunk/engines/mohawk/mohawk.h	2010-02-17 19:59:08 UTC (rev 48080)
@@ -86,7 +86,7 @@
 	Sound *_sound;
 	VideoManager *_video;
 
-	Common::SeekableReadStream *getRawData(uint32 tag, uint16 id);
+	virtual Common::SeekableReadStream *getRawData(uint32 tag, uint16 id);
 	bool hasResource(uint32 tag, uint16 id);
 	uint32 getResourceOffset(uint32 tag, uint16 id);
 

Modified: scummvm/trunk/engines/mohawk/myst.cpp
===================================================================
--- scummvm/trunk/engines/mohawk/myst.cpp	2010-02-17 16:18:23 UTC (rev 48079)
+++ scummvm/trunk/engines/mohawk/myst.cpp	2010-02-17 19:59:08 UTC (rev 48080)
@@ -31,6 +31,7 @@
 #include "mohawk/myst_saveload.h"
 #include "mohawk/dialogs.h"
 #include "mohawk/resource.h"
+#include "mohawk/resource_cache.h"
 #include "mohawk/video/video.h"
 
 namespace Mohawk {
@@ -45,6 +46,7 @@
 	Common::addDebugChannel(kDebugEXIT, "Exit", "Track Card Exit Script (EXIT) Parsing");
 	Common::addDebugChannel(kDebugScript, "Script", "Track Script Execution");
 	Common::addDebugChannel(kDebugHelp, "Help", "Track Help File (HELP) Parsing");
+	Common::addDebugChannel(kDebugCache, "Cache", "Track Resource Cache Accesses");
 
 	_zipMode = false;
 	_transitionsEnabled = false;
@@ -90,6 +92,40 @@
 	_resources.clear();
 }
 
+// Uses cached data objects in preference to disk access
+Common::SeekableReadStream *MohawkEngine_Myst::getRawData(uint32 tag, uint16 id) {
+	Common::SeekableReadStream *ret;
+
+	ret = _cache.search(tag, id);
+	if(ret != NULL) return ret;
+
+	for (uint32 i = 0; i < _mhk.size(); i++)
+		if (_mhk[i]->hasResource(tag, id)) {
+			ret = _mhk[i]->getRawData(tag, id);
+			_cache.add(tag, id, ret);
+			return ret;
+		}
+
+	error ("Could not find a \'%s\' resource with ID %04x", tag2str(tag), id);
+	return NULL;
+}
+
+void MohawkEngine_Myst::cachePreload(uint32 tag, uint16 id) {
+	Common::SeekableReadStream *tempData;
+
+	if (!_cache.enabled) return;
+
+	for (uint32 i = 0; i < _mhk.size(); i++)
+		if (_mhk[i]->hasResource(tag, id)) {
+			tempData = _mhk[i]->getRawData(tag, id);
+			_cache.add(tag, id, tempData);
+			delete tempData;
+			return;
+		}
+
+	warning ("cachePreload : Could not find a \'%s\' resource with ID %04x", tag2str(tag), id);
+}
+
 static const char *mystFiles[] = {
 	"channel.dat",
 	"credits.dat",
@@ -299,6 +335,8 @@
 		_gfx->loadExternalPictureFile(_curStack);
 
 	_runExitScript = false;
+
+	_cache.clear();
 }
 
 void MohawkEngine_Myst::changeToCard(uint16 card) {
@@ -316,6 +354,8 @@
 
 	unloadCard();
 
+	_cache.clear();
+
 	_curCard = card;
 
 	// Load a bunch of stuff
@@ -550,6 +590,52 @@
 	_view.exit = viewStream->readUint16LE();
 
 	delete viewStream;
+
+	// Precache Card Resources
+	// TODO: Deal with Mac ME External Picture File
+	uint32 cacheImageType;
+	if (getFeatures() & GF_ME)
+		cacheImageType = ID_PICT;
+	else
+		cacheImageType = ID_WDIB;
+
+	// Precache Image Block data
+	if (_view.conditionalImageCount != 0) {
+		for (uint16 i = 0; i < _view.conditionalImageCount; i++)
+			for (uint16 j = 0; j < _view.conditionalImages[i].numStates; j++)
+				cachePreload(cacheImageType, _view.conditionalImages[i].values[j]);
+	} else
+		cachePreload(cacheImageType, _view.mainImage);
+
+	// Precache Sound Block data
+	if (_view.sound > 0)
+		cachePreload(ID_MSND, _view.sound);
+	else if (_view.sound == kMystSoundActionConditional) {
+		for (uint16 i = 0; i < _view.soundCount; i++) {
+			if (_view.soundList[i] > 0)
+				cachePreload(ID_MSND, _view.soundList[i]);
+		}
+	}
+
+	// Precache Script Resources
+	if (_view.scriptResCount != 0) {
+		for (uint16 i = 0; i < _view.scriptResCount; i++) {
+			switch (_view.scriptResources[i].type) {
+			case 1:
+				cachePreload(cacheImageType, _view.scriptResources[i].id);
+				break;
+			case 2:
+				cachePreload(ID_MSND, _view.scriptResources[i].id);
+				break;
+			case 3:
+				warning("TODO: Precaching of Script Resource List not supported");
+				break;
+			default:
+				warning("Unknown Resource in Script Resource List Precaching");
+				break;
+			}
+		}
+	}
 }
 
 void MohawkEngine_Myst::unloadCard() {

Modified: scummvm/trunk/engines/mohawk/myst.h
===================================================================
--- scummvm/trunk/engines/mohawk/myst.h	2010-02-17 16:18:23 UTC (rev 48079)
+++ scummvm/trunk/engines/mohawk/myst.h	2010-02-17 19:59:08 UTC (rev 48080)
@@ -28,6 +28,7 @@
 
 #include "mohawk/console.h"
 #include "mohawk/mohawk.h"
+#include "mohawk/resource_cache.h"
 #include "mohawk/myst_vars.h"
 
 #include "gui/saveload.h"
@@ -52,7 +53,8 @@
 	kDebugINIT     = (1 << 5),
 	kDebugEXIT     = (1 << 6),
 	kDebugScript   = (1 << 7),
-	kDebugHelp     = (1 << 8)
+	kDebugHelp     = (1 << 8),
+	kDebugCache    = (1 << 9)
 };
 
 // Myst Stacks
@@ -342,6 +344,8 @@
 	MohawkEngine_Myst(OSystem *syst, const MohawkGameDescription *gamedesc);
 	virtual ~MohawkEngine_Myst();
 
+	Common::SeekableReadStream *getRawData(uint32 tag, uint16 id);
+
 	Common::String wrapMovieFilename(Common::String movieName, uint16 stack);
 
 	void reloadSaveList();
@@ -369,6 +373,9 @@
 	bool _showResourceRects;
 	void setResourceEnabled(uint16 resourceId, bool enable);
 
+	void setCacheState(bool state) { _cache.enabled = state; }
+	bool getCacheState(void) { return _cache.enabled; }
+
 	GUI::Debugger *getDebugger() { return _console; }
 
 	bool canLoadGameStateCurrently() { return !(getFeatures() & GF_DEMO); }
@@ -381,6 +388,8 @@
 	MystConsole *_console;
 	GUI::SaveLoadChooser *_loadDialog;
 	MystOptionsDialog *_optionsDialog;
+	ResourceCache _cache;
+	void cachePreload(uint32 tag, uint16 id);
 
 	uint16 _curStack;
 	uint16 _curCard;
@@ -406,7 +415,7 @@
 	void checkCursorHints();
 	Common::Point _mousePos;
 	uint16 _currentCursor;
-	uint16 _mainCursor;	// Also defines the current page being held (white, blue, red, or none)
+	uint16 _mainCursor; // Also defines the current page being held (white, blue, red, or none)
 };
 
 } // End of namespace Mohawk

Added: scummvm/trunk/engines/mohawk/resource_cache.cpp
===================================================================
--- scummvm/trunk/engines/mohawk/resource_cache.cpp	                        (rev 0)
+++ scummvm/trunk/engines/mohawk/resource_cache.cpp	2010-02-17 19:59:08 UTC (rev 48080)
@@ -0,0 +1,84 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#include "common/debug.h"
+#include "mohawk/myst.h"
+#include "mohawk/resource_cache.h"
+
+namespace Mohawk {
+
+ResourceCache::ResourceCache() {
+	enabled = true;
+}
+
+ResourceCache::~ResourceCache() {
+	clear();
+}
+
+void ResourceCache::clear() {
+	if (!enabled) return;
+
+	debugC(kDebugCache, "Clearing Cache...");
+
+	// TODO : Not sure if need to explicitly delete dataObject.data element ie.
+	//        returned by readStream method here.
+	store.clear();
+}
+
+void ResourceCache::add(uint32 tag, uint16 id, Common::SeekableReadStream *data) {
+	if (!enabled) return;
+
+	debugC(kDebugCache, "Adding item %u - tag 0x%04X id %u", store.size(), tag, id);
+
+	dataObject current;
+	current.tag = tag;
+	current.id = id;
+	uint32 dataCurPos  = data->pos();
+	current.data = data->readStream(data->size());
+	data->seek(dataCurPos, SEEK_SET);
+	store.push_back(current);
+}
+
+// Returns NULL if not found
+Common::SeekableReadStream *ResourceCache::search(uint32 tag, uint16 id) {
+	if (!enabled) return NULL;
+
+	debugC(kDebugCache, "Searching for tag 0x%04X id %u", tag, id);
+
+	for (uint32 i = 0; i < store.size(); i++) {
+		if (tag == store[i].tag && id == store[i].id) {
+			debugC(kDebugCache, "Found cached tag 0x%04X id %u", tag, id);
+			uint32 dataCurPos  = store[i].data->pos();
+			Common::SeekableReadStream *ret = store[i].data->readStream(store[i].data->size());
+			store[i].data->seek(dataCurPos, SEEK_SET);
+			return ret;
+		}
+	}
+
+	debugC(kDebugCache, "tag 0x%04X id %u not found", tag, id);
+	return NULL;
+}
+
+} // End of namespace Mohawk


Property changes on: scummvm/trunk/engines/mohawk/resource_cache.cpp
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native

Added: scummvm/trunk/engines/mohawk/resource_cache.h
===================================================================
--- scummvm/trunk/engines/mohawk/resource_cache.h	                        (rev 0)
+++ scummvm/trunk/engines/mohawk/resource_cache.h	2010-02-17 19:59:08 UTC (rev 48080)
@@ -0,0 +1,59 @@
+/* ScummVM - Graphic Adventure Engine
+ *
+ * ScummVM is the legal property of its developers, whose names
+ * are too numerous to list here. Please refer to the COPYRIGHT
+ * file distributed with this source distribution.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * $URL$
+ * $Id$
+ *
+ */
+
+#ifndef RESOURCE_CACHE_H
+#define RESOURCE_CACHE_H
+
+#include "common/array.h"
+#include "common/stream.h"
+
+namespace Mohawk {
+
+class ResourceCache {
+public:
+	ResourceCache();
+	~ResourceCache();
+
+	bool enabled;
+
+	void clear();
+	void add(uint32 tag, uint16 id, Common::SeekableReadStream *data);
+
+	// Returns NULL if not found
+	Common::SeekableReadStream *search(uint32 tag, uint16 id);
+
+private:
+	typedef struct {
+		uint32 tag;
+		uint16 id;
+		Common::SeekableReadStream *data;
+	} dataObject;
+
+	Common::Array<dataObject> store;
+};
+
+} // End of namespace Mohawk
+
+#endif


Property changes on: scummvm/trunk/engines/mohawk/resource_cache.h
___________________________________________________________________
Added: svn:mime-type
   + text/plain
Added: svn:keywords
   + Date Rev Author URL Id
Added: svn:eol-style
   + native


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