diff options
Diffstat (limited to 'engines')
-rw-r--r-- | engines/sword25/gfx/animation.cpp | 151 | ||||
-rw-r--r-- | engines/sword25/gfx/animation.h | 16 | ||||
-rw-r--r-- | engines/sword25/gfx/graphicengine_script.cpp | 15 | ||||
-rw-r--r-- | engines/sword25/kernel/callbackregistry.cpp | 111 | ||||
-rw-r--r-- | engines/sword25/kernel/callbackregistry.h | 84 | ||||
-rw-r--r-- | engines/sword25/module.mk | 1 |
6 files changed, 71 insertions, 307 deletions
diff --git a/engines/sword25/gfx/animation.cpp b/engines/sword25/gfx/animation.cpp index fd302be222..ed0f807234 100644 --- a/engines/sword25/gfx/animation.cpp +++ b/engines/sword25/gfx/animation.cpp @@ -38,7 +38,6 @@ #include "sword25/kernel/resmanager.h" #include "sword25/kernel/inputpersistenceblock.h" #include "sword25/kernel/outputpersistenceblock.h" -#include "sword25/kernel/callbackregistry.h" #include "sword25/package/packagemanager.h" #include "sword25/gfx/image/image.h" #include "sword25/gfx/animationtemplate.h" @@ -133,14 +132,14 @@ Animation::~Animation() { getAnimationDescription()->unlock(); } - // Delete Callbacks - Common::Array<ANIMATION_CALLBACK_DATA>::iterator it = _deleteCallbacks.begin(); - for (; it != _deleteCallbacks.end(); it++)((*it).Callback)((*it).Data); + // Invoke the "delete" callback + if (_deleteCallback) + (_deleteCallback)(getHandle()); } void Animation::play() { - // Wenn die Animation zuvor komplett durchgelaufen ist, wird sie wieder von Anfang abgespielt + // If the animation was completed, then play it again from the start. if (_finished) stop(); @@ -242,28 +241,20 @@ void Animation::frameNotification(int timeElapsed) { BS_ASSERT(0); } - // Überläufe behandeln + // Deal with overflows if (tmpCurFrame < 0) { - // Loop-Point Callbacks - for (uint i = 0; i < _loopPointCallbacks.size();) { - if ((_loopPointCallbacks[i].Callback)(_loopPointCallbacks[i].Data) == false) { - _loopPointCallbacks.remove_at(i); - } else - i++; - } + // Loop-Point callback + if (_loopPointCallback && !(_loopPointCallback)(getHandle())) + _loopPointCallback = 0; - // Ein Unterlauf darf nur auftreten, wenn der Animationstyp JOJO ist. + // An underflow may only occur if the animation type is JOJO. BS_ASSERT(animationDescriptionPtr->getAnimationType() == AT_JOJO); tmpCurFrame = - tmpCurFrame; _direction = FORWARD; } else if (static_cast<uint>(tmpCurFrame) >= animationDescriptionPtr->getFrameCount()) { - // Loop-Point Callbacks - for (uint i = 0; i < _loopPointCallbacks.size();) { - if ((_loopPointCallbacks[i].Callback)(_loopPointCallbacks[i].Data) == false) { - _loopPointCallbacks.remove_at(i); - } else - i++; - } + // Loop-Point callback + if (_loopPointCallback && !(_loopPointCallback)(getHandle())) + _loopPointCallback = 0; switch (animationDescriptionPtr->getAnimationType()) { case AT_ONESHOT: @@ -290,13 +281,9 @@ void Animation::frameNotification(int timeElapsed) { forceRefresh(); if (animationDescriptionPtr->getFrame(_currentFrame).action != "") { - // Action Callbacks - for (uint i = 0; i < _actionCallbacks.size();) { - if ((_actionCallbacks[i].Callback)(_actionCallbacks[i].Data) == false) { - _actionCallbacks.remove_at(i); - } else - i++; - } + // action callback + if (_actionCallback && !(_actionCallback)(getHandle())) + _actionCallback = 0; } } @@ -555,63 +542,6 @@ int Animation::computeYModifier() const { return result; } -void Animation::registerActionCallback(ANIMATION_CALLBACK callback, uint data) { - ANIMATION_CALLBACK_DATA cd; - cd.Callback = callback; - cd.Data = data; - _actionCallbacks.push_back(cd); -} - -void Animation::registerLoopPointCallback(ANIMATION_CALLBACK callback, uint data) { - ANIMATION_CALLBACK_DATA cd; - cd.Callback = callback; - cd.Data = data; - _loopPointCallbacks.push_back(cd); -} - -void Animation::registerDeleteCallback(ANIMATION_CALLBACK callback, uint data) { - ANIMATION_CALLBACK_DATA cd; - cd.Callback = callback; - cd.Data = data; - _deleteCallbacks.push_back(cd); -} - -void Animation::persistCallbackVector(OutputPersistenceBlock &writer, const Common::Array<ANIMATION_CALLBACK_DATA> &vector) { - // Anzahl an Callbacks persistieren. - writer.write(vector.size()); - - // Alle Callbacks einzeln persistieren. - Common::Array<ANIMATION_CALLBACK_DATA>::const_iterator it = vector.begin(); - while (it != vector.end()) { - writer.write(CallbackRegistry::instance().resolveCallbackPointer((void (*)(int))it->Callback)); - writer.write(it->Data); - - ++it; - } -} - -void Animation::unpersistCallbackVector(InputPersistenceBlock &reader, Common::Array<ANIMATION_CALLBACK_DATA> &vector) { - // Callbackvector leeren. - vector.resize(0); - - // Anzahl an Callbacks einlesen. - uint callbackCount; - reader.read(callbackCount); - - // Alle Callbacks einzeln wieder herstellen. - for (uint i = 0; i < callbackCount; ++i) { - ANIMATION_CALLBACK_DATA callbackData; - - Common::String callbackFunctionName; - reader.read(callbackFunctionName); - callbackData.Callback = reinterpret_cast<ANIMATION_CALLBACK>(CallbackRegistry::instance().resolveCallbackFunction(callbackFunctionName)); - - reader.read(callbackData.Data); - - vector.push_back(callbackData); - } -} - bool Animation::persist(OutputPersistenceBlock &writer) { bool result = true; @@ -644,9 +574,18 @@ bool Animation::persist(OutputPersistenceBlock &writer) { //writer.write(_AnimationDescriptionPtr); writer.write(_framesLocked); - persistCallbackVector(writer, _loopPointCallbacks); - persistCallbackVector(writer, _actionCallbacks); - persistCallbackVector(writer, _deleteCallbacks); + + // The following is only there to for compatibility with older saves + // resp. the original engine. + writer.write((uint)1); + writer.write(Common::String("LuaLoopPointCB")); + writer.write(getHandle()); + writer.write((uint)1); + writer.write(Common::String("LuaActionCB")); + writer.write(getHandle()); + writer.write((uint)1); + writer.write(Common::String("LuaDeleteCB")); + writer.write(getHandle()); result &= RenderObject::persistChildren(writer); @@ -690,9 +629,39 @@ bool Animation::unpersist(InputPersistenceBlock &reader) { if (_framesLocked) lockAllFrames(); - unpersistCallbackVector(reader, _loopPointCallbacks); - unpersistCallbackVector(reader, _actionCallbacks); - unpersistCallbackVector(reader, _deleteCallbacks); + + // The following is only there to for compatibility with older saves + // resp. the original engine. + uint callbackCount; + Common::String callbackFunctionName; + uint callbackData; + + // loop point callback + reader.read(callbackCount); + assert(callbackCount == 1); + reader.read(callbackFunctionName); + assert(callbackFunctionName == "LuaLoopPointCB"); + reader.read(callbackData); + assert(callbackData == getHandle()); + + // loop point callback + reader.read(callbackCount); + assert(callbackCount == 1); + reader.read(callbackFunctionName); + assert(callbackFunctionName == "LuaActionCB"); + reader.read(callbackData); + assert(callbackData == getHandle()); + + // loop point callback + reader.read(callbackCount); + assert(callbackCount == 1); + reader.read(callbackFunctionName); + assert(callbackFunctionName == "LuaDeleteCB"); + reader.read(callbackData); + assert(callbackData == getHandle()); + + // Set the callbacks + setCallbacks(); result &= RenderObject::unpersistChildren(reader); diff --git a/engines/sword25/gfx/animation.h b/engines/sword25/gfx/animation.h index 0676c0c116..72fe7e2de8 100644 --- a/engines/sword25/gfx/animation.h +++ b/engines/sword25/gfx/animation.h @@ -151,9 +151,7 @@ public: typedef bool (*ANIMATION_CALLBACK)(uint); - void registerLoopPointCallback(ANIMATION_CALLBACK callback, uint data = 0); - void registerActionCallback(ANIMATION_CALLBACK callback, uint data = 0); - void registerDeleteCallback(ANIMATION_CALLBACK Callback, uint Data = 0); + void setCallbacks(); protected: virtual bool doRender(); @@ -178,13 +176,9 @@ private: uint _animationTemplateHandle; bool _framesLocked; - struct ANIMATION_CALLBACK_DATA { - ANIMATION_CALLBACK Callback; - uint Data; - }; - Common::Array<ANIMATION_CALLBACK_DATA> _loopPointCallbacks; - Common::Array<ANIMATION_CALLBACK_DATA> _actionCallbacks; - Common::Array<ANIMATION_CALLBACK_DATA> _deleteCallbacks; + ANIMATION_CALLBACK _loopPointCallback; + ANIMATION_CALLBACK _actionCallback; + ANIMATION_CALLBACK _deleteCallback; /** @brief Lockt alle Frames. @@ -216,8 +210,6 @@ private: int computeYModifier() const; void initMembers(); - void persistCallbackVector(OutputPersistenceBlock &writer, const Common::Array<ANIMATION_CALLBACK_DATA> &vector); - void unpersistCallbackVector(InputPersistenceBlock &reader, Common::Array<ANIMATION_CALLBACK_DATA> &vector); AnimationDescription *getAnimationDescription() const; void initializeAnimationResource(const Common::String &fileName); }; diff --git a/engines/sword25/gfx/graphicengine_script.cpp b/engines/sword25/gfx/graphicengine_script.cpp index b2a6309e48..0814a23871 100644 --- a/engines/sword25/gfx/graphicengine_script.cpp +++ b/engines/sword25/gfx/graphicengine_script.cpp @@ -34,7 +34,6 @@ #include "sword25/kernel/common.h" #include "sword25/kernel/kernel.h" -#include "sword25/kernel/callbackregistry.h" #include "sword25/script/script.h" #include "sword25/script/luabindhelper.h" #include "sword25/script/luacallback.h" @@ -621,15 +620,19 @@ static int ro_addAnimation(lua_State *L) { lua_setmetatable(L, -2); // Alle Animationscallbacks registrieren. - animationPtr->registerDeleteCallback(animationDeleteCallback, animationPtr->getHandle()); - animationPtr->registerLoopPointCallback(animationLoopPointCallback, animationPtr->getHandle()); - animationPtr->registerActionCallback(animationActionCallback, animationPtr->getHandle()); + animationPtr->setCallbacks(); } else lua_pushnil(L); return 1; } +void Animation::setCallbacks() { + _actionCallback = animationActionCallback; + _loopPointCallback = animationLoopPointCallback; + _deleteCallback = animationDeleteCallback; +} + static const luaL_reg RENDEROBJECT_METHODS[] = { {"AddAnimation", ro_addAnimation}, {"AddText", ro_addText}, @@ -1288,10 +1291,6 @@ bool GraphicEngine::registerScriptBindings() { assert(actionCallbackPtr == 0); actionCallbackPtr = new ActionCallback(L); - CallbackRegistry::instance().registerCallbackFunction("LuaLoopPointCB", (void ( *)(int))animationLoopPointCallback); - CallbackRegistry::instance().registerCallbackFunction("LuaActionCB", (void ( *)(int))animationActionCallback); - CallbackRegistry::instance().registerCallbackFunction("LuaDeleteCB", (void ( *)(int))animationDeleteCallback); - return true; } diff --git a/engines/sword25/kernel/callbackregistry.cpp b/engines/sword25/kernel/callbackregistry.cpp deleted file mode 100644 index 673da0679c..0000000000 --- a/engines/sword25/kernel/callbackregistry.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* 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$ - * - */ - -/* - * This code is based on Broken Sword 2.5 engine - * - * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer - * - * Licensed under GNU GPL v2 - * - */ - -// Alle Callbackfunktionen die von Objekten gerufen werden, die persistiert werden können, müssen hier registriert werden. -// Beim Speichern wird statt des Pointers der Bezeichner gespeichert. Beim Laden wird der Bezeichner wieder in einen Pointer umgewandelt. -// Diese Klasse führt also so etwas ähnliches wie eine Importtabelle für Callback-Funktionen. -// -// Dieses Vorgehen hat mehrere Vorteile: -// 1. Die Speicherstände sind plattformunabhängig. Es werden keine Pointer auf Funktionen gespeichert, sondern nur Namen von Callbackfunktionen. -// Diese können beim Laden über diese Klasse in systemabhängige Pointer umgewandelt werden. -// 2. Speicherstände können auch nach einem Engineupdate weiterhin benutzt werden. Beim Erstellen einer neun Binary verschieben sich häufig die -// Funktionen. Eine Callbackfunktion könnte sich also nach einem Update an einer anderen Stelle befinden als davor. Wenn im Spielstand der -// Pointer gespeichert war, stürtzt das Programm beim Äufrufen dieser Callbackfunktion ab. Durch das Auflösungverfahren wird beim Laden der -// Callbackbezeichner in den neuen Funktionspointer umgewandelt und der Aufruf kann erfolgen. - -#define BS_LOG_PREFIX "CALLBACKREGISTRY" - -#include "sword25/kernel/callbackregistry.h" - -namespace Sword25 { - -bool CallbackRegistry::registerCallbackFunction(const Common::String &name, CallbackPtr ptr) { - if (name == "") { - BS_LOG_ERRORLN("The empty string is not allowed as a callback function name."); - return false; - } - - if (findPtrByName(name) != 0) { - BS_LOG_ERRORLN("There is already a callback function with the name \"%s\".", name.c_str()); - return false; - } - if (findNameByPtr(ptr) != "") { - BS_LOG_ERRORLN("There is already a callback function with the pointer 0x%x.", ptr); - return false; - } - - storeCallbackFunction(name, ptr); - - return true; -} - -CallbackPtr CallbackRegistry::resolveCallbackFunction(const Common::String &name) const { - CallbackPtr result = findPtrByName(name); - - if (!result) { - BS_LOG_ERRORLN("There is no callback function with the name \"%s\".", name.c_str()); - } - - return result; -} - -Common::String CallbackRegistry::resolveCallbackPointer(CallbackPtr ptr) const { - const Common::String &result = findNameByPtr(ptr); - - if (result == "") { - BS_LOG_ERRORLN("There is no callback function with the pointer 0x%x.", ptr); - } - - return result; -} - -CallbackPtr CallbackRegistry::findPtrByName(const Common::String &name) const { - // Eintrag in der Map finden und den Pointer zurückgeben. - NameToPtrMap::const_iterator it = _nameToPtrMap.find(name); - return it == _nameToPtrMap.end() ? 0 : it->_value; -} - -Common::String CallbackRegistry::findNameByPtr(CallbackPtr ptr) const { - // Eintrag in der Map finden und den Namen zurückgeben. - PtrToNameMap::const_iterator it = _ptrToNameMap.find(ptr); - return it == _ptrToNameMap.end() ? "" : it->_value; -} - -void CallbackRegistry::storeCallbackFunction(const Common::String &name, CallbackPtr ptr) { - // Callback-Funktion in beide Maps eintragen. - _nameToPtrMap[name] = ptr; - _ptrToNameMap[ptr] = name; -} - -} // End of namespace Sword25 diff --git a/engines/sword25/kernel/callbackregistry.h b/engines/sword25/kernel/callbackregistry.h deleted file mode 100644 index f73a1341f7..0000000000 --- a/engines/sword25/kernel/callbackregistry.h +++ /dev/null @@ -1,84 +0,0 @@ -/* 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$ - * - */ - -/* - * This code is based on Broken Sword 2.5 engine - * - * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer - * - * Licensed under GNU GPL v2 - * - */ - -#ifndef SWORD25_CALLBACK_REGISTRY_H -#define SWORD25_CALLBACK_REGISTRY_H - -#include "common/scummsys.h" -#include "common/str.h" -#include "common/hash-str.h" -#include "common/hashmap.h" -#include "sword25/kernel/common.h" - -namespace Sword25 { - -typedef void (*CallbackPtr)(int command); - -class CallbackRegistry { -public: - static CallbackRegistry &instance() { - static CallbackRegistry _instance; - return _instance; - } - - bool registerCallbackFunction(const Common::String &name, CallbackPtr ptr); - CallbackPtr resolveCallbackFunction(const Common::String &name) const; - Common::String resolveCallbackPointer(CallbackPtr ptr) const; - -private: - typedef Common::HashMap<Common::String, CallbackPtr, Common::CaseSensitiveString_Hash, Common::CaseSensitiveString_EqualTo> NameToPtrMap; - NameToPtrMap _nameToPtrMap; - - struct CallbackPtr_EqualTo { - bool operator()(CallbackPtr x, CallbackPtr y) const { - return x == y; - } - }; - struct CallbackPtr_Hash { - uint operator()(CallbackPtr x) const { - return *(uint *)&x; - } - }; - - typedef Common::HashMap<CallbackPtr, Common::String, CallbackPtr_Hash, CallbackPtr_EqualTo> PtrToNameMap; - PtrToNameMap _ptrToNameMap; - - CallbackPtr findPtrByName(const Common::String &name) const; - Common::String findNameByPtr(CallbackPtr ptr) const; - void storeCallbackFunction(const Common::String &name, CallbackPtr ptr); -}; - -} // End of namespace Sword25 - -#endif diff --git a/engines/sword25/module.mk b/engines/sword25/module.mk index 6146e9393e..f10c6be5a9 100644 --- a/engines/sword25/module.mk +++ b/engines/sword25/module.mk @@ -33,7 +33,6 @@ MODULE_OBJS := \ gfx/image/vectorimagerenderer.o \ input/inputengine.o \ input/inputengine_script.o \ - kernel/callbackregistry.o \ kernel/filesystemutil.o \ kernel/inputpersistenceblock.o \ kernel/kernel.o \ |